Skip to content

Commit

Permalink
fix: custom headers, cleanup docs, regenerate with the latest specs (#59
Browse files Browse the repository at this point in the history
)

* fix: fix header support and some cleanup
  • Loading branch information
shwetha-manvinkurke authored Apr 12, 2021
1 parent 5b36fd8 commit e316290
Show file tree
Hide file tree
Showing 87 changed files with 9,976 additions and 10,010 deletions.
7 changes: 3 additions & 4 deletions client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"net/url"
)

// BaseClient is the interface that wraps the Get/Post/Delete calls.
type BaseClient interface {
Post(path string, bodyData url.Values, headers interface{}) (*http.Response, error)
Get(path string, queryData interface{}, headers interface{}) (*http.Response, error)
Delete(path string, nothing interface{}, headers interface{}) (*http.Response, error)
Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error)
Get(path string, queryData interface{}, headers map[string]interface{}) (*http.Response, error)
Delete(path string, nothing interface{}, headers map[string]interface{}) (*http.Response, error)
}
20 changes: 13 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -78,7 +79,8 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
}

// SendRequest verifies, constructs, and authorizes an HTTP request.
func (c Client) SendRequest(method string, rawURL string, queryParams interface{}, formData url.Values) (*http.Response, error) {
func (c Client) SendRequest(method string, rawURL string, queryParams interface{}, formData url.Values,
headers map[string]interface{}) (*http.Response, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -109,23 +111,27 @@ func (c Client) SendRequest(method string, rawURL string, queryParams interface{
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
}

for k, v := range headers {
req.Header.Add(k, fmt.Sprint(v))
}

return c.doWithErr(req)
}

// Post performs a POST request on the object at the provided URI in the context of the Request's BaseURL
// with the provided data as parameters.
func (c Client) Post(path string, bodyData url.Values, headers interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodPost, path, nil, bodyData)
func (c Client) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodPost, path, nil, bodyData, headers)
}

// Get performs a GET request on the object at the provided URI in the context of the Request's BaseURL
// with the provided data as parameters.
func (c Client) Get(path string, queryData interface{}, headers interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodGet, path, queryData, nil)
func (c Client) Get(path string, queryData interface{}, headers map[string]interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodGet, path, queryData, nil, headers)
}

// Delete performs a DELETE request on the object at the provided URI in the context of the Request's BaseURL
// with the provided data as parameters.
func (c Client) Delete(path string, nothing interface{}, headers interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodDelete, path, nil, nil)
func (c Client) Delete(path string, nothing interface{}, headers map[string]interface{}) (*http.Response, error) {
return c.SendRequest(http.MethodDelete, path, nil, nil, headers)
}
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestClient_SendRequestError(t *testing.T) {
defer mockServer.Close()

client := NewClient("user", "pass")
resp, err := client.SendRequest("get", mockServer.URL, nil, nil)
resp, err := client.SendRequest("get", mockServer.URL, nil, nil, nil) //nolint:bodyclose
twilioError := err.(*error.TwilioRestError)
assert.Nil(t, resp)
assert.Equal(t, 400, twilioError.Status)
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestClient_SendRequestErrorWithDetails(t *testing.T) {
defer mockServer.Close()

client := NewClient("user", "pass")
resp, err := client.SendRequest("get", mockServer.URL, nil, nil)
resp, err := client.SendRequest("get", mockServer.URL, nil, nil, nil) //nolint:bodyclose
twilioError := err.(*error.TwilioRestError)
details := make(map[string]interface{})
details["foo"] = "bar"
Expand All @@ -89,7 +89,7 @@ func TestClient_SendRequestWithRedirect(t *testing.T) {
defer mockServer.Close()

client := NewClient("user", "pass")
resp, _ := client.SendRequest("get", mockServer.URL, nil, nil)
resp, _ := client.SendRequest("get", mockServer.URL, nil, nil, nil) //nolint:bodyclose
assert.Equal(t, 307, resp.StatusCode)
}

Expand All @@ -112,7 +112,7 @@ func TestClient_SetTimeoutTimesOut(t *testing.T) {

client := NewClient("user", "pass")
client.SetTimeout(10 * time.Microsecond)
_, err := client.SendRequest("get", mockServer.URL, nil, nil)
_, err := client.SendRequest("get", mockServer.URL, nil, nil, nil) //nolint:bodyclose
assert.Error(t, err)
}

Expand All @@ -135,7 +135,7 @@ func TestClient_SetTimeoutSucceeds(t *testing.T) {

client := NewClient("user", "pass")
client.SetTimeout(10 * time.Second)
resp, err := client.SendRequest("get", mockServer.URL, nil, nil)
resp, err := client.SendRequest("get", mockServer.URL, nil, nil, nil) //nolint:bodyclose
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}
26 changes: 13 additions & 13 deletions twilio/rest/accounts/v1/api_default.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e316290

Please sign in to comment.