Skip to content

Commit

Permalink
Implement cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Oct 8, 2024
1 parent 97485bd commit b9518d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions junction/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ async def create_booking(self, offer: t.OfferId, passengers: Iterable[t.Passenge
await booking.refresh()
return booking

async def cancel_booking(self, booking_id: t.BookingId) -> t.RefundInformation:
path = f"/bookings/{booking_id}/request-cancellation"
url = URL.build(scheme="https", host=self._host, path=path)
async with self._client.post(url):
if not resp.ok:
await raise_error(resp)
result = await resp.json()
return result["refundInformation"]

async def cancel_booking_confirm(self, booking_id: t.BookingId) -> t.RefundInformation:
path = f"/bookings/{booking_id}/confirm-cancellation"
url = URL.build(scheme="https", host=self._host, path=path)
async with self._client.post(url):
if not resp.ok:
await raise_error(resp)
result = await resp.json()
return result["refundInformation"]

async def __aenter__(self) -> Self:
self._client = ClientSession(headers={"x-api-key": self._api_key}, json_serialize=partial(json.dumps, cls=CustomEncoder))
self._scheduler = aiojobs.Scheduler(wait_timeout=0)
Expand Down
6 changes: 6 additions & 0 deletions junction/typedefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ class Passenger(TypedDict):
phoneNumber: str
passportInformation: _PassportInformation
residentialAddress: _Address


class RefundInformation(TypedDict):
status = Literal["requested", "confirmed"]
bookingPrice = _Price
refundAmount = _Price
47 changes: 47 additions & 0 deletions tests/test_booking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import datetime

from junction import JunctionClient, Passenger

async def test_cancellation(client: JunctionClient) -> None:
orig = "place_01j44f6jw3erbr4rgna3xdtvxn"
dest = "place_01j44f3vfje1pbbr16etj2s26c"
day = datetime.date.today() + datetime.timedelta(days=7)
depart = datetime.datetime.combine(day, datetime.time(12, 30), datetime.UTC)
birth = datetime.date(2000, 1, 1)
offer = await anext(await client.train_search(orig, dest, depart, (birth,)))

passenger: Passenger = {
"dateOfBirth": birth,
"firstName": "foo",
"lastName": "bar",
"gender": "male",
"email": "foo@bar.com",
"phoneNumber": "+4407770000001",
"passportInformation": {
"documentNumber": "1",
"issueCountry": "GB",
"nationality": "GB",
"expirationDate": datetime.date.today() + datetime.timedelta(days=365),
"issueDate": datetime.date.today() - datetime.timedelta(days=365)
},
"residentialAddress": {
"addressLines": ("1 Foo Road",),
"countryCode": "GB",
"postalCode": "BN1",
"city": "Brighton"
}
}
booking = await client.create_booking(offer["id"], (passenger,))
assert not booking.confirmed
await booking.confirm()
assert booking.confirmed

refund = await client.cancel_booking(booking.id)
assert refund["status"] == "requested"
assert float(refund["bookingPrice"]["amount"]) > 0
refund_amount = refund["refundAmount"]["amount"]
assert float(refund_amount) > 0

refund = await client.cancel_booking_confirm(cancellation_id)
assert refund["status"] == "confirmed"
assert refund["refundAmount"]["amount"] == refund_amount

0 comments on commit b9518d6

Please sign in to comment.