/
transport.go
212 lines (186 loc) · 4.96 KB
/
transport.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
package bitmex
import (
"bytes"
"encoding/hex"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
log "github.com/sirupsen/logrus"
)
type Transport struct {
*httptransport.Runtime
Key string
Secret string
}
func NewTransport(host, basePath, key, secret string, schemes []string) (t *Transport) {
t = new(Transport)
t.Key = key
t.Secret = secret
t.Runtime = httptransport.New(host, basePath, schemes)
t.Runtime.Producers["application/x-www-form-urlencoded"] = runtime.TextProducer()
return
}
func (t *Transport) Submit(operation *runtime.ClientOperation) (interface{}, error) {
if operation.Method == "POST" || operation.Method == "DELETE" || operation.Method == "PUT" {
operation.ConsumesMediaTypes = []string{"application/x-www-form-urlencoded", "application/json"}
}
var fn runtime.ClientAuthInfoWriterFunc
fn = func(req runtime.ClientRequest, formats strfmt.Registry) error {
expires, sign := t.signature(req, operation, formats)
req.SetTimeout(30 * time.Second)
req.SetHeaderParam("api-key", t.Key)
req.SetHeaderParam("api-nonce", expires)
req.SetHeaderParam("api-signature", sign)
return nil
}
operation.AuthInfo = fn
return t.Runtime.Submit(operation)
}
func (t *Transport) signature(req runtime.ClientRequest, operation *runtime.ClientOperation, formats strfmt.Registry) (expires, sign string) {
method := strings.ToUpper(req.GetMethod())
path := t.BasePath + req.GetPath()
query := req.GetQueryParams()
if len(query) > 0 {
path += "?" + query.Encode()
}
nonce := time.Now().UnixNano()
expires = strconv.FormatInt(nonce, 10)[:13]
var data string
switch method {
case "GET":
data = ""
case "POST", "DELETE", "PUT":
rb := NewRequestBuffer()
err := operation.Params.WriteToRequest(rb, formats)
if err != nil {
log.Error("WriteToRequest error:", err.Error())
}
data = rb.GetFormParams().Encode()
default:
log.Info("unsupport method:", method)
}
content := method + path + expires + data
// fmt.Println("content:", content)
hmac := GetHMAC(HashSHA256, []byte(content), []byte(t.Secret))
sign = hex.EncodeToString(hmac)
return
}
type RequestBuffer struct {
pathPattern string
method string
pathParams map[string]string
header http.Header
query url.Values
formFields url.Values
fileFields map[string][]runtime.NamedReadCloser
payload interface{}
timeout time.Duration
buf *bytes.Buffer
}
func NewRequestBuffer() (r *RequestBuffer) {
r = new(RequestBuffer)
return
}
func (r *RequestBuffer) SetHeaderParam(name string, values ...string) error {
if r.header == nil {
r.header = make(http.Header)
}
r.header[http.CanonicalHeaderKey(name)] = values
return nil
}
func (r *RequestBuffer) SetQueryParam(name string, values ...string) error {
if r.query == nil {
r.query = make(url.Values)
}
r.query[name] = values
return nil
}
func (r *RequestBuffer) SetFormParam(name string, values ...string) error {
if r.formFields == nil {
r.formFields = make(url.Values)
}
r.formFields[name] = values
return nil
}
func (r *RequestBuffer) SetPathParam(name string, value string) error {
if r.pathParams == nil {
r.pathParams = make(map[string]string)
}
r.pathParams[name] = value
return nil
}
func (r *RequestBuffer) GetFormParams() url.Values {
var result = make(url.Values)
for key, value := range r.formFields {
result[key] = append([]string{}, value...)
}
return result
}
func (r *RequestBuffer) GetQueryParams() url.Values {
var result = make(url.Values)
for key, value := range r.query {
result[key] = append([]string{}, value...)
}
return result
}
func (r *RequestBuffer) SetFileParam(name string, files ...runtime.NamedReadCloser) error {
for _, file := range files {
if actualFile, ok := file.(*os.File); ok {
fi, err := os.Stat(actualFile.Name())
if err != nil {
return err
}
if fi.IsDir() {
return fmt.Errorf("%q is a directory, only files are supported", file.Name())
}
}
}
if r.fileFields == nil {
r.fileFields = make(map[string][]runtime.NamedReadCloser)
}
if r.formFields == nil {
r.formFields = make(url.Values)
}
r.fileFields[name] = files
return nil
}
func (r *RequestBuffer) SetBodyParam(payload interface{}) error {
r.payload = payload
return nil
}
func (r *RequestBuffer) SetTimeout(timeout time.Duration) error {
r.timeout = timeout
return nil
}
func (r *RequestBuffer) GetMethod() string {
return r.method
}
func (r *RequestBuffer) GetPath() string {
path := r.pathPattern
for k, v := range r.pathParams {
path = strings.Replace(path, "{"+k+"}", v, -1)
}
return path
}
func (r *RequestBuffer) GetBody() []byte {
if r.buf == nil {
return nil
}
return r.buf.Bytes()
}
func (r *RequestBuffer) GetBodyParam() interface{} {
return r.payload
}
func (r *RequestBuffer) GetFileParam() map[string][]runtime.NamedReadCloser {
return r.fileFields
}
func (r *RequestBuffer) GetHeaderParams() http.Header {
return r.header
}