Skip to content

Commit affe463

Browse files
authored
Merge pull request #234 from tjhop/feat/url-param-support
feat: add support for setting URL parameters on client requests
2 parents b1f0b3a + e9d50c4 commit affe463

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

mockns1/testcase.go

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/stretchr/testify/assert"
11+
api "gopkg.in/ns1/ns1-go.v2/rest"
1112
)
1213

1314
type testCase struct {
@@ -29,13 +30,23 @@ func (s *Service) AddTestCase(
2930
method, uri string, returnStatus int,
3031
requestHeaders, responseHeaders http.Header,
3132
requestBody, responseBody interface{},
33+
params ...api.Param,
3234
) error {
3335
s.stopTimer()
3436
defer s.startTimer()
3537

3638
if !strings.HasPrefix(uri, "/v1/") {
3739
uri = "/v1/" + uri
3840
}
41+
42+
if len(params) > 0 {
43+
uri = fmt.Sprintf("%s?%s=%s", uri, params[0].Key, params[0].Value)
44+
45+
for _, p := range params[1:] {
46+
uri = fmt.Sprintf("%s&%s=%s", uri, p.Key, p.Value)
47+
}
48+
}
49+
3950
uri = strings.Replace(uri, "//", "/", -1)
4051

4152
tc := &testCase{

rest/client.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,22 @@ func SetDDIAPI() func(*Client) {
187187
return func(c *Client) { c.DDI = true }
188188
}
189189

190+
// Param is a container struct which holds a `Key` and `Value` field corresponding to the values of a URL parameter.
191+
type Param struct {
192+
Key, Value string
193+
}
194+
190195
// Do satisfies the Doer interface. resp will be nil if a non-HTTP error
191196
// occurs, otherwise it is available for inspection when the error reflects a
192-
// non-2XX response.
193-
func (c Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
197+
// non-2XX response. It accepts a variadic number of optional URL parameters to
198+
// supply to the request. URL parameters are of type `rest.Param`.
199+
func (c Client) Do(req *http.Request, v interface{}, params ...Param) (*http.Response, error) {
200+
q := req.URL.Query()
201+
for _, p := range params {
202+
q.Set(p.Key, p.Value)
203+
}
204+
req.URL.RawQuery = q.Encode()
205+
194206
resp, err := c.httpClient.Do(req)
195207
if err != nil {
196208
return nil, err
@@ -229,9 +241,11 @@ type NextFunc func(v *interface{}, uri string) (*http.Response, error)
229241
// DoWithPagination Does, and follows Link headers for pagination. The returned
230242
// Response is from the last URI visited - either the last page, or one that
231243
// responded with a non-2XX status. If a non-HTTP error occurs, resp will be
232-
// nil.
233-
func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc) (*http.Response, error) {
234-
resp, err := c.Do(req, v)
244+
// nil. It accepts a variadic number of optional URL parameters to supply to
245+
// the underlying `.Do()` method request(s). URL parameters are of type
246+
// `rest.Param`.
247+
func (c Client) DoWithPagination(req *http.Request, v interface{}, f NextFunc, params ...Param) (*http.Response, error) {
248+
resp, err := c.Do(req, v, params...)
235249
if err != nil {
236250
return resp, err
237251
}

0 commit comments

Comments
 (0)