-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
85 lines (73 loc) · 1.67 KB
/
request.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
package phemex
import (
"fmt"
"io"
"net/http"
"net/url"
)
type secType int
const (
secTypeNone secType = iota
secTypeAPIKey
secTypeSigned // if the 'timestamp' parameter is required
)
type params map[string]interface{}
// request define an API request
type request struct {
method string
endpoint string
query url.Values
form map[string]interface{}
recvWindow int64
secType secType
header http.Header
body io.Reader
fullURL string
}
// setParam set param with key/value to query string
func (r *request) setParam(key string, value interface{}) *request {
if r.query == nil {
r.query = url.Values{}
}
r.query.Set(key, fmt.Sprintf("%v", value))
return r
}
// setParams set params with key/values to query string
func (r *request) setParams(m params) *request {
for k, v := range m {
r.setParam(k, v)
}
return r
}
// setFormParam set param with key/value to request form body
func (r *request) setFormParam(key string, value interface{}) *request {
if r.form == nil {
r.form = map[string]interface{}{}
}
r.form[key] = value
return r
}
// setFormParams set params with key/values to request form body
func (r *request) setFormParams(m params) *request {
for k, v := range m {
r.setFormParam(k, v)
}
return r
}
func (r *request) validate() (err error) {
if r.query == nil {
r.query = url.Values{}
}
if r.form == nil {
r.form = map[string]interface{}{}
}
return nil
}
// RequestOption define option type for request
type RequestOption func(*request)
// WithRecvWindow set recvWindow param for the request
func WithRecvWindow(recvWindow int64) RequestOption {
return func(r *request) {
r.recvWindow = recvWindow
}
}