-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
172 lines (142 loc) · 3.35 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
package bitflyer
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/go-numb/go-bitflyer/hidden"
"github.com/go-numb/go-bitflyer/private/auth"
)
const (
V1 = "https://api.bitflyer.com/v1/"
)
type Client struct {
h *http.Client
// Auth
Auth *auth.Client
}
func New(key, secret string) *Client {
return &Client{
h: _newClient(),
Auth: auth.New(key, secret),
}
}
func (p *Client) SetClient(c *http.Client) *Client {
p.h = c
return p
}
func CallHidden() *hidden.Client {
return &hidden.Client{}
}
type Require interface {
IsPrivate() bool
Path() string
Method() string
Query() string
Payload() []byte
}
type Response struct {
Code int
Status string
Result interface{}
Limit APILimit
Error error
}
func (p *Client) request(req Require, results interface{}) (*APILimit, error) {
res, err := p._do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// // Header
// X-Frame-Options [SAMEORIGIN]
// Strict-Transport-Security [max-age=31536000]
// X-Ratelimit-Reset [1674714614]
// X-Xss-Protection [1; mode=block]
// Content-Security-Policy [default-src http: https: ws: wss: data: 'unsafe-inline' 'unsafe-eval'; img-src https: data: blob: 'self']
// Vary [Accept-Encoding]
// Date [Thu, 26 Jan 2023 06:25:14 GMT]
// X-Ratelimit-Period [300]
// X-Ratelimit-Remaining [499]
// Content-Type [application/json; charset=utf-8]
// Expires
manage := new(APILimit)
manage.Set(res.Header)
// response change to result structure
if err := _decode(res, results); err != nil {
return nil, err
}
return manage, nil
}
func (p *Client) _do(req Require) (*http.Response, error) {
r := p._newRequest(req)
res, err := p.h.Do(r)
if err != nil {
return nil, err
}
// no usefull headers
if res.StatusCode != 200 {
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if strings.Contains(string(b), "Order not found") {
return nil, fmt.Errorf("status: %s, order could not be found. check the designated ID", res.Status)
}
return nil, fmt.Errorf("status: %s, body: %q", res.Status, b)
}
return res, nil
}
func _newClient() *http.Client {
c := http.DefaultClient
c.Timeout = 3 * time.Second
return c
}
func (p *Client) _newRequest(req Require) *http.Request {
u, _ := url.Parse(V1)
u.Path = path.Join(u.Path, req.Path())
u.RawQuery = req.Query()
r, err := http.NewRequest(req.Method(), u.String(), bytes.NewBuffer(req.Payload()))
if err != nil {
return nil
}
if req.IsPrivate() {
nonce := time.Now().String()
var path = u.Path
if u.RawQuery != "" {
path += "?" + u.RawQuery
}
payload := nonce + req.Method() + path + string(req.Payload())
key, _ := p.Auth.Get()
r.Header.Set("Content-Type", "application/json")
r.Header.Set("ACCESS-KEY", key)
r.Header.Set("ACCESS-SIGN", p._signature(payload))
r.Header.Set("ACCESS-TIMESTAMP", nonce)
}
return r
}
func _decode(res *http.Response, out interface{}) error {
// b, err := io.ReadAll(res.Body)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("----\n%s\n----------\n", string(b))
// fmt.Println(res.StatusCode)
if err := json.NewDecoder(res.Body).Decode(out); err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
return nil
}
func (p *Client) _signature(payload string) string {
return p.Auth.Signature(payload)
}