This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from toorop/go-bittrex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
195 lines (163 loc) · 4.61 KB
/
client.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package bittrex
import (
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"strings"
"time"
)
//Client struct
type Client struct {
apiKey string
apiSecret string
httpClient *http.Client
httpTimeout time.Duration
debug bool
}
// NewClient return a new Bittrex HTTP client
func NewClient(apiKey, apiSecret string) (c *Client) {
return &Client{apiKey, apiSecret, &http.Client{}, 1 * time.Second, false}
}
// NewClientWithCustomHTTPConfig returns a new Bittrex HTTP client using the predefined http client
func NewClientWithCustomHTTPConfig(apiKey, apiSecret string, httpClient *http.Client) (c *Client) {
timeout := httpClient.Timeout
if timeout <= 0 {
timeout = 30 * time.Second
}
return &Client{apiKey, apiSecret, httpClient, timeout, false}
}
// NewClientWithCustomTimeout returns a new Bittrex HTTP client with custom timeout
func NewClientWithCustomTimeout(apiKey, apiSecret string, timeout time.Duration) (c *Client) {
return &Client{apiKey, apiSecret, &http.Client{}, timeout, false}
}
func (c Client) dumpRequest(r *http.Request) {
if r == nil {
log.Print("dumpReq ok: <nil>")
return
}
dump, err := httputil.DumpRequest(r, true)
if err != nil {
log.Print("dumpReq err:", err)
} else {
log.Print("dumpReq ok:", string(dump))
}
}
func (c Client) dumpResponse(r *http.Response) {
if r == nil {
log.Print("dumpResponse ok: <nil>")
return
}
dump, err := httputil.DumpResponse(r, true)
if err != nil {
fmt.Print("dumpResponse err:", err)
} else {
fmt.Print("dumpResponse ok:", string(dump))
}
}
// doTimeoutRequest do a HTTP request with timeout
func (c *Client) doTimeoutRequest(timer *time.Timer, req *http.Request) (*http.Response, error) {
// Do the request in the background so we can check the timeout
type result struct {
resp *http.Response
err error
}
done := make(chan result, 1)
go func() {
if c.debug {
c.dumpRequest(req)
}
resp, err := c.httpClient.Do(req)
if c.debug {
c.dumpResponse(resp)
}
done <- result{resp, err}
}()
// Wait for the read or the timeout
select {
case r := <-done:
return r.resp, r.err
case <-timer.C:
return nil, errors.New("timeout on reading data from Bittrex API")
}
}
// do prepare and process HTTP request to Bittrex API
func (c *Client) do(method string, resource string, payload string, authNeeded bool) (response []byte, err error) {
connectTimer := time.NewTimer(c.httpTimeout)
var rawurl string
if strings.HasPrefix(resource, "http") {
rawurl = resource
} else {
rawurl = fmt.Sprintf("%s%s/%s", APIBASE, APIVERSION, resource)
}
req, err := http.NewRequest(method, rawurl, strings.NewReader(payload))
if err != nil {
return
}
if method == "POST" || method == "PUT" {
req.Header.Add("Content-Type", "application/json;charset=utf-8")
}
req.Header.Add("Accept", "application/json")
// Auth
if authNeeded {
if len(c.apiKey) == 0 || len(c.apiSecret) == 0 {
err = errors.New("You need to set API Key and API Secret to call this method")
return
}
apiTimestamp := fmt.Sprintf("%d", time.Now().UnixNano()/1000000)
sha512Bytes := sha512.Sum512([]byte(payload))
apiContentHash := hex.EncodeToString(sha512Bytes[:])
req.Header.Add("Api-Key", c.apiKey)
req.Header.Add("Api-Timestamp", apiTimestamp)
req.Header.Add("Api-Content-Hash", apiContentHash)
preSign := strings.Join([]string{apiTimestamp, rawurl, method, apiContentHash}, "")
mac := hmac.New(sha512.New, []byte(c.apiSecret))
_, err = mac.Write([]byte(preSign))
sig := hex.EncodeToString(mac.Sum(nil))
req.Header.Add("Api-Signature", sig)
}
resp, err := c.doTimeoutRequest(connectTimer, req)
if err != nil {
return
}
defer resp.Body.Close()
response, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 && method == "POST" {
err = errors.New(resp.Status)
}
if resp.StatusCode != 200 && (method == "GET" || method == "DELETE") {
err = errors.New(resp.Status)
}
return response, err
}
// do2 prepare and process HTTP request to Bittrex API
func (c *Client) do2(resource string) (*http.Response, error) {
connectTimer := time.NewTimer(c.httpTimeout)
var rawurl string
if strings.HasPrefix(resource, "http") {
rawurl = resource
} else {
rawurl = fmt.Sprintf("%s%s/%s", APIBASE, APIVERSION, resource)
}
req, err := http.NewRequest("GET", rawurl, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := c.doTimeoutRequest(connectTimer, req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
return resp, err
}