-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
127 lines (109 loc) · 2.6 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
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
package okex
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 url.Values
bodyJson map[string]string // new field
recvWindow int64
secType secType
header http.Header
body io.Reader
fullURL string
}
// addParam add param with key/value to query string
func (r *request) addParam(key string, value interface{}) *request {
if r.query == nil {
r.query = url.Values{}
}
r.query.Add(key, fmt.Sprintf("%v", value))
return r
}
// 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 = url.Values{}
}
r.form.Set(key, fmt.Sprintf("%v", 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
}
// setFormParam set param with key/value to request form body
func (r *request) setBodyParam(key string, value string) *request {
if r.bodyJson == nil {
r.bodyJson = make(map[string]string)
}
r.bodyJson[key] = value
return r
}
func (r *request) validate() (err error) {
if r.query == nil {
r.query = url.Values{}
}
if r.form == nil {
r.form = url.Values{}
}
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
}
}
// WithHeader set or add a header value to the request
func WithHeader(key, value string, replace bool) RequestOption {
return func(r *request) {
if r.header == nil {
r.header = http.Header{}
}
if replace {
r.header.Set(key, value)
} else {
r.header.Add(key, value)
}
}
}
// WithHeaders set or replace the headers of the request
func WithHeaders(header http.Header) RequestOption {
return func(r *request) {
r.header = header.Clone()
}
}