-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrending.go
51 lines (44 loc) · 1.07 KB
/
trending.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gifgo
import (
"encoding/json"
"net/http"
"net/url"
)
// Trending returns the current trending GIFs
func (c *Client) Trending(query TrendingReq) (*MultipleGIF, error) {
reqURL := c.baseURL.ResolveReference(trendingPath)
return trending(query, reqURL, c.apiKey, c.client)
}
// TrendingReq contains info for a trending GIF search
type TrendingReq struct {
Limit int
Rating Rating
}
func (s TrendingReq) toValues() url.Values {
values := url.Values{}
if s.Limit != 0 {
values.Add("limit", string(s.Limit))
}
if s.Rating != "" {
values.Add("rating", string(s.Rating))
}
return values
}
func trending(query TrendingReq, reqURL *url.URL, key string, client *http.Client) (*MultipleGIF, error) {
q := query.toValues()
q.Add("api_key", key)
reqURL.RawQuery = q.Encode()
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
data := new(MultipleGIF)
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}