-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
51 lines (42 loc) · 1 KB
/
random.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"
)
// Random returns a random GIF
func (c *Client) Random(query RandomReq) (*RandomGIF, error) {
reqURL := c.baseURL.ResolveReference(randomPath)
return random(query, reqURL, c.apiKey, c.client)
}
// RandomReq contains options for listing a random GIF
type RandomReq struct {
Tag string
Rating Rating
}
func (s RandomReq) toValues() url.Values {
values := url.Values{}
values.Add("tag", string(s.Tag))
if s.Rating != "" {
values.Add("rating", string(s.Rating))
}
return values
}
func random(query RandomReq, reqURL *url.URL, key string, c *http.Client) (*RandomGIF, 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 := c.Do(req)
if err != nil {
return nil, err
}
data := new(RandomGIF)
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}