/
tmdb.go
206 lines (171 loc) · 4.72 KB
/
tmdb.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2019 Cyro Dubeux. License MIT.
// Package tmdb is a wrapper for working with TMDb API.
package tmdb
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
// TMDb constants
const (
baseURL = "https://api.themoviedb.org/3"
permissionURL = "https://www.themoviedb.org/authenticate/"
authenticationURL = "/authentication/"
movieURL = "/movie/"
tvURL = "/tv/"
tvSeasonURL = "/season/"
tvEpisodeURL = "/episode/"
personURL = "/person/"
searchURL = "/search/"
collectionURL = "/collection/"
companyURL = "/company/"
configurationURL = "/configuration/"
creditURL = "/credit/"
discoverURL = "/discover/"
networkURL = "/network/"
keywordURL = "/keyword/"
genreURL = "/genre/"
guestSessionURL = "/guest_session/"
listURL = "/list/"
)
// Auto retry default duration.
const defaultRetryDuration = time.Second * 5
// Client type is a struct to instantiate this pkg.
type Client struct {
// TMDb apiKey to use the client.
apiKey string
// Auto retry flag to indicates if the client
// should retry the previous operation.
autoRetry bool
// http.Client for custom configuration.
http http.Client
}
// Error type represents an error returned by the TMDB API.
type Error struct {
StatusMessage string `json:"status_message,omitempty"`
Success bool `json:"success,omitempty"`
StatusCode int `json:"status_code,omitempty"`
}
// Init setups the Client with an apiKey.
func Init(apiKey string) (*Client, error) {
if apiKey == "" {
return nil, errors.New("APIKey is empty")
}
return &Client{apiKey: apiKey}, nil
}
// SetClientConfig sets a custom configuration for the http.Client.
func (c *Client) SetClientConfig(httpClient http.Client) {
c.http = httpClient
}
// SetClientAutoRetry sets autoRetry flag to true.
func (c *Client) SetClientAutoRetry() {
c.autoRetry = true
}
// retryDuration calculates the retry duration time.
func retryDuration(resp *http.Response) time.Duration {
retryTime := resp.Header.Get("Retry-After")
if retryTime == "" {
return defaultRetryDuration
}
seconds, err := strconv.ParseInt(retryTime, 10, 32)
if err != nil {
return defaultRetryDuration
}
return time.Duration(seconds) * time.Second
}
func (c *Client) get(url string, data interface{}) error {
if url == "" {
return errors.New("url field is empty")
}
// Setting default timeout to 10 seconds, if none is provided.
if c.http.Timeout == 0 {
c.http.Timeout = time.Second * 10
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("could not fetch the url: %s", err)
}
// Setting context.
req = req.WithContext(context.Background())
req.Header.Add("content-type", "application/json;charset=utf-8")
for {
res, err := c.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode == http.StatusTooManyRequests && c.autoRetry {
time.Sleep(retryDuration(res))
continue
}
if res.StatusCode == http.StatusNoContent {
return nil
}
if res.StatusCode != http.StatusOK {
return c.decodeError(res)
}
if err = json.NewDecoder(res.Body).Decode(data); err != nil {
return fmt.Errorf("could not decode the data: %s", err)
}
break
}
return nil
}
// TODO: Improve post function.
func (c *Client) post(url string, params []byte, data interface{}) error {
if url == "" {
return errors.New("url field is empty")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(params))
if err != nil {
return errors.New(err.Error())
}
req.Header.Add("content-type", "application/json;charset=utf-8")
res, err := c.http.Do(req)
if err != nil {
return errors.New(err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return c.decodeError(res)
}
if err = json.NewDecoder(res.Body).Decode(data); err != nil {
return fmt.Errorf("could not decode the data: %s", err)
}
return nil
}
func (c *Client) fmtOptions(o map[string]string) string {
options := ""
if len(o) > 0 {
for k, v := range o {
options += fmt.Sprintf("&%s=%s", k, url.QueryEscape(v))
}
}
return options
}
func (e Error) Error() string {
return e.StatusMessage
}
func (c *Client) decodeError(r *http.Response) error {
resBody, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("could not read body response: %s", err)
}
if len(resBody) == 0 {
return fmt.Errorf("[%d]: empty body %s", r.StatusCode, http.StatusText(r.StatusCode))
}
buf := bytes.NewBuffer(resBody)
var e Error
err = json.NewDecoder(buf).Decode(&e)
if err != nil {
return fmt.Errorf("couldn't decode error: (%d) [%s]", len(resBody), resBody)
}
return e
}