diff --git a/duffel_api/models/__init__.py b/duffel_api/models/__init__.py index 6c3ccc9e..690ba6c7 100644 --- a/duffel_api/models/__init__.py +++ b/duffel_api/models/__init__.py @@ -1,7 +1,6 @@ from .aircraft import Aircraft from .airline import Airline -from .airport import Airport -from .city import City +from .airport import Airport, City, Place, Refund from .loyalty_programme_account import LoyaltyProgrammeAccount from .offer import ( Offer, @@ -21,8 +20,6 @@ from .order_change_request import OrderChangeRequest from .payment import Payment from .payment_intent import PaymentIntent -from .place import Place -from .refund import Refund from .seat_map import SeatMap from .webhook import Webhook diff --git a/duffel_api/models/airport.py b/duffel_api/models/airport.py index 9b00b072..9d48d92a 100644 --- a/duffel_api/models/airport.py +++ b/duffel_api/models/airport.py @@ -1,10 +1,33 @@ from dataclasses import dataclass -from typing import Optional +from datetime import datetime +from typing import Optional, Sequence -from duffel_api.models import City from duffel_api.utils import get_and_transform +@dataclass +class City: + """The metropolitan area where the airport is located. + Only present for airports which are registered with IATA as + belonging to a metropolitan area. + """ + + id: str + name: str + iata_code: str + iata_country_code: str + + @classmethod + def from_json(cls, json: dict): + """Construct a class instance from a JSON response.""" + return cls( + id=json["id"], + name=json["name"], + iata_code=json["iata_code"], + iata_country_code=json["iata_country_code"], + ) + + @dataclass class Airport: """Airports are used to identify origins and destinations in journey @@ -34,3 +57,93 @@ def from_json(cls, json: dict): time_zone=json["time_zone"], city=get_and_transform(json, "city", City.from_json), ) + + +@dataclass +class Place: + """The city or airport""" + + id: str + name: str + type: str + iata_city_code: Optional[str] + iata_country_code: str + latitude: Optional[float] + longitude: Optional[float] + icao_code: Optional[str] + time_zone: Optional[str] + city_name: Optional[str] + city: Optional[City] + airports: Optional[Sequence[Airport]] + + allowed_types = ["airport", "city"] + + class InvalidType(Exception): + """Invalid type of place""" + + def __post_init__(self): + if self.type not in Place.allowed_types: + raise Place.InvalidType(self.type) + + return self + + @classmethod + def from_json(cls, json: dict): + """Construct a class instance from a JSON response.""" + return cls( + id=json["id"], + name=json["name"], + type=json["type"], + iata_city_code=json.get("iata_city_code"), + iata_country_code=json["iata_country_code"], + latitude=json.get("latitude"), + longitude=json.get("longitude"), + icao_code=json.get("icao_code"), + time_zone=json.get("time_zone"), + city_name=json.get("city_name"), + city=get_and_transform(json, "city", City.from_json), + airports=get_and_transform( + json, + "airports", + lambda value: [Airport.from_json(airport) for airport in value], + ), + ) + + +@dataclass +class Refund: + """A Refund allows you to refund money that you had collected from a customer with a + Payment Intent. You're able to do partial refunds and also able to do multiple + refunds for the same Payment Intent. + """ + + id: str + live_mode: bool + payment_intent_id: str + amount: str + currency: str + net_amount: Optional[str] + net_currency: Optional[str] + status: str + destination: str + arrival: str + created_at: datetime + updated_at: datetime + + @classmethod + def from_json(cls, json: dict): + """Construct a class instance from a JSON response.""" + return cls( + id=json["id"], + live_mode=json["live_mode"], + payment_intent_id=json["payment_intent_id"], + amount=json["amount"], + currency=json["currency"], + net_amount=json.get("net_amount"), + net_currency=json.get("net_currency"), + status=json["status"], + destination=json["destination"], + arrival=json["arrival"], + created_at=datetime.strptime(json["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"), + updated_at=datetime.strptime(json["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"), + ) diff --git a/duffel_api/models/city.py b/duffel_api/models/city.py deleted file mode 100644 index 69f55954..00000000 --- a/duffel_api/models/city.py +++ /dev/null @@ -1,24 +0,0 @@ -from dataclasses import dataclass - - -@dataclass -class City: - """The metropolitan area where the airport is located. - Only present for airports which are registered with IATA as - belonging to a metropolitan area. - """ - - id: str - name: str - iata_code: str - iata_country_code: str - - @classmethod - def from_json(cls, json: dict): - """Construct a class instance from a JSON response.""" - return cls( - id=json["id"], - name=json["name"], - iata_code=json["iata_code"], - iata_country_code=json["iata_country_code"], - ) diff --git a/duffel_api/models/place.py b/duffel_api/models/place.py deleted file mode 100644 index 937a7be2..00000000 --- a/duffel_api/models/place.py +++ /dev/null @@ -1,56 +0,0 @@ -from dataclasses import dataclass -from typing import Optional, Sequence - -from duffel_api.models import Airport, City -from duffel_api.utils import get_and_transform - - -@dataclass -class Place: - """The city or airport""" - - id: str - name: str - type: str - iata_city_code: Optional[str] - iata_country_code: str - latitude: Optional[float] - longitude: Optional[float] - icao_code: Optional[str] - time_zone: Optional[str] - city_name: Optional[str] - city: Optional[City] - airports: Optional[Sequence[Airport]] - - allowed_types = ["airport", "city"] - - class InvalidType(Exception): - """Invalid type of place""" - - def __post_init__(self): - if self.type not in Place.allowed_types: - raise Place.InvalidType(self.type) - - return self - - @classmethod - def from_json(cls, json: dict): - """Construct a class instance from a JSON response.""" - return cls( - id=json["id"], - name=json["name"], - type=json["type"], - iata_city_code=json.get("iata_city_code"), - iata_country_code=json["iata_country_code"], - latitude=json.get("latitude"), - longitude=json.get("longitude"), - icao_code=json.get("icao_code"), - time_zone=json.get("time_zone"), - city_name=json.get("city_name"), - city=get_and_transform(json, "city", City.from_json), - airports=get_and_transform( - json, - "airports", - lambda value: [Airport.from_json(airport) for airport in value], - ), - ) diff --git a/duffel_api/models/refund.py b/duffel_api/models/refund.py deleted file mode 100644 index f265099c..00000000 --- a/duffel_api/models/refund.py +++ /dev/null @@ -1,42 +0,0 @@ -from dataclasses import dataclass -from datetime import datetime -from typing import Optional - - -@dataclass -class Refund: - """A Refund allows you to refund money that you had collected from a customer with a - Payment Intent. You're able to do partial refunds and also able to do multiple - refunds for the same Payment Intent. - """ - - id: str - live_mode: bool - payment_intent_id: str - amount: str - currency: str - net_amount: Optional[str] - net_currency: Optional[str] - status: str - destination: str - arrival: str - created_at: datetime - updated_at: datetime - - @classmethod - def from_json(cls, json: dict): - """Construct a class instance from a JSON response.""" - return cls( - id=json["id"], - live_mode=json["live_mode"], - payment_intent_id=json["payment_intent_id"], - amount=json["amount"], - currency=json["currency"], - net_amount=json.get("net_amount"), - net_currency=json.get("net_currency"), - status=json["status"], - destination=json["destination"], - arrival=json["arrival"], - created_at=datetime.strptime(json["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"), - updated_at=datetime.strptime(json["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"), - )