|
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 (c *RequestHandler) sendRequest(ctx context.Context, method string, rawURL string, data url.Values, |
26 | 33 | headers map[string]interface{}, body ...byte) (*http.Response, error) { |
27 | 34 | parsedURL, err := c.BuildUrl(rawURL) |
28 | 35 | if err != nil { |
29 | 36 | return nil, err |
30 | 37 | } |
| 38 | + if c.clientWithContext != nil { |
| 39 | + return c.clientWithContext.SendRequestWithContext(ctx, method, parsedURL, data, headers, body...) |
| 40 | + } |
31 | 41 | return c.Client.SendRequest(method, parsedURL, data, headers, body...) |
32 | 42 | } |
33 | 43 |
|
@@ -83,21 +93,41 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) { |
83 | 93 | } |
84 | 94 |
|
85 | 95 | 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...) |
| 96 | + return c.PostWithContext(context.Background(), path, bodyData, headers, body...) |
| 97 | +} |
| 98 | + |
| 99 | +func (c *RequestHandler) PostWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 100 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPost, path, bodyData, headers, body...) |
87 | 101 | } |
88 | 102 |
|
89 | 103 | 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...) |
| 104 | + return c.PutWithContext(context.Background(), path, bodyData, headers, body...) |
| 105 | +} |
| 106 | + |
| 107 | +func (c *RequestHandler) PutWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 108 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPut, path, bodyData, headers, body...) |
91 | 109 | } |
92 | 110 |
|
93 | 111 | 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...) |
| 112 | + return c.PatchWithContext(context.Background(), path, bodyData, headers, body...) |
| 113 | +} |
| 114 | + |
| 115 | +func (c *RequestHandler) PatchWithContext(ctx context.Context, path string, bodyData url.Values, headers map[string]interface{}, body ...byte) (*http.Response, error) { |
| 116 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodPatch, path, bodyData, headers, body...) |
95 | 117 | } |
96 | 118 |
|
97 | 119 | 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) |
| 120 | + return c.GetWithContext(context.Background(), path, queryData, headers) |
| 121 | +} |
| 122 | + |
| 123 | +func (c *RequestHandler) GetWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
| 124 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodGet, path, queryData, headers) |
99 | 125 | } |
100 | 126 |
|
101 | 127 | 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) |
| 128 | + return c.DeleteWithContext(context.Background(), path, queryData, headers) |
| 129 | +} |
| 130 | + |
| 131 | +func (c *RequestHandler) DeleteWithContext(ctx context.Context, path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) { |
| 132 | + return c.clientWithContext.SendRequestWithContext(ctx, http.MethodDelete, path, queryData, headers) |
103 | 133 | } |
0 commit comments