Skip to content

Commit

Permalink
Add support for query parameters for all supported HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AsabuHere committed Oct 31, 2023
1 parent 3946662 commit 5b2fd1d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 78 deletions.
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)
}
139 changes: 70 additions & 69 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,112 @@
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 && queryParams[0] != nil{
v, _ := form.EncodeToStringWith(queryParams[0], delimiter, escapee, keepZeros)
fmt.Println("v is ", v)
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 +136,16 @@ 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)
fmt.Println("req\n\n\n",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...)
}

0 comments on commit 5b2fd1d

Please sign in to comment.