-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
182 lines (148 loc) · 4.02 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
//go:generate go run generate/generate.go generate/template_asset.go
package kraken
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const (
// APIUrl is the Kraken API Endpoint
apiURL = "https://api.kraken.com"
// APIVersion is the Kraken API version number
apiVersion = "0"
)
type Response struct {
Error []string `json:"error"`
Result interface{} `json:"result"`
}
type Client struct {
httpClient *http.Client
apiKey string
apiSecret string
}
// New inits a new Client
func New() *Client {
return &Client{
httpClient: http.DefaultClient,
}
}
// WithAuthentification allows to specify a custom secret in order to execute private requests
func (c *Client) WithAuthentification(key, secret string) {
c.apiKey = key
c.apiSecret = secret
}
func (c *Client) SetTimezone(location string) error {
loc, err := time.LoadLocation(location)
if err != nil {
return nil
}
time.Local = loc
return nil
}
func (c *Client) doRequest(endpoint string, isPrivate bool, data url.Values, respType interface{}) error {
var (
req *http.Request
err error
)
if isPrivate {
req, err = c.buildPrivateRequest(endpoint, data)
if err != nil {
return err
}
} else {
req, err = c.buildPublicRequest(endpoint, data)
if err != nil {
return err
}
}
resp, err := c.httpClient.Do(req)
if err != nil {
return errors.New("failed to make http request")
}
defer resp.Body.Close()
return c.parseResponse(resp, respType)
}
func (c *Client) buildPublicRequest(endpoint string, data url.Values) (*http.Request, error) {
if data == nil {
data = url.Values{}
}
URL := fmt.Sprintf("%s/%s/public/%s", apiURL, apiVersion, endpoint)
req, err := http.NewRequest("POST", URL, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to create public request: %s", err.Error())
}
return req, nil
}
func (c *Client) buildPrivateRequest(endpoint string, data url.Values) (*http.Request, error) {
if c.apiKey == "" || c.apiSecret == "" {
return nil, fmt.Errorf("failed to create private request: key or secret is empty")
}
if data == nil {
data = url.Values{}
}
data.Set("nonce", fmt.Sprintf("%d", time.Now().UTC().UnixMilli()))
URL := fmt.Sprintf("%s/%s/private/%s", apiURL, apiVersion, endpoint)
req, err := http.NewRequest("POST", URL, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to create private request: %s", err.Error())
}
req.Header.Add("API-Key", c.apiKey)
signature, err := c.getSignature(fmt.Sprintf("/%s/private/%s", apiVersion, endpoint), data)
if err != nil {
return nil, fmt.Errorf("failed to sign private request: %s", err.Error())
}
req.Header.Add("API-Sign", signature)
return req, nil
}
func (c *Client) getSignature(requestURL string, data url.Values) (string, error) {
sha := sha256.New()
_, err := sha.Write([]byte(data.Get("nonce") + data.Encode()))
if err != nil {
return "", err
}
shasum := sha.Sum(nil)
secret, err := base64.StdEncoding.DecodeString(c.apiSecret)
if err != nil {
return "", err
}
mac := hmac.New(sha512.New, secret)
_, err = mac.Write(append([]byte(requestURL), shasum...))
if err != nil {
return "", err
}
macsum := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(macsum), nil
}
func (c *Client) parseResponse(response *http.Response, respType interface{}) error {
if response.StatusCode != 200 {
return fmt.Errorf("failed to get a successful response. status %d", response.StatusCode)
}
if response.Body == nil {
return fmt.Errorf("failed to get a response body")
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %+v", err)
}
var resp Response
if respType != nil {
resp.Result = respType
}
err = json.Unmarshal(body, &resp)
if err != nil {
return fmt.Errorf("failed to unmarshal response: %+v", err)
}
if len(resp.Error) > 0 {
return fmt.Errorf("got server errors: %+v", resp.Error)
}
return nil
}