22package client
33
44import (
5+ "context"
56 "net/http"
67 "net/url"
78 "os"
89 "strings"
910)
1011
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 ) {
12+ func buildUrlInternal (overrideEdge , overrideRegion , rawURL string ) (string , error ) {
3713 u , err := url .Parse (rawURL )
3814 if err != nil {
3915 return "" , err
@@ -63,12 +39,12 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
6339 region = pieces [2 ]
6440 }
6541
66- if c . Edge != "" {
67- edge = c . Edge
42+ if overrideEdge != "" {
43+ edge = overrideEdge
6844 }
6945
70- if c . Region != "" {
71- region = c . Region
46+ if overrideRegion != "" {
47+ region = overrideRegion
7248 } else if region == "" && edge != "" {
7349 region = "us1"
7450 }
@@ -83,14 +59,96 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
8359 return u .String (), nil
8460}
8561
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
8692func (c * RequestHandler ) Post (path string , bodyData url.Values , headers map [string ]interface {}) (* http.Response , error ) {
8793 return c .sendRequest (http .MethodPost , path , bodyData , headers )
8894}
8995
96+ // deprecated
9097func (c * RequestHandler ) Get (path string , queryData url.Values , headers map [string ]interface {}) (* http.Response , error ) {
9198 return c .sendRequest (http .MethodGet , path , queryData , headers )
9299}
93100
101+ // deprecated
94102func (c * RequestHandler ) Delete (path string , nothing url.Values , headers map [string ]interface {}) (* http.Response , error ) {
95103 return c .sendRequest (http .MethodDelete , path , nil , headers )
96104}
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