Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Add max_connections to OfferRequest creation #213

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions duffel_api/api/booking/offer_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ class InvalidPassenger(Exception):
class InvalidSlice(Exception):
"""Invalid slice data provided"""

class InvalidMaxConnectionValue(Exception):
"""Invalid max connection value provided"""

def __init__(self, client):
self._client = client
self._return_offers = "false"
self._cabin_class = "economy"
self._passengers = []
self._slices = []
self._max_connections = 1

@staticmethod
def _validate_cabin_class(cabin_class):
Expand Down Expand Up @@ -85,6 +89,12 @@ def _validate_slices(slices):
):
raise OfferRequestCreate.InvalidSlice(travel_slice)

@staticmethod
def _validate_max_connections(max_connections):
"""Validate the max connection number"""
if max_connections < 0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only support integers in the API, I wonder if we should:

Suggested change
if max_connections < 0:
if isinstance(max_connections, int) and max_connections < 0:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good shout! Assuming isinstance returns True in this case when it is an int, I think we would want:
if not isinstance(max_connections, int) or max_connections < 0:
as we raise and error when True. I'll update now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Should these kinds of validations occur in the client, e.g. should these validations occur here and in the API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree with @sgerrand that we've generally avoided any sort of explicit type checking in the client and only performed basic consistency checks (i.e. that there are non-zero elements or certain attributes exist).

raise OfferRequestCreate.InvalidMaxConnectionValue(max_connections)

def return_offers(self):
"""Set return_offers to 'true'"""
self._return_offers = "true"
Expand All @@ -108,6 +118,12 @@ def slices(self, slices):
self._slices = slices
return self

def max_connections(self, max_connections):
"""Set the max_connections for the journey we want to travel"""
OfferRequestCreate._validate_max_connections(max_connections)
self._max_connections = max_connections
return self

def execute(self):
"""POST /air/offer_requests - trigger the call to create the offer_request"""
OfferRequestCreate._validate_passengers(self._passengers)
Expand All @@ -119,6 +135,7 @@ def execute(self):
"data": {
"cabin_class": self._cabin_class,
"passengers": self._passengers,
"max_connections": self._max_connections,
"slices": self._slices,
}
},
Expand Down