-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdk.go
148 lines (123 loc) · 2.99 KB
/
sdk.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package binance
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type Sdk struct {
client Client
clock Clock
}
type Clock interface {
Now() *int64
}
type clock struct {
}
func (t clock) Now() *int64 {
now := int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)
return &now
}
type Client interface {
Do(request *request) ([]byte, error)
}
type client struct {
baseUrl string
apiKey string
apiSecret string
}
type request struct {
method string
path string
parameters url.Values
signed bool
}
func newRequest(method string, path string) *request {
return &request{
method: method,
path: path,
parameters: url.Values{},
}
}
func (r *request) Param(key string, value string) *request {
r.parameters.Set(key, value)
return r
}
func (r *request) StringParam(key string, value *string) *request {
if value != nil {
r.parameters.Set(key, *value)
}
return r
}
func (r *request) Float64Param(key string, value *float64) *request {
if value != nil {
r.parameters.Set(key, strconv.FormatFloat(*value, 'f', 8, 64))
}
return r
}
func (r *request) Int64Param(key string, value *int64) *request {
if value != nil {
r.parameters.Set(key, strconv.FormatInt(*value, 10))
}
return r
}
func (r *request) Sign() *request {
r.signed = true
return r
}
func (c *client) Do(request *request) ([]byte, error) {
r, _ := c.createRequest(request)
c.addSignature(request, r)
r.Header.Set("X-MBX-APIKEY", c.apiKey)
client := http.Client{}
response, err := client.Do(r)
if err != nil {
return nil, err
}
responseBody, _ := ioutil.ReadAll(response.Body)
if response.StatusCode >= 300 {
return nil, errors.New("Error " + string(response.StatusCode) + ": " + string(responseBody))
}
return responseBody, nil
}
func (c *client) createRequest(request *request) (*http.Request, error) {
if request.method == "GET" {
return c.get(request)
}
return c.post(request)
}
func (c *client) sign(request *request) string {
signature := hmac.New(sha256.New, []byte(c.apiSecret))
signature.Write([]byte(request.parameters.Encode()))
return hex.EncodeToString(signature.Sum(nil))
}
func (c *client) addSignature(request *request, httpRequest *http.Request) {
if request.signed {
query := httpRequest.URL.Query()
httpRequest.URL.RawQuery = query.Encode() + "&signature=" + c.sign(request)
}
}
func (c *client) get(request *request) (*http.Request, error) {
parameters := request.parameters.Encode()
return http.NewRequest(request.method, c.baseUrl+request.path + "?" + parameters, nil)
}
func (c *client) post(request *request) (*http.Request, error) {
form := request.parameters.Encode()
return http.NewRequest(request.method, c.baseUrl+request.path, strings.NewReader(form))
}
func New(apiKey string, apiSecret string) Sdk {
return Sdk{
client: &client{
baseUrl: "https://api.binance.com",
apiKey: apiKey,
apiSecret: apiSecret,
},
clock: clock{},
}
}