forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
186 lines (156 loc) · 3.69 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
package coinbasepro
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net/http"
"os"
"strconv"
"time"
)
type Client struct {
BaseURL string
Secret string
Key string
Passphrase string
HTTPClient *http.Client
RetryCount int
}
type ClientConfig struct {
BaseURL string
Key string
Passphrase string
Secret string
}
func NewClient() *Client {
baseURL := os.Getenv("COINBASE_PRO_BASEURL")
if baseURL == "" {
baseURL = "https://api.pro.coinbase.com"
}
client := Client{
BaseURL: baseURL,
Key: os.Getenv("COINBASE_PRO_KEY"),
Passphrase: os.Getenv("COINBASE_PRO_PASSPHRASE"),
Secret: os.Getenv("COINBASE_PRO_SECRET"),
HTTPClient: &http.Client{
Timeout: 15 * time.Second,
},
RetryCount: 0,
}
if os.Getenv("COINBASE_PRO_SANDBOX") == "1" {
client.UpdateConfig(&ClientConfig{
BaseURL: "https://api-public.sandbox.pro.coinbase.com",
})
}
return &client
}
func (c *Client) UpdateConfig(config *ClientConfig) {
baseURL := config.BaseURL
key := config.Key
passphrase := config.Passphrase
secret := config.Secret
if baseURL != "" {
c.BaseURL = baseURL
}
if key != "" {
c.Key = key
}
if passphrase != "" {
c.Passphrase = passphrase
}
if secret != "" {
c.Secret = secret
}
}
func (c *Client) Request(method string, url string,
params, result interface{}) (res *http.Response, err error) {
for i := 0; i < c.RetryCount+1; i++ {
retryDuration := time.Duration((math.Pow(2, float64(i))-1)/2*1000) * time.Millisecond
time.Sleep(retryDuration)
res, err = c.request(method, url, params, result)
if res != nil && res.StatusCode == 429 {
continue
} else {
break
}
}
return res, err
}
func (c *Client) request(method string, url string,
params, result interface{}) (res *http.Response, err error) {
var data []byte
body := bytes.NewReader(make([]byte, 0))
if params != nil {
data, err = json.Marshal(params)
if err != nil {
return res, err
}
body = bytes.NewReader(data)
}
fullURL := fmt.Sprintf("%s%s", c.BaseURL, url)
req, err := http.NewRequest(method, fullURL, body)
if err != nil {
return res, err
}
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
// XXX: Sandbox time is off right now
if os.Getenv("TEST_COINBASE_OFFSET") != "" {
inc, err := strconv.Atoi(os.Getenv("TEST_COINBASE_OFFSET"))
if err != nil {
return res, err
}
timestamp = strconv.FormatInt(time.Now().Unix()+int64(inc), 10)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "Go Coinbase Pro Client 1.0")
h, err := c.Headers(method, url, timestamp, string(data))
if err != nil {
return res, err
}
for k, v := range h {
req.Header.Add(k, v)
}
res, err = c.HTTPClient.Do(req)
if err != nil {
return res, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
defer res.Body.Close()
coinbaseError := Error{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&coinbaseError); err != nil {
return res, err
}
return res, error(coinbaseError)
}
if result != nil {
decoder := json.NewDecoder(res.Body)
if err = decoder.Decode(result); err != nil {
return res, err
}
}
return res, nil
}
// Headers generates a map that can be used as headers to authenticate a request
func (c *Client) Headers(method, url, timestamp, data string) (map[string]string, error) {
h := make(map[string]string)
h["CB-ACCESS-KEY"] = c.Key
h["CB-ACCESS-PASSPHRASE"] = c.Passphrase
h["CB-ACCESS-TIMESTAMP"] = timestamp
message := fmt.Sprintf(
"%s%s%s%s",
timestamp,
method,
url,
data,
)
sig, err := generateSig(message, c.Secret)
if err != nil {
return nil, err
}
h["CB-ACCESS-SIGN"] = sig
return h, nil
}