forked from xntltd/golang-http-api
/
request.go
356 lines (309 loc) · 8.16 KB
/
request.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package httplib
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
const (
// JWTDefaultTTL default lieftime of jwt token
JWTDefaultTTL int = 30
// APIv1 api version
APIv1 string = "1.0"
// APIv2 api version
APIv2 string = "2.0"
// APIv3 api version
APIv3 string = "3.0"
// BasicAuth type
BasicAuth string = "BasicAuth"
// JWTAuth type
JWTAuth string = "JWTAuth"
// MDAPICategory md api url prefix
MDAPICategory string = "md"
// TRADEAPICategory traders api url prefix
TRADEAPICategory string = "trade"
// CurrentAPIVersion default api version
CurrentAPIVersion string = APIv2
emptyString string = ""
commaSeparator string = ","
slashSeparator string = "/"
jwtAuthTokenPrefix string = "Bearer"
basicAuthPreffix string = "Basic"
acceptJSON string = "application/json"
acceptStream string = "application/x-json-stream"
acceptEncoding string = "gzip"
contentEncodingHeader string = "Content-Encoding"
authHeader string = "Authorization"
)
// APIVersions list of available api verisons
var APIVersions = [3]string{APIv1, APIv2, APIv3}
// Scopes is JWT Auth scopes
var Scopes = []string{
"crossrates", "change", "crossrates", "summary",
"symbols", "feed", "ohlc", "orders", "transactions",
"accounts",
}
// Represents last remember auth token
var currentJwtAuthToken string
func setReqHeader(r *http.Request, h, v string) { r.Header.Set(h, v) }
// Standard jwt-go claims does not support multiple audience
type claimsWithMultiAudSupport struct {
Aud []string `json:"aud"`
jwt.StandardClaims
}
type requestData struct {
category, action, version, auth, pathParams string
queryStringParams map[string]string
}
// getAuth getter for requestData.auth field
// return JWT is default auth type
func (h HTTPApi) getAuth(u requestData) IAuth {
switch u.auth {
case JWTAuth:
return h.Auth.JWT
case BasicAuth:
return h.Auth.Basic
default:
return h.Auth.JWT
}
}
// getAPIVersion getter for requestData.version field
// return APIv2 is default API version
func (r requestData) getAPIVersion() string {
switch r.version {
case APIv1:
case APIv2:
case APIv3:
default:
return APIv2
}
return r.version
}
// getCategory getter for requestData.category field
// return md is default api category
func (r requestData) getCategory() string {
switch r.category {
case MDAPICategory:
case TRADEAPICategory:
case emptyString:
return MDAPICategory
default:
panic(errUndefinedCategoryAPIMessage)
}
return r.category
}
// libTransport custom transport layer
type libTransport struct {
underlyingTransport http.RoundTripper
}
// RoundTrip with custom headers setup
func (t *libTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Accept", acceptJSON)
req.Header.Add("Accept-Encoding", acceptEncoding)
if req.Method == http.MethodPost {
req.Header.Add("Content-type", acceptJSON)
}
if strings.Contains(req.URL.Path, "stream") {
req.Header.Add("Accept", acceptStream)
}
return t.underlyingTransport.RoundTrip(req)
}
// JWTAuthMethod ...
type JWTAuthMethod struct {
ApplicationID, ClientID, SharedKey string
JwtTTL int
}
// BasicAuthMethod ...
type BasicAuthMethod struct{ Username, Password string }
// Auth ...
type Auth struct {
JWT JWTAuthMethod
Basic BasicAuthMethod
}
// HTTPApi struct provide communication with xnt services
type HTTPApi struct {
Auth Auth
httpClient *http.Client
baseAPIURL, version string
}
// IAuth ...
type IAuth interface{ authenticate(r *http.Request) }
func (b BasicAuthMethod) authenticate(r *http.Request) {
r.SetBasicAuth(b.Username, b.Password)
}
func (j JWTAuthMethod) authenticate(r *http.Request) {
isValidToken := j.validateJwtToken()
if !isValidToken {
currentJwtAuthToken = j.getJWTToken()
}
authValue := fmt.Sprintf(
"%s %s", jwtAuthTokenPrefix, currentJwtAuthToken)
setReqHeader(r, authHeader, authValue)
}
func (j JWTAuthMethod) validateJwtToken() bool {
if currentJwtAuthToken == emptyString {
return false
}
token, _ := jwt.ParseWithClaims(
currentJwtAuthToken, &claimsWithMultiAudSupport{},
func(token *jwt.Token) (interface{}, error) {
return []byte(j.SharedKey), nil
})
return token.Valid
}
func (j JWTAuthMethod) getJWTToken() string {
now := time.Now()
ttl := j.getTTL()
jwtExpiresAt := now.Add(ttl).Unix()
jwtIssueAt := now.Unix()
claims := claimsWithMultiAudSupport{
Scopes,
jwt.StandardClaims{
Issuer: j.ClientID,
Subject: j.ApplicationID,
IssuedAt: jwtIssueAt,
ExpiresAt: jwtExpiresAt,
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenString, _ := token.SignedString([]byte(j.SharedKey))
return tokenString
}
func (j JWTAuthMethod) getTTL() time.Duration {
v := JWTDefaultTTL
if j.JwtTTL > 0 {
v = j.JwtTTL
}
ttl := time.Second * time.Duration(v)
return ttl
}
func (h HTTPApi) getVersion() string {
for _, apiVersion := range APIVersions {
if apiVersion == h.version {
return apiVersion
}
}
panic(errUndefinedAPIVersionMessage)
}
func (h HTTPApi) request(
method, url string, a IAuth, p *bytes.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, url, p)
if err != nil {
return nil, err
}
a.authenticate(req)
resp, err := h.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (h HTTPApi) buildURL(u requestData) string {
varsion := u.getAPIVersion()
apiURL := fmt.Sprintf(
"%s/%s/%s/%s", h.baseAPIURL, u.getCategory(), varsion, u.action)
if len(u.pathParams) > 0 {
apiURL = fmt.Sprintf("%s/%s/", apiURL, u.pathParams)
}
if len(u.queryStringParams) > 0 {
qParams := url.Values{}
for k := range u.queryStringParams {
qParams.Add(k, u.queryStringParams[k])
}
apiURL = apiURL + "?" + qParams.Encode()
}
return apiURL
}
func (h HTTPApi) get(m interface{}, rm interface{}, u requestData) error {
err := h.fetch(http.MethodGet, m, rm, u, emptyPostPayload)
return err
}
func (h HTTPApi) post(m interface{}, rm interface{}, u requestData) error {
objByte, err := h.preparePostPayload(m)
if err != nil {
return err
}
err = h.fetch(http.MethodPost, m, rm, u, objByte)
return err
}
func (h HTTPApi) preparePostPayload(m interface{}) (*bytes.Reader, error) {
o, err := typeToJSON(m)
if err != nil {
return nil, err
}
objByte := bytes.NewReader(o)
return objByte, err
}
func (h HTTPApi) stream(u requestData, stopChan chan bool, outChan chan []byte) {
resp, err := h.request(
http.MethodGet, h.buildURL(u), h.getAuth(u), emptyPostPayload)
if err != nil {
panic(err)
}
var buf bytes.Buffer
g, err := gzip.NewReader(resp.Body)
io.TeeReader(g, &buf)
for {
select {
default:
r, err := ioutil.ReadAll(&buf)
if err != nil {
panic(err)
}
outChan <- r
case <-stopChan:
return
}
}
}
func (h HTTPApi) runStream(u requestData) (chan []byte, chan bool) {
stopChan := make(chan bool)
outChan := make(chan []byte)
go h.stream(u, stopChan, outChan)
return outChan, stopChan
}
func (h HTTPApi) fetch(
httpMethod string, m interface{}, rm interface{}, u requestData, payload *bytes.Reader) error {
resp, err := h.request(httpMethod, h.buildURL(u), h.getAuth(u), payload)
if err != nil {
return err
}
preparedData, err := h.processResponse(resp)
if err != nil {
return err
}
err = h.serialize(preparedData, m, rm)
return err
}
func (h HTTPApi) processResponse(resp *http.Response) ([]byte, error) {
var responseBody []byte
switch resp.Header.Get(contentEncodingHeader) {
case acceptEncoding:
g, _ := gzip.NewReader(resp.Body)
responseBody, _ = ioutil.ReadAll(g)
defer g.Close()
default:
responseBody, _ = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
}
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
return responseBody, nil
}
return nil, fmt.Errorf(
"Response status code - %d \n %s", resp.StatusCode, responseBody)
}
func (h HTTPApi) serialize(data []byte, model interface{}, responseModel interface{}) (err error) {
if responseModel != nil {
err = json.Unmarshal(data, &responseModel)
return
}
err = json.Unmarshal(data, &model)
return
}