forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
144 lines (120 loc) · 2.9 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
package gdax
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
}
func NewClient(secret, key, passphrase string) *Client {
client := Client{
BaseURL: "https://api.gdax.com",
Secret: secret,
Key: key,
Passphrase: passphrase,
HttpClient: &http.Client{
Timeout: 15 * time.Second,
},
RetryCount: 0,
}
return &client
}
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.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", "Baylatent Bot 2.0")
h, err := c.Headers(method, url, timestamp, string(data))
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
}