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

Commit 07a9d6b

Browse files
authored
Merge pull request #40 from duffelhq/more-examples
Additional examples, run them as integration tests
2 parents 6a2e219 + 6a9efc4 commit 07a9d6b

File tree

5 files changed

+455
-0
lines changed

5 files changed

+455
-0
lines changed

.github/workflows/main.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ jobs:
4242
user: __token__
4343
password: ${{ secrets.PYPI_API_TOKEN }}
4444

45+
integration-test:
46+
runs-on: ubuntu-latest
47+
48+
env:
49+
DUFFEL_ACCESS_TOKEN: ${{ secrets.DUFFEL_ACCESS_TOKEN }}
50+
51+
steps:
52+
- uses: actions/checkout@v2
53+
- name: Set up Python
54+
uses: actions/setup-python@v2
55+
with:
56+
python-version: 3.8
57+
- name: Install local version of package
58+
run: pip install --editable .
59+
- name: Run search and book example
60+
run: python examples/search-and-book.py
61+
- name: Run hold and pay later example
62+
run: python examples/hold-and-pay-later.py
63+
- name: Run book with seat example
64+
run: python examples/book-with-seat.py
65+
- name: Run book and change example
66+
run: python examples/book-and-change.py
67+
4568
test:
4669
runs-on: ubuntu-latest
4770
strategy:

examples/book-and-change.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from datetime import date, timedelta
2+
3+
from duffel_api import Duffel
4+
5+
6+
if __name__ == "__main__":
7+
print("Duffel Flights API - book and change example")
8+
client = Duffel()
9+
departure_date = date.today().replace(date.today().year + 1)
10+
slices = [
11+
{
12+
"origin": "LHR",
13+
"destination": "STN",
14+
"departure_date": departure_date.strftime("%Y-%m-%d"),
15+
},
16+
]
17+
offer_request = (
18+
client.offer_requests.create()
19+
.passengers([{"type": "adult"}])
20+
.slices(slices)
21+
.execute()
22+
)
23+
24+
print("Created offer request: %s" % (offer_request.id))
25+
26+
offers = client.offers.list(offer_request.id)
27+
offers_list = list(enumerate(offers))
28+
29+
print("Got %d offers" % len(offers_list))
30+
31+
selected_offer = offers_list[0][1]
32+
33+
print("Selected offer %s to book" % (selected_offer.id))
34+
35+
priced_offer = client.offers.get(selected_offer.id)
36+
37+
print(
38+
"The final price for offer %s is %s (%s)"
39+
% (priced_offer.id, priced_offer.total_amount, priced_offer.total_currency)
40+
)
41+
42+
payments = [
43+
{
44+
"currency": selected_offer.total_currency,
45+
"amount": selected_offer.total_amount,
46+
"type": "balance",
47+
}
48+
]
49+
passengers = [
50+
{
51+
"born_on": "1976-01-21",
52+
"email": "[email protected]",
53+
"family_name": "Corde",
54+
"gender": "f",
55+
"given_name": "Conelia",
56+
"id": offer_request.passengers[0].id,
57+
"phone_number": "+442080160508",
58+
"title": "ms",
59+
}
60+
]
61+
62+
order = (
63+
client.orders.create()
64+
.payments(payments)
65+
.passengers(passengers)
66+
.selected_offers([selected_offer.id])
67+
.execute()
68+
)
69+
70+
print(
71+
"Created order %s with booking reference %s"
72+
% (order.id, order.booking_reference)
73+
)
74+
75+
slices = {
76+
"add": [
77+
{
78+
"cabin_class": "economy",
79+
"departure_date": (departure_date + timedelta(weeks=4)).strftime(
80+
"%Y-%m-%d"
81+
),
82+
"origin": "LHR",
83+
"destination": "STN",
84+
}
85+
],
86+
"remove": [
87+
{
88+
"slice_id": order.slices[0].id,
89+
}
90+
],
91+
}
92+
93+
order_change_request = (
94+
client.order_change_requests.create(order.id).slices(slices).execute()
95+
)
96+
97+
order_change_offers = client.order_change_offers.list(order_change_request.id)
98+
order_change_offers_list = list(enumerate(order_change_offers))
99+
100+
print(
101+
"Got %d options for changing the order; picking first option"
102+
% (len(order_change_offers_list))
103+
)
104+
105+
order_change = client.order_changes.create(order_change_offers_list[0][1].id)
106+
107+
print("Created order change %s, confirming..." % order_change.id)
108+
109+
payment = {
110+
"amount": order_change.change_total_amount,
111+
"currency": order_change.change_total_currency,
112+
"type": "balance",
113+
}
114+
115+
client.order_changes.confirm(order_change.id, payment)
116+
117+
print(
118+
"Processed change to order %s costing %s (%s)"
119+
% (
120+
order.id,
121+
order_change.change_total_amount,
122+
order_change.change_total_currency,
123+
)
124+
)

examples/book-with-seat.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from datetime import date
2+
from decimal import Decimal
3+
4+
from duffel_api import Duffel
5+
6+
7+
if __name__ == "__main__":
8+
print("Duffel Flights API - book with seat example")
9+
client = Duffel()
10+
departure_date = date.today().replace(date.today().year + 1)
11+
slices = [
12+
{
13+
"origin": "LHR",
14+
"destination": "STN",
15+
"departure_date": departure_date.strftime("%Y-%m-%d"),
16+
},
17+
]
18+
offer_request = (
19+
client.offer_requests.create()
20+
.passengers([{"type": "adult"}])
21+
.slices(slices)
22+
.execute()
23+
)
24+
25+
print("Created offer request: %s" % (offer_request.id))
26+
27+
offers = client.offers.list(offer_request.id)
28+
offers_list = list(enumerate(offers))
29+
30+
print("Got %d offers" % len(offers_list))
31+
32+
selected_offer = offers_list[0][1]
33+
34+
print("Selected offer %s to book" % (selected_offer.id))
35+
36+
priced_offer = client.offers.get(selected_offer.id)
37+
38+
print(
39+
"The final price for offer %s is %s (%s)"
40+
% (priced_offer.id, priced_offer.total_amount, priced_offer.total_currency)
41+
)
42+
43+
seat_maps = client.seat_maps.get(priced_offer.id)
44+
45+
available_seats = []
46+
for _idx, row in enumerate(seat_maps[0].cabins[0].rows):
47+
for _idx, section in enumerate(row.sections):
48+
for _idx, element in enumerate(section.elements):
49+
if (
50+
element.type == "seat"
51+
and element.available_services is not None
52+
and len(element.available_services) > 0
53+
):
54+
available_seats.append(element)
55+
56+
available_seat = available_seats[0]
57+
58+
available_seat_service = available_seat.available_services[0]
59+
60+
print(
61+
"Adding seat %s costing %s (%s)"
62+
% (
63+
available_seat.designator,
64+
available_seat_service.total_amount,
65+
available_seat_service.total_currency,
66+
)
67+
)
68+
69+
total_amount = str(
70+
Decimal(priced_offer.total_amount)
71+
+ Decimal(available_seat_service.total_amount)
72+
)
73+
74+
services = [
75+
{
76+
"id": available_seat_service.id,
77+
"quantity": 1,
78+
}
79+
]
80+
payments = [
81+
{
82+
"currency": priced_offer.total_currency,
83+
"amount": total_amount,
84+
"type": "balance",
85+
}
86+
]
87+
passengers = [
88+
{
89+
"born_on": "1976-01-21",
90+
"email": "[email protected]",
91+
"family_name": "Corde",
92+
"gender": "f",
93+
"given_name": "Conelia",
94+
"id": offer_request.passengers[0].id,
95+
"phone_number": "+442080160508",
96+
"title": "ms",
97+
}
98+
]
99+
100+
order = (
101+
client.orders.create()
102+
.payments(payments)
103+
.passengers(passengers)
104+
.selected_offers([selected_offer.id])
105+
.services(services)
106+
.execute()
107+
)
108+
109+
print(
110+
"Created order %s with booking reference %s"
111+
% (order.id, order.booking_reference)
112+
)

examples/hold-and-pay-later.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from datetime import date
2+
3+
from duffel_api import Duffel
4+
5+
6+
if __name__ == "__main__":
7+
print("Duffel Flights API - book hold order and pay later example")
8+
client = Duffel()
9+
departure_date = date.today().replace(date.today().year + 1)
10+
slices = [
11+
{
12+
"origin": "LHR",
13+
"destination": "STN",
14+
"departure_date": departure_date.strftime("%Y-%m-%d"),
15+
},
16+
]
17+
offer_request = (
18+
client.offer_requests.create()
19+
.passengers([{"type": "adult"}])
20+
.slices(slices)
21+
.execute()
22+
)
23+
24+
print("Created offer request: %s" % (offer_request.id))
25+
26+
offers = client.offers.list(offer_request.id)
27+
offers_list = list(enumerate(offers))
28+
29+
print("Got %d offers" % len(offers_list))
30+
31+
selected_offer = offers_list[0][1]
32+
33+
print("Selected offer %s to book" % (selected_offer.id))
34+
35+
priced_offer = client.offers.get(selected_offer.id)
36+
passengers = [
37+
{
38+
"born_on": "1976-01-21",
39+
"email": "[email protected]",
40+
"family_name": "Corde",
41+
"gender": "f",
42+
"given_name": "Conelia",
43+
"id": offer_request.passengers[0].id,
44+
"phone_number": "+442080160508",
45+
"title": "ms",
46+
}
47+
]
48+
49+
order = (
50+
client.orders.create()
51+
.passengers(passengers)
52+
.selected_offers([selected_offer.id])
53+
.hold()
54+
.execute()
55+
)
56+
57+
print(
58+
"Created hold order %s with booking reference %s"
59+
% (order.id, order.booking_reference)
60+
)
61+
62+
updated_order = client.orders.get(order.id)
63+
64+
print(
65+
"Retrieved order and up-to-date price is %s (%s)"
66+
% (updated_order.total_amount, updated_order.total_currency)
67+
)
68+
69+
payment = (
70+
client.payments.create()
71+
.order(order.id)
72+
.payment(
73+
{
74+
"currency": updated_order.total_currency,
75+
"amount": updated_order.total_amount,
76+
"type": "balance",
77+
}
78+
)
79+
.execute()
80+
)
81+
82+
print("Paid for order %s with payment %s" % (order.id, payment.id))
83+
84+
paid_order = client.orders.get(order.id)
85+
86+
print("After payment, order has %d documents" % len(paid_order.documents))

0 commit comments

Comments
 (0)