-
Notifications
You must be signed in to change notification settings - Fork 0
/
byid.go
68 lines (56 loc) · 1.32 KB
/
byid.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package gifgo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
// GIFByID returns a single GIF by it's giphy ID
func (c *Client) GIFByID(id string) (*SingleGIF, error) {
path, err := url.Parse(id)
if err != nil {
return nil, err
}
reqURL := c.baseURL.ResolveReference(path)
q := reqURL.Query()
q.Add("api_key", c.apiKey)
reqURL.RawQuery = q.Encode()
fmt.Println(reqURL.String())
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
data := new(SingleGIF)
if err := json.NewDecoder(resp.Body).Decode(data); err != nil {
return nil, err
}
return data, nil
}
// GIFsByID returns 1 or more GIFs based on the IDs passed in
func (c *Client) GIFsByID(ids ...string) (*MultipleGIF, error) {
reqURL := c.baseURL.ResolveReference(idsPath)
q := reqURL.Query()
q.Add("api_key", c.apiKey)
idstr := strings.Join(ids, ",")
q.Add("ids", idstr)
reqURL.RawQuery = q.Encode()
fmt.Println(reqURL.String())
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.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
}