summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/peterhellberg/giphy/gif.go
blob: 8c317c97a2126a29beb3a89aca6d16561df05543 (plain) (blame)
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
package giphy

import (
	"encoding/json"
	"fmt"
	"strings"
)

// GIF returns a ID response from the Giphy API
func (c *Client) GIF(id string) (GIF, error) {
	if strings.ContainsAny(id, "/&?") {
		return GIF{}, fmt.Errorf("Invalid giphy id: `%v`", id)
	}

	req, err := c.NewRequest("/gifs/" + id)
	if err != nil {
		return GIF{}, err
	}

	var gif GIF
	if _, err = c.Do(req, &gif); err != nil {
		return GIF{}, err
	}

	if gif.RawData == nil || gif.RawData[0] == '[' {
		return GIF{}, ErrNoImageFound
	}

	// Check if the first character in Data is a {
	if gif.RawData[0] == '{' {
		var d Data

		err = json.Unmarshal(gif.RawData, &d)
		if err != nil {
			return GIF{}, err
		}

		gif.Data = d

		return gif, nil
	}

	return GIF{}, ErrUnknown
}