-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
87 lines (71 loc) · 1.97 KB
/
api.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
package spotify
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
)
const APIHost = "api.spotify.com"
// Error represents an ErrorObject in the Spotify API
// https://developer.spotify.com/documentation/web-api/reference/#object-errorobject
type Error struct {
Status int `json:"status"`
Message string `json:"message"`
}
type API struct {
token string
}
func NewAPI(token string) *API {
return &API{token}
}
func (a *API) get(apiVersion, endpoint string, query url.Values, res interface{}) error {
return a.call(http.MethodGet, apiVersion, endpoint, query, nil, res)
}
func (a *API) post(apiVersion, endpoint string, query url.Values, body io.Reader) error {
return a.call(http.MethodPost, apiVersion, endpoint, query, body, nil)
}
func (a *API) put(apiVersion, endpoint string, query url.Values, body io.Reader) error {
return a.call(http.MethodPut, apiVersion, endpoint, query, body, nil)
}
func (a *API) delete(apiVersion, endpoint string, query url.Values) error {
return a.call(http.MethodDelete, apiVersion, endpoint, query, nil, nil)
}
func (a *API) call(method, apiVersion, endpoint string, query url.Values, body io.Reader, result interface{}) error {
url := url.URL{
Host: APIHost,
Path: path.Join(apiVersion, endpoint),
RawQuery: query.Encode(),
Scheme: "https",
}
req, err := http.NewRequest(method, url.String(), body)
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.token))
client := http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
// Success
if res.StatusCode >= 200 && res.StatusCode < 300 {
if result != nil {
if err := json.NewDecoder(res.Body).Decode(result); err != nil {
return err
}
}
return nil
}
// Error
spotifyErr := &struct {
Error Error `json:"error"`
}{}
if err := json.NewDecoder(res.Body).Decode(spotifyErr); err != nil {
return err
}
return errors.New(spotifyErr.Error.Message)
}