22package client
33
44import (
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
9286func (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
9790func (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
10294func (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