-
Notifications
You must be signed in to change notification settings - Fork 13
/
request.go
131 lines (105 loc) · 2.85 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
package mixin
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
var (
xRequestID = http.CanonicalHeaderKey("x-request-id")
xIntegrityToken = http.CanonicalHeaderKey("x-integrity-token")
xForceAuthentication = http.CanonicalHeaderKey("x-force-authentication")
ErrResponseVerifyFailed = errors.New("response verify failed")
)
var httpClient = resty.New().
SetHeader("Content-Type", "application/json").
SetBaseURL(DefaultApiHost).
SetTimeout(10 * time.Second).
SetPreRequestHook(func(c *resty.Client, r *http.Request) error {
ctx := r.Context()
requestID := r.Header.Get(xRequestID)
if requestID == "" {
requestID = RequestIdFromContext(ctx)
r.Header.Set(xRequestID, requestID)
}
if s, ok := ctx.Value(signerKey).(Signer); ok {
token := s.SignToken(SignRequest(r), requestID, time.Minute)
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set(xForceAuthentication, "true")
}
return nil
}).
OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
if r.IsError() {
return nil
}
if err := checkResponseRequestID(r); err != nil {
return err
}
if v, ok := r.Request.Context().Value(verifierKey).(Verifier); ok {
if err := v.Verify(r); err != nil {
return ErrResponseVerifyFailed
}
}
return nil
})
func GetClient() *http.Client {
return httpClient.GetClient()
}
func GetRestyClient() *resty.Client {
return httpClient
}
func checkResponseRequestID(r *resty.Response) error {
expect := r.Request.Header.Get(xRequestID)
got := r.Header().Get(xRequestID)
if expect != got {
return fmt.Errorf("%s mismatch, expect %q but got %q", xRequestID, expect, got)
}
return nil
}
func Request(ctx context.Context) *resty.Request {
return httpClient.R().SetContext(ctx)
}
func DecodeResponse(resp *resty.Response) ([]byte, error) {
var body struct {
Error *Error `json:"error,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
if err := json.Unmarshal(resp.Body(), &body); err != nil {
if resp.IsError() {
return nil, createError(resp.StatusCode(), resp.StatusCode(), resp.Status())
}
return nil, createError(resp.StatusCode(), resp.StatusCode(), err.Error())
}
if body.Error != nil && body.Error.Code > 0 {
return nil, body.Error
}
return body.Data, nil
}
func UnmarshalResponse(resp *resty.Response, v interface{}) (err error) {
if requestID := extractRequestID(resp); requestID != "" {
defer bindRequestID(&err, requestID)
}
data, err := DecodeResponse(resp)
if err != nil {
return err
}
if v != nil {
return json.Unmarshal(data, v)
}
return nil
}
func extractRequestID(r *resty.Response) string {
if r != nil {
return r.Request.Header.Get(xRequestID)
}
return ""
}
func bindRequestID(errp *error, id string) {
if err := *errp; err != nil {
*errp = WrapErrWithRequestID(err, id)
}
}