-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
209 lines (185 loc) · 4.71 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
package top
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
simplejson "github.com/bitly/go-simplejson"
)
const (
// api endpoint of open taobao platform.
apiURL = "https://eco.taobao.com/router/rest"
// batch api endpoint of open taobao platform.
apiBatchURL = "https://eco.taobao.com/router/batch"
)
var (
defaultOptions = options{
apiURL: apiURL,
apiBatchURL: apiBatchURL,
apiTimeout: 10 * time.Second,
}
)
type options struct {
appKey string
appSecret string
apiURL string
apiBatchURL string
session string
apiTimeout time.Duration
}
// ClientOption for building up `Client`.
type ClientOption func(opts *options)
// WithApiURL returns a `Option` that sets the api URL.
func WithApiURL(url string) ClientOption {
return func(opts *options) {
opts.apiURL = url
}
}
// WithApiBatchURL returns a `Option` that sets the batch api URL.
func WithApiBatchURL(batchURL string) ClientOption {
return func(opts *options) {
opts.apiBatchURL = batchURL
}
}
// WithSession returns a `Option` that set the session key.
// https://open.taobao.com/doc.htm?docId=102635&docType=1
func WithSession(session string) ClientOption {
return func(opts *options) {
opts.session = session
}
}
// NewClient creates the `Client` according to the given options.
func NewClient(appKey, appSecret string, opts ...ClientOption) (*Client, error) {
if appKey == "" {
return nil, errors.New("app key cannot be empty")
} else if appSecret == "" {
return nil, errors.New("app secret cannot be empty")
}
opt := defaultOptions
opt.appKey = appKey
opt.appSecret = appSecret
for _, o := range opts {
o(&opt)
}
return &Client{
opt: opt,
httpClient: &http.Client{
Timeout: opt.apiTimeout,
},
}, nil
}
// Client defines the open taobap client.
type Client struct {
opt options
httpClient *http.Client
}
// Parameters defines the key value pair of parameters.
type Parameters map[string]interface{}
func (c *Client) do(ctx context.Context, param Parameters) (bytes []byte, err error) {
var req *http.Request
req, err = http.NewRequestWithContext(ctx, "POST", c.opt.apiURL, strings.NewReader(param.getRequestData()))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
var response *http.Response
response, err = c.httpClient.Do(req)
if err != nil {
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("%d:%s", response.StatusCode, response.Status)
return
}
bytes, err = ioutil.ReadAll(response.Body)
return
}
// DoJson executes the taobao api.
// https://open.taobao.com/doc.htm?docId=101617&docType=1
func (c *Client) DoJson(ctx context.Context, method string, param Parameters) (res *simplejson.Json, err error) {
param["method"] = method
param.setCommonParams(c.opt.appKey, c.opt.session)
param.sign(c.opt.appSecret, "" /* body */)
var bodyBytes []byte
bodyBytes, err = c.do(ctx, param)
if err != nil {
return
}
return bytesToResult(bodyBytes)
}
func bytesToResult(bytes []byte) (res *simplejson.Json, err error) {
res, err = simplejson.NewJson(bytes)
if err != nil {
return
}
if err = checkErrorResp(res); err != nil {
res = nil
}
return
}
func checkErrorResp(errResp *simplejson.Json) error {
if resp, ok := errResp.CheckGet("error_response"); ok {
bs, err := resp.Encode()
if err != nil {
return err
}
errResp := new(ErrorResponse)
if err := json.Unmarshal(bs, errResp); err != nil {
return err
}
return errResp
}
return nil
}
// setCommonParams sets the required parameters.
// https://open.taobao.com/doc.htm?docId=101617&docType=1
func (ps Parameters) setCommonParams(appKey, session string) {
ps["app_key"] = appKey
ps["timestamp"] = strconv.FormatInt(time.Now().In(cst).Unix(), 10)
ps["format"] = "json"
ps["v"] = "2.0"
ps["sign_method"] = "md5"
if session != "" {
ps["session"] = session
}
}
// sign gets the signature of params with optional body.
// https://open.taobao.com/doc.htm?docId=101617&docType=1
func (ps Parameters) sign(appSecret, body string) {
keys := make([]string, 0, len(ps))
for k := range ps {
keys = append(keys, k)
}
sort.Strings(keys)
query := bytes.NewBufferString(appSecret)
for _, k := range keys {
query.WriteString(k)
query.WriteString(interfaceToString(ps[k]))
}
if body != "" {
query.WriteString(body)
}
query.WriteString(appSecret)
h := md5.New()
io.Copy(h, query)
ps["sign"] = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
}
func (ps Parameters) getRequestData() string {
args := url.Values{}
for key, val := range ps {
args.Set(key, interfaceToString(val))
}
return args.Encode()
}