-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
226 lines (207 loc) · 4.87 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Package client is a very limited Tezos RPC client library
package client
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"github.com/ecadlabs/gotez/v2/encoding"
)
type Logger interface {
Printf(format string, a ...any)
}
type Client struct {
Client *http.Client
URL string
APIKey string
DebugLogger Logger
}
type Error struct {
Status int
Raw *http.Response
Body []byte
}
func (e *Error) Error() string {
return fmt.Sprintf("gotez-client: http status %d", e.Status)
}
func (c *Client) client() *http.Client {
if c.Client != nil {
return c.Client
}
return http.DefaultClient
}
func (client *Client) mkURL(path string, params map[string]any) (*url.URL, error) {
u, err := url.Parse(client.URL)
if err != nil {
return nil, err
}
values := make(url.Values, len(params))
for k, v := range params {
switch x := v.(type) {
case string:
if x != "" {
values[k] = []string{x}
}
case Flag:
if x {
values[k] = []string{"yes"}
}
default:
tmp := reflect.ValueOf(v)
if (tmp.Kind() != reflect.Interface && tmp.Kind() != reflect.Pointer && tmp.Kind() != reflect.Slice) || !tmp.IsNil() {
values[k] = []string{fmt.Sprintf("%v", v)}
}
}
}
u.Path = path
u.RawQuery = values.Encode()
return u, nil
}
func (c *Client) debug(format string, a ...any) {
if c.DebugLogger != nil {
c.DebugLogger.Printf(format, a...)
}
}
func wrapErr(err error) error {
return fmt.Errorf("gotez-client: %w", err)
}
func (client *Client) request(ctx context.Context, method string, path string, params map[string]any, payload, out any) error {
u, err := client.mkURL(path, params)
if err != nil {
return wrapErr(err)
}
client.debug("%s %s", method, u.String())
var req *http.Request
if method == "POST" {
var body bytes.Buffer
if err = encoding.Encode(&body, payload, encoding.Dynamic()); err != nil {
return wrapErr(err)
}
req, err = http.NewRequestWithContext(ctx, method, u.String(), &body)
if err != nil {
return wrapErr(err)
}
req.Header.Set("Content-Type", "application/octet-stream")
} else {
req, err = http.NewRequestWithContext(ctx, method, u.String(), nil)
if err != nil {
return wrapErr(err)
}
}
req.Header.Set("Accept", "application/octet-stream")
if client.APIKey != "" {
req.Header.Add("X-Api-Key", client.APIKey)
}
res, err := client.client().Do(req)
if err != nil {
return wrapErr(err)
}
defer res.Body.Close()
if res.StatusCode/100 != 2 {
e := &Error{
Status: res.StatusCode,
Raw: res,
}
body, err := io.ReadAll(res.Body)
if err == nil {
e.Body = body
}
return e
}
body, err := io.ReadAll(res.Body)
if err != nil {
return wrapErr(err)
}
if _, err = encoding.Decode(body, out, encoding.Dynamic()); err != nil {
return wrapErr(err)
}
return nil
}
func stream[T any](ctx context.Context, client *Client, path string, params map[string]any) (<-chan *T, <-chan error, error) {
u, err := client.mkURL(path, params)
if err != nil {
return nil, nil, wrapErr(err)
}
client.debug("GET %s", u.String())
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return nil, nil, wrapErr(err)
}
req.Header.Set("Accept", "application/octet-stream")
if client.APIKey != "" {
req.Header.Add("X-Api-Key", client.APIKey)
}
res, err := client.client().Do(req)
if err != nil {
return nil, nil, wrapErr(err)
}
if res.StatusCode/100 != 2 {
e := &Error{
Status: res.StatusCode,
Raw: res,
}
body, err := io.ReadAll(res.Body)
if err == nil {
e.Body = body
}
res.Body.Close()
return nil, nil, e
}
streamCh := make(chan *T)
errCh := make(chan error)
go func() {
defer func() {
res.Body.Close()
close(streamCh)
close(errCh)
client.debug("gotez-client: stream closed")
}()
for {
// decoder is zero copy so new buffer must be allocated for each read
var l [4]uint8
if _, err := io.ReadFull(res.Body, l[:]); err != nil {
errCh <- wrapErr(err)
return
}
len := binary.BigEndian.Uint32(l[:])
buf := make([]byte, int(len))
if _, err := io.ReadFull(res.Body, buf); err != nil {
errCh <- wrapErr(err)
return
}
v := new(T)
if _, err = encoding.Decode(buf, v); err != nil {
errCh <- wrapErr(err)
return
}
// v refers buf
streamCh <- v
}
}()
return streamCh, errCh, nil
}
// BasicBlockInfo returns hash and protocol of the block (usually head) to be used for sequent requests
func (client *Client) BasicBlockInfo(ctx context.Context, chain string, block string) (*BasicBlockInfo, error) {
hash, err := client.BlockHash(ctx, &SimpleRequest{
Chain: chain,
Block: block,
})
if err != nil {
return nil, err
}
proto, err := client.BlockProtocols(ctx, &SimpleRequest{
Chain: chain,
Block: hash.String(),
})
if err != nil {
return nil, err
}
return &BasicBlockInfo{
Hash: hash,
Protocol: proto.Protocol,
}, nil
}