Skip to content

Commit

Permalink
feat: add support for setting URL parameters on client requests
Browse files Browse the repository at this point in the history
Several NS1 API endpoints allow setting URL parameters to alter the
behavior of the endpoint. For instance, a `limit` parameter can be set
on the `/account/activity` endpoint to control how many events are
returned in the response. This would be useful in ns1#233.

This commit adds support for URL parameters by adding a new struct
`Param` to the `rest` package, and updating `rest.client.Do()` and
`rest.client.DoWithPagination()` to accept a variable amount of params.
In order for higher level services based off of the common service to
set url params, they will need to pass them through when invoking the
client's `Do()` method, like so:

```golang
resp, err := s.client.Do(req, &al, params...)
```
  • Loading branch information
tjhop committed Apr 4, 2024
1 parent cb9e29b commit ad0b536
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 7 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,19 @@ 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
}

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

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

tc := &testCase{
Expand Down
16 changes: 13 additions & 3 deletions rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,20 @@ func SetDDIAPI() func(*Client) {
return func(c *Client) { c.DDI = true }
}

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) {
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 @@ -228,8 +238,8 @@ type NextFunc func(v *interface{}, uri string) (*http.Response, error)
// 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)
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 ad0b536

Please sign in to comment.