-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from go-resty/enhancements
- Loading branch information
Showing
4 changed files
with
68 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ var ( | |
hdrContentTypeKey = http.CanonicalHeaderKey("Content-Type") | ||
hdrContentLengthKey = http.CanonicalHeaderKey("Content-Length") | ||
hdrContentEncodingKey = http.CanonicalHeaderKey("Content-Encoding") | ||
hdrAuthorizationKey = http.CanonicalHeaderKey("Authorization") | ||
hdrLocationKey = http.CanonicalHeaderKey("Location") | ||
|
||
plainTextType = "text/plain; charset=utf-8" | ||
|
@@ -113,6 +112,10 @@ type Client struct { | |
JSONMarshal func(v interface{}) ([]byte, error) | ||
JSONUnmarshal func(data []byte, v interface{}) error | ||
|
||
// HeaderAuthorizationKey is used to set/access Request Authorization header | ||
// value when `SetAuthToken` option is used. | ||
HeaderAuthorizationKey string | ||
|
||
jsonEscapeHTML bool | ||
setContentLength bool | ||
closeConnection bool | ||
|
@@ -742,6 +745,22 @@ func (c *Client) SetDoNotParseResponse(parse bool) *Client { | |
return c | ||
} | ||
|
||
// SetPathParam method sets single URL path key-value pair in the | ||
// Resty client instance. | ||
// client.SetPathParam("userId", "[email protected]") | ||
// | ||
// Result: | ||
// URL - /v1/users/{userId}/details | ||
// Composed URL - /v1/users/[email protected]/details | ||
// It replaces the value of the key while composing the request URL. | ||
// | ||
// Also it can be overridden at request level Path Params options, | ||
// see `Request.SetPathParam` or `Request.SetPathParams`. | ||
func (c *Client) SetPathParam(param, value string) *Client { | ||
c.pathParams[param] = value | ||
return c | ||
} | ||
|
||
// SetPathParams method sets multiple URL path key-value pairs at one go in the | ||
// Resty client instance. | ||
// client.SetPathParams(map[string]string{ | ||
|
@@ -752,11 +771,13 @@ func (c *Client) SetDoNotParseResponse(parse bool) *Client { | |
// Result: | ||
// URL - /v1/users/{userId}/{subAccountId}/details | ||
// Composed URL - /v1/users/[email protected]/100002/details | ||
// It replace the value of the key while composing request URL. Also it can be | ||
// overridden at request level Path Params options, see `Request.SetPathParams`. | ||
// It replaces the value of the key while composing the request URL. | ||
// | ||
// Also it can be overridden at request level Path Params options, | ||
// see `Request.SetPathParam` or `Request.SetPathParams`. | ||
func (c *Client) SetPathParams(params map[string]string) *Client { | ||
for p, v := range params { | ||
c.pathParams[p] = v | ||
c.SetPathParam(p, v) | ||
} | ||
return c | ||
} | ||
|
@@ -989,18 +1010,19 @@ func createClient(hc *http.Client) *Client { | |
} | ||
|
||
c := &Client{ // not setting lang default values | ||
QueryParam: url.Values{}, | ||
FormData: url.Values{}, | ||
Header: http.Header{}, | ||
Cookies: make([]*http.Cookie, 0), | ||
RetryWaitTime: defaultWaitTime, | ||
RetryMaxWaitTime: defaultMaxWaitTime, | ||
JSONMarshal: json.Marshal, | ||
JSONUnmarshal: json.Unmarshal, | ||
jsonEscapeHTML: true, | ||
httpClient: hc, | ||
debugBodySizeLimit: math.MaxInt32, | ||
pathParams: make(map[string]string), | ||
QueryParam: url.Values{}, | ||
FormData: url.Values{}, | ||
Header: http.Header{}, | ||
Cookies: make([]*http.Cookie, 0), | ||
RetryWaitTime: defaultWaitTime, | ||
RetryMaxWaitTime: defaultMaxWaitTime, | ||
JSONMarshal: json.Marshal, | ||
JSONUnmarshal: json.Unmarshal, | ||
HeaderAuthorizationKey: http.CanonicalHeaderKey("Authorization"), | ||
jsonEscapeHTML: true, | ||
httpClient: hc, | ||
debugBodySizeLimit: math.MaxInt32, | ||
pathParams: make(map[string]string), | ||
} | ||
|
||
// Logger | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -469,6 +469,20 @@ func (r *Request) SetDoNotParseResponse(parse bool) *Request { | |
return r | ||
} | ||
|
||
// SetPathParam method sets single URL path key-value pair in the | ||
// Resty current request instance. | ||
// client.R().SetPathParam("userId", "[email protected]") | ||
// | ||
// Result: | ||
// URL - /v1/users/{userId}/details | ||
// Composed URL - /v1/users/[email protected]/details | ||
// It replaces the value of the key while composing the request URL. Also you can | ||
// override Path Params value, which was set at client instance level. | ||
func (r *Request) SetPathParam(param, value string) *Request { | ||
r.pathParams[param] = value | ||
return r | ||
} | ||
|
||
// SetPathParams method sets multiple URL path key-value pairs at one go in the | ||
// Resty current request instance. | ||
// client.R().SetPathParams(map[string]string{ | ||
|
@@ -479,11 +493,11 @@ func (r *Request) SetDoNotParseResponse(parse bool) *Request { | |
// Result: | ||
// URL - /v1/users/{userId}/{subAccountId}/details | ||
// Composed URL - /v1/users/[email protected]/100002/details | ||
// It replace the value of the key while composing request URL. Also you can | ||
// It replaces the value of the key while composing request URL. Also you can | ||
// override Path Params value, which was set at client instance level. | ||
func (r *Request) SetPathParams(params map[string]string) *Request { | ||
for p, v := range params { | ||
r.pathParams[p] = v | ||
r.SetPathParam(p, v) | ||
} | ||
return r | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1498,19 +1498,15 @@ func TestRequestExpectContentTypeTest(t *testing.T) { | |
assertEqual(t, "", firstNonEmpty("", "")) | ||
} | ||
|
||
func TestGetPathParams(t *testing.T) { | ||
func TestGetPathParamAndPathParams(t *testing.T) { | ||
ts := createGetServer(t) | ||
defer ts.Close() | ||
|
||
c := dc() | ||
c.SetHostURL(ts.URL). | ||
SetPathParams(map[string]string{ | ||
"userId": "[email protected]", | ||
}) | ||
c := dc(). | ||
SetHostURL(ts.URL). | ||
SetPathParam("userId", "[email protected]") | ||
|
||
resp, err := c.R().SetPathParams(map[string]string{ | ||
"subAccountId": "100002", | ||
}). | ||
resp, err := c.R().SetPathParam("subAccountId", "100002"). | ||
Get("/v1/users/{userId}/{subAccountId}/details") | ||
|
||
assertError(t, err) | ||
|
@@ -1808,12 +1804,12 @@ func TestDebugLoggerRequestBodyTooLarge(t *testing.T) { | |
assertEqual(t, true, strings.Contains(output.String(), "REQUEST TOO LARGE")) | ||
} | ||
|
||
func TestPostMapTemporaryRedirect(t *testing.T) { | ||
func TestPostMapTemporaryRedirect(t *testing.T) { | ||
ts := createPostServer(t) | ||
defer ts.Close() | ||
|
||
c := dc() | ||
resp, err := c.R().SetBody(map[string]string{"username":"testuser", "password": "testpass"}). | ||
resp, err := c.R().SetBody(map[string]string{"username": "testuser", "password": "testpass"}). | ||
Post(ts.URL + "/redirect") | ||
|
||
assertNil(t, err) | ||
|
@@ -1831,7 +1827,7 @@ func (b brokenReadCloser) Close() error { | |
return nil | ||
} | ||
|
||
func TestPostBodyError(t *testing.T) { | ||
func TestPostBodyError(t *testing.T) { | ||
ts := createPostServer(t) | ||
defer ts.Close() | ||
|
||
|
@@ -1840,4 +1836,4 @@ func TestPostBodyError(t *testing.T) { | |
assertNotNil(t, err) | ||
assertEqual(t, "read error", err.Error()) | ||
assertNil(t, resp) | ||
} | ||
} |