-
Notifications
You must be signed in to change notification settings - Fork 1
/
kv.go
221 lines (175 loc) · 5.31 KB
/
kv.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
210
211
212
213
214
215
216
217
package zxl_go_sdk
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"github.com/zhixinlian/zxl-go-sdk/sm/sm3"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
const (
KvSaveUrl = "/api/v1/spider/sdk/kv/save"
KvQueryUrl = "/api/v1/spider/sdk/kv/query"
)
type KvSaveReq struct {
/**
* 字符串 key
*/
KvKey string `json:"kvKey"`
/**
* 加密字符串
*/
KvValue string `json:"kvValue"`
/**
* 代理用户Id
*/
RepresentAppId string `json:"representAppId"`
/**
* 私钥, 如果是代理用户,则传代理用户的私钥
*/
Sk string `json:"sk"`
AppId string `json:"appId"`
KvKeyHash string `json:"kvKeyHash"`
KvKeyValueHash string `json:"kvKeyValueHash"`
Signature string `json:"signature"`
}
type KvSaveResp struct {
KvKey string `json:"kvKey"`
RequestId string `json:"requestId"`
}
type KvQueryReq struct {
KvKey string `json:"kvKey"`
KvKeyHash string `json:"kvKeyHash"`
}
type KvQueryResp struct {
KvKey string `json:"kvKey"`
KvValue string `json:"kvValue"`
CreateTime string `json:"createTime"`
Status int `json:"status"`
RequestId string `json:"requestId"`
}
func (zxl *ZxlImpl) KvSave(req KvSaveReq, timeout time.Duration) (*KvSaveResp, error) {
resp := &KvSaveResp{}
url := defConf.ServerAddr + KvSaveUrl
if req.KvKey == ""{
return resp, errors.New("KvKey 不能为空")
}
if req.KvValue == "" {
return resp, errors.New("KvValue 不能为空")
}
if req.Sk == "" {
return resp, errors.New("私钥不能为空")
}
req.KvKeyHash = getHash(req.KvKey)
req.KvKeyValueHash = getHash(req.KvKey+":"+req.KvValue)
appId := zxl.appId
if req.RepresentAppId != "" {
appId = req.RepresentAppId
}
req.AppId = zxl.appId
sign, err := zxl.Sign(req.Sk, []byte(strings.Join([]string{appId, req.KvKey, req.KvValue}, ",")))
if err != nil {
return resp, errors.New("get sign failed"+err.Error())
}
req.Signature = sign
params, err := json.Marshal(req)
if err != nil {
return resp, errors.New("get req json failed")
}
data, cri, err := sendKvRequest(zxl.appId, zxl.appKey, "POST", url, params, timeout)
if err != nil {
return resp, errors.New("kv saved failed"+err.Error())
}
resp.RequestId = cri.RequestId
var commonData commonResult
err = json.Unmarshal(data, &commonData)
if err != nil {
return nil, errors.New("returned data format error:" + err.Error())
}
if commonData.RetCode != 0 {
return nil, errors.New("http response error info : " + strconv.Itoa(commonData.RetCode) + ","+commonData.RetMsg)
}
jsonData, _ := json.Marshal(commonData.Data)
err = json.Unmarshal(jsonData, resp)
if err != nil {
return resp, errors.New("format data error "+ err.Error())
}
return resp, nil
}
func (zxl *ZxlImpl) KvQuery(req KvQueryReq, timeout time.Duration) (*KvQueryResp, error) {
resp := &KvQueryResp{}
url := defConf.ServerAddr + KvQueryUrl
req.KvKeyHash = getHash(req.KvKey)
params, err := json.Marshal(req)
if err != nil {
return resp, errors.New("get req json failed")
}
data, cri, err := sendKvRequest(zxl.appId, zxl.appKey, "POST", url, params, timeout)
if err != nil {
return resp, errors.New("kv query failed"+ err.Error())
}
resp.RequestId = cri.RequestId
var commonData commonResult
err = json.Unmarshal(data, &commonData)
if err != nil {
return nil, errors.New("returned data format error:" + err.Error())
}
if commonData.RetCode != 0 {
return nil, errors.New("http response error info : " + strconv.Itoa(commonData.RetCode) + ","+commonData.RetMsg)
}
jsonData, _ := json.Marshal(commonData.Data)
err = json.Unmarshal(jsonData, resp)
if err != nil {
return resp, errors.New("format data error "+ err.Error())
}
return resp, nil
}
func getHash(str string) string{
return hex.EncodeToString(sm3.SumSM3([]byte(str)))
}
func sendKvRequest(appId, appKey, method, url string, body []byte, timeout time.Duration) ([]byte, *commReqInfo, error) {
var byteReader io.Reader = nil
if body != nil {
byteReader = bytes.NewReader(body)
}
cri := commReqInfo{}
cli := buildHttpClient(defConf.IsProxy, timeout)
req, err := http.NewRequest(method, url, byteReader)
if err != nil {
return nil, &cri, errors.New("NewRequest error:" + err.Error())
}
req.Header.Add("appId", appId)
// 不直接传递 appKey,和时间戳结合使用
signatureTime := strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)
signature := hex.EncodeToString(sm3.SumSM3([]byte(appId+","+appKey+","+signatureTime)))
req.Header.Add("signatureTime", signatureTime)
req.Header.Add("signature", signature)
req.Header.Add("content-type", "application/json")
resp, err := cli.Do(req)
if resp != nil {
cri.RequestId = resp.Header.Get(REQUEST_ID)
}
if err != nil {
return nil, &cri, errors.New("cli.Do error:" + err.Error()+ ", requestId:"+ cri.RequestId)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode == 400 || resp.StatusCode == 500 || resp.StatusCode == 401 {
data, _ := ioutil.ReadAll(resp.Body)
var commonData CommonRet
_ = json.Unmarshal(data, &commonData)
return nil, &cri, errors.New("http response error info : " + commonData.Message)
}
return nil, &cri, errors.New("cli.Do error bad status : " + resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, &cri, errors.New("returned data format error:" + string(data))
}
return data, &cri, nil
}