Skip to content

feat: Introduce Context #287

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

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
3 changes: 3 additions & 0 deletions client/base_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 All @@ -13,4 +14,6 @@ type BaseClient interface {
headers map[string]interface{}, body ...byte) (*http.Response, error)
SetOauth(auth OAuth)
OAuth() OAuth
SendRequestWithContext(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}, body ...byte) (*http.Response, error)
}
19 changes: 17 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

var alphanumericRegex *regexp.Regexp
var delimitingRegex *regexp.Regexp
var goVersion string
var goVersionOnce sync.Once

func init() {
alphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
Expand Down Expand Up @@ -140,6 +142,12 @@ var userAgentOnce sync.Once
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}, body ...byte) (*http.Response, error) {

return c.SendRequestWithContext(context.TODO(), method, rawURL, data, headers, body...)
}

func (c *Client) SendRequestWithContext(ctx context.Context, method string, rawURL string, data url.Values,
headers map[string]interface{}, body ...byte) (*http.Response, error) {

contentType := extractContentTypeHeader(headers)

u, err := url.Parse(rawURL)
Expand Down Expand Up @@ -167,7 +175,7 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
//data is already processed and information will be added to u(the url) in the
//previous step. Now body will solely contain json payload
if contentType == jsonContentType {
req, err = http.NewRequest(method, u.String(), bytes.NewBuffer(body))
req, err = http.NewRequestWithContext(ctx, method, u.String(), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -203,7 +211,7 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
}
if c.OAuth() != nil {
oauth := c.OAuth()
token, _ := c.OAuth().GetAccessToken(context.TODO())
token, _ := c.OAuth().GetAccessToken(ctx)
if token != "" {
req.Header.Add("Authorization", "Bearer "+token)
}
Expand Down Expand Up @@ -237,3 +245,10 @@ func (c *Client) SetOauth(oauth OAuth) {
func (c *Client) OAuth() OAuth {
return c.oAuth
}

func getGoVersion() string {
goVersionOnce.Do(func() {
goVersion = runtime.Version()
})
return goVersion
}
5 changes: 3 additions & 2 deletions client/page_util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"encoding/json"
"fmt"
"strings"
Expand All @@ -25,13 +26,13 @@ func ReadLimits(pageSize *int, limit *int) int {
}
}

func GetNext(baseUrl string, response interface{}, getNextPage func(nextPageUri string) (interface{}, error)) (interface{}, error) {
func GetNextWithContext(ctx context.Context, baseUrl string, response interface{}, getNextPage func(ctx context.Context, nextPageUri string) (interface{}, error)) (interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add this as a second method, similar to the SendRequestWithContext, otherwise we're going to break everyone's clients.

nextPageUrl, err := getNextPageUrl(baseUrl, response)
if err != nil {
return nil, err
}

return getNextPage(nextPageUrl)
return getNextPage(ctx, nextPageUrl)
}

func toMap(s interface{}) (map[string]interface{}, error) {
Expand Down
7 changes: 4 additions & 3 deletions client/page_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -140,7 +141,7 @@ type testMessage struct {
To *string `json:"to,omitempty"`
}

func getSomething(nextPageUrl string) (interface{}, error) {
func getSomething(ctx context.Context, nextPageUrl string) (interface{}, error) {
return nextPageUrl, nil
}

Expand All @@ -151,11 +152,11 @@ func TestPageUtil_GetNext(t *testing.T) {
ps := &testResponse{}
_ = json.NewDecoder(response.Body).Decode(ps)

nextPageUrl, err := GetNext(baseUrl, ps, getSomething)
nextPageUrl, err := GetNextWithContext(context.TODO(), baseUrl, ps, getSomething)
assert.Equal(t, "https://api.twilio.com/2010-04-01/Accounts/ACXX/Messages.json?From=9999999999&PageNumber=&To=4444444444&PageSize=2&Page=1&PageToken=PASMXX", nextPageUrl)
assert.Nil(t, err)

nextPageUrl, err = GetNext(baseUrl, nil, getSomething)
nextPageUrl, err = GetNextWithContext(context.TODO(), baseUrl, nil, getSomething)
assert.Empty(t, nextPageUrl)
assert.Nil(t, err)
}
Expand Down
23 changes: 13 additions & 10 deletions client/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package client

import (
"context"
"net/http"
"net/url"
"os"
Expand All @@ -12,13 +13,15 @@ type RequestHandler struct {
Client BaseClient
Edge string
Region string
ctx context.Context
}

func NewRequestHandler(client BaseClient) *RequestHandler {
return &RequestHandler{
Client: client,
Edge: os.Getenv("TWILIO_EDGE"),
Region: os.Getenv("TWILIO_REGION"),
ctx: context.TODO(),
}
}

Expand Down Expand Up @@ -82,22 +85,22 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
return u.String(), nil
}

func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.sendRequest(http.MethodPost, path, bodyData, headers, body...)
func (c *RequestHandler) PostWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.Client.SendRequestWithContext(ctx, http.MethodPost, path, bodyData, headers, body...)
}

func (c *RequestHandler) Put(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.sendRequest(http.MethodPut, path, bodyData, headers, body...)
func (c *RequestHandler) PutWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.Client.SendRequestWithContext(ctx, http.MethodPut, path, bodyData, headers, body...)
}

func (c *RequestHandler) Patch(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.sendRequest(http.MethodPatch, path, bodyData, headers, body...)
func (c *RequestHandler) PatchWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) {
return c.Client.SendRequestWithContext(ctx, http.MethodPatch, path, bodyData, headers, body...)
}

func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.sendRequest(http.MethodGet, path, queryData, headers)
func (c *RequestHandler) GetWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.Client.SendRequestWithContext(ctx, http.MethodGet, path, queryData, headers)
}

func (c *RequestHandler) Delete(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.sendRequest(http.MethodDelete, path, queryData, headers)
func (c *RequestHandler) DeleteWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.Client.SendRequestWithContext(ctx, http.MethodDelete, path, queryData, headers)
}
11 changes: 6 additions & 5 deletions client/request_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client_test

import (
"context"
"errors"
"net/http"
"net/http/httptest"
Expand All @@ -11,9 +12,9 @@ import (
"github.com/twilio/twilio-go/client"
)

func NewRequestHandler(accountSid string, authToken string) *client.RequestHandler {
func NewRequestHandler(accountSid string, authToken string) *client.RequestHandlerWithContext {
c := NewClient(accountSid, authToken)
return client.NewRequestHandler(c)
return client.NewRequestHandlerWithContext(c)
}

func TestRequestHandler_BuildUrlSetRegion(t *testing.T) {
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestRequestHandler_BuildUrlInvalidCTLCharacter(t *testing.T) {
assert.Equal(t, parsedURL, "")
}

func assertAndGetURL(t *testing.T, requestHandler *client.RequestHandler, rawURL string) string {
func assertAndGetURL(t *testing.T, requestHandler *client.RequestHandlerWithContext, rawURL string) string {
parsedURL, err := requestHandler.BuildUrl(rawURL)
assert.Nil(t, err)
return parsedURL
Expand All @@ -83,7 +84,7 @@ func TestRequestHandler_SendGetRequest(t *testing.T) {
defer errorServer.Close()

requestHandler := NewRequestHandler("user", "pass")
resp, err := requestHandler.Get(errorServer.URL, nil, nil) //nolint:bodyclose
resp, err := requestHandler.GetWithContext(context.TODO(), errorServer.URL, nil, nil) //nolint:bodyclose
twilioError := err.(*client.TwilioRestError)
assert.Nil(t, resp)
assert.Equal(t, 400, twilioError.Status)
Expand All @@ -108,7 +109,7 @@ func TestRequestHandler_SendPostRequest(t *testing.T) {
defer errorServer.Close()

requestHandler := NewRequestHandler("user", "pass")
resp, err := requestHandler.Post(errorServer.URL, nil, nil) //nolint:bodyclose
resp, err := requestHandler.PostWithContext(context.TODO(), errorServer.URL, nil, nil) //nolint:bodyclose
twilioError := err.(*client.TwilioRestError)
assert.Nil(t, resp)
assert.Equal(t, 400, twilioError.Status)
Expand Down
Loading