Skip to content

Commit

Permalink
Merge pull request #12 from airheartdev/feature/better-seatmap-support
Browse files Browse the repository at this point in the history
Fixing Seat map support
  • Loading branch information
ivanvanderbyl authored Jun 27, 2022
2 parents f6c74df + 2a44f4c commit 910f27a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
28 changes: 27 additions & 1 deletion examples/offer-requests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
}
Expand Down
2 changes: 2 additions & 0 deletions offers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions seatmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)

Expand All @@ -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 {
Expand Down
21 changes: 9 additions & 12 deletions seatmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 910f27a

Please sign in to comment.