-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
common.go
96 lines (80 loc) · 2.77 KB
/
common.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
package private
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-numb/go-dydx/helpers"
)
func (p *Private) get(endpoint string, params url.Values) ([]byte, error) {
return p.request(http.MethodGet, helpers.GenerateQueryPath(endpoint, params), "")
}
func (p *Private) post(endpoint string, data interface{}) ([]byte, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
return p.request(http.MethodPost, endpoint, string(b))
}
func (p *Private) delete(endpoint string, params url.Values) ([]byte, error) {
return p.request(http.MethodDelete, helpers.GenerateQueryPath(endpoint, params), "")
}
func (p *Private) request(method, endpoint string, data string) ([]byte, error) {
isoTimestamp := generateNowISO()
requestPath := fmt.Sprintf("/v3/%s", endpoint)
headers := map[string]string{
"DYDX-SIGNATURE": p.Sign(requestPath, method, isoTimestamp, data),
"DYDX-API-KEY": p.ApiKeyCredentials.Key,
"DYDX-TIMESTAMP": isoTimestamp,
"DYDX-PASSPHRASE": p.ApiKeyCredentials.Passphrase,
}
res, err := p.execute(method, requestPath, headers, data)
if err != nil {
return nil, err
}
defer res.Body.Close()
// Rate Limit
p.RateLimit.Remaining = res.Header.Get("RateLimit-Remaining")
p.RateLimit.Reset = res.Header.Get("RateLimit-Reset")
p.RateLimit.RetryAfter = res.Header.Get("Retry-After")
p.RateLimit.Limit = res.Header.Get("RateLimit-Limit")
if res.StatusCode < 200 || res.StatusCode > 300 {
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
p.Logger.Printf("uri: %s, code: %d, err msg: %s", requestPath, res.StatusCode, buf.String())
return nil, fmt.Errorf("uri: %v , status code: %d", requestPath, res.StatusCode)
}
b, err := ioutil.ReadAll(res.Body)
p.Logger.Printf("uri: %s, response body: %s", requestPath, b)
return b, err
}
func (p *Private) execute(method string, requestPath string, headers map[string]string, data string) (*http.Response, error) {
requestPath = fmt.Sprintf("%s%s", p.Host, requestPath)
req, err := http.NewRequest(method, requestPath, strings.NewReader(data))
if err != nil {
return nil, err
}
for key, val := range headers {
req.Header.Add(key, val)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "go-dydx")
return p.HttpClient.Do(req)
}
func generateNowISO() string {
return time.Now().UTC().Format("2006-01-02T15:04:05.999Z")
}
func (p *Private) Sign(requestPath, method, isoTimestamp, body string) string {
message := fmt.Sprintf("%s%s%s%s", isoTimestamp, method, requestPath, body)
secret, _ := base64.URLEncoding.DecodeString(p.ApiKeyCredentials.Secret)
h := hmac.New(sha256.New, secret)
h.Write([]byte(message))
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}