From ca3f8a7976f24910d102132b6617d9253d9af85c Mon Sep 17 00:00:00 2001 From: Nate Brennand Date: Sat, 29 Oct 2022 18:27:06 -0700 Subject: [PATCH 1/4] update pr template --- PULL_REQUEST_TEMPLATE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md index f8e58f655..a8160cd40 100644 --- a/PULL_REQUEST_TEMPLATE.md +++ b/PULL_REQUEST_TEMPLATE.md @@ -22,7 +22,7 @@ A short description of what this PR does. - [x] I acknowledge that all my contributions will be made under the project's license - [ ] Run `make test-docker` - [ ] Verify affected language: - - [ ] Generate [twilio-go](https://github.com/twilio/twilio-go) from our [OpenAPI specification](https://github.com/twilio/twilio-oai) using the [build_twilio_go.py](./examples/build_twilio_go.py) using `python examples/build_twilio_go.py path/to/twilio-oai/spec/yaml path/to/twilio-go` and inspect the diff + - [ ] Generate [twilio-go](https://github.com/twilio/twilio-go) from our [OpenAPI specification](https://github.com/twilio/twilio-oai) using the [build_twilio_library.py](./scripts/build_twilio_library.py) using `python3 scripts/build_twilio_library.py -lang go path/to/twilio-oai/spec/yaml path/to/twilio-go` and inspect the diff - [ ] Run `make test` in `twilio-go` - [ ] Create a pull request in `twilio-go` - [ ] Provide a link below to the pull request From cb960beb3f7f88e9b9d2dfeb0ad69acf9a9fbba7 Mon Sep 17 00:00:00 2001 From: Nate Brennand Date: Sat, 29 Oct 2022 18:10:08 -0700 Subject: [PATCH 2/4] [go-client] update templates to use context methods --- src/main/resources/twilio-go/api.mustache | 41 +++++++++++++++---- .../resources/twilio-go/api_service.mustache | 10 ++++- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/resources/twilio-go/api.mustache b/src/main/resources/twilio-go/api.mustache index 2e5c89a9d..1369c4ef6 100644 --- a/src/main/resources/twilio-go/api.mustache +++ b/src/main/resources/twilio-go/api.mustache @@ -3,6 +3,7 @@ package {{packageName}} {{#operations}} import ( + "context" "encoding/json" "fmt" "net/url" @@ -37,6 +38,13 @@ func (params *{{{nickname}}}Params) Set{{paramName}}({{paramName}} {{{dataType}} // {{notes}} {{/notes}} func (c *ApiService) {{{nickname}}}({{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}params *{{{nickname}}}Params{{/hasOptionalParams}}) ({{#returnType}}*{{{returnType}}}, {{/returnType}}error) { + return c.{{{nickname}}}WithCtx(context.TODO(), {{#allParams}}{{#required}}{{paramName}} {{^last}}, {{/last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}params {{/hasOptionalParams}}) +} + +{{#notes}} +// {{notes}} +{{/notes}} +func (c *ApiService) {{{nickname}}}WithCtx(ctx context.Context, {{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}{{#hasOptionalParams}}params *{{{nickname}}}Params{{/hasOptionalParams}}) ({{#returnType}}*{{{returnType}}}, {{/returnType}}error) { path := "{{{path}}}" {{>partial_serialization}} @@ -48,7 +56,7 @@ func (c *ApiService) {{{nickname}}}({{#allParams}}{{#required}}{{paramName}} {{{ {{/headerParams}} {{/hasHeaderParams}} - resp, err := c.requestHandler.{{httpMethod}}(c.baseURL+path, data, headers) + resp, err := c.requestHandler.{{httpMethod}}(ctx, c.baseURL+path, data, headers) {{#returnType}} if err != nil { return nil, err @@ -77,6 +85,11 @@ func (c *ApiService) {{{nickname}}}({{#allParams}}{{#required}}{{paramName}} {{{ {{#vendorExtensions.x-is-read-operation}} // Retrieve a single page of {{{vendorExtensions.x-domain-name}}} records from the API. Request is executed immediately. func (c *ApiService) Page{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params, pageToken, pageNumber string) (*{{{returnContainer}}}, error) { + return c.Page{{{vendorExtensions.x-domain-name}}}WithCtx(context.TODO(), {{#allParams}}{{#required}}{{paramName}} {{^last}}, {{/last}}{{/required}}{{/allParams}}params , pageToken, pageNumber) +} + +// Retrieve a single page of {{{vendorExtensions.x-domain-name}}} records from the API. Request is executed immediately. +func (c *ApiService) Page{{{vendorExtensions.x-domain-name}}}WithCtx(ctx context.Context, {{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params, pageToken, pageNumber string) (*{{{returnContainer}}}, error) { path := "{{{path}}}" {{>partial_serialization}} @@ -87,7 +100,7 @@ func (c *ApiService) Page{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#r data.Set("Page", pageNumber) } - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -104,7 +117,12 @@ func (c *ApiService) Page{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#r // Lists {{{vendorExtensions.x-domain-name}}} records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. func (c *ApiService) List{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params) ({{{returnType}}}, error) { - response, errors := c.Stream{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#required}}{{paramName}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params) + return c.List{{{vendorExtensions.x-domain-name}}}WithCtx(context.TODO(), {{#allParams}}{{#required}}{{paramName}} {{^last}}, {{/last}}{{/required}}{{/allParams}}params) +} + +// Lists {{{vendorExtensions.x-domain-name}}} records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. +func (c *ApiService) List{{{vendorExtensions.x-domain-name}}}WithCtx(ctx context.Context, {{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params) ({{{returnType}}}, error) { + response, errors := c.Stream{{{vendorExtensions.x-domain-name}}}WithCtx(ctx, {{#allParams}}{{#required}}{{paramName}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params) records := make({{{returnType}}}, 0) for record := range response { @@ -120,6 +138,11 @@ func (c *ApiService) List{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#r // Streams {{{vendorExtensions.x-domain-name}}} records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. func (c *ApiService) Stream{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params) (chan {{{returnBaseType}}}, chan error) { + return c.Stream{{{vendorExtensions.x-domain-name}}}WithCtx(context.TODO(), {{#allParams}}{{#required}}{{paramName}} {{^last}}, {{/last}}{{/required}}{{/allParams}}params) +} + +// Streams {{{vendorExtensions.x-domain-name}}} records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. +func (c *ApiService) Stream{{{vendorExtensions.x-domain-name}}}WithCtx(ctx context.Context, {{#allParams}}{{#required}}{{paramName}} {{{dataType}}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params *{{{nickname}}}Params) (chan {{{returnBaseType}}}, chan error) { if params == nil { params = &{{{nickname}}}Params{} } @@ -128,20 +151,20 @@ func (c *ApiService) Stream{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{ recordChannel := make(chan {{{returnBaseType}}}, 1) errorChannel := make(chan error, 1) - response, err := c.Page{{{vendorExtensions.x-domain-name}}}({{#allParams}}{{#required}}{{paramName}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params, "", "") + response, err := c.Page{{{vendorExtensions.x-domain-name}}}WithCtx(ctx, {{#allParams}}{{#required}}{{paramName}}{{^last}}, {{/last}}{{/required}}{{/allParams}}params, "", "") if err != nil { errorChannel <- err close(recordChannel) close(errorChannel) } else { - go c.stream{{{vendorExtensions.x-domain-name}}}(response, params, recordChannel, errorChannel) + go c.stream{{{vendorExtensions.x-domain-name}}}(ctx, response, params, recordChannel, errorChannel) } return recordChannel, errorChannel } -func (c *ApiService) stream{{{vendorExtensions.x-domain-name}}}(response *{{{returnContainer}}}, params *{{{nickname}}}Params, recordChannel chan {{{returnBaseType}}}, errorChannel chan error) { +func (c *ApiService) stream{{{vendorExtensions.x-domain-name}}}(ctx context.Context, response *{{{returnContainer}}}, params *{{{nickname}}}Params, recordChannel chan {{{returnBaseType}}}, errorChannel chan error) { curRecord := 1 for response != nil { @@ -156,7 +179,7 @@ func (c *ApiService) stream{{{vendorExtensions.x-domain-name}}}(response *{{{ret } } - record, err := client.GetNext(c.baseURL, response, c.getNext{{{returnContainer}}}) + record, err := client.GetNextWithCtx(ctx, c.baseURL, response, c.getNext{{{returnContainer}}}) if err != nil { errorChannel <- err break @@ -171,11 +194,11 @@ func (c *ApiService) stream{{{vendorExtensions.x-domain-name}}}(response *{{{ret close(errorChannel) } -func (c *ApiService) getNext{{{returnContainer}}}(nextPageUrl string) (interface{}, error) { +func (c *ApiService) getNext{{{returnContainer}}}(ctx context.Context, nextPageUrl string) (interface{}, error) { if nextPageUrl == "" { return nil, nil } - resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) + resp, err := c.requestHandler.Get(ctx, nextPageUrl, nil, nil) if err != nil { return nil, err } diff --git a/src/main/resources/twilio-go/api_service.mustache b/src/main/resources/twilio-go/api_service.mustache index bc9e96477..49653d240 100644 --- a/src/main/resources/twilio-go/api_service.mustache +++ b/src/main/resources/twilio-go/api_service.mustache @@ -7,10 +7,14 @@ import ( type ApiService struct { baseURL string - requestHandler *twilio.RequestHandler + requestHandler *twilio.RequestHandlerWithCtx } func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { + return NewApiServiceWithCtx(twilio.UpgradeRequestHandler(requestHandler)) +} + +func NewApiServiceWithCtx(requestHandler *twilio.RequestHandlerWithCtx) *ApiService { return &ApiService { requestHandler: requestHandler, baseURL: "{{#apiInfo}}{{#apis}}{{#operations}}{{#-first}}{{#operation}}{{#-first}}{{#servers}}{{url}}{{/servers}}{{/-first}}{{/operation}}{{/-first}}{{/operations}}{{/apis}}{{/apiInfo}}", @@ -20,3 +24,7 @@ func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { func NewApiServiceWithClient(client twilio.BaseClient) *ApiService { return NewApiService(twilio.NewRequestHandler(client)) } + +func NewApiServiceWithClientWithCtx(client twilio.BaseClientWithCtx) *ApiService { + return NewApiServiceWithCtx(twilio.NewRequestHandlerWithCtx(client)) +} From 2ec6804b6926c2e0fb7426e68379150f76c525bc Mon Sep 17 00:00:00 2001 From: Nate Brennand Date: Sat, 29 Oct 2022 18:11:05 -0700 Subject: [PATCH 3/4] regenerate examples --- .../helper/rest/api/v2010/accounts.go | 56 +++++++++++++++---- .../helper/rest/api/v2010/accounts_calls.go | 19 ++++++- .../v2010/accounts_calls_feedback_summary.go | 7 ++- .../helper/rest/api/v2010/api_service.go | 10 +++- .../helper/rest/flex/v1/api_service.go | 10 +++- .../helper/rest/flex/v1/credentials_aws.go | 56 +++++++++++++++---- .../rest/flex/v1/credentials_aws_history.go | 7 ++- .../go/go-client/helper/rest/flex/v1/voice.go | 7 ++- 8 files changed, 140 insertions(+), 32 deletions(-) diff --git a/examples/go/go-client/helper/rest/api/v2010/accounts.go b/examples/go/go-client/helper/rest/api/v2010/accounts.go index 2da3f9447..b77fe1655 100644 --- a/examples/go/go-client/helper/rest/api/v2010/accounts.go +++ b/examples/go/go-client/helper/rest/api/v2010/accounts.go @@ -15,6 +15,7 @@ package openapi import ( + "context" "encoding/json" "fmt" "net/url" @@ -48,6 +49,10 @@ func (params *CreateAccountParams) SetRecordingStatusCallbackEvent(RecordingStat } func (c *ApiService) CreateAccount(params *CreateAccountParams) (*TestResponseObject, error) { + return c.CreateAccountWithCtx(context.TODO(), params) +} + +func (c *ApiService) CreateAccountWithCtx(ctx context.Context, params *CreateAccountParams) (*TestResponseObject, error) { path := "/2010-04-01/Accounts.json" data := url.Values{} @@ -66,7 +71,7 @@ func (c *ApiService) CreateAccount(params *CreateAccountParams) (*TestResponseOb headers["X-Twilio-Webhook-Enabled"] = *params.XTwilioWebhookEnabled } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -82,13 +87,17 @@ func (c *ApiService) CreateAccount(params *CreateAccountParams) (*TestResponseOb } func (c *ApiService) DeleteAccount(Sid string) error { + return c.DeleteAccountWithCtx(context.TODO(), Sid) +} + +func (c *ApiService) DeleteAccountWithCtx(ctx context.Context, Sid string) error { path := "/2010-04-01/Accounts/{Sid}.json" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Delete(ctx, c.baseURL+path, data, headers) if err != nil { return err } @@ -99,13 +108,17 @@ func (c *ApiService) DeleteAccount(Sid string) error { } func (c *ApiService) FetchAccount(Sid string) (*TestResponseObject, error) { + return c.FetchAccountWithCtx(context.TODO(), Sid) +} + +func (c *ApiService) FetchAccountWithCtx(ctx context.Context, Sid string) (*TestResponseObject, error) { path := "/2010-04-01/Accounts/{Sid}.json" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -163,6 +176,11 @@ func (params *ListAccountParams) SetLimit(Limit int) *ListAccountParams { // Retrieve a single page of Account records from the API. Request is executed immediately. func (c *ApiService) PageAccount(params *ListAccountParams, pageToken, pageNumber string) (*ListAccountResponse, error) { + return c.PageAccountWithCtx(context.TODO(), params, pageToken, pageNumber) +} + +// Retrieve a single page of Account records from the API. Request is executed immediately. +func (c *ApiService) PageAccountWithCtx(ctx context.Context, params *ListAccountParams, pageToken, pageNumber string) (*ListAccountResponse, error) { path := "/2010-04-01/Accounts.json" data := url.Values{} @@ -191,7 +209,7 @@ func (c *ApiService) PageAccount(params *ListAccountParams, pageToken, pageNumbe data.Set("Page", pageNumber) } - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -208,7 +226,12 @@ func (c *ApiService) PageAccount(params *ListAccountParams, pageToken, pageNumbe // Lists Account records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. func (c *ApiService) ListAccount(params *ListAccountParams) ([]TestResponseObject, error) { - response, errors := c.StreamAccount(params) + return c.ListAccountWithCtx(context.TODO(), params) +} + +// Lists Account records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. +func (c *ApiService) ListAccountWithCtx(ctx context.Context, params *ListAccountParams) ([]TestResponseObject, error) { + response, errors := c.StreamAccountWithCtx(ctx, params) records := make([]TestResponseObject, 0) for record := range response { @@ -224,6 +247,11 @@ func (c *ApiService) ListAccount(params *ListAccountParams) ([]TestResponseObjec // Streams Account records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. func (c *ApiService) StreamAccount(params *ListAccountParams) (chan TestResponseObject, chan error) { + return c.StreamAccountWithCtx(context.TODO(), params) +} + +// Streams Account records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. +func (c *ApiService) StreamAccountWithCtx(ctx context.Context, params *ListAccountParams) (chan TestResponseObject, chan error) { if params == nil { params = &ListAccountParams{} } @@ -232,19 +260,19 @@ func (c *ApiService) StreamAccount(params *ListAccountParams) (chan TestResponse recordChannel := make(chan TestResponseObject, 1) errorChannel := make(chan error, 1) - response, err := c.PageAccount(params, "", "") + response, err := c.PageAccountWithCtx(ctx, params, "", "") if err != nil { errorChannel <- err close(recordChannel) close(errorChannel) } else { - go c.streamAccount(response, params, recordChannel, errorChannel) + go c.streamAccount(ctx, response, params, recordChannel, errorChannel) } return recordChannel, errorChannel } -func (c *ApiService) streamAccount(response *ListAccountResponse, params *ListAccountParams, recordChannel chan TestResponseObject, errorChannel chan error) { +func (c *ApiService) streamAccount(ctx context.Context, response *ListAccountResponse, params *ListAccountParams, recordChannel chan TestResponseObject, errorChannel chan error) { curRecord := 1 for response != nil { @@ -259,7 +287,7 @@ func (c *ApiService) streamAccount(response *ListAccountResponse, params *ListAc } } - record, err := client.GetNext(c.baseURL, response, c.getNextListAccountResponse) + record, err := client.GetNextWithCtx(ctx, c.baseURL, response, c.getNextListAccountResponse) if err != nil { errorChannel <- err break @@ -274,11 +302,11 @@ func (c *ApiService) streamAccount(response *ListAccountResponse, params *ListAc close(errorChannel) } -func (c *ApiService) getNextListAccountResponse(nextPageUrl string) (interface{}, error) { +func (c *ApiService) getNextListAccountResponse(ctx context.Context, nextPageUrl string) (interface{}, error) { if nextPageUrl == "" { return nil, nil } - resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) + resp, err := c.requestHandler.Get(ctx, nextPageUrl, nil, nil) if err != nil { return nil, err } @@ -310,6 +338,10 @@ func (params *UpdateAccountParams) SetStatus(Status string) *UpdateAccountParams } func (c *ApiService) UpdateAccount(Sid string, params *UpdateAccountParams) (*TestResponseObject, error) { + return c.UpdateAccountWithCtx(context.TODO(), Sid, params) +} + +func (c *ApiService) UpdateAccountWithCtx(ctx context.Context, Sid string, params *UpdateAccountParams) (*TestResponseObject, error) { path := "/2010-04-01/Accounts/{Sid}.json" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) @@ -323,7 +355,7 @@ func (c *ApiService) UpdateAccount(Sid string, params *UpdateAccountParams) (*Te data.Set("Status", *params.Status) } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } diff --git a/examples/go/go-client/helper/rest/api/v2010/accounts_calls.go b/examples/go/go-client/helper/rest/api/v2010/accounts_calls.go index e94843eb2..c400a3061 100644 --- a/examples/go/go-client/helper/rest/api/v2010/accounts_calls.go +++ b/examples/go/go-client/helper/rest/api/v2010/accounts_calls.go @@ -15,6 +15,7 @@ package openapi import ( + "context" "encoding/json" "fmt" "net/url" @@ -51,6 +52,10 @@ func (params *CreateCallParams) SetTestArrayOfUri(TestArrayOfUri []string) *Crea } func (c *ApiService) CreateCall(params *CreateCallParams) (*TestResponseObject, error) { + return c.CreateCallWithCtx(context.TODO(), params) +} + +func (c *ApiService) CreateCallWithCtx(ctx context.Context, params *CreateCallParams) (*TestResponseObject, error) { path := "/2010-04-01/Accounts/{AccountSid}/Calls.json" if params != nil && params.PathAccountSid != nil { path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) @@ -75,7 +80,7 @@ func (c *ApiService) CreateCall(params *CreateCallParams) (*TestResponseObject, } } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -102,6 +107,10 @@ func (params *DeleteCallParams) SetPathAccountSid(PathAccountSid string) *Delete } func (c *ApiService) DeleteCall(TestInteger int, params *DeleteCallParams) error { + return c.DeleteCallWithCtx(context.TODO(), TestInteger, params) +} + +func (c *ApiService) DeleteCallWithCtx(ctx context.Context, TestInteger int, params *DeleteCallParams) error { path := "/2010-04-01/Accounts/{AccountSid}/Calls/{TestInteger}.json" if params != nil && params.PathAccountSid != nil { path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) @@ -113,7 +122,7 @@ func (c *ApiService) DeleteCall(TestInteger int, params *DeleteCallParams) error data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Delete(ctx, c.baseURL+path, data, headers) if err != nil { return err } @@ -135,6 +144,10 @@ func (params *FetchCallParams) SetPathAccountSid(PathAccountSid string) *FetchCa } func (c *ApiService) FetchCall(TestInteger int, params *FetchCallParams) (*TestResponseObject, error) { + return c.FetchCallWithCtx(context.TODO(), TestInteger, params) +} + +func (c *ApiService) FetchCallWithCtx(ctx context.Context, TestInteger int, params *FetchCallParams) (*TestResponseObject, error) { path := "/2010-04-01/Accounts/{AccountSid}/Calls/{TestInteger}.json" if params != nil && params.PathAccountSid != nil { path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) @@ -146,7 +159,7 @@ func (c *ApiService) FetchCall(TestInteger int, params *FetchCallParams) (*TestR data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } diff --git a/examples/go/go-client/helper/rest/api/v2010/accounts_calls_feedback_summary.go b/examples/go/go-client/helper/rest/api/v2010/accounts_calls_feedback_summary.go index 2af1473db..da30d5095 100644 --- a/examples/go/go-client/helper/rest/api/v2010/accounts_calls_feedback_summary.go +++ b/examples/go/go-client/helper/rest/api/v2010/accounts_calls_feedback_summary.go @@ -15,6 +15,7 @@ package openapi import ( + "context" "encoding/json" "fmt" "net/url" @@ -51,6 +52,10 @@ func (params *UpdateCallFeedbackSummaryParams) SetStartDate(StartDate string) *U } func (c *ApiService) UpdateCallFeedbackSummary(Sid string, params *UpdateCallFeedbackSummaryParams) (*TestResponseObject, error) { + return c.UpdateCallFeedbackSummaryWithCtx(context.TODO(), Sid, params) +} + +func (c *ApiService) UpdateCallFeedbackSummaryWithCtx(ctx context.Context, Sid string, params *UpdateCallFeedbackSummaryParams) (*TestResponseObject, error) { path := "/2010-04-01/Accounts/{AccountSid}/Calls/Feedback/Summary/{Sid}.json" if params != nil && params.PathAccountSid != nil { path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) @@ -72,7 +77,7 @@ func (c *ApiService) UpdateCallFeedbackSummary(Sid string, params *UpdateCallFee data.Set("StartDate", fmt.Sprint(*params.StartDate)) } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } diff --git a/examples/go/go-client/helper/rest/api/v2010/api_service.go b/examples/go/go-client/helper/rest/api/v2010/api_service.go index ec27f348d..786e678fc 100644 --- a/examples/go/go-client/helper/rest/api/v2010/api_service.go +++ b/examples/go/go-client/helper/rest/api/v2010/api_service.go @@ -20,10 +20,14 @@ import ( type ApiService struct { baseURL string - requestHandler *twilio.RequestHandler + requestHandler *twilio.RequestHandlerWithCtx } func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { + return NewApiServiceWithCtx(twilio.UpgradeRequestHandler(requestHandler)) +} + +func NewApiServiceWithCtx(requestHandler *twilio.RequestHandlerWithCtx) *ApiService { return &ApiService{ requestHandler: requestHandler, baseURL: "https://api.twilio.com", @@ -33,3 +37,7 @@ func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { func NewApiServiceWithClient(client twilio.BaseClient) *ApiService { return NewApiService(twilio.NewRequestHandler(client)) } + +func NewApiServiceWithClientWithCtx(client twilio.BaseClientWithCtx) *ApiService { + return NewApiServiceWithCtx(twilio.NewRequestHandlerWithCtx(client)) +} diff --git a/examples/go/go-client/helper/rest/flex/v1/api_service.go b/examples/go/go-client/helper/rest/flex/v1/api_service.go index 82f8c08f2..04fdff0d6 100644 --- a/examples/go/go-client/helper/rest/flex/v1/api_service.go +++ b/examples/go/go-client/helper/rest/flex/v1/api_service.go @@ -20,10 +20,14 @@ import ( type ApiService struct { baseURL string - requestHandler *twilio.RequestHandler + requestHandler *twilio.RequestHandlerWithCtx } func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { + return NewApiServiceWithCtx(twilio.UpgradeRequestHandler(requestHandler)) +} + +func NewApiServiceWithCtx(requestHandler *twilio.RequestHandlerWithCtx) *ApiService { return &ApiService{ requestHandler: requestHandler, baseURL: "https://flex-api.twilio.com", @@ -33,3 +37,7 @@ func NewApiService(requestHandler *twilio.RequestHandler) *ApiService { func NewApiServiceWithClient(client twilio.BaseClient) *ApiService { return NewApiService(twilio.NewRequestHandler(client)) } + +func NewApiServiceWithClientWithCtx(client twilio.BaseClientWithCtx) *ApiService { + return NewApiServiceWithCtx(twilio.NewRequestHandlerWithCtx(client)) +} diff --git a/examples/go/go-client/helper/rest/flex/v1/credentials_aws.go b/examples/go/go-client/helper/rest/flex/v1/credentials_aws.go index 686eb5450..acfd70937 100644 --- a/examples/go/go-client/helper/rest/flex/v1/credentials_aws.go +++ b/examples/go/go-client/helper/rest/flex/v1/credentials_aws.go @@ -15,6 +15,7 @@ package openapi import ( + "context" "encoding/json" "fmt" "net/url" @@ -132,6 +133,10 @@ func (params *CreateCredentialAwsParams) SetSomeA2PThing(SomeA2PThing string) *C } func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*TestResponseObject, error) { + return c.CreateCredentialAwsWithCtx(context.TODO(), params) +} + +func (c *ApiService) CreateCredentialAwsWithCtx(ctx context.Context, params *CreateCredentialAwsParams) (*TestResponseObject, error) { path := "/v1/Credentials/AWS" data := url.Values{} @@ -219,7 +224,7 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Te data.Set("SomeA2PThing", *params.SomeA2PThing) } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -235,13 +240,17 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Te } func (c *ApiService) DeleteCredentialAws(Sid string) error { + return c.DeleteCredentialAwsWithCtx(context.TODO(), Sid) +} + +func (c *ApiService) DeleteCredentialAwsWithCtx(ctx context.Context, Sid string) error { path := "/v1/Credentials/AWS/{Sid}" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Delete(ctx, c.baseURL+path, data, headers) if err != nil { return err } @@ -252,13 +261,17 @@ func (c *ApiService) DeleteCredentialAws(Sid string) error { } func (c *ApiService) FetchCredentialAws(Sid string) (*TestResponseObject, error) { + return c.FetchCredentialAwsWithCtx(context.TODO(), Sid) +} + +func (c *ApiService) FetchCredentialAwsWithCtx(ctx context.Context, Sid string) (*TestResponseObject, error) { path := "/v1/Credentials/AWS/{Sid}" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -292,6 +305,11 @@ func (params *ListCredentialAwsParams) SetLimit(Limit int) *ListCredentialAwsPar // Retrieve a single page of CredentialAws records from the API. Request is executed immediately. func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToken, pageNumber string) (*ListCredentialAwsResponse, error) { + return c.PageCredentialAwsWithCtx(context.TODO(), params, pageToken, pageNumber) +} + +// Retrieve a single page of CredentialAws records from the API. Request is executed immediately. +func (c *ApiService) PageCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams, pageToken, pageNumber string) (*ListCredentialAwsResponse, error) { path := "/v1/Credentials/AWS" data := url.Values{} @@ -308,7 +326,7 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke data.Set("Page", pageNumber) } - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } @@ -325,7 +343,12 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke // Lists CredentialAws records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. func (c *ApiService) ListCredentialAws(params *ListCredentialAwsParams) ([]TestResponseObject, error) { - response, errors := c.StreamCredentialAws(params) + return c.ListCredentialAwsWithCtx(context.TODO(), params) +} + +// Lists CredentialAws records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. +func (c *ApiService) ListCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams) ([]TestResponseObject, error) { + response, errors := c.StreamCredentialAwsWithCtx(ctx, params) records := make([]TestResponseObject, 0) for record := range response { @@ -341,6 +364,11 @@ func (c *ApiService) ListCredentialAws(params *ListCredentialAwsParams) ([]TestR // Streams CredentialAws records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. func (c *ApiService) StreamCredentialAws(params *ListCredentialAwsParams) (chan TestResponseObject, chan error) { + return c.StreamCredentialAwsWithCtx(context.TODO(), params) +} + +// Streams CredentialAws records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. +func (c *ApiService) StreamCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams) (chan TestResponseObject, chan error) { if params == nil { params = &ListCredentialAwsParams{} } @@ -349,19 +377,19 @@ func (c *ApiService) StreamCredentialAws(params *ListCredentialAwsParams) (chan recordChannel := make(chan TestResponseObject, 1) errorChannel := make(chan error, 1) - response, err := c.PageCredentialAws(params, "", "") + response, err := c.PageCredentialAwsWithCtx(ctx, params, "", "") if err != nil { errorChannel <- err close(recordChannel) close(errorChannel) } else { - go c.streamCredentialAws(response, params, recordChannel, errorChannel) + go c.streamCredentialAws(ctx, response, params, recordChannel, errorChannel) } return recordChannel, errorChannel } -func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, params *ListCredentialAwsParams, recordChannel chan TestResponseObject, errorChannel chan error) { +func (c *ApiService) streamCredentialAws(ctx context.Context, response *ListCredentialAwsResponse, params *ListCredentialAwsParams, recordChannel chan TestResponseObject, errorChannel chan error) { curRecord := 1 for response != nil { @@ -376,7 +404,7 @@ func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, pa } } - record, err := client.GetNext(c.baseURL, response, c.getNextListCredentialAwsResponse) + record, err := client.GetNextWithCtx(ctx, c.baseURL, response, c.getNextListCredentialAwsResponse) if err != nil { errorChannel <- err break @@ -391,11 +419,11 @@ func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, pa close(errorChannel) } -func (c *ApiService) getNextListCredentialAwsResponse(nextPageUrl string) (interface{}, error) { +func (c *ApiService) getNextListCredentialAwsResponse(ctx context.Context, nextPageUrl string) (interface{}, error) { if nextPageUrl == "" { return nil, nil } - resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) + resp, err := c.requestHandler.Get(ctx, nextPageUrl, nil, nil) if err != nil { return nil, err } @@ -427,6 +455,10 @@ func (params *UpdateCredentialAwsParams) SetTestBoolean(TestBoolean bool) *Updat } func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAwsParams) (*TestResponseObject, error) { + return c.UpdateCredentialAwsWithCtx(context.TODO(), Sid, params) +} + +func (c *ApiService) UpdateCredentialAwsWithCtx(ctx context.Context, Sid string, params *UpdateCredentialAwsParams) (*TestResponseObject, error) { path := "/v1/Credentials/AWS/{Sid}" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) @@ -440,7 +472,7 @@ func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAws data.Set("TestBoolean", fmt.Sprint(*params.TestBoolean)) } - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } diff --git a/examples/go/go-client/helper/rest/flex/v1/credentials_aws_history.go b/examples/go/go-client/helper/rest/flex/v1/credentials_aws_history.go index edf4465ac..70bef789e 100644 --- a/examples/go/go-client/helper/rest/flex/v1/credentials_aws_history.go +++ b/examples/go/go-client/helper/rest/flex/v1/credentials_aws_history.go @@ -15,6 +15,7 @@ package openapi import ( + "context" "encoding/json" "net/url" "strings" @@ -32,6 +33,10 @@ func (params *FetchCredentialHistoryParams) SetAddOnsData(AddOnsData map[string] } func (c *ApiService) FetchCredentialHistory(Sid string, params *FetchCredentialHistoryParams) (*TestResponseObject, error) { + return c.FetchCredentialHistoryWithCtx(context.TODO(), Sid, params) +} + +func (c *ApiService) FetchCredentialHistoryWithCtx(ctx context.Context, Sid string, params *FetchCredentialHistoryParams) (*TestResponseObject, error) { path := "/v1/Credentials/AWS/{Sid}/History" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) @@ -48,7 +53,7 @@ func (c *ApiService) FetchCredentialHistory(Sid string, params *FetchCredentialH data.Set("AddOnsData", string(v)) } - resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } diff --git a/examples/go/go-client/helper/rest/flex/v1/voice.go b/examples/go/go-client/helper/rest/flex/v1/voice.go index 88f989e29..c6db7dcbd 100644 --- a/examples/go/go-client/helper/rest/flex/v1/voice.go +++ b/examples/go/go-client/helper/rest/flex/v1/voice.go @@ -15,19 +15,24 @@ package openapi import ( + "context" "encoding/json" "net/url" "strings" ) func (c *ApiService) UpdateCall(Sid string) (*UpdateCall200Response, error) { + return c.UpdateCallWithCtx(context.TODO(), Sid) +} + +func (c *ApiService) UpdateCallWithCtx(ctx context.Context, Sid string) (*UpdateCall200Response, error) { path := "/v1/Voice/{Sid}" path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) data := url.Values{} headers := make(map[string]interface{}) - resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) + resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers) if err != nil { return nil, err } From 26eed7c3351bc1946e412e698c0c81372c8f4bb9 Mon Sep 17 00:00:00 2001 From: childish-sambino Date: Tue, 8 Nov 2022 16:23:55 -0600 Subject: [PATCH 4/4] context fixes for go-client tests --- examples/go/go-client/helper/client/test_client.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/go/go-client/helper/client/test_client.go b/examples/go/go-client/helper/client/test_client.go index 264f6ee9b..07e07e332 100644 --- a/examples/go/go-client/helper/client/test_client.go +++ b/examples/go/go-client/helper/client/test_client.go @@ -1,6 +1,7 @@ package client import ( + "context" "net/http" "net/url" "time" @@ -37,6 +38,6 @@ func (tc *TestClient) getParsedUrl(path string) *url.URL { return parsedUrl } -func (tc *TestClient) SendRequest(method string, rawURL string, data url.Values, headers map[string]interface{}) (*http.Response, error) { - return tc.Client.SendRequest(method, tc.getParsedUrl(rawURL).String(), data, headers) +func (tc *TestClient) SendRequestWithCtx(context context.Context, method string, rawURL string, data url.Values, headers map[string]interface{}) (*http.Response, error) { + return tc.Client.SendRequestWithCtx(context, method, tc.getParsedUrl(rawURL).String(), data, headers) }