-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
166 lines (150 loc) · 3.91 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
package core
import (
"encoding/base64"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
"github.com/bububa/openpdd/core/internal/debug"
"github.com/bububa/openpdd/model"
"github.com/bububa/openpdd/util"
"github.com/bububa/openpdd/util/query"
)
// SDKClient sdk client
type SDKClient struct {
clientID string
secret string
dataType model.RequestDataType
gw string
debug bool
}
// NewSDKClient 创建SDKClient
func NewSDKClient(clientID string, secret string) *SDKClient {
return &SDKClient{
clientID: clientID,
secret: secret,
gw: GATEWAY,
dataType: model.RequestDataType_JSON,
}
}
// SetDebug 设置debug模式
func (c *SDKClient) SetDebug(debug bool) {
c.debug = debug
}
// SetGateway 设置gateway
func (c *SDKClient) SetGateway(gw string) {
c.gw = gw
}
// SetDataType 设置返回数据格式
func (c *SDKClient) SetDataType(t model.RequestDataType) {
c.dataType = t
}
// ClientID returns client client_id
func (c *SDKClient) ClientID() string {
return c.clientID
}
func (c *SDKClient) Do(req model.Request, resp model.Response, accessToken string) error {
values, err := query.Values(req)
if err != nil {
return err
}
values.Set("type", req.GetType())
values.Set("client_id", c.clientID)
values.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
values.Set("data_type", string(c.dataType))
if accessToken != "" {
values.Set("access_token", accessToken)
}
values.Set("sign", c.sign(values))
return c.get(values, resp)
}
func (c *SDKClient) post(req url.Values, resp model.Response) error {
var builder strings.Builder
builder.WriteString(c.gw)
if req != nil {
builder.WriteString("?")
builder.WriteString(req.Encode())
}
reqUrl := builder.String()
debug.PrintGetRequest(reqUrl, c.debug)
httpReq, err := http.NewRequest("POST", c.gw, strings.NewReader(req.Encode()))
if err != nil {
return err
}
httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
return c.fetch(httpReq, resp)
}
func (c *SDKClient) get(req url.Values, resp model.Response) error {
var builder strings.Builder
builder.WriteString(c.gw)
if req != nil {
builder.WriteString("?")
builder.WriteString(req.Encode())
}
reqUrl := builder.String()
debug.PrintGetRequest(reqUrl, c.debug)
httpReq, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
return err
}
return c.fetch(httpReq, resp)
}
// fetch execute http request
func (c *SDKClient) fetch(httpReq *http.Request, resp model.Response) error {
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return err
}
defer httpResp.Body.Close()
if resp == nil {
resp = &model.CommonResponse{}
}
err = debug.DecodeJSONHttpResponse(httpResp.Body, resp, c.debug)
if err != nil {
debug.PrintError(err, c.debug)
return err
}
if resp.IsError() {
return resp.Error()
}
return nil
}
func (c *SDKClient) sign(values url.Values) string {
params := make([]string, 0, len(values))
for k := range values {
var builder strings.Builder
builder.WriteString(k)
builder.WriteString(values.Get(k))
params = append(params, builder.String())
}
sort.Strings(params)
var builder strings.Builder
builder.WriteString(c.secret)
for _, v := range params {
builder.WriteString(v)
}
builder.WriteString(c.secret)
rawSign := builder.String()
return strings.ToUpper(util.Md5String(rawSign))
}
func (c *SDKClient) WSSUrl() string {
ts := strconv.FormatInt(time.Now().UnixMilli(), 10)
var builder strings.Builder
builder.WriteString(WSSEndPoint)
builder.WriteString("/")
builder.WriteString(c.clientID)
builder.WriteString("/")
builder.WriteString(ts)
builder.WriteString("/")
builder.WriteString(c.signWSS(ts))
return builder.String()
}
func (c *SDKClient) signWSS(ts string) string {
var builder strings.Builder
builder.WriteString(c.clientID)
builder.WriteString(ts)
builder.WriteString(c.secret)
return base64.StdEncoding.EncodeToString([]byte(util.Md5String(builder.String())))
}