Improvements
This commit is contained in:
parent
7599f655ad
commit
b6f0c88320
7 changed files with 139 additions and 27 deletions
|
|
@ -19,6 +19,7 @@ ports = {
|
|||
"CHERBOURG": "FRCER",
|
||||
"ST MALO": "FRSML",
|
||||
"LE HAVRE": "FRLEH",
|
||||
"ROSCOFF": "FRROS",
|
||||
}
|
||||
|
||||
port_lookup = {code: name for name, code in ports.items()}
|
||||
|
|
|
|||
21
ferry/api.py
21
ferry/api.py
|
|
@ -3,6 +3,7 @@
|
|||
from typing import Any, TypedDict
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
from . import Vehicle
|
||||
|
||||
|
|
@ -19,17 +20,18 @@ class VehicleDict(TypedDict):
|
|||
registrations: list[str]
|
||||
height: int
|
||||
length: int
|
||||
extras: dict[str, None]
|
||||
extras: dict[str, None | bool]
|
||||
|
||||
|
||||
def vehicle_dict(v: Vehicle) -> VehicleDict:
|
||||
def vehicle_dict(v: Vehicle, rear_mounted_bike_carrier: bool = False) -> VehicleDict:
|
||||
"""Return vehicle detail in the format for the Brittany Ferries API."""
|
||||
rmbc = True if rear_mounted_bike_carrier else None
|
||||
return {
|
||||
"type": v.type,
|
||||
"registrations": [v.registration],
|
||||
"height": v.height,
|
||||
"length": v.length,
|
||||
"extras": {"rearMountedBikeCarrier": None},
|
||||
"extras": {"rearMountedBikeCarrier": rmbc},
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -41,6 +43,7 @@ def get_prices(
|
|||
vehicle: Vehicle,
|
||||
adults: int = 2,
|
||||
small_dogs: int = 1,
|
||||
rear_mounted_bike_carrier: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Call Brittany Ferries API to get details of crossings."""
|
||||
url = api_root_url + "crossing/prices"
|
||||
|
|
@ -49,7 +52,7 @@ def get_prices(
|
|||
"bookingReference": None,
|
||||
"pets": {"smallDogs": small_dogs, "largeDogs": 0, "cats": 0},
|
||||
"passengers": {"adults": adults, "children": 0, "infants": 0},
|
||||
"vehicle": vehicle_dict(vehicle),
|
||||
"vehicle": vehicle_dict(vehicle, rear_mounted_bike_carrier),
|
||||
"departurePort": departure_port,
|
||||
"arrivalPort": arrival_port,
|
||||
"disability": None,
|
||||
|
|
@ -60,6 +63,8 @@ def get_prices(
|
|||
|
||||
r = requests.post(url, json=post_data, headers=headers)
|
||||
data: dict[str, Any] = r.json()
|
||||
if "crossings" not in data:
|
||||
print(json.dumps(data, indent=2))
|
||||
return data
|
||||
|
||||
|
||||
|
|
@ -71,6 +76,7 @@ def get_accommodations(
|
|||
vehicle: Vehicle,
|
||||
adults: int,
|
||||
small_dogs: int,
|
||||
rear_mounted_bike_carrier: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""Grab cabin details."""
|
||||
url = api_root_url + "crossing/accommodations"
|
||||
|
|
@ -81,7 +87,7 @@ def get_accommodations(
|
|||
"departureDate": departure_date,
|
||||
"passengers": {"adults": adults, "children": 0, "infants": 0},
|
||||
"disability": None,
|
||||
"vehicle": vehicle_dict(vehicle),
|
||||
"vehicle": vehicle_dict(vehicle, rear_mounted_bike_carrier),
|
||||
"petCabinsNeeded": True,
|
||||
"ticketTier": ticket_tier,
|
||||
"pets": {"smallDogs": small_dogs, "largeDogs": 0, "cats": 0},
|
||||
|
|
@ -92,4 +98,9 @@ def get_accommodations(
|
|||
json_data: dict[str, Any] = requests.post(
|
||||
url, json=post_data, headers=headers
|
||||
).json()
|
||||
if "crossings" not in json_data:
|
||||
print()
|
||||
print(json.dumps(post_data, indent=2))
|
||||
print()
|
||||
print(json.dumps(json_data, indent=2))
|
||||
return json_data
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue