Skip to content

Commit 79f196e

Browse files
author
Sam Harrison
committed
Revert "feat: introduce base client that utilizes context (#196)"
This reverts commit b61e334.
1 parent e89399c commit 79f196e

File tree

5 files changed

+32
-170
lines changed

5 files changed

+32
-170
lines changed

client/base_client.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package client
22

33
import (
4-
"context"
54
"net/http"
65
"net/url"
76
"time"
@@ -13,37 +12,3 @@ type BaseClient interface {
1312
SendRequest(method string, rawURL string, data url.Values,
1413
headers map[string]interface{}) (*http.Response, error)
1514
}
16-
17-
// BaseClientWithCtx is an extension of BaseClient with the ability to associate a contex with
18-
// the request
19-
type BaseClientWithCtx interface {
20-
BaseClient
21-
SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
22-
headers map[string]interface{}) (*http.Response, error)
23-
}
24-
25-
// wrapperClient wraps the lower level BaseClient to fulfill the BaseClientWithCtx interface. This
26-
// allows the SDK to utilize the BaseClientWithCtx method throughout the codebase.
27-
//
28-
// All *WithCtx methods of a wrapped client will not actually use their context.Context argument.
29-
type wrapperClient struct {
30-
// embed the BaseClient so the functions remain accessible
31-
BaseClient
32-
}
33-
34-
// SendRequestWithCtx passes the request through to the underlying BaseClient. The context.Context
35-
// argument is not utilized.
36-
func (w wrapperClient) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
37-
headers map[string]interface{}) (*http.Response, error) {
38-
return w.SendRequest(method, rawURL, data, headers)
39-
}
40-
41-
// wrapBaseClientWithNoopCtx "upgrades" a BaseClient to BaseClientWithCtx so that requests can be
42-
// send with a request context.
43-
func wrapBaseClientWithNoopCtx(c BaseClient) BaseClientWithCtx {
44-
// the default library client has SendRequestWithCtx, use it if available.
45-
if typedClient, ok := c.(BaseClientWithCtx); ok {
46-
return typedClient
47-
}
48-
return wrapperClient{BaseClient: c}
49-
}

client/client.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package client
33

44
import (
5-
"context"
65
"encoding/json"
76
"fmt"
87
"net/http"
@@ -45,7 +44,7 @@ func defaultHTTPClient() *http.Client {
4544
}
4645
}
4746

48-
func (c *Client) basicAuth() (username, password string) {
47+
func (c *Client) basicAuth() (string, string) {
4948
return c.Credentials.Username, c.Credentials.Password
5049
}
5150

@@ -90,12 +89,6 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
9089

9190
// SendRequest verifies, constructs, and authorizes an HTTP request.
9291
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
93-
headers map[string]interface{}) (*http.Response, error) {
94-
return c.SendRequestWithCtx(context.TODO(), method, rawURL, data, headers)
95-
}
96-
97-
// SendRequestWithCtx verifies, constructs, and authorizes an HTTP request.
98-
func (c *Client) SendRequestWithCtx(ctx context.Context, method string, rawURL string, data url.Values,
9992
headers map[string]interface{}) (*http.Response, error) {
10093
u, err := url.Parse(rawURL)
10194
if err != nil {
@@ -119,7 +112,7 @@ func (c *Client) SendRequestWithCtx(ctx context.Context, method string, rawURL s
119112
valueReader = strings.NewReader(data.Encode())
120113
}
121114

122-
req, err := http.NewRequestWithContext(ctx, method, u.String(), valueReader)
115+
req, err := http.NewRequest(method, u.String(), valueReader)
123116
if err != nil {
124117
return nil, err
125118
}

client/client_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package client_test
22

33
import (
4-
"context"
54
"encoding/json"
65
"io"
76
"net/http"
@@ -211,33 +210,6 @@ func TestClient_SetTimeoutTimesOut(t *testing.T) {
211210
assert.Error(t, err)
212211
}
213212

214-
func TestClient_SetTimeoutTimesOutViaContext(t *testing.T) {
215-
handlerDelay := 100 * time.Microsecond
216-
clientTimeout := 10 * time.Microsecond
217-
assert.True(t, clientTimeout < handlerDelay)
218-
219-
timeoutServer := httptest.NewServer(http.HandlerFunc(
220-
func(writer http.ResponseWriter, _ *http.Request) {
221-
d := map[string]interface{}{
222-
"response": "ok",
223-
}
224-
time.Sleep(100 * time.Microsecond)
225-
encoder := json.NewEncoder(writer)
226-
err := encoder.Encode(&d)
227-
if err != nil {
228-
t.Error(err)
229-
}
230-
writer.WriteHeader(http.StatusOK)
231-
}))
232-
defer timeoutServer.Close()
233-
234-
c := NewClient("user", "pass")
235-
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Microsecond)
236-
defer cancel()
237-
_, err := c.SendRequestWithCtx(ctx, "GET", timeoutServer.URL, nil, nil) //nolint:bodyclose
238-
assert.Error(t, err)
239-
}
240-
241213
func TestClient_SetTimeoutSucceeds(t *testing.T) {
242214
timeoutServer := httptest.NewServer(http.HandlerFunc(
243215
func(writer http.ResponseWriter, request *http.Request) {

client/page_util.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package client
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"strings"
@@ -35,15 +34,6 @@ func GetNext(baseUrl string, response interface{}, getNextPage func(nextPageUri
3534
return getNextPage(nextPageUrl)
3635
}
3736

38-
func GetNextWithCtx(ctx context.Context, baseUrl string, response interface{}, getNextPage func(ctx context.Context, nextPageUri string) (interface{}, error)) (interface{}, error) {
39-
nextPageUrl, err := getNextPageUrl(baseUrl, response)
40-
if err != nil {
41-
return nil, err
42-
}
43-
44-
return getNextPage(ctx, nextPageUrl)
45-
}
46-
4737
func toMap(s interface{}) (map[string]interface{}, error) {
4838
var payload map[string]interface{}
4939
data, err := json.Marshal(s)

client/request_handler.go

Lines changed: 30 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22
package client
33

44
import (
5-
"context"
65
"net/http"
76
"net/url"
87
"os"
98
"strings"
109
)
1110

12-
func buildUrlInternal(overrideEdge, overrideRegion, rawURL string) (string, error) {
11+
type RequestHandler struct {
12+
Client BaseClient
13+
Edge string
14+
Region string
15+
}
16+
17+
func NewRequestHandler(client BaseClient) *RequestHandler {
18+
return &RequestHandler{
19+
Client: client,
20+
Edge: os.Getenv("TWILIO_EDGE"),
21+
Region: os.Getenv("TWILIO_REGION"),
22+
}
23+
}
24+
25+
func (c *RequestHandler) sendRequest(method string, rawURL string, data url.Values,
26+
headers map[string]interface{}) (*http.Response, error) {
27+
parsedURL, err := c.BuildUrl(rawURL)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return c.Client.SendRequest(method, parsedURL, data, headers)
33+
}
34+
35+
// BuildUrl builds the target host string taking into account region and edge configurations.
36+
func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
1337
u, err := url.Parse(rawURL)
1438
if err != nil {
1539
return "", err
@@ -39,12 +63,12 @@ func buildUrlInternal(overrideEdge, overrideRegion, rawURL string) (string, erro
3963
region = pieces[2]
4064
}
4165

42-
if overrideEdge != "" {
43-
edge = overrideEdge
66+
if c.Edge != "" {
67+
edge = c.Edge
4468
}
4569

46-
if overrideRegion != "" {
47-
region = overrideRegion
70+
if c.Region != "" {
71+
region = c.Region
4872
} else if region == "" && edge != "" {
4973
region = "us1"
5074
}
@@ -59,96 +83,14 @@ func buildUrlInternal(overrideEdge, overrideRegion, rawURL string) (string, erro
5983
return u.String(), nil
6084
}
6185

62-
type RequestHandler struct {
63-
Client BaseClient
64-
Edge string
65-
Region string
66-
}
67-
68-
func NewRequestHandler(client BaseClient) *RequestHandler {
69-
return &RequestHandler{
70-
Client: client,
71-
Edge: os.Getenv("TWILIO_EDGE"),
72-
Region: os.Getenv("TWILIO_REGION"),
73-
}
74-
}
75-
76-
func (c *RequestHandler) sendRequest(method string, rawURL string, data url.Values,
77-
headers map[string]interface{}) (*http.Response, error) {
78-
parsedURL, err := c.BuildUrl(rawURL)
79-
if err != nil {
80-
return nil, err
81-
}
82-
83-
return c.Client.SendRequest(method, parsedURL, data, headers)
84-
}
85-
86-
// BuildUrl builds the target host string taking into account region and edge configurations.
87-
func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
88-
return buildUrlInternal(c.Edge, c.Region, rawURL)
89-
}
90-
91-
// deprecated
9286
func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) {
9387
return c.sendRequest(http.MethodPost, path, bodyData, headers)
9488
}
9589

96-
// deprecated
9790
func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
9891
return c.sendRequest(http.MethodGet, path, queryData, headers)
9992
}
10093

101-
// deprecated
10294
func (c *RequestHandler) Delete(path string, nothing url.Values, headers map[string]interface{}) (*http.Response, error) {
10395
return c.sendRequest(http.MethodDelete, path, nil, headers)
10496
}
105-
106-
func UpgradeRequestHandler(h *RequestHandler) *RequestHandlerWithCtx {
107-
return &RequestHandlerWithCtx{
108-
// wrapped client will supply context.TODO() to all API calls
109-
Client: wrapBaseClientWithNoopCtx(h.Client),
110-
Edge: h.Edge,
111-
Region: h.Region,
112-
}
113-
}
114-
115-
type RequestHandlerWithCtx struct {
116-
Client BaseClientWithCtx
117-
Edge string
118-
Region string
119-
}
120-
121-
func (c *RequestHandlerWithCtx) sendRequest(ctx context.Context, method string, rawURL string, data url.Values,
122-
headers map[string]interface{}) (*http.Response, error) {
123-
parsedURL, err := c.BuildUrl(rawURL)
124-
if err != nil {
125-
return nil, err
126-
}
127-
128-
return c.Client.SendRequestWithCtx(ctx, method, parsedURL, data, headers)
129-
}
130-
131-
func NewRequestHandlerWithCtx(client BaseClientWithCtx) *RequestHandlerWithCtx {
132-
return &RequestHandlerWithCtx{
133-
Client: client,
134-
Edge: os.Getenv("TWILIO_EDGE"),
135-
Region: os.Getenv("TWILIO_REGION"),
136-
}
137-
}
138-
139-
// BuildUrl builds the target host string taking into account region and edge configurations.
140-
func (c *RequestHandlerWithCtx) BuildUrl(rawURL string) (string, error) {
141-
return buildUrlInternal(c.Edge, c.Region, rawURL)
142-
}
143-
144-
func (c *RequestHandlerWithCtx) Post(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) {
145-
return c.sendRequest(ctx, http.MethodPost, path, bodyData, headers)
146-
}
147-
148-
func (c *RequestHandlerWithCtx) Get(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
149-
return c.sendRequest(ctx, http.MethodGet, path, queryData, headers)
150-
}
151-
152-
func (c *RequestHandlerWithCtx) Delete(ctx context.Context, path string, nothing url.Values, headers map[string]interface{}) (*http.Response, error) {
153-
return c.sendRequest(ctx, http.MethodDelete, path, nil, headers)
154-
}

0 commit comments

Comments
 (0)