-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtop_client.go
165 lines (131 loc) · 3.16 KB
/
top_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
//
//
// alidayu-go
//
// Created by 甘文鹏 on 2017/7/5.
// Copyright (c) 2017 甘文鹏. All rights reserved.
//
package alidayu
import (
"net/http"
"net/url"
"errors"
"encoding/json"
"sort"
"fmt"
"crypto/md5"
"time"
"io/ioutil"
"strings"
"github.com/gwpp/alidayu-go/request"
)
type TopClient struct {
AppKey string
TargetAppKey string
Session string
Timestamp string
Format string
V string
PartnerId string
Simplify bool
gatewayUrl string
secretKey string
}
func NewTopClient(appKey string, secretKey string) *TopClient {
top := new(TopClient)
top.AppKey = appKey
top.secretKey = secretKey
top.gatewayUrl = "http://gw.api.taobao.com/router/rest"
top.Format = "json"
top.V = "2.0"
return top
}
func (client *TopClient) Execute(req request.AlibabaRequest) (result map[string]interface{}, err error) {
params, err := client.buildParams(req)
if err != nil {
return
}
response, err := http.DefaultClient.PostForm(client.gatewayUrl, params)
if err != nil {
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
result = make(map[string]interface{})
err = json.Unmarshal(body, &result)
return
}
func (client *TopClient) buildParams(req request.AlibabaRequest) (params url.Values, err error) {
if err = req.ParamsIsValid(); err != nil {
return
}
if len(req.GetMethodName()) == 0 {
err = errors.New("method is required")
return
}
if len(client.AppKey) == 0 {
err = errors.New("app_key is required")
return
}
// common params
paramsCommon := make(map[string]string)
paramsCommon["method"] = req.GetMethodName()
paramsCommon["app_key"] = client.AppKey
paramsCommon["target_app_key"] = client.TargetAppKey
paramsCommon["sign_method"] = "md5"
paramsCommon["session"] = client.Session
paramsCommon["timestamp"] = client.timestamp()
paramsCommon["format"] = client.Format
paramsCommon["v"] = client.V
paramsCommon["partner_id"] = client.PartnerId
if client.Simplify {
paramsCommon["simplify"] = "1"
} else {
paramsCommon["simplify"] = "0"
}
data, err := json.Marshal(req)
if err != nil {
return
}
// request params
paramsApi := make(map[string]string)
if err = json.Unmarshal(data, ¶msApi); err != nil {
return
}
// sign
paramsCommon["sign"] = client.sign(paramsApi, paramsCommon)
params = make(url.Values)
for key, value := range paramsApi {
params.Set(key, value)
}
for key, value := range paramsCommon {
params.Set(key, value)
}
return
}
func (client *TopClient) timestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func (client *TopClient) sign(paramsApi map[string]string, paramsCommon map[string]string) string {
ks := make([]string, 0)
params := make(map[string]string)
for key, value := range paramsApi {
ks = append(ks, key)
params[key] = value
}
for key, value := range paramsCommon {
ks = append(ks, key)
params[key] = value
}
sort.Strings(ks)
str := ""
for _, k := range ks {
str = fmt.Sprintf("%v%v%v", str, k, params[k])
}
str = fmt.Sprintf("%v%v%v", client.secretKey, str, client.secretKey)
hash := md5.Sum([]byte(str))
sign := fmt.Sprintf("%x", hash)
return strings.ToUpper(sign)
}