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

Commit

Permalink
Re-combine (some) classes into single file
Browse files Browse the repository at this point in the history
We need to refactor our classes to avoid circular dependencies.
  • Loading branch information
Jesse Claven committed Jan 24, 2022
1 parent cca4d6b commit db5f94e
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 128 deletions.
5 changes: 1 addition & 4 deletions duffel_api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

Expand Down
117 changes: 115 additions & 2 deletions duffel_api/models/airport.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"),
)
24 changes: 0 additions & 24 deletions duffel_api/models/city.py

This file was deleted.

56 changes: 0 additions & 56 deletions duffel_api/models/place.py

This file was deleted.

42 changes: 0 additions & 42 deletions duffel_api/models/refund.py

This file was deleted.

0 comments on commit db5f94e

Please sign in to comment.