forked from airheartdev/duffel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Batch Offer Requests (#12)
* fix(duffel): fix panic when req.Body == nil * feat: add Batch Offer Requests
- Loading branch information
Showing
7 changed files
with
456 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package duffel | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type ( | ||
BatchOfferRequestClient interface { | ||
CreateBatchOfferRequest(ctx context.Context, requestInput CreateBatchOfferRequestInput) (*BatchOfferRequest, error) | ||
GetBatchOfferRequest(ctx context.Context, id string) (*BatchOfferRequest, error) | ||
} | ||
|
||
CreateBatchOfferRequestInput struct { | ||
// The passengers who want to travel. If you specify an age for a passenger, the type may differ for the same passenger in different offers due to airline's different rules. e.g. one airline may treat a 14 year old as an adult, and another as a young adult. You may only specify an age or a type – not both. | ||
Passengers []OfferRequestPassenger `json:"passengers" url:"-"` | ||
// The slices that make up this offer request. One-way journeys can be expressed using one slice, whereas return trips will need two. | ||
Slices []OfferRequestSlice `json:"slices" url:"-"` | ||
// The cabin that the passengers want to travel in | ||
CabinClass CabinClass `json:"cabin_class" url:"-"` | ||
// The maximum number of connections within any slice of the offer. For example 0 means a direct flight which will have a single segment within each slice and 1 means a maximum of two segments within each slice of the offer. | ||
MaxConnections *int `json:"max_connections,omitempty" url:"-"` | ||
// The maximum amount of time in milliseconds to wait for each airline to respond | ||
SupplierTimeout int `json:"-" url:"supplier_timeout,omitempty"` | ||
} | ||
|
||
BatchOfferRequest struct { | ||
TotalBatches int `json:"total_batches"` | ||
RemainingBatches int `json:"remaining_batches"` | ||
ID string `json:"id"` | ||
Offers []Offer `json:"offers,omitempty"` | ||
CreatedAt DateTime `json:"created_at"` | ||
} | ||
) | ||
|
||
func (a *API) CreateBatchOfferRequest(ctx context.Context, requestInput CreateBatchOfferRequestInput) (*BatchOfferRequest, error) { | ||
return newRequestWithAPI[CreateBatchOfferRequestInput, BatchOfferRequest](a). | ||
Post("/air/batch_offer_requests", &requestInput). | ||
Single(ctx) | ||
} | ||
|
||
func (a *API) GetBatchOfferRequest(ctx context.Context, id string) (*BatchOfferRequest, error) { | ||
return newRequestWithAPI[EmptyPayload, BatchOfferRequest](a). | ||
Getf("/air/batch_offer_requests/%s", id). | ||
Single(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package duffel | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/h2non/gock.v1" | ||
) | ||
|
||
func TestCreateBatchOfferRequest(t *testing.T) { | ||
defer gock.Off() | ||
a := assert.New(t) | ||
gock.New("https://api.duffel.com"). | ||
Post("/air/batch_offer_requests"). | ||
Reply(200). | ||
SetHeader("Ratelimit-Limit", "5"). | ||
SetHeader("Ratelimit-Remaining", "5"). | ||
SetHeader("Ratelimit-Reset", time.Now().Format(time.RFC1123)). | ||
SetHeader("Date", time.Now().Format(time.RFC1123)). | ||
File("fixtures/200-create-batch-offer-request.json") | ||
|
||
ctx := context.TODO() | ||
|
||
client := New("duffel_test_123") | ||
data, err := client.CreateBatchOfferRequest(ctx, CreateBatchOfferRequestInput{ | ||
Passengers: []OfferRequestPassenger{ | ||
{ | ||
FamilyName: "Earhardt", | ||
GivenName: "Amelia", | ||
Type: PassengerTypeAdult, | ||
}, | ||
{ | ||
Age: 14, | ||
}, | ||
}, | ||
CabinClass: CabinClassEconomy, | ||
Slices: []OfferRequestSlice{ | ||
{ | ||
DepartureDate: Date(time.Now().AddDate(0, 0, 7)), | ||
Origin: "JFK", | ||
Destination: "AUS", | ||
}, | ||
}, | ||
}) | ||
a.NoError(err) | ||
a.NotNil(data) | ||
|
||
a.Equal(7, data.RemainingBatches) | ||
a.Equal(7, data.TotalBatches) | ||
} | ||
|
||
func TestGetBatchOfferRequest(t *testing.T) { | ||
defer gock.Off() | ||
a := assert.New(t) | ||
gock.New("https://api.duffel.com"). | ||
Get("/air/batch_offer_requests/orq_0000AhTmH2Thpl6RrM97qK"). | ||
Reply(200). | ||
SetHeader("Ratelimit-Limit", "5"). | ||
SetHeader("Ratelimit-Remaining", "5"). | ||
SetHeader("Ratelimit-Reset", time.Now().Format(time.RFC1123)). | ||
SetHeader("Date", time.Now().Format(time.RFC1123)). | ||
File("fixtures/200-get-batch-offer-request.json") | ||
|
||
ctx := context.TODO() | ||
|
||
client := New("duffel_test_123") | ||
|
||
data, err := client.GetBatchOfferRequest(ctx, "orq_0000AhTmH2Thpl6RrM97qK") | ||
a.NoError(err) | ||
a.NotNil(data) | ||
|
||
a.Equal(2, data.TotalBatches) | ||
a.Equal(2, data.RemainingBatches) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"data": { | ||
"remaining_batches": 7, | ||
"total_batches": 7, | ||
"client_key": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTQ3MzU4NDAsImxpdmVfbW9kZSI6ZmFsc2UsIm9yZ2FuaXNhdGlvbl9pZCI6Im9yZ18wMDAwQU4xdXZobHNaQlF6RDhPSTFZIn0.1dOy3eTaUskIgfphTgAlW0aStrwklPModabSf-Znam0", | ||
"created_at": "2024-05-02T11:30:40.589820Z", | ||
"live_mode": false, | ||
"id": "orq_0000AhTmH2Thpl6RrM97qK" | ||
} | ||
} |
Oops, something went wrong.