forked from jsgoyette/gemini
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
142 lines (115 loc) · 2.52 KB
/
client.go
File metadata and controls
142 lines (115 loc) · 2.52 KB
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
package geminix
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type Client struct {
url string
key string
secret string
}
func NewClient(key string, secret string, sandbox bool) *Client {
var url string
if sandbox {
url = SandboxBaseUrl
} else {
url = BaseUrl
}
return &Client{url: url, key: key, secret: secret}
}
func (c *Client) BuildHeader(req *map[string]interface{}) (http.Header, error) {
reqStr, err := json.Marshal(req)
if err != nil {
return nil, err
}
payload := base64.StdEncoding.EncodeToString([]byte(reqStr))
mac := hmac.New(sha512.New384, []byte(c.secret))
mac.Write([]byte(payload))
signature := hex.EncodeToString(mac.Sum(nil))
header := http.Header{}
header.Set("X-GEMINI-APIKEY", c.key)
header.Set("X-GEMINI-PAYLOAD", payload)
header.Set("X-GEMINI-SIGNATURE", signature)
return header, nil
}
type Response struct {
Result string
ApiError
}
type ApiError struct {
Reason string
Message string
}
func (e *ApiError) Error() string {
return fmt.Sprintf("[%v] %v", e.Reason, e.Message)
}
func (c *Client) Request(verb string, uri string, params map[string]interface{}) ([]byte, error) {
url := c.url + uri
req, err := http.NewRequest(verb, url, bytes.NewBuffer([]byte{}))
if err != nil {
return nil, err
}
if params != nil {
if verb == "GET" {
q := req.URL.Query()
for key, val := range params {
q.Add(key, val.(string))
}
req.URL.RawQuery = q.Encode()
} else {
req.Header, err = c.BuildHeader(¶ms)
if err != nil {
return nil, err
}
}
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var res Response
json.Unmarshal(body, &res)
if res.Result == "error" {
return nil, &res.ApiError
}
return body, nil
}
func (c *Client) PublicRequest(uri string) ([]byte, error) {
body, err := c.Request("GET", uri, nil)
return body, err
}
func Nonce() int64 {
return time.Now().UnixNano()
}
func (c *Client) PrivateRequest(uri string, params map[string]interface{}) ([]byte, error) {
if params == nil {
params = map[string]interface{}{
"request": uri,
"nonce": Nonce(),
}
} else {
params["request"] = uri
params["nonce"] = Nonce()
/*for key, value := range params {
if value == nil {
delete(params, key)
}
}*/
}
body, err := c.Request("POST", uri, params)
return body, err
}