-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
166 lines (136 loc) · 2.83 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
package hitbtc
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const (
BaseURL = "https://api.hitbtc.com"
)
var (
spr = fmt.Sprintf
throttle = time.Tick(time.Second / 9)
)
type HitBtc struct {
api_key string
api_secret string
httpClient *http.Client
}
func NewClient(api_key, api_secret string) (client *HitBtc, err error) {
client = &HitBtc{
api_key: api_key,
api_secret: api_secret,
httpClient: &http.Client{Timeout: time.Second * 15},
}
return
}
func (h *HitBtc) publicRequest(action string, respch chan<- []byte, errch chan<- error) {
defer close(respch)
defer close(errch)
rawurl := BaseURL + action
req, err := http.NewRequest("GET", rawurl, nil)
if err != nil {
respch <- nil
errch <- Error(RequestError)
return
}
req.Header.Add("Accept", "application/json")
<-throttle
resp, err := h.httpClient.Do(req)
if err != nil {
respch <- nil
errch <- Error(ConnectError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
respch <- body
errch <- err
return
}
err = checkServerError(body)
if err != nil {
respch <- nil
errch <- err
}
respch <- body
errch <- nil
}
type serverError struct {
Error struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
func checkServerError(response []byte) error {
var check serverError
err := json.Unmarshal(response, &check)
if err != nil {
return nil
}
if check.Error.Message != "" {
return Error(ServerError, check.Error.Message)
} else {
return nil
}
}
func (h *HitBtc) tradeRequest(method, action string, parameters map[string]string, respch chan<- []byte, errch chan<- error) {
defer close(respch)
defer close(errch)
if len(h.api_key) == 0 || len(h.api_secret) == 0 {
respch <- nil
errch <- Error(SetApiError)
}
rawurl := spr("%s%s", BaseURL, action)
method = strings.ToUpper(method)
formValues := url.Values{}
for k, v := range parameters {
formValues.Set(k, v)
}
formData := formValues.Encode()
if method == "GET" && parameters != nil {
var URL *url.URL
URL, err := url.Parse(rawurl)
if err != nil {
respch <- nil
errch <- Error(UrlParseError)
}
URL.RawQuery = formData
rawurl = URL.String()
}
req, err := http.NewRequest(method, rawurl,
strings.NewReader(formData))
if err != nil {
respch <- nil
errch <- Error(RequestError)
return
}
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(h.api_key, h.api_secret)
<-throttle
resp, err := h.httpClient.Do(req)
if err != nil {
respch <- nil
errch <- Error(ConnectError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
respch <- body
errch <- err
return
}
err = checkServerError(body)
if err != nil {
respch <- nil
errch <- err
}
respch <- body
errch <- nil
}