Revamp listing decoding, include after/before anchors in response

Now, instead of returning an object containing a list of results + the
anchors, we return just the list. The anchors are available in the
response object. Much cleaner this way in my opinion

go-github and godo do it this way too. They include some meta
information in the returned response objects

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-08-29 14:20:30 -04:00
parent 37e712b334
commit 2a1806ec33
15 changed files with 520 additions and 579 deletions
+15
View File
@@ -269,6 +269,12 @@ func (c *Client) NewRequestWithForm(method string, path string, form url.Values)
// Response is a Reddit response. This wraps the standard http.Response returned from Reddit.
type Response struct {
*http.Response
// Pagination anchor indicating there are more results after this id.
After string
// Pagination anchor indicating there are more results before this id.
// todo: not sure yet if responses ever contain this
Before string
}
// newResponse creates a new Response for the provided http.Response.
@@ -277,6 +283,11 @@ func newResponse(r *http.Response) *Response {
return &response
}
func (r *Response) populateAnchors(a anchor) {
r.After = a.After()
r.Before = a.Before()
}
// Do sends an API request and returns the API response. The API response is JSON decoded and stored in the value
// pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface,
// the raw response will be written to v, without attempting to decode it.
@@ -310,6 +321,10 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Res
return nil, err
}
}
if anchor, ok := v.(anchor); ok {
response.populateAnchors(anchor)
}
}
return response, nil