Skip to content

Commit

Permalink
Revert "Revert "Extend releases signature to permit query parameters""
Browse files Browse the repository at this point in the history
This reverts commit c70a642.
  • Loading branch information
Ka Hin Ng committed Nov 13, 2019
1 parent c70a642 commit cf09861
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
45 changes: 45 additions & 0 deletions pivnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type AccessTokenOrLegacyToken struct {
userAgent string
}

type QueryParameter struct {
Key string
Value string
}

func (o AccessTokenOrLegacyToken) AccessToken() (string, error) {
const legacyAPITokenLength = 20
if len(o.refreshToken) > legacyAPITokenLength {
Expand Down Expand Up @@ -245,6 +250,46 @@ func (c Client) MakeRequest(
return resp, nil
}

func (c Client) MakeRequestWithParams(
requestType string,
endpoint string,
expectedStatusCode int,
params []QueryParameter,
body io.Reader,
) (*http.Response, error) {
req, err := c.CreateRequest(requestType, endpoint, body)
if err != nil {
return nil, err
}

q := req.URL.Query()
for _, param := range params {
q.Add(param.Key, param.Value)
}
req.URL.RawQuery = q.Encode()

reqBytes, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}

c.logger.Debug("Making request", logger.Data{"request": string(reqBytes)})

resp, err := c.HTTP.Do(req)
if err != nil {
return nil, err
}

c.logger.Debug("Response status code", logger.Data{"status code": resp.StatusCode})
c.logger.Debug("Response headers", logger.Data{"headers": resp.Header})

if expectedStatusCode > 0 && resp.StatusCode != expectedStatusCode {
return nil, c.handleUnexpectedResponse(resp)
}

return resp, nil
}

func (c Client) stripHostPrefix(downloadLink string) string {
if strings.HasPrefix(downloadLink, apiVersion) {
return downloadLink
Expand Down
4 changes: 2 additions & 2 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ type CreateReleaseConfig struct {
CopyMetadata bool
}

func (r ReleasesService) List(productSlug string) ([]Release, error) {
func (r ReleasesService) List(productSlug string, params ...QueryParameter) ([]Release, error) {
url := fmt.Sprintf("/products/%s/releases", productSlug)

var response ReleasesResponse
resp, err := r.client.MakeRequest("GET", url, http.StatusOK, nil)
resp, err := r.client.MakeRequestWithParams("GET", url, http.StatusOK, params, nil)
if err != nil {
return nil, err
}
Expand Down
17 changes: 17 additions & 0 deletions releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ var _ = Describe("PivnetClient - product files", func() {
Expect(releases[1].ID).To(Equal(3))
})

Context("when specifying a limit", func() {
It("passes the limit to the API endpoint in the form of query params", func() {
response := `{"releases": [{"id": 3, "version": "3.2.1", "_links": {"product_files": {"href":"https://banana.org/cookies/download"}}}]}`
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", apiPrefix+"/products/banana/releases", "limit=1"),
ghttp.RespondWith(http.StatusOK, response),
),
)

releases, err := client.Releases.List("banana", pivnet.QueryParameter{"limit", "1"})
Expect(err).NotTo(HaveOccurred())
Expect(releases).To(HaveLen(1))
Expect(releases[0].ID).To(Equal(3))
})
})

Context("when the server responds with a non-2XX status code", func() {
var (
body []byte
Expand Down

0 comments on commit cf09861

Please sign in to comment.