-
Notifications
You must be signed in to change notification settings - Fork 0
/
topclient.go
199 lines (178 loc) · 5.07 KB
/
topclient.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
package topsdk
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/lianmengworld/topsdk/util"
)
type TopClient struct {
AppKey string
AppSecret string
ServerUrl string
Format string
SignMethod string
ConnectTimeout int64
ReadTimeout int64
Version string
Simplify bool
httpClient *http.Client
}
type HttpTransportConfig struct {
DialTimeout int64
KeepAlive int64
MaxIdleConns int
MaxIdleConnsPerHost int
IdleConnTimeout int64
MaxConnsPerHost int
}
func NewDefaultTopClient(AppKey string,AppSecret string,ServerUrl string, connectTimeount int64, readTimeout int64) TopClient {
var httpTransportConfig = &HttpTransportConfig{
DialTimeout: 30000,
KeepAlive: 30000,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 50,
IdleConnTimeout: 30000,
}
return NewTopClientWithConfig(AppKey,AppSecret,ServerUrl,connectTimeount,readTimeout,httpTransportConfig)
}
func NewTopClientWithConfig(AppKey string,AppSecret string,ServerUrl string, connectTimeount int64, readTimeout int64,httpTransportConfig *HttpTransportConfig) TopClient {
httpClient := http.Client{
Timeout: time.Duration(connectTimeount) * time.Millisecond,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Duration(httpTransportConfig.DialTimeout) * time.Millisecond,
KeepAlive: time.Duration(httpTransportConfig.KeepAlive) * time.Millisecond,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: httpTransportConfig.MaxIdleConns,
MaxIdleConnsPerHost: httpTransportConfig.MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(httpTransportConfig.IdleConnTimeout) * time.Millisecond,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
return TopClient{
AppKey: AppKey,
AppSecret: AppSecret,
ServerUrl: ServerUrl,
Format: ApiFormat,
SignMethod: SignMethod,
ConnectTimeout: connectTimeount,
ReadTimeout: readTimeout,
Version: TopVersion,
Simplify: true,
httpClient: &httpClient,
}
}
func (client *TopClient) ExecuteWithSession(method string,data map[string]interface{},fileData map[string]interface{},session string) (string,error){
var publicParam = make(map[string]interface{})
publicParam["method"] = method
publicParam["app_key"] = client.AppKey
publicParam["timestamp"] = time.Now().Format(DateFormat)
publicParam["v"] = client.Version
publicParam["sign_method"] = client.SignMethod
publicParam["format"] = client.Format
publicParam["simplify"] = client.Simplify
publicParam["partner_id"] = SdkVersion
if session != "" {
publicParam["session"] = session
}
sign := util.GetSign(publicParam, data, client.AppSecret)
// 构建url
serverUrl, _ := url.Parse(client.ServerUrl)
urlValues := url.Values{}
urlValues.Add("sign",sign)
for k,v := range publicParam{
urlValues.Add(k,fmt.Sprint(v))
}
serverUrl.RawQuery = urlValues.Encode()
urlPath := serverUrl.String()
// 构建body
if fileData != nil && len(fileData) > 0 {
return doPostWithFile(urlPath,data,fileData,client.httpClient)
}else {
return doPost(urlPath, data, client.httpClient)
}
}
func doPost(urlPath string,data map[string]interface{},httpClient *http.Client) (string,error){
bodyParam := url.Values{}
for k,v := range data{
bodyParam.Add(k,fmt.Sprint(v))
}
resp, err := httpClient.Post(urlPath,"application/x-www-form-urlencoded",strings.NewReader(bodyParam.Encode()))
if resp != nil {
defer resp.Body.Close()
}
if(err != nil){
log.Println("http.PostForm error",err)
return "",err
}
body, err := ioutil.ReadAll(resp.Body)
if(err != nil){
log.Println("ioutil.ReadAll",err)
return "",err
}
return string(body),nil
}
func doPostWithFile(urlPath string,data map[string]interface{},fileData map[string]interface{}, httpClient *http.Client) (string,error){
bodyBuf := &bytes.Buffer{}
writer := multipart.NewWriter(bodyBuf)
for k,v := range data{
err := writer.WriteField(k, fmt.Sprint(v))
if err != nil {
return "", err
}
}
for k,v := range fileData {
value , ok := v.([]byte)
if ok {
fileWriter, err := writer.CreateFormFile(k, "file")
if err != nil {
return "",err
}
_, err = io.Copy(fileWriter, bytes.NewReader(value))
if err != nil {
return "",err
}
}else {
value , ok := v.(*util.FileItem)
if ok {
fileWriter, err := writer.CreateFormFile(k, value.FileName)
if err != nil {
return "",err
}
_, err = io.Copy(fileWriter, bytes.NewReader(value.Content))
if err != nil {
return "",err
}
}
}
}
err := writer.Close()
if(err != nil){
return "",err
}
resp, err := httpClient.Post(urlPath,writer.FormDataContentType(),bodyBuf)
if err != nil {
log.Println("http.PostForm error",err)
return "",err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ioutil.ReadAll",err)
return "",err
}
return string(body),nil
}
func (client *TopClient) Execute(method string,data map[string]interface{},fileData map[string]interface{}) (string,error){
return client.ExecuteWithSession(method,data,fileData,"")
}