/
client.go
202 lines (178 loc) · 4.84 KB
/
client.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
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"golang.org/x/oauth2"
)
// Error contains the status and message that can be received from the Spotify API if the request fails.
type Error struct {
Status int `json:"status"`
Message string `json:"message"`
}
// Spotify represents the Spotify API client, which provides all the functionality needed to communicate with the API.
type Spotify struct {
client *http.Client
// The base url of the Spotify
url string
}
// spotifyRequestData is used to unify the parameters of the request functions into a single struct.
type spotifyRequestData struct {
// The object used to store json.Unmarshal results.
response interface{}
// The method of the request.
// The methods used are GET, PUT, POST, DELETE.
method string
// The endpoint of the request.
endpoint string
// The params of the request.
params []Param
// The headers of the request.
// The headers used are "Content-Type:application/json" and "Content-Type:image/jpeg".
headers map[string]string
// The body of the request.
body io.Reader
}
// Get is responsible for sending GET requests with the specified endpoint and parameters.
func (s *Spotify) Get(response interface{}, endpoint string, params ...Param) error {
requestData := spotifyRequestData{
response,
http.MethodGet,
endpoint,
params,
map[string]string{},
bytes.NewBuffer([]byte{}),
}
return s.doRequest(requestData)
}
// Put is responsible for sending PUT requests with the specified endpoint, body and parameters.
func (s *Spotify) Put(
response interface{},
endpoint string,
body []byte,
params ...Param,
) error {
requestData := spotifyRequestData{
response,
http.MethodPut,
endpoint,
params,
map[string]string{"Content-Type": "application/json"},
bytes.NewBuffer(body),
}
return s.doRequest(requestData)
}
// PutImage is responsible for sending PUT requests with the specified endpoint, body containing base64 encoded image and parameters.
func (s *Spotify) PutImage(
response interface{},
endpoint, body string,
params ...Param,
) error {
requestData := spotifyRequestData{
response,
http.MethodPut,
endpoint,
params,
map[string]string{"Content-Type": "image/jpeg"},
strings.NewReader(body),
}
return s.doRequest(requestData)
}
// Post is responsible for sending POST requests with the specified endpoint, body and parameters.
func (s *Spotify) Post(
response interface{},
endpoint string,
body []byte,
params ...Param,
) error {
requestData := spotifyRequestData{
response,
http.MethodPost,
endpoint,
params,
map[string]string{"Content-Type": "application/json"},
bytes.NewBuffer(body),
}
return s.doRequest(requestData)
}
// Delete is responsible for sending DELETE requests with the specified endpoint, body and parameters.
func (s *Spotify) Delete(
response interface{},
endpoint string,
body []byte,
params ...Param,
) error {
requestData := spotifyRequestData{
response,
http.MethodDelete,
endpoint,
params,
map[string]string{},
bytes.NewBuffer(body),
}
return s.doRequest(requestData)
}
// doRequest responsible for connecting the create and send methods.
func (s *Spotify) doRequest(data spotifyRequestData) error {
req, err := s.createRequest(data)
if err != nil {
return err
}
return s.sendRequest(data.response, req)
}
// createRequest responsible for creating the request with given spotifyRequestData.
func (s *Spotify) createRequest(data spotifyRequestData) (*http.Request, error) {
endpoint, err := buildUrl(data.endpoint, data.params...)
if err != nil {
return nil, err
}
req, err := http.NewRequest(data.method, s.url+endpoint, data.body)
if err != nil {
return nil, err
}
for k, v := range data.headers {
req.Header.Set(k, v)
}
return req, err
}
// sendRequest sends a request to the Spotify API using the Spotify client and the created request.
// Response data, if any, will be marshalled to the response object.
func (s *Spotify) sendRequest(response interface{}, req *http.Request) error {
res, err := s.client.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return s.handleError(body)
}
if response == nil {
return nil
}
return json.Unmarshal(body, response)
}
// handleError parses the error returned by the Spotify API and formats it into the Go error.
func (s *Spotify) handleError(body []byte) error {
var w struct {
Error Error `json:"error"`
}
err := json.Unmarshal(body, &w)
if err != nil {
return err
}
return fmt.Errorf("spotify request error: %v", w.Error)
}
// NewSpotifyClient creates a Spotify client, with the appropriate Spotify base URL.
func NewSpotifyClient(ctx context.Context, token *oauth2.Token) Spotify {
return Spotify{
oauth2.NewClient(ctx, oauth2.StaticTokenSource(token)),
"https://api.spotify.com/v1",
}
}