-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
179 lines (152 loc) · 3.69 KB
/
util.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
package bitso
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
const (
BASEURL = "https://api.bitso.com/v3/"
)
type Error struct {
Message string `json:"message"`
Code int `json:"code,string"`
}
func (api *API) ask(service string, params map[string]string) ([]byte, error) {
hc := http.Client{}
// only GET for public queries
req, err := http.NewRequest(http.MethodGet, BASEURL+service, nil)
if err != nil {
return nil, err
}
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if api.Devel {
fmt.Println("RAW DATA:", string(data))
}
return data, nil
}
func (api *API) askPrivateGet(service string, query string) ([]byte, error) {
hc := http.Client{}
req, err := http.NewRequest(http.MethodGet, BASEURL+service+query, nil)
if err != nil {
return nil, err
}
auth := api.signature(http.MethodGet, "/v3/"+service+query, "")
if api.Devel {
fmt.Println("Authorization:", auth)
}
req.Header.Add("Authorization", auth)
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if api.Devel {
fmt.Println("RAW DATA:", string(data))
}
return data, nil
}
func (api *API) askPrivatePost(service string, query string, body string) ([]byte, error) {
hc := http.Client{}
req, err := http.NewRequest(http.MethodPost, BASEURL+service+query, bytes.NewBufferString(body))
if err != nil {
return nil, err
}
auth := api.signature(http.MethodPost, "/v3/"+service+query, body)
if api.Devel {
fmt.Println("Auth: ", auth)
}
req.Header.Add("Authorization", auth)
req.Header.Set("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if api.Devel {
fmt.Println("RAW DATA:", string(data))
}
return data, nil
}
func (api *API) askPrivateDelete(service string, query string, body string) ([]byte, error) {
hc := http.Client{}
req, err := http.NewRequest(http.MethodDelete, BASEURL+service+query, bytes.NewBufferString(body))
if err != nil {
return nil, err
}
auth := api.signature(http.MethodDelete, "/v3/"+service+query, "")
if api.Devel {
fmt.Println("Authorization: ", auth)
}
req.Header.Add("Authorization", auth)
req.Header.Set("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if api.Devel {
fmt.Println("RAW DATA:", string(data))
}
return data, nil
}
func (api *API) signature(method string, url string, body string) string {
nonce := strconv.FormatInt(time.Now().UnixNano()/1000000, 10)
signstring := nonce + method + url + body
signer := hmac.New(sha256.New, []byte(api.Secret))
signer.Write([]byte(signstring))
signature := hex.EncodeToString(signer.Sum(nil))
return fmt.Sprintf("Bitso %s:%s:%s", api.Key, nonce, signature)
}
func (api *API) haveKeys() error {
if api.Key == "" || api.Secret == "" {
return errors.New("There is no keys to access to private APIs")
}
return nil
}
func (api *API) haveTransfer() error {
err := api.haveKeys()
if err != nil {
return err
}
if !api.HaveTransfer {
return errors.New("There is no transfer flag set on the API")
}
return nil
}
func (api *API) haveRemittance() error {
err := api.haveKeys()
if err != nil {
return err
}
if !api.HaveRemittance {
return errors.New("There is no remittance flag set on the API")
}
return nil
}