forked from addcnos/youdu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
156 lines (126 loc) · 2.89 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
package youdu
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
)
var (
ErrUnexpectedResponseCode = errors.New("youdu sdk: unexpected response code")
)
type Request struct {
Buin int `json:"buin"`
AppId string `json:"appId"`
Encrypt string `json:"encrypt"`
}
type requestOptions struct {
params url.Values
body interface{}
needEncrypt bool
needAccessToken bool
}
func newRequestOptions(opts ...requestOption) *requestOptions {
args := &requestOptions{
body: nil,
params: url.Values{},
}
for _, opt := range opts {
opt(args)
}
return args
}
func (r *requestOptions) bodyReader(body interface{}) (io.Reader, error) {
if body == nil {
return nil, nil
}
if v, ok := body.(io.Reader); ok {
return v, nil
}
reqBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(reqBytes), nil
}
type requestOption func(*requestOptions)
func withRequestBody(body interface{}) requestOption {
return func(args *requestOptions) {
args.body = body
}
}
func withRequestEncrypt() requestOption {
return func(args *requestOptions) {
args.needEncrypt = true
}
}
func withRequestParams(params url.Values) requestOption { //nolint:unused
return func(args *requestOptions) {
args.params = params
}
}
func withRequestParamsKV(key, value string) requestOption {
return func(args *requestOptions) {
args.params.Add(key, value)
}
}
func withRequestAccessToken() requestOption {
return func(args *requestOptions) {
args.needAccessToken = true
}
}
func (c *Client) newRequest(ctx context.Context, method string, path string, opts ...requestOption) (req *http.Request, err error) {
var (
opt = newRequestOptions(opts...)
urlPath = c.config.Addr + path
)
// body
bodyReader, err := c.encodeRequestBody(opt)
if err != nil {
return nil, err
}
// access_token
if opt.needAccessToken {
if token, err := c.GetToken(ctx); err != nil {
return nil, err
} else {
opt.params.Add("accessToken", token)
}
}
req, err = http.NewRequestWithContext(ctx, method, urlPath+"?"+opt.params.Encode(), bodyReader)
return
}
func (c *Client) encodeRequestBody(opt *requestOptions) (io.Reader, error) {
if opt.body == nil {
return nil, nil
}
if !opt.needEncrypt {
return opt.bodyReader(opt.body)
}
reqBytes, err := json.Marshal(opt.body)
if err != nil {
return nil, err
}
cipherText, err := c.encryptor.Encrypt(reqBytes)
if err != nil {
return nil, err
}
return opt.bodyReader(Request{
Buin: c.config.Buin,
AppId: c.config.AppId,
Encrypt: cipherText,
})
}
func (c *Client) sendRequest(req *http.Request, resp interface{}, opts ...responseOption) error {
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return ErrUnexpectedResponseCode
}
return c.decodeResponse(res.Body, resp, opts...)
}