Open cglacet opened 1 year ago
Here is how it would look like:
client = Duffel(access_token="duffel_test_Utbb-kmsFGL44mi0Ixz8-jOpQzS7OmlkGuxfxX-F4rQ")
client.start() # TCP handshake, once and for all
offer_requests = client.offer_requests.list()
async for offer_request in offer_requests:
print(offer_request.id)
And with a more complete example (translating the one linked in the documentation):
destination = "CDG"
origin = "JFK"
departure_date = "2023-12-01"
slices = [
{
"origin": origin,
"destination": destination,
"departure_date": departure_date,
},
]
offer_request = await (
client.offer_requests.create()
.passengers([{"type": "adult"}])
.slices(slices)
.return_offers()
.execute()
)
offers = offer_request.offers
for idx, offer in enumerate(offers[:5]):
print(
f"{idx + 1}. {offer.owner.name} flight departing at "
+ f"{offer.slices[0].segments[0].departing_at} "
+ f"{offer.total_amount} {offer.total_currency}"
)
offer_index = 0
given_name = "Christian"
family_name = "Glacet"
dob = "1986-07-31"
title = "mr"
gender = "m"
phone_number = "+33624000097"
email = "xxxx@gmail.com"
print(f"\nHang tight! Booking offer {offer_index}...")
selected_offer = offers[int(offer_index) - 1]
payments = [
{
"currency": selected_offer.total_currency,
"amount": selected_offer.total_amount,
"type": "balance",
}
]
passengers = [
{
"phone_number": phone_number,
"email": email,
"title": title,
"gender": gender,
"family_name": family_name,
"given_name": given_name,
"born_on": dob,
"id": offer_request.passengers[0].id,
}
]
order = await (
client.orders.create()
.payments(payments)
.passengers(passengers)
.selected_offers([selected_offer.id])
.execute()
)
print("\n🎉 Flight booked. Congrats! You can start packing your (duffel?) bags")
print(f"Booking reference: {order.booking_reference}")
Hey @cglacet,
Thank you for the suggestion. I think a lot of the criticism is valid. We won't be taking this right this moment but definitely something we'll consider.
Hi @nlopes, don't hesitate to tag me here in case you need help. Depending on the timing I could help you find a good solution to this.
Is your feature request related to a problem? Please describe. The issue is that many (most?) modern python web frameworks are working in an asynchronous fashion (ASGI). The current API you are offering is purely synchronous which make it "impossible" to use with such frameworks.
Describe the solution you'd like Most of the time I'm using aiohttp, its interface is very close to
requests
which makes it a good candidate.Describe alternatives you've considered I'm currently re-writing it for myself.
Remark
Also, that's a bit related so I will answer this remark from your code:
I think it's not too much of a problem, the real problem is underlying. You are creating one session per init (and therefore per inherited class), the code looks a bit like this:
I think you should spawn a single HTTP session for all APIs (unless there is a good reason not to). Then, you could use that unique session in all subclasses (passing it as argument to the
HTTpClient
). You don't even need to have a singleton for that an can simply use a lazy property directly inDuffel
client, like so (the code is already usingaiohttp
because I've already rewrote this part):This way, all HTTP requests will use the same session (single TCP handshake).
You could also directly offer the enduser the opportunity to start the session on its own (for example when the server starts).