Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context.Context support to Go SDK #301

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/go/go-client/helper/client/test_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -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)
}
56 changes: 44 additions & 12 deletions examples/go/go-client/helper/rest/api/v2010/accounts.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 @@ -54,6 +55,10 @@ func (params *CreateAccountParams) SetTwiml(Twiml string) *CreateAccountParams {
}

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{}
Expand All @@ -75,7 +80,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
}
Expand All @@ -91,13 +96,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
}
Expand All @@ -108,13 +117,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
}
Expand Down Expand Up @@ -172,6 +185,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{}
Expand Down Expand Up @@ -200,7 +218,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
}
Expand All @@ -217,7 +235,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 {
Expand All @@ -233,6 +256,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{}
}
Expand All @@ -241,19 +269,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 {
Expand All @@ -268,7 +296,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
Expand All @@ -283,11 +311,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
}
Expand Down Expand Up @@ -319,6 +347,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)

Expand All @@ -332,7 +364,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
}
Expand Down
19 changes: 16 additions & 3 deletions examples/go/go-client/helper/rest/api/v2010/accounts_calls.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 @@ -57,6 +58,10 @@ func (params *CreateCallParams) SetTestMethod(TestMethod string) *CreateCallPara
}

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)
Expand Down Expand Up @@ -84,7 +89,7 @@ func (c *ApiService) CreateCall(params *CreateCallParams) (*TestResponseObject,
data.Set("TestMethod", *params.TestMethod)
}

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 @@ -111,6 +116,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)
Expand All @@ -122,7 +131,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
}
Expand All @@ -144,6 +153,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)
Expand All @@ -155,7 +168,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
}
Expand Down
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 @@ -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)
Expand All @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion examples/go/go-client/helper/rest/api/v2010/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://api.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))
}
10 changes: 9 additions & 1 deletion examples/go/go-client/helper/rest/flex/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://flex-api.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))
}
Loading