-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
223 lines (182 loc) · 4.15 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package top
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/51h5/go-sdk-top/internal/constants"
"github.com/51h5/go-sdk-top/internal/utils"
jsoniter "github.com/json-iterator/go"
)
type Client struct {
debug bool
checkRequest bool
keepAlive bool
ua string
appKey string
secretKey string
signType string
gateway string
httpClient *http.Client
}
func New(appKey, secretKey string, timeout time.Duration) (client *Client) {
client = &Client{
// debug: false,
keepAlive: true,
checkRequest: true,
appKey: appKey,
secretKey: secretKey,
gateway: kGatewayUrl,
signType: constants.SIGN_TYPE_MD5,
}
if timeout > 0 {
client.httpClient = &http.Client{
Timeout: timeout,
}
} else {
client.httpClient = &http.Client{
Timeout: kHttpClientTimeout,
}
}
return
}
// 调用接口
// - session: 用户的授权令牌
// - target_app_key: 被调用的目标AppKey,仅当被调用的API为第三方ISV提供时有效
func (c *Client) Execute(req Request, res Response, session string) (code uint, err error) {
if c.checkRequest {
code, err = req.Check()
if err != nil {
return
}
}
// 公共参数
sv := url.Values{
"app_key": {c.appKey},
"format": {kFormat},
"sign_method": {c.signType},
"v": {kApiVersion},
"timestamp": {time.Now().Format(kTimeFormat)},
"partner_id": {kPartnerId},
// "simplify": {"true"},
// "session": {session},
// "target_app_key": {req.TargetAppKey()},
"method": {req.Method()},
}
if session != "" {
sv.Set("session", session)
}
if req.TargetAppKey() != "" {
sv.Set("target_app_key", req.TargetAppKey())
}
if c.debug {
fmt.Printf("[top.Execute] 公共参数: %s\n", sv.Encode())
}
// 业务参数
av := req.Values()
if c.debug {
fmt.Printf("[top.Execute] 业务参数: %s\n", av.Encode())
}
// 计算签名
sv.Set(kKeySign, c.sign(sv, av, req))
if c.debug {
fmt.Printf("[top.Execute] 请求地址: %s?%s\n", c.gateway, sv.Encode())
}
var r *http.Request
if req.Body() == nil {
r, err = http.NewRequest(kMethodGet, c.gateway+"?"+sv.Encode(), nil)
} else {
r, err = http.NewRequest(kMethodPost, c.gateway+"?"+sv.Encode(), bytes.NewReader(req.Body()))
if err == nil {
r.Header.Set(kHeaderContentType, kContentTypeForm)
}
}
if err != nil {
code = 997
return
}
if !c.keepAlive {
r.Header.Set(kHeaderConnection, kConnectionClose)
}
if c.ua != "" {
r.Header.Set(kHeaderUserAgent, c.ua)
}
body, err := doRequest(c.httpClient, r)
if err != nil {
code = 998
return
}
if c.debug {
fmt.Printf("[top.Execute] 请求返回: %s\n", string(body))
}
err = parseJsonResponse(body, res)
if err != nil {
code = 999
return
}
// XXX: 淘宝TOP接口返回结构:混乱 & 垃圾
res.Fix()
return
}
func (c *Client) Debug(enable bool) {
c.debug = enable
}
func (c *Client) CheckRequest(enable bool) {
c.checkRequest = enable
}
func (c *Client) SetKeepAlive(v bool) {
c.keepAlive = v
}
func (c *Client) SetUserAgent(ua string) {
if ua != "" {
c.ua = ua
}
}
func (c *Client) SetGateway(v string) {
c.gateway = v
}
func (c *Client) SetSignType(v string) {
c.signType = v
}
func (c *Client) SetHttpClient(hc *http.Client) {
c.httpClient = hc
}
func (c *Client) sign(sysParams, apiParams url.Values, req Request) string {
if apiParams != nil {
for k := range apiParams {
if apiParams.Get(k) != "" {
sysParams.Set(k, apiParams.Get(k))
}
}
}
if c.debug {
fmt.Printf("[top.sign] 签名串: %v\n", sysParams)
}
s := utils.SignToRequest(sysParams, req.Body(), c.secretKey, c.signType)
if c.debug {
fmt.Printf("[top.sign] 签名: %s\n", s)
}
return s
}
func parseJsonResponse(body []byte, res Response) error {
return jsoniter.Unmarshal(body, res)
}
func doRequest(c *http.Client, r *http.Request) ([]byte, error) {
res, err := c.Do(r)
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response code %d", res.StatusCode)
}
bits, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return bits, nil
}