Skip to content

Commit

Permalink
feat: add context.Context support (#197)
Browse files Browse the repository at this point in the history
Fixes #98

This PR uses the new alternative base client interfaces, `BaseClientWithCtx` and `RequestHandlerWithCtx`, that allow `context.Context` objects to be provided to every API request. This will allow the Twilio SDK to support context cancellations, and for custom HTTP clients to access the context while executing the request.
  • Loading branch information
natebrennand authored Nov 8, 2022
1 parent b61e334 commit 66288bf
Show file tree
Hide file tree
Showing 434 changed files with 12,711 additions and 3,794 deletions.
2 changes: 1 addition & 1 deletion client/form/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2014 Alvaro J. Genial. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//nolint
// nolint
package form

import (
Expand Down
2 changes: 1 addition & 1 deletion client/form/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2014 Alvaro J. Genial. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//nolint
// nolint
package form

import (
Expand Down
2 changes: 1 addition & 1 deletion client/page_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

//Takes a limit on the max number of records to read and a max pageSize and calculates the max number of pages to read.
// Takes a limit on the max number of records to read and a max pageSize and calculates the max number of pages to read.
func ReadLimits(pageSize *int, limit *int) int {
//don't care about pageSize
if pageSize == nil {
Expand Down
10 changes: 9 additions & 1 deletion rest/accounts/v1/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://accounts.twilio.com",
Expand All @@ -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))
}
8 changes: 7 additions & 1 deletion rest/accounts/v1/auth_tokens_promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
package openapi

import (
"context"
"encoding/json"
"net/url"
)

// Promote the secondary Auth Token to primary. After promoting the new token, all requests to Twilio using your old primary Auth Token will result in an error.
func (c *ApiService) UpdateAuthTokenPromotion() (*AccountsV1AuthTokenPromotion, error) {
return c.UpdateAuthTokenPromotionWithCtx(context.TODO())
}

// Promote the secondary Auth Token to primary. After promoting the new token, all requests to Twilio using your old primary Auth Token will result in an error.
func (c *ApiService) UpdateAuthTokenPromotionWithCtx(ctx context.Context) (*AccountsV1AuthTokenPromotion, error) {
path := "/v1/AuthTokens/Promote"

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
}
Expand Down
15 changes: 13 additions & 2 deletions rest/accounts/v1/auth_tokens_secondary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
package openapi

import (
"context"
"encoding/json"
"net/url"
)

// Create a new secondary Auth Token
func (c *ApiService) CreateSecondaryAuthToken() (*AccountsV1SecondaryAuthToken, error) {
return c.CreateSecondaryAuthTokenWithCtx(context.TODO())
}

// Create a new secondary Auth Token
func (c *ApiService) CreateSecondaryAuthTokenWithCtx(ctx context.Context) (*AccountsV1SecondaryAuthToken, error) {
path := "/v1/AuthTokens/Secondary"

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
}
Expand All @@ -43,12 +49,17 @@ func (c *ApiService) CreateSecondaryAuthToken() (*AccountsV1SecondaryAuthToken,

// Delete the secondary Auth Token from your account
func (c *ApiService) DeleteSecondaryAuthToken() error {
return c.DeleteSecondaryAuthTokenWithCtx(context.TODO())
}

// Delete the secondary Auth Token from your account
func (c *ApiService) DeleteSecondaryAuthTokenWithCtx(ctx context.Context) error {
path := "/v1/AuthTokens/Secondary"

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
}
Expand Down
60 changes: 48 additions & 12 deletions rest/accounts/v1/credentials_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package openapi

import (
"context"
"encoding/json"
"fmt"
"net/url"
Expand Down Expand Up @@ -48,6 +49,11 @@ func (params *CreateCredentialAwsParams) SetAccountSid(AccountSid string) *Creat

// Create a new AWS Credential
func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
return c.CreateCredentialAwsWithCtx(context.TODO(), params)
}

// Create a new AWS Credential
func (c *ApiService) CreateCredentialAwsWithCtx(ctx context.Context, params *CreateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
path := "/v1/Credentials/AWS"

data := url.Values{}
Expand All @@ -63,7 +69,7 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Ac
data.Set("AccountSid", *params.AccountSid)
}

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
}
Expand All @@ -80,13 +86,18 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Ac

// Delete a Credential from your account
func (c *ApiService) DeleteCredentialAws(Sid string) error {
return c.DeleteCredentialAwsWithCtx(context.TODO(), Sid)
}

// Delete a Credential from your account
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
}
Expand All @@ -98,13 +109,18 @@ func (c *ApiService) DeleteCredentialAws(Sid string) error {

// Fetch the AWS credentials specified by the provided Credential Sid
func (c *ApiService) FetchCredentialAws(Sid string) (*AccountsV1CredentialAws, error) {
return c.FetchCredentialAwsWithCtx(context.TODO(), Sid)
}

// Fetch the AWS credentials specified by the provided Credential Sid
func (c *ApiService) FetchCredentialAwsWithCtx(ctx context.Context, Sid string) (*AccountsV1CredentialAws, 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
}
Expand Down Expand Up @@ -138,6 +154,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{}
Expand All @@ -154,7 +175,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
}
Expand All @@ -171,7 +192,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) ([]AccountsV1CredentialAws, 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) ([]AccountsV1CredentialAws, error) {
response, errors := c.StreamCredentialAwsWithCtx(ctx, params)

records := make([]AccountsV1CredentialAws, 0)
for record := range response {
Expand All @@ -187,6 +213,11 @@ func (c *ApiService) ListCredentialAws(params *ListCredentialAwsParams) ([]Accou

// 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 AccountsV1CredentialAws, 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 AccountsV1CredentialAws, chan error) {
if params == nil {
params = &ListCredentialAwsParams{}
}
Expand All @@ -195,19 +226,19 @@ func (c *ApiService) StreamCredentialAws(params *ListCredentialAwsParams) (chan
recordChannel := make(chan AccountsV1CredentialAws, 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 AccountsV1CredentialAws, errorChannel chan error) {
func (c *ApiService) streamCredentialAws(ctx context.Context, response *ListCredentialAwsResponse, params *ListCredentialAwsParams, recordChannel chan AccountsV1CredentialAws, errorChannel chan error) {
curRecord := 1

for response != nil {
Expand All @@ -222,7 +253,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
Expand All @@ -237,11 +268,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
}
Expand All @@ -268,6 +299,11 @@ func (params *UpdateCredentialAwsParams) SetFriendlyName(FriendlyName string) *U

// Modify the properties of a given Account
func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
return c.UpdateCredentialAwsWithCtx(context.TODO(), Sid, params)
}

// Modify the properties of a given Account
func (c *ApiService) UpdateCredentialAwsWithCtx(ctx context.Context, Sid string, params *UpdateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
path := "/v1/Credentials/AWS/{Sid}"
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)

Expand All @@ -278,7 +314,7 @@ func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAws
data.Set("FriendlyName", *params.FriendlyName)
}

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
}
Expand Down
Loading

0 comments on commit 66288bf

Please sign in to comment.