|
| 1 | +from ...http_client import HttpClient |
| 2 | +from ...models import OfferRequest |
| 3 | + |
| 4 | + |
| 5 | +class PartialOfferRequestClient(HttpClient): |
| 6 | + """To search for and select flights separately for each slice of the journey, you'll |
| 7 | + need to create a partial offer reques. A partial offer request describes the |
| 8 | + passengers and where and when they want to travel (in the form of a list of |
| 9 | + slices). It may also include additional filters (e.g. a particular cabin to |
| 10 | + travel in). |
| 11 | +
|
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self, **kwargs): |
| 15 | + self._url = "/air/partial_offer_requests" |
| 16 | + super().__init__(**kwargs) |
| 17 | + |
| 18 | + def get(self, id_, selected_partial_offer=None): |
| 19 | + """GET /air/partial_offer_requests/:id |
| 20 | +
|
| 21 | + If a selected_partial_offer is passed: |
| 22 | + GET /air/partial_offer_requests/:id?selected_partial_offer[]=:selected_partial_offer |
| 23 | +
|
| 24 | +
|
| 25 | + Retrieves a partial offers request by its ID, only including partial offers for |
| 26 | + the current slice of multi-step search flow. |
| 27 | + """ # noqa: E501 |
| 28 | + response = None |
| 29 | + if selected_partial_offer is None: |
| 30 | + response = self.do_get(f"{self._url}/{id_}") |
| 31 | + else: |
| 32 | + response = self.do_get( |
| 33 | + f"{self._url}/{id_}", |
| 34 | + query_params={"selected_partial_offer[]": selected_partial_offer}, |
| 35 | + ) |
| 36 | + |
| 37 | + if response is not None: |
| 38 | + return OfferRequest.from_json(response["data"]) |
| 39 | + |
| 40 | + def fares(self, id_, selected_partial_offers=[]): |
| 41 | + """GET /air/partial_offer_requests/:id/fares |
| 42 | +
|
| 43 | + Retrieves an offer request with offers for fares matching selected partial offers. |
| 44 | + """ |
| 45 | + response = self.do_get( |
| 46 | + f"{self._url}/{id_}/fares", |
| 47 | + query_params={"selected_partial_offer[]": selected_partial_offers}, |
| 48 | + ) |
| 49 | + if response is not None: |
| 50 | + return OfferRequest.from_json(response["data"]) |
| 51 | + |
| 52 | + def create(self): |
| 53 | + """Initiate creation of a Partial Offer Request""" |
| 54 | + return PartialOfferRequestCreate(self) |
| 55 | + |
| 56 | + |
| 57 | +class PartialOfferRequestCreate(object): |
| 58 | + """Auxiliary class to provide methods for partial offer request creation related data""" # noqa: E501 |
| 59 | + |
| 60 | + class InvalidCabinClass(Exception): |
| 61 | + """Invalid cabin class provided""" |
| 62 | + |
| 63 | + class InvalidNumberOfPassengers(Exception): |
| 64 | + """Invalid number of passengers provided""" |
| 65 | + |
| 66 | + class InvalidNumberOfSlices(Exception): |
| 67 | + """Invalid number of slices provided""" |
| 68 | + |
| 69 | + class InvalidPassenger(Exception): |
| 70 | + """Invalid passenger data provided""" |
| 71 | + |
| 72 | + class InvalidSlice(Exception): |
| 73 | + """Invalid slice data provided""" |
| 74 | + |
| 75 | + class InvalidMaxConnectionValue(Exception): |
| 76 | + """Invalid max connection value provided""" |
| 77 | + |
| 78 | + def __init__(self, client): |
| 79 | + self._client = client |
| 80 | + self._cabin_class = "economy" |
| 81 | + self._passengers = [] |
| 82 | + self._slices = [] |
| 83 | + self._max_connections = 1 |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def _validate_cabin_class(cabin_class): |
| 87 | + """Validate cabin class""" |
| 88 | + if cabin_class not in [ |
| 89 | + "first", |
| 90 | + "business", |
| 91 | + "economy", |
| 92 | + "premium_economy", |
| 93 | + ]: |
| 94 | + raise PartialOfferRequestCreate.InvalidCabinClass(cabin_class) |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def _validate_passengers(passengers): |
| 98 | + """Validate passenger count and the data provided for each if any were given""" |
| 99 | + if len(passengers) == 0: |
| 100 | + raise PartialOfferRequestCreate.InvalidNumberOfPassengers(passengers) |
| 101 | + for passenger in passengers: |
| 102 | + if not ("type" in passenger or "age" in passenger): |
| 103 | + raise PartialOfferRequestCreate.InvalidPassenger(passenger) |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def _validate_slices(slices): |
| 107 | + """Validate number of slices and the data provided for each if any were given""" |
| 108 | + if len(slices) == 0: |
| 109 | + raise PartialOfferRequestCreate.InvalidNumberOfSlices(slices) |
| 110 | + for travel_slice in slices: |
| 111 | + if set(travel_slice.keys()) != set( |
| 112 | + ["departure_date", "destination", "origin"] |
| 113 | + ): |
| 114 | + raise PartialOfferRequestCreate.InvalidSlice(travel_slice) |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def _validate_max_connections(max_connections): |
| 118 | + """Validate the max connection number""" |
| 119 | + if not isinstance(max_connections, int) or max_connections < 0: |
| 120 | + raise PartialOfferRequestCreate.InvalidMaxConnectionValue(max_connections) |
| 121 | + |
| 122 | + def cabin_class(self, cabin_class): |
| 123 | + """Set cabin_class - defaults to 'economy'""" |
| 124 | + PartialOfferRequestCreate._validate_cabin_class(cabin_class) |
| 125 | + self._cabin_class = cabin_class |
| 126 | + return self |
| 127 | + |
| 128 | + def passengers(self, passengers): |
| 129 | + """Set the passengers that will be travelling""" |
| 130 | + PartialOfferRequestCreate._validate_passengers(passengers) |
| 131 | + self._passengers = passengers |
| 132 | + return self |
| 133 | + |
| 134 | + def slices(self, slices): |
| 135 | + """Set the slices for the origin-destination we want to travel""" |
| 136 | + PartialOfferRequestCreate._validate_slices(slices) |
| 137 | + self._slices = slices |
| 138 | + return self |
| 139 | + |
| 140 | + def max_connections(self, max_connections): |
| 141 | + """Set the max_connections for the journey we want to travel""" |
| 142 | + PartialOfferRequestCreate._validate_max_connections(max_connections) |
| 143 | + self._max_connections = max_connections |
| 144 | + return self |
| 145 | + |
| 146 | + def execute(self): |
| 147 | + """POST /air/partial_offer_requests - trigger the call to create the offer_request""" # noqa: E501 |
| 148 | + PartialOfferRequestCreate._validate_passengers(self._passengers) |
| 149 | + PartialOfferRequestCreate._validate_slices(self._slices) |
| 150 | + res = self._client.do_post( |
| 151 | + self._client._url, |
| 152 | + body={ |
| 153 | + "data": { |
| 154 | + "cabin_class": self._cabin_class, |
| 155 | + "passengers": self._passengers, |
| 156 | + "max_connections": self._max_connections, |
| 157 | + "slices": self._slices, |
| 158 | + } |
| 159 | + }, |
| 160 | + ) |
| 161 | + return OfferRequest.from_json(res["data"]) |
0 commit comments