|
2 | 2 | package client |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "context" |
5 | 6 | "net/http" |
6 | 7 | "net/url" |
7 | 8 | "os" |
8 | 9 | "strings" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | type RequestHandler struct { |
12 | | - Client BaseClient |
13 | | - Edge string |
14 | | - Region string |
| 13 | + Client BaseClient |
| 14 | + Edge string |
| 15 | + Region string |
| 16 | + clientWithContext BaseClientWithContext |
15 | 17 | } |
16 | 18 |
|
17 | 19 | func NewRequestHandler(client BaseClient) *RequestHandler { |
| 20 | + // If the base client supports context, add it to the request handler. |
| 21 | + // Otherwise we leave it nil and the base client will be used. |
| 22 | + clientWithContext, _ := client.(BaseClientWithContext) |
| 23 | + |
18 | 24 | return &RequestHandler{ |
19 | | - Client: client, |
20 | | - Edge: os.Getenv("TWILIO_EDGE"), |
21 | | - Region: os.Getenv("TWILIO_REGION"), |
| 25 | + Client: client, |
| 26 | + Edge: os.Getenv("TWILIO_EDGE"), |
| 27 | + Region: os.Getenv("TWILIO_REGION"), |
| 28 | + clientWithContext: clientWithContext, |
22 | 29 | } |
23 | 30 | } |
24 | 31 |
|
25 | | -func (c *RequestHandler) sendRequest(method string, rawURL string, data url.Values, |
| 32 | +func NewRequestHandlerWithContext(client BaseClientWithContext) *RequestHandler { |
| 33 | + return NewRequestHandler(client) |
| 34 | +} |
| 35 | + |
| 36 | +func (c *RequestHandler) sendRequest(ctx context.Context, method string, rawURL string, data url.Values, |
26 | 37 | headers map[string]interface{}, body ...byte) (*http.Response, error) { |
27 | 38 | parsedURL, err := c.BuildUrl(rawURL) |
28 | 39 | if err != nil { |
29 | 40 | return nil, err |
30 | 41 | } |
| 42 | + if c.clientWithContext != nil { |
| 43 | + return c.clientWithContext.SendRequestWithContext(ctx, method, parsedURL, data, headers, body...) |
| 44 | + } |
31 | 45 | return c.Client.SendRequest(method, parsedURL, data, headers, body...) |
32 | 46 | } |
33 | 47 |
|
@@ -83,21 +97,41 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) { |
83 | 97 | } |
84 | 98 |
|
85 | 99 | func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
86 | | - return c.sendRequest(http.MethodPost, path, bodyData, headers, body...) |
| 100 | + return c.PostWithContext(context.Background(), path, bodyData, headers, body...) |
| 101 | +} |
| 102 | + |
| 103 | +func (c *RequestHandler) PostWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 104 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPost, path, bodyData, headers, body...) |
87 | 105 | } |
88 | 106 |
|
89 | 107 | func (c *RequestHandler) Put(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
90 | | - return c.sendRequest(http.MethodPut, path, bodyData, headers, body...) |
| 108 | + return c.PutWithContext(context.Background(), path, bodyData, headers, body...) |
| 109 | +} |
| 110 | + |
| 111 | +func (c *RequestHandler) PutWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 112 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPut, path, bodyData, headers, body...) |
91 | 113 | } |
92 | 114 |
|
93 | 115 | func (c *RequestHandler) Patch(path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
94 | | - return c.sendRequest(http.MethodPatch, path, bodyData, headers, body...) |
| 116 | + return c.PatchWithContext(context.Background(), path, bodyData, headers, body...) |
| 117 | +} |
| 118 | + |
| 119 | +func (c *RequestHandler) PatchWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 120 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPatch, path, bodyData, headers, body...) |
95 | 121 | } |
96 | 122 |
|
97 | 123 | func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
98 | | - return c.sendRequest(http.MethodGet, path, queryData, headers) |
| 124 | + return c.GetWithContext(context.Background(), path, queryData, headers) |
| 125 | +} |
| 126 | + |
| 127 | +func (c *RequestHandler) GetWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
| 128 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodGet, path, queryData, headers) |
99 | 129 | } |
100 | 130 |
|
101 | 131 | func (c *RequestHandler) Delete(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
102 | | - return c.sendRequest(http.MethodDelete, path, queryData, headers) |
| 132 | + return c.DeleteWithContext(context.Background(), path, queryData, headers) |
| 133 | +} |
| 134 | + |
| 135 | +func (c *RequestHandler) DeleteWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
| 136 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodDelete, path, queryData, headers) |
103 | 137 | } |
0 commit comments