87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
|
"""Interface with the Brittany Ferries API."""
|
||
|
|
||
|
from typing import Any, TypedDict
|
||
|
|
||
|
import requests
|
||
|
|
||
|
from . import Vehicle
|
||
|
|
||
|
api_root_url = "https://www.brittany-ferries.co.uk/api/ferry/v1/"
|
||
|
|
||
|
|
||
|
class VehicleDict(TypedDict):
|
||
|
"""Description of vechicle in the format expected by the API."""
|
||
|
|
||
|
type: str
|
||
|
registrations: list[str]
|
||
|
height: int
|
||
|
length: int
|
||
|
extras: dict[str, None]
|
||
|
|
||
|
|
||
|
def vehicle_dict(v: Vehicle) -> VehicleDict:
|
||
|
"""Return vehicle detail in the format for the Brittany Ferries API."""
|
||
|
return {
|
||
|
"type": v.type,
|
||
|
"registrations": [v.registration],
|
||
|
"height": v.height,
|
||
|
"length": v.length,
|
||
|
"extras": {"rearMountedBikeCarrier": None},
|
||
|
}
|
||
|
|
||
|
|
||
|
def get_prices(
|
||
|
departure_port: str,
|
||
|
arrival_port: str,
|
||
|
from_date: str,
|
||
|
to_date: str,
|
||
|
vehicle: Vehicle,
|
||
|
) -> dict[str, Any]:
|
||
|
"""Call Brittany Ferries API to get details of crossings."""
|
||
|
url = api_root_url + "crossing/prices"
|
||
|
|
||
|
post_data = {
|
||
|
"bookingReference": None,
|
||
|
"pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0},
|
||
|
"passengers": {"adults": 2, "children": 0, "infants": 0},
|
||
|
"vehicle": vehicle_dict(vehicle),
|
||
|
"departurePort": departure_port,
|
||
|
"arrivalPort": arrival_port,
|
||
|
"disability": None,
|
||
|
"sponsor": None,
|
||
|
"fromDate": f"{from_date}T00:00:00",
|
||
|
"toDate": f"{to_date}T23:59:59",
|
||
|
}
|
||
|
|
||
|
r = requests.post(url, json=post_data)
|
||
|
data: dict[str, Any] = r.json()
|
||
|
return data
|
||
|
|
||
|
|
||
|
def get_accommodations(
|
||
|
departure_port: str,
|
||
|
arrival_port: str,
|
||
|
departure_date: str,
|
||
|
ticket_tier: str,
|
||
|
vehicle: Vehicle,
|
||
|
) -> dict[str, Any]:
|
||
|
"""Grab cabin details."""
|
||
|
url = api_root_url + "crossing/accommodations"
|
||
|
post_data = {
|
||
|
"bookingReference": None,
|
||
|
"departurePort": departure_port,
|
||
|
"arrivalPort": arrival_port,
|
||
|
"departureDate": departure_date,
|
||
|
"passengers": {"adults": 2, "children": 0, "infants": 0},
|
||
|
"disability": None,
|
||
|
"vehicle": vehicle_dict(vehicle),
|
||
|
"petCabinsNeeded": True,
|
||
|
"ticketTier": ticket_tier,
|
||
|
"pets": {"smallDogs": 1, "largeDogs": 0, "cats": 0},
|
||
|
"sponsor": None,
|
||
|
"offerType": "NONE",
|
||
|
}
|
||
|
|
||
|
json_data: dict[str, Any] = requests.post(url, json=post_data).json()
|
||
|
return json_data
|