github.com/twilio/twilio-go@v1.20.1/client/request_handler.go (about)

     1  // Package client provides internal utilities for the twilio-go client library.
     2  package client
     3  
     4  import (
     5  	"net/http"
     6  	"net/url"
     7  	"os"
     8  	"strings"
     9  )
    10  
    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{}, body ...byte) (*http.Response, error) {
    27  	parsedURL, err := c.BuildUrl(rawURL)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return c.Client.SendRequest(method, parsedURL, data, headers, body...)
    32  }
    33  
    34  // BuildUrl builds the target host string taking into account region and edge configurations.
    35  func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
    36  	u, err := url.Parse(rawURL)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  
    41  	var (
    42  		edge    = ""
    43  		region  = ""
    44  		pieces  = strings.Split(u.Host, ".")
    45  		product = pieces[0]
    46  		result  []string
    47  	)
    48  	suffix := ""
    49  
    50  	if len(pieces) >= 3 {
    51  		suffix = strings.Join(pieces[len(pieces)-2:], ".")
    52  	} else {
    53  		return u.String(), nil
    54  	}
    55  
    56  	if len(pieces) == 4 {
    57  		// product.region.twilio.com
    58  		region = pieces[1]
    59  	} else if len(pieces) == 5 {
    60  		// product.edge.region.twilio.com
    61  		edge = pieces[1]
    62  		region = pieces[2]
    63  	}
    64  
    65  	if c.Edge != "" {
    66  		edge = c.Edge
    67  	}
    68  
    69  	if c.Region != "" {
    70  		region = c.Region
    71  	} else if region == "" && edge != "" {
    72  		region = "us1"
    73  	}
    74  
    75  	for _, item := range []string{product, edge, region, suffix} {
    76  		if item != "" {
    77  			result = append(result, item)
    78  		}
    79  	}
    80  
    81  	u.Host = strings.Join(result, ".")
    82  	return u.String(), nil
    83  }
    84  
    85  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...)
    87  }
    88  
    89  func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
    90  	return c.sendRequest(http.MethodGet, path, queryData, headers)
    91  }
    92  
    93  func (c *RequestHandler) Delete(path string, nothing url.Values, headers map[string]interface{}) (*http.Response, error) {
    94  	return c.sendRequest(http.MethodDelete, path, nil, headers)
    95  }