Skip to content

Commit

Permalink
Merge pull request #234 from tjhop/feat/url-param-support
Browse files Browse the repository at this point in the history
feat: add support for setting URL parameters on client requests
  • Loading branch information
bduggan-ns1 authored Apr 18, 2024
2 parents b1f0b3a + e9d50c4 commit affe463
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 11 additions & 0 deletions mockns1/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/stretchr/testify/assert"
api "gopkg.in/ns1/ns1-go.v2/rest"
)

type testCase struct {
Expand All @@ -29,13 +30,23 @@ func (s *Service) AddTestCase(
method, uri string, returnStatus int,
requestHeaders, responseHeaders http.Header,
requestBody, responseBody interface{},
params ...api.Param,
) error {
s.stopTimer()
defer s.startTimer()

if !strings.HasPrefix(uri, "/v1/") {
uri = "/v1/" + uri
}

if len(params) > 0 {
uri = fmt.Sprintf("%s?%s=%s", uri, params[0].Key, params[0].Value)

for _, p := range params[1:] {
uri = fmt.Sprintf("%s&%s=%s", uri, p.Key, p.Value)
}
}

uri = strings.Replace(uri, "//", "/", -1)

tc := &testCase{
Expand Down
24 changes: 19 additions & 5 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,22 @@ func SetDDIAPI() func(*Client) {
return func(c *Client) { c.DDI = true }
}

// Param is a container struct which holds a `Key` and `Value` field corresponding to the values of a URL parameter.
type Param struct {
Key, Value string
}

// Do satisfies the Doer interface. resp will be nil if a non-HTTP error
// occurs, otherwise it is available for inspection when the error reflects a
// non-2XX response.
func (c Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
// non-2XX response. It accepts a variadic number of optional URL parameters to
// supply to the request. URL parameters are of type `rest.Param`.
func (c Client) Do(req *http.Request, v interface{}, params ...Param) (*http.Response, error) {
q := req.URL.Query()
for _, p := range params {
q.Set(p.Key, p.Value)
}
req.URL.RawQuery = q.Encode()

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -229,9 +241,11 @@ type NextFunc func(v *interface{}, uri string) (*http.Response, error)
// DoWithPagination Does, and follows Link headers for pagination. The returned
// Response is from the last URI visited - either the last page, or one that
// responded with a non-2XX status. If a non-HTTP error occurs, resp will be
// nil.
func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc) (*http.Response, error) {
resp, err := c.Do(req, v)
// nil. It accepts a variadic number of optional URL parameters to supply to
// the underlying `.Do()` method request(s). URL parameters are of type
// `rest.Param`.
func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc, params ...Param) (*http.Response, error) {
resp, err := c.Do(req, v, params...)
if err != nil {
return resp, err
}
Expand Down

0 comments on commit affe463

Please sign in to comment.