-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
71 lines (63 loc) · 1.65 KB
/
common.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
package tvdb
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"net/http"
"net/url"
)
type RequestArgs struct {
Body interface{}
Method string
Params url.Values
Path string
}
type Errors struct {
InvalidFilters []string `json:"invalidFilters"`
InvalidLanguage string `json:"invalidLanguage"`
InvalidQueryParams []string `json:"invalidQueryParams"`
}
type Links struct {
First int `json:"first"`
Last int `json:"last"`
Next int `json:"next"`
Previous int `json:"previous"`
}
const ApplicationJson = "application/json"
const BaseURL string = "https://api.thetvdb.com"
const DefaultLanguage = "en" // English
func buildUrlPath(path string) (result string) {
result = fmt.Sprintf("%s%s", BaseURL, path)
return
}
func parseResponse(body io.ReadCloser, data interface{}) error {
return json.NewDecoder(body).Decode(data)
}
func (c *Client) doRequest(args RequestArgs) (resp *http.Response, err error) {
var body io.Reader
if args.Body != nil {
marshal, _ := json.Marshal(args.Body)
body = bytes.NewBuffer(marshal)
}
req, err := http.NewRequest(args.Method, buildUrlPath(args.Path), body)
if err != nil {
return
}
req.Header.Set("Content-Type", ApplicationJson)
req.Header.Set("Accept", ApplicationJson)
req.Header.Set("Accept-Language", c.Language)
if c.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
}
if args.Params != nil {
req.URL.RawQuery = args.Params.Encode()
}
resp, err = c.Client.Do(req)
if err == nil && resp.StatusCode != http.StatusOK {
err = errors.Errorf("Received error status code (%d): %s", resp.StatusCode,resp.Status)
return
}
return
}