-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrequest.go
120 lines (109 loc) · 2.97 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
package binance
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/dirname/binance/logging"
"io/ioutil"
http "net/http"
"time"
)
// Signer calculating signature
type Signer struct {
Key []byte
}
// Sum Generation signature
func (s *Signer) Sum(queryString string) string {
h := hmac.New(sha256.New, s.Key)
h.Write([]byte(queryString))
return hex.EncodeToString(h.Sum(nil))
}
// PublicUrlBuilder build public url
type PublicUrlBuilder struct {
host string
}
// PrivateUrlBuilder build private url
type PrivateUrlBuilder struct {
host string
appKey string
appSecret string
signer *Signer
}
// NewPublicUrlBuilder factory function
func NewPublicUrlBuilder(host string) *PublicUrlBuilder {
return &PublicUrlBuilder{host: host}
}
// NewPrivateUrlBuilder factory function
func NewPrivateUrlBuilder(host, appKey, appSecret string) *PrivateUrlBuilder {
return &PrivateUrlBuilder{
host: host,
appKey: appKey,
appSecret: appSecret,
signer: &Signer{Key: []byte(appSecret)},
}
}
// Build build a public url
func (p *PublicUrlBuilder) Build(method, path, params string) (*http.Request, error) {
switch params {
case "":
return http.NewRequest(method, fmt.Sprintf("https://%s%s", p.host, path), nil)
default:
return http.NewRequest(method, fmt.Sprintf("https://%s%s?%s", p.host, path, params), nil)
}
}
// Build build a private url
func (p *PrivateUrlBuilder) Build(method, path, params string, sign bool, timeStamp bool, recv time.Duration) (*http.Request, error) {
var err error
var req *http.Request
if timeStamp {
params += fmt.Sprintf("×tamp=%d", time.Now().UnixNano()/1e6)
}
if recv > 0 {
if recv > 60*time.Second {
recv = 60 * time.Second
}
params += fmt.Sprintf("&recvWindow=%d", recv.Milliseconds())
}
if sign {
params += "&signature=" + p.signer.Sum(params)
}
switch params {
case "":
req, err = http.NewRequest(method, fmt.Sprintf("https://%s%s", p.host, path), nil)
default:
req, err = http.NewRequest(method, fmt.Sprintf("https://%s%s?%s", p.host, path, params), nil)
}
req.Header.Set("X-MBX-APIKEY", p.appKey)
switch method {
case http.MethodGet:
break
case http.MethodPost:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case http.MethodPut:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case http.MethodDelete:
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
return req, err
}
// SetAPIKey set API information
func (p *PrivateUrlBuilder) SetAPIKey(appKey, appSecret string) {
p.appKey = appKey
p.appSecret = appSecret
}
// HttpRequest send a http request
func HttpRequest(request *http.Request) ([]byte, error) {
var err error
logger := logging.GetInstance()
logger.Start()
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
logger.StopAndLog(request.Method, request.URL.String())
return body, err
}