Opt-in typed views over Duffel API responses.
Resource functions return raw string-keyed maps — that is the default and
it does not change. When you want a struct with named fields instead, pass
the map to the matching schema's from_map/1:
{:ok, order} = Duffel.Orders.get(client, "ord_123")
order = Duffel.Schema.Order.from_map(order)
order.booking_reference
#=> "RZPNX8"Nested resources that have their own schema are decoded too, so
order.slices is a list of Duffel.Schema.Slice structs and each
slice.segments is a list of Duffel.Schema.Segment structs.
Fields without a dedicated schema (an offer's owner airline, a place, a
payment requirement) are left as raw maps. Decoding is shallow and total:
unknown keys are dropped, missing keys become nil, missing lists become
[], and a missing nested object stays nil.
Decoding is also safe to repeat — handing from_map/1 something it has
already decoded gives the same struct back, so mapping over a list twice
costs nothing and raises nothing.
Schemas cover three areas:
Flights:
Duffel.Schema.OfferRequest,Duffel.Schema.Offer,Duffel.Schema.Order,Duffel.Schema.Slice,Duffel.Schema.Segment,Duffel.Schema.PassengerandDuffel.Schema.Payment. A search asked for withview: "itineraries"comes back in a different shape, and has its own schema inDuffel.Schema.ItineraryView.Stays:
Duffel.Schema.Stays.SearchResult,Duffel.Schema.Stays.Accommodation,Duffel.Schema.Stays.Room,Duffel.Schema.Stays.Rate,Duffel.Schema.Stays.QuoteandDuffel.Schema.Stays.Booking.Cars:
Duffel.Schema.Cars.Search,Duffel.Schema.Cars.Rate,Duffel.Schema.Cars.QuoteandDuffel.Schema.Cars.Booking.
To decode a page of results, map over its data:
{:ok, page} = Duffel.Orders.list(client)
orders = Enum.map(page.data, &Duffel.Schema.Order.from_map/1)
Summary
Functions
Decodes a single raw map into a struct of module.
Decodes a list of raw maps into structs of module.
Functions
Decodes a single raw map into a struct of module.
Returns nil when given nil, so a missing nested field stays nil
rather than becoming an empty struct.
Decodes a list of raw maps into structs of module.
Returns [] when given nil, so a missing list field decodes to an empty
list rather than nil.