-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
198 lines (170 loc) · 4.94 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
package waterlink
import (
"bytes"
"encoding/json"
"fmt"
"github.com/lukasl-dev/waterlink/v2/internal/pkgerror"
"github.com/lukasl-dev/waterlink/v2/track"
"github.com/lukasl-dev/waterlink/v2/track/query"
"io"
"net/http"
"net/url"
)
// Client is used to perform HTTP requests to the server node.
type Client struct {
// url is the base url to perform requests to.
url url.URL
// creds
creds Credentials
}
// NewClient creates a new client that performs rest actions at the given base
// url authorized by the given credentials. For instance, a base url can be:
// "http://localhost:2333".
func NewClient(baseURL string, creds Credentials) (*Client, error) {
u, err := url.ParseRequestURI(baseURL)
if err != nil {
return nil, pkgerror.Wrap("client: invalid base url", err)
}
return &Client{url: *u, creds: creds}, nil
}
// request represents an dispatchable HTTP action.
type request struct {
// method is the HTTP method to use.
method string
// path is the relative path to the endpoint.
path string
// params is a map of query parameters to send with.
params map[string]string
// body is the request body to send as JSON.
body interface{}
// result is the pointer to the result object. After a successful request,
// the response body will be unmarshalled into this object.
result interface{}
// validate is the function to validate the response's status code.
validate func(code int) error
}
// LoadTracks resolves the given query to a track.LoadResult and preloads the
// resulting tracks.
func (c Client) LoadTracks(q query.Query) (res *track.LoadResult, err error) {
return res, c.dispatch("load tracks", request{
method: http.MethodGet,
path: "/loadtracks",
params: map[string]string{
"identifier": string(q),
},
result: &res,
validate: validateStatus(http.StatusOK),
})
}
// DecodeTrack decodes the given track ID into track.Info to gain more detailed
// information about a track.
func (c Client) DecodeTrack(trackID string) (res *track.Info, err error) {
return res, c.dispatch("decode track", request{
method: http.MethodGet,
path: "/decodetrack",
params: map[string]string{
"track": trackID,
},
result: &res,
validate: validateStatus(http.StatusOK),
})
}
// DecodeTracks decodes the given track IDs into an array of track.Info to gain
// more detailed information about the given preloaded tracks.
func (c Client) DecodeTracks(trackIDs []string) (res []track.Track, err error) {
return res, c.dispatch("decode tracks", request{
method: http.MethodPost,
path: "/decodetracks",
body: trackIDs,
result: &res,
validate: validateStatus(http.StatusOK),
})
}
// dispatch performs the request and wraps any errors into a package error.
func (c Client) dispatch(action string, req request) error {
return pkgerror.Wrap(fmt.Sprintf("client: %s", action), c.doRequest(req))
}
// doRequest performs the request and returns the error.
func (c Client) doRequest(req request) error {
r, err := c.createRequest(req)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(r)
if err != nil {
return err
}
//goland:noinspection GoUnhandledErrorResult
defer resp.Body.Close()
if err = c.validate(req, resp); err != nil {
return err
}
return c.unmarshalResponse(req, resp)
}
// createRequest creates a new HTTP request.
func (c Client) createRequest(req request) (*http.Request, error) {
body, err := c.createBody(req)
if err != nil {
return nil, err
}
u := c.createURL(req)
r, err := http.NewRequest(req.method, u.String(), body)
if err != nil {
return nil, err
}
r.Header = c.creds.header()
r.Header.Set("Content-Type", "application/json")
return r, nil
}
// createBody returns the request body to send to the server.
func (c Client) createBody(req request) (io.Reader, error) {
if req.body != nil {
b, err := json.Marshal(req.body)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
return nil, nil
}
// createURL creates a URL from the request's path and query parameters.
func (c Client) createURL(req request) url.URL {
u := c.url
u.Path = req.path
if req.params != nil {
q := u.Query()
for key, val := range req.params {
q.Set(key, val)
}
u.RawQuery = q.Encode()
}
return u
}
// validate validates the response's status code against the request's
// validation function.
func (c Client) validate(req request, resp *http.Response) error {
if req.validate == nil {
return nil
}
return req.validate(resp.StatusCode)
}
// unmarshalResponse unmarshals the response body into the request's result
// object.
func (c Client) unmarshalResponse(req request, resp *http.Response) error {
if resp.Body == nil {
return nil
}
return json.NewDecoder(resp.Body).Decode(req.result)
}
func validateStatus(allowed ...int) func(code int) error {
set := make(map[int]bool)
for _, code := range allowed {
set[code] = true
}
return func(code int) error {
if !set[code] {
return fmt.Errorf("unexpected status code %d", code)
}
return nil
}
}