2023-11-03 16:29:17 +02:00
|
|
|
package bybit_connector
|
2023-10-30 20:26:54 +02:00
|
|
|
|
|
|
|
import (
|
2023-11-05 18:33:56 +02:00
|
|
|
"encoding/json"
|
2023-10-30 20:26:54 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-11-05 18:33:56 +02:00
|
|
|
"log"
|
2023-10-30 20:26:54 +02:00
|
|
|
"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
|
2023-11-05 18:33:56 +02:00
|
|
|
params []byte
|
2023-10-30 20:26:54 +02:00
|
|
|
fullURL string
|
2023-11-05 18:33:56 +02:00
|
|
|
body io.Reader
|
2023-10-30 20:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-11-05 18:33:56 +02:00
|
|
|
// setParams set params with key/values to query string or body
|
2023-10-30 20:26:54 +02:00
|
|
|
func (r *request) setParams(m params) *request {
|
2023-11-05 18:33:56 +02:00
|
|
|
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
|
2023-10-30 20:26:54 +02:00
|
|
|
}
|
2023-11-05 18:33:56 +02:00
|
|
|
|
2023-10-30 20:26:54 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *request) validate() (err error) {
|
|
|
|
if r.query == nil {
|
|
|
|
r.query = url.Values{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 18:33:56 +02:00
|
|
|
// WithRecvWindow Append `WithRecvWindow(insert_recvWindow)` to request to modify the default recvWindow value
|
2023-10-30 20:26:54 +02:00
|
|
|
func WithRecvWindow(recvWindow string) RequestOption {
|
|
|
|
return func(r *request) {
|
|
|
|
r.recvWindow = recvWindow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestOption define option type for request
|
|
|
|
type RequestOption func(*request)
|