Skip to content

Commit 66288bf

Browse files
authored
feat: add context.Context support (#197)
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.
1 parent b61e334 commit 66288bf

File tree

434 files changed

+12711
-3794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

434 files changed

+12711
-3794
lines changed

client/form/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright 2014 Alvaro J. Genial. All rights reserved.
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
6-
//nolint
6+
// nolint
77
package form
88

99
import (

client/form/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright 2014 Alvaro J. Genial. All rights reserved.
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
6-
//nolint
6+
// nolint
77
package form
88

99
import (

client/page_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
)
99

10-
//Takes a limit on the max number of records to read and a max pageSize and calculates the max number of pages to read.
10+
// Takes a limit on the max number of records to read and a max pageSize and calculates the max number of pages to read.
1111
func ReadLimits(pageSize *int, limit *int) int {
1212
//don't care about pageSize
1313
if pageSize == nil {

rest/accounts/v1/api_service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ import (
2020

2121
type ApiService struct {
2222
baseURL string
23-
requestHandler *twilio.RequestHandler
23+
requestHandler *twilio.RequestHandlerWithCtx
2424
}
2525

2626
func NewApiService(requestHandler *twilio.RequestHandler) *ApiService {
27+
return NewApiServiceWithCtx(twilio.UpgradeRequestHandler(requestHandler))
28+
}
29+
30+
func NewApiServiceWithCtx(requestHandler *twilio.RequestHandlerWithCtx) *ApiService {
2731
return &ApiService{
2832
requestHandler: requestHandler,
2933
baseURL: "https://accounts.twilio.com",
@@ -33,3 +37,7 @@ func NewApiService(requestHandler *twilio.RequestHandler) *ApiService {
3337
func NewApiServiceWithClient(client twilio.BaseClient) *ApiService {
3438
return NewApiService(twilio.NewRequestHandler(client))
3539
}
40+
41+
func NewApiServiceWithClientWithCtx(client twilio.BaseClientWithCtx) *ApiService {
42+
return NewApiServiceWithCtx(twilio.NewRequestHandlerWithCtx(client))
43+
}

rest/accounts/v1/auth_tokens_promote.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
package openapi
1616

1717
import (
18+
"context"
1819
"encoding/json"
1920
"net/url"
2021
)
2122

2223
// 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.
2324
func (c *ApiService) UpdateAuthTokenPromotion() (*AccountsV1AuthTokenPromotion, error) {
25+
return c.UpdateAuthTokenPromotionWithCtx(context.TODO())
26+
}
27+
28+
// 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.
29+
func (c *ApiService) UpdateAuthTokenPromotionWithCtx(ctx context.Context) (*AccountsV1AuthTokenPromotion, error) {
2430
path := "/v1/AuthTokens/Promote"
2531

2632
data := url.Values{}
2733
headers := make(map[string]interface{})
2834

29-
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
35+
resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers)
3036
if err != nil {
3137
return nil, err
3238
}

rest/accounts/v1/auth_tokens_secondary.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
package openapi
1616

1717
import (
18+
"context"
1819
"encoding/json"
1920
"net/url"
2021
)
2122

2223
// Create a new secondary Auth Token
2324
func (c *ApiService) CreateSecondaryAuthToken() (*AccountsV1SecondaryAuthToken, error) {
25+
return c.CreateSecondaryAuthTokenWithCtx(context.TODO())
26+
}
27+
28+
// Create a new secondary Auth Token
29+
func (c *ApiService) CreateSecondaryAuthTokenWithCtx(ctx context.Context) (*AccountsV1SecondaryAuthToken, error) {
2430
path := "/v1/AuthTokens/Secondary"
2531

2632
data := url.Values{}
2733
headers := make(map[string]interface{})
2834

29-
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
35+
resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers)
3036
if err != nil {
3137
return nil, err
3238
}
@@ -43,12 +49,17 @@ func (c *ApiService) CreateSecondaryAuthToken() (*AccountsV1SecondaryAuthToken,
4349

4450
// Delete the secondary Auth Token from your account
4551
func (c *ApiService) DeleteSecondaryAuthToken() error {
52+
return c.DeleteSecondaryAuthTokenWithCtx(context.TODO())
53+
}
54+
55+
// Delete the secondary Auth Token from your account
56+
func (c *ApiService) DeleteSecondaryAuthTokenWithCtx(ctx context.Context) error {
4657
path := "/v1/AuthTokens/Secondary"
4758

4859
data := url.Values{}
4960
headers := make(map[string]interface{})
5061

51-
resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
62+
resp, err := c.requestHandler.Delete(ctx, c.baseURL+path, data, headers)
5263
if err != nil {
5364
return err
5465
}

rest/accounts/v1/credentials_aws.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package openapi
1616

1717
import (
18+
"context"
1819
"encoding/json"
1920
"fmt"
2021
"net/url"
@@ -48,6 +49,11 @@ func (params *CreateCredentialAwsParams) SetAccountSid(AccountSid string) *Creat
4849

4950
// Create a new AWS Credential
5051
func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
52+
return c.CreateCredentialAwsWithCtx(context.TODO(), params)
53+
}
54+
55+
// Create a new AWS Credential
56+
func (c *ApiService) CreateCredentialAwsWithCtx(ctx context.Context, params *CreateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
5157
path := "/v1/Credentials/AWS"
5258

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

66-
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
72+
resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers)
6773
if err != nil {
6874
return nil, err
6975
}
@@ -80,13 +86,18 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Ac
8086

8187
// Delete a Credential from your account
8288
func (c *ApiService) DeleteCredentialAws(Sid string) error {
89+
return c.DeleteCredentialAwsWithCtx(context.TODO(), Sid)
90+
}
91+
92+
// Delete a Credential from your account
93+
func (c *ApiService) DeleteCredentialAwsWithCtx(ctx context.Context, Sid string) error {
8394
path := "/v1/Credentials/AWS/{Sid}"
8495
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
8596

8697
data := url.Values{}
8798
headers := make(map[string]interface{})
8899

89-
resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
100+
resp, err := c.requestHandler.Delete(ctx, c.baseURL+path, data, headers)
90101
if err != nil {
91102
return err
92103
}
@@ -98,13 +109,18 @@ func (c *ApiService) DeleteCredentialAws(Sid string) error {
98109

99110
// Fetch the AWS credentials specified by the provided Credential Sid
100111
func (c *ApiService) FetchCredentialAws(Sid string) (*AccountsV1CredentialAws, error) {
112+
return c.FetchCredentialAwsWithCtx(context.TODO(), Sid)
113+
}
114+
115+
// Fetch the AWS credentials specified by the provided Credential Sid
116+
func (c *ApiService) FetchCredentialAwsWithCtx(ctx context.Context, Sid string) (*AccountsV1CredentialAws, error) {
101117
path := "/v1/Credentials/AWS/{Sid}"
102118
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
103119

104120
data := url.Values{}
105121
headers := make(map[string]interface{})
106122

107-
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
123+
resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers)
108124
if err != nil {
109125
return nil, err
110126
}
@@ -138,6 +154,11 @@ func (params *ListCredentialAwsParams) SetLimit(Limit int) *ListCredentialAwsPar
138154

139155
// Retrieve a single page of CredentialAws records from the API. Request is executed immediately.
140156
func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToken, pageNumber string) (*ListCredentialAwsResponse, error) {
157+
return c.PageCredentialAwsWithCtx(context.TODO(), params, pageToken, pageNumber)
158+
}
159+
160+
// Retrieve a single page of CredentialAws records from the API. Request is executed immediately.
161+
func (c *ApiService) PageCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams, pageToken, pageNumber string) (*ListCredentialAwsResponse, error) {
141162
path := "/v1/Credentials/AWS"
142163

143164
data := url.Values{}
@@ -154,7 +175,7 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke
154175
data.Set("Page", pageNumber)
155176
}
156177

157-
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
178+
resp, err := c.requestHandler.Get(ctx, c.baseURL+path, data, headers)
158179
if err != nil {
159180
return nil, err
160181
}
@@ -171,7 +192,12 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke
171192

172193
// Lists CredentialAws records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
173194
func (c *ApiService) ListCredentialAws(params *ListCredentialAwsParams) ([]AccountsV1CredentialAws, error) {
174-
response, errors := c.StreamCredentialAws(params)
195+
return c.ListCredentialAwsWithCtx(context.TODO(), params)
196+
}
197+
198+
// Lists CredentialAws records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
199+
func (c *ApiService) ListCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams) ([]AccountsV1CredentialAws, error) {
200+
response, errors := c.StreamCredentialAwsWithCtx(ctx, params)
175201

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

188214
// Streams CredentialAws records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
189215
func (c *ApiService) StreamCredentialAws(params *ListCredentialAwsParams) (chan AccountsV1CredentialAws, chan error) {
216+
return c.StreamCredentialAwsWithCtx(context.TODO(), params)
217+
}
218+
219+
// Streams CredentialAws records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
220+
func (c *ApiService) StreamCredentialAwsWithCtx(ctx context.Context, params *ListCredentialAwsParams) (chan AccountsV1CredentialAws, chan error) {
190221
if params == nil {
191222
params = &ListCredentialAwsParams{}
192223
}
@@ -195,19 +226,19 @@ func (c *ApiService) StreamCredentialAws(params *ListCredentialAwsParams) (chan
195226
recordChannel := make(chan AccountsV1CredentialAws, 1)
196227
errorChannel := make(chan error, 1)
197228

198-
response, err := c.PageCredentialAws(params, "", "")
229+
response, err := c.PageCredentialAwsWithCtx(ctx, params, "", "")
199230
if err != nil {
200231
errorChannel <- err
201232
close(recordChannel)
202233
close(errorChannel)
203234
} else {
204-
go c.streamCredentialAws(response, params, recordChannel, errorChannel)
235+
go c.streamCredentialAws(ctx, response, params, recordChannel, errorChannel)
205236
}
206237

207238
return recordChannel, errorChannel
208239
}
209240

210-
func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, params *ListCredentialAwsParams, recordChannel chan AccountsV1CredentialAws, errorChannel chan error) {
241+
func (c *ApiService) streamCredentialAws(ctx context.Context, response *ListCredentialAwsResponse, params *ListCredentialAwsParams, recordChannel chan AccountsV1CredentialAws, errorChannel chan error) {
211242
curRecord := 1
212243

213244
for response != nil {
@@ -222,7 +253,7 @@ func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, pa
222253
}
223254
}
224255

225-
record, err := client.GetNext(c.baseURL, response, c.getNextListCredentialAwsResponse)
256+
record, err := client.GetNextWithCtx(ctx, c.baseURL, response, c.getNextListCredentialAwsResponse)
226257
if err != nil {
227258
errorChannel <- err
228259
break
@@ -237,11 +268,11 @@ func (c *ApiService) streamCredentialAws(response *ListCredentialAwsResponse, pa
237268
close(errorChannel)
238269
}
239270

240-
func (c *ApiService) getNextListCredentialAwsResponse(nextPageUrl string) (interface{}, error) {
271+
func (c *ApiService) getNextListCredentialAwsResponse(ctx context.Context, nextPageUrl string) (interface{}, error) {
241272
if nextPageUrl == "" {
242273
return nil, nil
243274
}
244-
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
275+
resp, err := c.requestHandler.Get(ctx, nextPageUrl, nil, nil)
245276
if err != nil {
246277
return nil, err
247278
}
@@ -268,6 +299,11 @@ func (params *UpdateCredentialAwsParams) SetFriendlyName(FriendlyName string) *U
268299

269300
// Modify the properties of a given Account
270301
func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
302+
return c.UpdateCredentialAwsWithCtx(context.TODO(), Sid, params)
303+
}
304+
305+
// Modify the properties of a given Account
306+
func (c *ApiService) UpdateCredentialAwsWithCtx(ctx context.Context, Sid string, params *UpdateCredentialAwsParams) (*AccountsV1CredentialAws, error) {
271307
path := "/v1/Credentials/AWS/{Sid}"
272308
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
273309

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

281-
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
317+
resp, err := c.requestHandler.Post(ctx, c.baseURL+path, data, headers)
282318
if err != nil {
283319
return nil, err
284320
}

0 commit comments

Comments
 (0)