Skip to content

Commit

Permalink
[go-client] update templates to use context methods
Browse files Browse the repository at this point in the history
  • Loading branch information
natebrennand committed Oct 30, 2022
1 parent 6bc271e commit 8a88b80
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
41 changes: 32 additions & 9 deletions src/main/resources/twilio-go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package {{packageName}}

{{#operations}}
import (
"context"
"encoding/json"
"fmt"
"net/url"
Expand Down Expand Up @@ -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}}

Expand All @@ -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
Expand Down Expand Up @@ -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}}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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) {
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{}
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/twilio-go/api_service.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand All @@ -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))
}

0 comments on commit 8a88b80

Please sign in to comment.