Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review query param changes #224

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ type BaseClient interface {
AccountSid() string
SetTimeout(timeout time.Duration)
SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error)
headers map[string]interface{}, queryParams url.Values) (*http.Response, error)
}
137 changes: 68 additions & 69 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,111 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
"github.com/twilio/twilio-go/client/form"
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
"github.com/twilio/twilio-go/client/form"
)

// Credentials store user authentication credentials.
type Credentials struct {
Username string
Password string
Username string
Password string
}

func NewCredentials(username string, password string) *Credentials {
return &Credentials{Username: username, Password: password}
return &Credentials{Username: username, Password: password}
}

// Client encapsulates a standard HTTP backend with authorization.
type Client struct {
*Credentials
HTTPClient *http.Client
accountSid string
UserAgentExtensions []string
*Credentials
HTTPClient *http.Client
accountSid string
UserAgentExtensions []string
}

// default http Client should not follow redirects and return the most recent response.
func defaultHTTPClient() *http.Client {
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Second * 10,
}
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: time.Second * 10,
}
}

func (c *Client) basicAuth() (string, string) {
return c.Credentials.Username, c.Credentials.Password
return c.Credentials.Username, c.Credentials.Password
}

// SetTimeout sets the Timeout for HTTP requests.
func (c *Client) SetTimeout(timeout time.Duration) {
if c.HTTPClient == nil {
c.HTTPClient = defaultHTTPClient()
}
c.HTTPClient.Timeout = timeout
if c.HTTPClient == nil {
c.HTTPClient = defaultHTTPClient()
}
c.HTTPClient.Timeout = timeout
}

const (
keepZeros = true
delimiter = '.'
escapee = '\\'
keepZeros = true
delimiter = '.'
escapee = '\\'
)

func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
client := c.HTTPClient

if client == nil {
client = defaultHTTPClient()
}

res, err := client.Do(req)
if err != nil {
return nil, err
}

// Note that 3XX response codes are allowed for fetches
if res.StatusCode < 200 || res.StatusCode >= 400 {
err = &TwilioRestError{}
if decodeErr := json.NewDecoder(res.Body).Decode(err); decodeErr != nil {
err = errors.Wrap(decodeErr, "error decoding the response for an HTTP error code: "+strconv.Itoa(res.StatusCode))
return nil, err
}

return nil, err
}
return res, nil
client := c.HTTPClient

if client == nil {
client = defaultHTTPClient()
}

res, err := client.Do(req)
if err != nil {
return nil, err
}

// Note that 3XX response codes are allowed for fetches
if res.StatusCode < 200 || res.StatusCode >= 400 {
err = &TwilioRestError{}
if decodeErr := json.NewDecoder(res.Body).Decode(err); decodeErr != nil {
err = errors.Wrap(decodeErr, "error decoding the response for an HTTP error code: "+strconv.Itoa(res.StatusCode))
return nil, err
}

return nil, err
}
return res, nil
}

// SendRequest verifies, constructs, and authorizes an HTTP request.
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
u, err := url.Parse(rawURL)
headers map[string]interface{}, queryParams ...url.Values) (*http.Response, error) {

u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}

valueReader := &strings.Reader{}
goVersion := runtime.Version()

if method == http.MethodGet {
if data != nil {
v, _ := form.EncodeToStringWith(data, delimiter, escapee, keepZeros)
regex := regexp.MustCompile(`\.\d+`)
s := regex.ReplaceAllString(v, "")
if queryParams != nil {
v, _ := form.EncodeToStringWith(queryParams, delimiter, escapee, keepZeros)
regex := regexp.MustCompile(`\.\d+`)
s := regex.ReplaceAllString(v, "")

u.RawQuery = s
}
}
u.RawQuery = s
}

if method == http.MethodPost {
valueReader = strings.NewReader(data.Encode())
Expand Down Expand Up @@ -135,16 +135,15 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
for k, v := range headers {
req.Header.Add(k, fmt.Sprint(v))
}

return c.doWithErr(req)
return c.doWithErr(req)
}

// SetAccountSid sets the Client's accountSid field
func (c *Client) SetAccountSid(sid string) {
c.accountSid = sid
c.accountSid = sid
}

// Returns the Account SID.
func (c *Client) AccountSid() string {
return c.accountSid
return c.accountSid
}
16 changes: 8 additions & 8 deletions client/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func NewRequestHandler(client BaseClient) *RequestHandler {
}

func (c *RequestHandler) sendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}) (*http.Response, error) {
headers map[string]interface{}, queryParams url.Values) (*http.Response, error) {
parsedURL, err := c.BuildUrl(rawURL)
if err != nil {
return nil, err
}

return c.Client.SendRequest(method, parsedURL, data, headers)
return c.Client.SendRequest(method, parsedURL, data, headers, queryParams)
}

// BuildUrl builds the target host string taking into account region and edge configurations.
Expand Down Expand Up @@ -83,14 +83,14 @@ func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
return u.String(), nil
}

func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.sendRequest(http.MethodPost, path, bodyData, headers)
func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}, queryParams url.Values) (*http.Response, error) {
return c.sendRequest(http.MethodPost, path, bodyData, headers, queryParams)
}

func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.sendRequest(http.MethodGet, path, queryData, headers)
func (c *RequestHandler) Get(path string, queryData url.Values, headers map[string]interface{}, queryParams url.Values) (*http.Response, error) {
return c.sendRequest(http.MethodGet, path, queryData, headers, queryParams)
}

func (c *RequestHandler) Delete(path string, nothing url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.sendRequest(http.MethodDelete, path, nil, headers)
func (c *RequestHandler) Delete(path string, nothing url.Values, headers map[string]interface{}, queryParams url.Values) (*http.Response, error) {
return c.sendRequest(http.MethodDelete, path, nil, headers, queryParams)
}
3 changes: 2 additions & 1 deletion rest/accounts/v1/auth_tokens_promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func (c *ApiService) UpdateAuthTokenPromotion() (*AccountsV1AuthTokenPromotion,
path := "/v1/AuthTokens/Promote"

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions rest/accounts/v1/auth_tokens_secondary.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ func (c *ApiService) CreateSecondaryAuthToken() (*AccountsV1SecondaryAuthToken,
path := "/v1/AuthTokens/Secondary"

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand All @@ -46,9 +47,10 @@ func (c *ApiService) DeleteSecondaryAuthToken() error {
path := "/v1/AuthTokens/Secondary"

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers, queryParams)
if err != nil {
return err
}
Expand Down
19 changes: 12 additions & 7 deletions rest/accounts/v1/credentials_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Ac
path := "/v1/Credentials/AWS"

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

if params != nil && params.Credentials != nil {
Expand All @@ -63,7 +64,7 @@ func (c *ApiService) CreateCredentialAws(params *CreateCredentialAwsParams) (*Ac
data.Set("AccountSid", *params.AccountSid)
}

resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand All @@ -84,9 +85,10 @@ func (c *ApiService) DeleteCredentialAws(Sid string) error {
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers, queryParams)
if err != nil {
return err
}
Expand All @@ -102,9 +104,10 @@ func (c *ApiService) FetchCredentialAws(Sid string) (*AccountsV1CredentialAws, e
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,10 +144,11 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke
path := "/v1/Credentials/AWS"

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

if params != nil && params.PageSize != nil {
data.Set("PageSize", fmt.Sprint(*params.PageSize))
queryParams.Set("PageSize", fmt.Sprint(*params.PageSize))
}

if pageToken != "" {
Expand All @@ -154,7 +158,7 @@ func (c *ApiService) PageCredentialAws(params *ListCredentialAwsParams, pageToke
data.Set("Page", pageNumber)
}

resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Get(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -241,7 +245,7 @@ func (c *ApiService) getNextListCredentialAwsResponse(nextPageUrl string) (inter
if nextPageUrl == "" {
return nil, nil
}
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
resp, err := c.requestHandler.Get(nextPageUrl, nil, nil, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -272,13 +276,14 @@ func (c *ApiService) UpdateCredentialAws(Sid string, params *UpdateCredentialAws
path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)

data := url.Values{}
queryParams := url.Values{}
headers := make(map[string]interface{})

if params != nil && params.FriendlyName != nil {
data.Set("FriendlyName", *params.FriendlyName)
}

resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
resp, err := c.requestHandler.Post(c.baseURL+path, data, headers, queryParams)
if err != nil {
return nil, err
}
Expand Down
Loading