From 2a44f4cf625a2b0ad3ab585fd12d59e735a41c84 Mon Sep 17 00:00:00 2001 From: Ivan Vanderbyl Date: Mon, 27 Jun 2022 12:05:15 +1000 Subject: [PATCH] Fixing Seatmap support --- examples/offer-requests/main.go | 28 +++++++++++++++++++++++++++- offers.go | 2 ++ seatmaps.go | 13 +++++++++---- seatmaps_test.go | 21 +++++++++------------ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/examples/offer-requests/main.go b/examples/offer-requests/main.go index f3fc536..15cdc2e 100644 --- a/examples/offer-requests/main.go +++ b/examples/offer-requests/main.go @@ -17,10 +17,11 @@ import ( func main() { apiToken := os.Getenv("DUFFEL_TOKEN") client := duffel.New(apiToken, duffel.WithDebug()) + ctx := context.Background() // childAge := 1 - data, err := client.CreateOfferRequest(context.Background(), duffel.OfferRequestInput{ + data, err := client.CreateOfferRequest(ctx, duffel.OfferRequestInput{ ReturnOffers: true, Passengers: []duffel.OfferRequestPassenger{ @@ -70,6 +71,10 @@ func main() { fmt.Println() for _, offer := range data.Offers { + // if offer.Owner.IATACode != "AA" { + // continue + // } + fmt.Printf("===> Offer %s from %s\n Passengers: ", offer.ID, offer.Owner.Name) for i, p := range offer.Passengers { fmt.Printf("(%s) %s %s", p.Type, p.GivenName, p.FamilyName) @@ -92,7 +97,28 @@ func main() { } fmt.Printf(" 🛬 %s • %s\n", s.FareBrandName, time.Duration(s.Duration).String()) + } + + // seats, err := client.SeatmapForOffer(ctx, offer) + // if err != nil { + // log.Fatalln(err) + // } + + // for _, seat := range seats { + // for _, cab := range seat.Cabins { + // for _, row := range cab.Rows { + // fmt.Println() + // for _, sec := range row.Sections { + // fmt.Println() + // for _, el := range sec.Elements { + // fmt.Printf("%s ", el.Designator) + // } + // } + // } + // } + // } + fmt.Println() } } diff --git a/offers.go b/offers.go index 5882a6e..f795f0c 100644 --- a/offers.go +++ b/offers.go @@ -37,8 +37,10 @@ type ( Owner Airline `json:"owner"` Slices []Slice `json:"slices"` Passengers []OfferRequestPassenger `json:"passengers"` + Partial bool `json:"partial"` PassengerIdentityDocumentsRequired bool `json:"passenger_identity_documents_required"` PaymentRequirements OfferPaymentRequirement `json:"payment_requirements"` + AvailableServices []Service `json:"available_services"` } OfferPaymentRequirement struct { diff --git a/seatmaps.go b/seatmaps.go index 8ae5c7b..592005a 100644 --- a/seatmaps.go +++ b/seatmaps.go @@ -80,8 +80,9 @@ type ( } SeatmapClient interface { - // GetSeatmaps returns an iterator for the seatmaps of a given Offer. - GetSeatmaps(ctx context.Context, offerID string) *Iter[Seatmap] + // GetSeatmap returns an iterator for the seatmaps of a given Offer. + GetSeatmap(ctx context.Context, offerID string) ([]*Seatmap, error) + SeatmapForOffer(ctx context.Context, offer Offer) ([]*Seatmap, error) } ) @@ -100,11 +101,15 @@ func (e ElementType) String() string { return string(e) } -func (a *API) GetSeatmaps(ctx context.Context, offerID string) *Iter[Seatmap] { +func (a *API) SeatmapForOffer(ctx context.Context, offer Offer) ([]*Seatmap, error) { + return a.GetSeatmap(ctx, offer.ID) +} + +func (a *API) GetSeatmap(ctx context.Context, offerID string) ([]*Seatmap, error) { return newRequestWithAPI[EmptyPayload, Seatmap](a). Get("/air/seat_maps"). WithParam("offer_id", offerID). - Iter(ctx) + Slice(ctx) } func (s *SectionService) TotalAmount() currency.Amount { diff --git a/seatmaps_test.go b/seatmaps_test.go index efd6750..c4f1b93 100644 --- a/seatmaps_test.go +++ b/seatmaps_test.go @@ -30,21 +30,18 @@ func TestGetSeatmaps(t *testing.T) { ctx := context.TODO() client := New("duffel_test_123") - iter := client.GetSeatmaps(ctx, "off_00009htYpSCXrwaB9DnUm0") - - iter.Next() - data := iter.Current() - err := iter.Err() - reqID, _ := iter.LastRequestID() - + seats, err := client.GetSeatmap(ctx, "off_00009htYpSCXrwaB9DnUm0") a.NoError(err) - a.NotNil(data) + reqID, _ := client.LastRequestID() + + a.NotNil(seats) a.Equal("FvxRwfnMtKgc0EwCCoXE", reqID) - a.Equal("sea_00003hthlsHZ8W4LxXjkzo", data.ID) - a.Equal("seg_00009htYpSCXrwaB9Dn456", data.SegmentID) - a.Equal("sli_00009htYpSCXrwaB9Dn123", data.SliceID) + seat := seats[0] + a.Equal("sea_00003hthlsHZ8W4LxXjkzo", seat.ID) + a.Equal("seg_00009htYpSCXrwaB9Dn456", seat.SegmentID) + a.Equal("sli_00009htYpSCXrwaB9Dn123", seat.SliceID) - serviceAmount := data.Cabins[0].Rows[0].Sections[0].Elements[0].AvailableServices[0].TotalAmount().String() + serviceAmount := seat.Cabins[0].Rows[0].Sections[0].Elements[0].AvailableServices[0].TotalAmount().String() a.Equal("30.00 GBP", serviceAmount) }