Skip to content

Commit f2b9cdd

Browse files
authored
fix(http): LimitedRequest limit ineffective sometime (#45)
1 parent 55b2a28 commit f2b9cdd

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

events/events_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ type fakeHTTPServer struct{}
4343

4444
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
4545
res.Header().Add("DRYCC_API_VERSION", drycc.APIVersion)
46-
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "ptype=example-go-web" {
46+
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "ptype=example-go-web&limit=100" {
4747
res.Write([]byte(ptypeEventsFixture))
4848
return
4949
}
50-
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "pod_name=example-go-web-6b44dbd6c8-h89cg" {
50+
if req.URL.Path == "/v2/apps/example-go/events/" && req.Method == "GET" && req.URL.RawQuery == "pod_name=example-go-web-6b44dbd6c8-h89cg&limit=100" {
5151
res.Write([]byte(podEventsFixture))
5252
return
5353
}

http.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"net/url"
1011
"strconv"
1112
"strings"
1213
)
@@ -85,7 +86,19 @@ func (c *Client) Request(method string, path string, body []byte) (*http.Respons
8586

8687
// LimitedRequest allows limiting the number of responses in a request.
8788
func (c *Client) LimitedRequest(path string, results int) (string, int, error) {
88-
res, reqErr := c.Request("GET", path+"?limit="+strconv.Itoa(results), nil)
89+
var query string
90+
u, err := url.Parse(path)
91+
if err != nil {
92+
return "", -1, err
93+
}
94+
95+
if len(u.Query()) > 0 {
96+
query = "&limit=" + strconv.Itoa(results)
97+
} else {
98+
query = "?limit=" + strconv.Itoa(results)
99+
}
100+
101+
res, reqErr := c.Request("GET", path+query, nil)
89102

90103
if reqErr != nil && !IsErrAPIMismatch(reqErr) {
91104
return "", -1, reqErr

0 commit comments

Comments
 (0)