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

Commit

Permalink
Add type annotations for models
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Claven committed Jan 20, 2022
1 parent ae4f90b commit a91b2c5
Show file tree
Hide file tree
Showing 52 changed files with 2,175 additions and 945 deletions.
6 changes: 4 additions & 2 deletions duffel_api/api/booking/offer_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/offer_requests/:id"""
return OfferRequest(self.do_get("{}/{}".format(self._url, id_))["data"])
return OfferRequest.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)

def list(self, limit=50):
"""GET /air/offer_requests"""
Expand Down Expand Up @@ -120,4 +122,4 @@ def execute(self):
}
},
)
return OfferRequest(res["data"])
return OfferRequest.from_json(res["data"])
6 changes: 3 additions & 3 deletions duffel_api/api/booking/offers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ...http_client import HttpClient, Pagination
from ...models import Offer, Passenger
from ...models import Offer, OfferPassenger


class OfferClient(HttpClient):
Expand Down Expand Up @@ -69,7 +69,7 @@ def get(self, id_, return_available_services=False):
params = {}
if return_available_services:
params["return_available_services"] = "true"
return Offer(
return Offer.from_json(
self.do_get("{}/{}".format(self._url, id_), query_params=params)["data"]
)

Expand Down Expand Up @@ -119,4 +119,4 @@ def update_passenger(

res = self.do_patch(url, body=body)

return Passenger(res["data"])
return OfferPassenger.from_json(res["data"])
8 changes: 5 additions & 3 deletions duffel_api/api/booking/order_cancellations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/order_cancellations/:id."""
return OrderCancellation(self.do_get("{}/{}".format(self._url, id_))["data"])
return OrderCancellation.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)

def list(self, order_id, limit=50):
"""Retrieve a paginated list of order cancellations."""
Expand All @@ -39,7 +41,7 @@ def create(self, order_id):
"""
res = self.do_post(self._url, body={"data": {"order_id": order_id}})
return OrderCancellation(res["data"])
return OrderCancellation.from_json(res["data"])

def confirm(self, id_):
"""Confirm an order cancellation.
Expand All @@ -54,4 +56,4 @@ def confirm(self, id_):
"""
url = "{}/{}/actions/confirm".format(self._url, id_)
return OrderCancellation(self.do_post(url)["data"])
return OrderCancellation.from_json(self.do_post(url)["data"])
4 changes: 3 additions & 1 deletion duffel_api/api/booking/order_change_offers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/order_change_offers/:id"""
return OrderChangeOffer(self.do_get("{}/{}".format(self._url, id_))["data"])
return OrderChangeOffer.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)

def list(self, order_change_request_id, sort=None, max_connections=None, limit=50):
"""GET /air/order_change_offers"""
Expand Down
6 changes: 4 additions & 2 deletions duffel_api/api/booking/order_change_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def create(self, order_id):

def get(self, id_):
"""GET /air/order_change_requests/:id"""
return OrderChangeRequest(self.do_get("{}/{}".format(self._url, id_))["data"])
return OrderChangeRequest.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)


class OrderChangeRequestCreate:
Expand Down Expand Up @@ -66,4 +68,4 @@ def execute(self):
}
},
)
return OrderChangeRequest(res["data"])
return OrderChangeRequest.from_json(res["data"])
8 changes: 5 additions & 3 deletions duffel_api/api/booking/order_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/order_changes/:id."""
return OrderChange(self.do_get("{}/{}".format(self._url, id_))["data"])
return OrderChange.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)

def create(self, selected_order_change_offer):
"""Create a pending order change.
Expand All @@ -42,7 +44,7 @@ def create(self, selected_order_change_offer):
self._url,
body={"data": {"selected_order_change_offer": selected_order_change_offer}},
)
return OrderChange(res["data"])
return OrderChange.from_json(res["data"])

def confirm(self, id_, payment_):
"""Confirm an order change.
Expand Down Expand Up @@ -76,4 +78,4 @@ def confirm(self, id_, payment_):
}
},
)
return OrderChange(res["data"])
return OrderChange.from_json(res["data"])
6 changes: 3 additions & 3 deletions duffel_api/api/booking/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/orders/:id."""
return Order(self.do_get("{}/{}".format(self._url, id_))["data"])
return Order.from_json(self.do_get("{}/{}".format(self._url, id_))["data"])

def list(self, awaiting_payment=False, sort=None, limit=50):
"""GET /air/orders."""
Expand Down Expand Up @@ -177,7 +177,7 @@ def execute(self):
self._client._url,
body={"data": self._build_order_payload()},
)
return Order(res["data"])
return Order.from_json(res["data"])


class OrderUpdate:
Expand Down Expand Up @@ -213,4 +213,4 @@ def execute(self):
url,
body={"data": {"metadata": self._metadata}},
)
return Order(res["data"])
return Order.from_json(res["data"])
2 changes: 1 addition & 1 deletion duffel_api/api/booking/payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def execute(self):
self._client._url,
body={"data": {"order_id": self._order_id, "payment": self._payment}},
)
return Payment(res["data"])
return Payment.from_json(res["data"])
2 changes: 1 addition & 1 deletion duffel_api/api/booking/seat_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def __init__(self, **kwargs):
def get(self, offer_id):
"""GET /air/seat_maps"""
data = self.do_get(self._url, query_params={"offer_id": offer_id})["data"]
return [SeatMap(m) for m in data]
return [SeatMap.from_json(m) for m in data]
8 changes: 5 additions & 3 deletions duffel_api/api/duffel_payments/payment_intents.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def get(self, id_):
You should use this API to get the complete, up-to-date information
about a Payment Intent.
"""
return PaymentIntent(self.do_get("{}/{}".format(self._url, id_))["data"])
return PaymentIntent.from_json(
self.do_get("{}/{}".format(self._url, id_))["data"]
)

def confirm(self, id_):
"""Confirm a Payment Intent
Expand All @@ -39,7 +41,7 @@ def confirm(self, id_):
to your Balance (minus any Duffel Payment fees).
"""
url = "{}/{}/actions/confirm".format(self._url, id_)
return PaymentIntent(self.do_post(url)["data"])
return PaymentIntent.from_json(self.do_post(url)["data"])


class PaymentIntentCreate:
Expand Down Expand Up @@ -85,4 +87,4 @@ def execute(self):
}
},
)
return PaymentIntent(res["data"])
return PaymentIntent.from_json(res["data"])
4 changes: 2 additions & 2 deletions duffel_api/api/notifications/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def execute(self):
self._client._url,
body={"data": {"events": self._events, "url": self._url}},
)
return Webhook(res["data"])
return Webhook.from_json(res["data"])


class WebhookUpdate(object):
Expand All @@ -79,4 +79,4 @@ def execute(self):
body={"data": {"active": self._active}},
)

return Webhook(res["data"])
return Webhook.from_json(res["data"])
2 changes: 1 addition & 1 deletion duffel_api/api/supporting/aircraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/aircraft/:id"""
return Aircraft(self.do_get("{}/{}".format(self._url, id_))["data"])
return Aircraft.from_json(self.do_get("{}/{}".format(self._url, id_))["data"])

def list(self, limit=50):
"""GET /air/aircraft"""
Expand Down
2 changes: 1 addition & 1 deletion duffel_api/api/supporting/airlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/airlines/:id"""
return Airline(self.do_get("{}/{}".format(self._url, id_))["data"])
return Airline.from_json(self.do_get("{}/{}".format(self._url, id_))["data"])

def list(self, limit=50):
"""GET /air/airlines"""
Expand Down
2 changes: 1 addition & 1 deletion duffel_api/api/supporting/airports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, **kwargs):

def get(self, id_):
"""GET /air/airports/:id"""
return Airport(self.do_get("{}/{}".format(self._url, id_))["data"])
return Airport.from_json(self.do_get("{}/{}".format(self._url, id_))["data"])

def list(self, limit=50):
"""GET /air/airports"""
Expand Down
11 changes: 7 additions & 4 deletions duffel_api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .aircraft import Aircraft
from .airport import Airport, City
from .airline import Airline
from .airport import Airport
from .city import City
from .loyalty_programme_account import LoyaltyProgrammeAccount
from .passenger import Passenger
from .place import Place
from .offer import (
Offer,
OfferConditionChangeBeforeDeparture,
OfferConditionRefundBeforeDeparture,
OfferPassenger,
)
from .offer_request import OfferRequest
from .order import (
Expand All @@ -21,6 +21,8 @@
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

Expand All @@ -30,9 +32,9 @@
"Airport",
"City",
"LoyaltyProgrammeAccount",
"Passenger",
"Place",
"Offer",
"OfferPassenger",
"OfferConditionChangeBeforeDeparture",
"OfferConditionRefundBeforeDeparture",
"OfferRequest",
Expand All @@ -45,6 +47,7 @@
"OrderChangeRequest",
"Payment",
"PaymentIntent",
"Refund",
"SeatMap",
"Webhook",
]
19 changes: 16 additions & 3 deletions duffel_api/models/aircraft.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from dataclasses import dataclass


@dataclass
class Aircraft:
"""Aircraft are used to describe what passengers will fly in for a given trip"""

def __init__(self, json):
for key in json:
setattr(self, key, json[key])
id: str
iata_code: str
name: str

@classmethod
def from_json(cls, json: dict):
"""Construct a class instance from a JSON response."""
return cls(
id=json["id"],
iata_code=json["iata_code"],
name=json["name"],
)
20 changes: 16 additions & 4 deletions duffel_api/models/airline.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from dataclasses import dataclass


@dataclass
class Airline:
"""Airlines are used to identify the air travel companies selling and operating
flights
"""

def __init__(self, json):
for key in json:
setattr(self, key, json[key])
id: str
name: str
iata_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"],
)
49 changes: 32 additions & 17 deletions duffel_api/models/airport.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
class Airport:
"""Airports are used to identify origins and destinations in journey slices"""
from dataclasses import dataclass
from typing import Optional

from duffel_api.models import City
from duffel_api.utils import get_and_transform

def __init__(self, json):
for key in json:
value = json[key]
if key == "city" and value:
setattr(self, key, City(value))
else:
setattr(self, key, value)

@dataclass
class Airport:
"""Airports are used to identify origins and destinations in journey
slices"""

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: Optional[str]
icao_code: Optional[str]
country_code: str
latitude: float
longitude: float
time_zone: str
city: Optional[City]

def __init__(self, json):
for key in json:
setattr(self, key, json[key])
@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.get("iata_code"),
icao_code=json.get("icao_code"),
country_code=json["country_code"],
latitude=json["latitude"],
longitude=json["longitude"],
time_zone=json["time_zone"],
city=get_and_transform(json, "city", City.from_json),
)
24 changes: 24 additions & 0 deletions duffel_api/models/city.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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"],
)
Loading

0 comments on commit a91b2c5

Please sign in to comment.