forked from VictorFrWu/bybit.go.api
-
Notifications
You must be signed in to change notification settings - Fork 14
/
request.go
83 lines (71 loc) · 1.64 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
package bybit_connector
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
)
type secType int
const (
secTypeNone secType = iota
secTypeSigned // private request
)
type params map[string]interface{}
// request define an API request
type request struct {
method string
endpoint string
query url.Values
recvWindow string
secType secType
header http.Header
params []byte
fullURL string
body io.Reader
}
// 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 or body
func (r *request) setParams(m params) *request {
if r.method == http.MethodGet {
for k, v := range m {
r.setParam(k, v)
}
} else if r.method == http.MethodPost {
jsonData, err := json.Marshal(m)
if err != nil {
log.Fatal(err)
}
r.params = jsonData
}
return r
}
func (r *request) validate() (err error) {
if r.query == nil {
r.query = url.Values{}
}
return nil
}
// WithRecvWindow Append `WithRecvWindow(insert_recvWindow)` to request to modify the default recvWindow value
func WithRecvWindow(recvWindow string) RequestOption {
return func(r *request) {
r.recvWindow = recvWindow
}
}
// RequestOption define option type for request
type RequestOption func(*request)