Skip to content

Commit

Permalink
#103 request path params feature (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 24, 2018
1 parent fc8cd45 commit ad03978
Show file tree
Hide file tree
Showing 9 changed files with 1,333 additions and 1,212 deletions.
21 changes: 21 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type Client struct {
notParseResponse bool
debugBodySizeLimit int64
logPrefix string
pathParams map[string]string
beforeRequest []func(*Client, *Request) error
udBeforeRequest []func(*Client, *Request) error
preReqHook func(*Client, *Request) error
Expand Down Expand Up @@ -311,6 +312,7 @@ func (c *Client) R() *Request {
client: c,
bodyBuf: nil,
multipartFiles: []*File{},
pathParams: make(map[string]string),
}

return r
Expand Down Expand Up @@ -719,6 +721,25 @@ func (c *Client) SetLogPrefix(prefix string) *Client {
return c
}

// SetPathParams method sets multiple URL path key-value pairs at one go in the
// resty client instance.
// resty.SetPathParams(map[string]string{
// "userId": "sample@sample.com",
// "subAccountId": "100002",
// })
//
// Result:
// URL - /v1/users/{userId}/{subAccountId}/details
// Composed URL - /v1/users/sample@sample.com/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`.
func (c *Client) SetPathParams(params map[string]string) *Client {
for p, v := range params {
c.pathParams[p] = v
}
return c
}

// IsProxySet method returns the true if proxy is set on client otherwise false.
func (c *Client) IsProxySet() bool {
return c.proxyURL != nil
Expand Down
11 changes: 10 additions & 1 deletion default.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ func SetDoNotParseResponse(parse bool) *Client {
return DefaultClient.SetDoNotParseResponse(parse)
}

// SetPathParams method sets the Request path parameter key-value pairs. See
// `Client.SetPathParams` for more information.
func SetPathParams(params map[string]string) *Client {
return DefaultClient.SetPathParams(params)
}

// IsProxySet method returns the true if proxy is set on client otherwise false.
// See `Client.IsProxySet` for more information.
func IsProxySet() bool {
Expand Down Expand Up @@ -284,9 +290,12 @@ func createClient(hc *http.Client) *Client {
JSONUnmarshal: json.Unmarshal,
httpClient: hc,
debugBodySizeLimit: math.MaxInt32,
logPrefix: "RESTY ",
pathParams: make(map[string]string),
}

// Log Prefix
c.SetLogPrefix("RESTY ")

// Default transport
c.SetTransport(&http.Transport{})

Expand Down
9 changes: 4 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ func parseRequestURL(c *Client, r *Request) error {
return err
}

// GitHub #103 Path Params
reqURL.Path = composeRequestURL(reqURL.Path, c, r)

// If Request.Url is relative path then added c.HostUrl into
// the request URL otherwise Request.Url will be used as-is
if !reqURL.IsAbs() {
if !strings.HasPrefix(r.URL, "/") {
r.URL = "/" + r.URL
}

reqURL, err = url.Parse(c.HostURL + r.URL)
reqURL, err = url.Parse(c.HostURL + reqURL.Path)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,25 @@ func (r *Request) SetDoNotParseResponse(parse bool) *Request {
return r
}

// SetPathParams method sets multiple URL path key-value pairs at one go in the
// resty current request instance.
// resty.R().SetPathParams(map[string]string{
// "userId": "sample@sample.com",
// "subAccountId": "100002",
// })
//
// Result:
// URL - /v1/users/{userId}/{subAccountId}/details
// Composed URL - /v1/users/sample@sample.com/100002/details
// It replace 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
}
return r
}

// ExpectContentType method allows to provide fallback `Content-Type` for automatic unmarshalling
// when `Content-Type` response header is unavailable.
func (r *Request) ExpectContentType(contentType string) *Request {
Expand Down
1 change: 1 addition & 0 deletions request16.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Request struct {
multipartFiles []*File
notParseResponse bool
fallbackContentType string
pathParams map[string]string
}

func (r *Request) addContextIfAvailable() {
Expand Down
1 change: 1 addition & 0 deletions request17.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Request struct {
notParseResponse bool
ctx context.Context
fallbackContentType string
pathParams map[string]string
}

// SetContext method sets the context.Context for current Request. It allows
Expand Down
Loading

0 comments on commit ad03978

Please sign in to comment.