-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
133 lines (119 loc) · 3.08 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
package openapi
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const defaultSize = 5
var (
// 30 seconds timeout
httpClient = &http.Client{Timeout: time.Second * 30}
)
type Client struct {
Key string
Keeper SecretKeeper
}
func NewClient(keeper SecretKeeper, key string) *Client {
return &Client{Keeper: keeper, Key: key}
}
func DefaultClient(key, sec string) *Client {
return &Client{
Key: key,
Keeper: DefaultProvider{
AppKey: key,
AppSecret: sec,
},
}
}
func (c *Client) Get(uri string, headers ...http.Header) (string, error) {
return c.requestWithHeader(http.MethodGet, uri, "", headers...)
}
func (c *Client) Post(uri, body string, headers ...http.Header) (string, error) {
return c.requestWithHeader(http.MethodPost, uri, body, headers...)
}
func (c *Client) Delete(uri, body string, headers ...http.Header) (string, error) {
return c.requestWithHeader(http.MethodDelete, uri, body, headers...)
}
func (c *Client) Put(uri, body string, headers ...http.Header) (string, error) {
return c.requestWithHeader(http.MethodPut, uri, body, headers...)
}
func (c *Client) requestWithHeader(method, url, body string, headers ...http.Header) (string, error) {
// 请求构造
var header http.Header
if len(headers) > 0 {
header = headers[0]
if len(body) > 0 {
if header == nil {
header = make(map[string][]string, defaultSize)
}
header["content-type"] = []string{"application/json"}
}
}
pairs := make([]KvPair, 0, 10)
if signHeader {
// add all headers
headerPairs := getPairsFromMap(header)
pairs = append(pairs, headerPairs...)
}
// add all params
timeMillis := time.Now().UnixNano() / int64(time.Millisecond)
paramPairs := getPairsFromUrl(url)
pairs = append(pairs, paramPairs...)
pairs = append(pairs, KvPair{
Key: timeParam,
Value: fmt.Sprintf("%d", timeMillis),
})
pairs = append(pairs, KvPair{
Key: appKey,
Value: c.Key,
})
content := buildParams(pairs)
secret, err := c.Keeper.GetSecret(c.Key)
if err != nil {
return "", err
}
signResult := Sign(content, secret)
if len(getPairsFromUrl(url)) == 0 {
url += fmt.Sprintf("?sign=%s&time=%d&app_key=%s", signResult, timeMillis, c.Key)
} else {
url += fmt.Sprintf("&sign=%s&time=%d&app_key=%s", signResult, timeMillis, c.Key)
}
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
return "", err
}
req.Header = header
req.Close = true
// 发送请求
resp, err := httpClient.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body)
return string(data), errors.New(fmt.Sprintf("Error with not correct status code %s", resp.Status))
}
data, err := ioutil.ReadAll(resp.Body)
defer safeClose(resp)
return string(data), err
}
func safeClose(resp *http.Response) {
if resp != nil && !resp.Close {
err := resp.Body.Close()
if err != nil {
log.Printf("error close response body:%v", err)
}
}
}
func getPairsFromUrl(rawUrl string) Pairs {
u, err := url.Parse(rawUrl)
if err != nil {
return nil
}
return getPairsFromMap(u.Query())
}