-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
169 lines (152 loc) · 4.43 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
package agent
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
"github.com/aviate-labs/agent-go/principal"
"github.com/fxamacker/cbor/v2"
)
// Client is a client for the IC agent.
type Client struct {
client http.Client
config ClientConfig
logger Logger
}
// NewClient creates a new client based on the given configuration.
func NewClient(cfg ClientConfig) Client {
return Client{
client: http.Client{},
config: cfg,
logger: new(NoopLogger),
}
}
// NewClientWithLogger creates a new client based on the given configuration and logger.
func NewClientWithLogger(cfg ClientConfig, logger Logger) Client {
if logger == nil {
logger = new(NoopLogger)
}
return Client{
client: http.Client{},
config: cfg,
logger: logger,
}
}
func (c Client) Call(ctx context.Context, canisterID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/canister/%s/call", canisterID.Encode()))
c.logger.Printf("[CLIENT] CALL %s", u)
req, err := c.newRequest(ctx, "POST", u, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusAccepted:
return io.ReadAll(resp.Body)
case http.StatusOK:
body, _ := io.ReadAll(resp.Body)
var err preprocessingError
if err := cbor.Unmarshal(body, &err); err != nil {
return nil, err
}
return nil, fmt.Errorf("(%d) %s: %s", err.RejectCode, err.Message, err.ErrorCode)
default:
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("(%d) %s: %s", resp.StatusCode, resp.Status, body)
}
}
func (c Client) Query(ctx context.Context, canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post(ctx, "query", canisterID, data)
}
func (c Client) ReadState(ctx context.Context, canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post(ctx, "read_state", canisterID, data)
}
func (c Client) ReadSubnetState(ctx context.Context, subnetID principal.Principal, data []byte) ([]byte, error) {
return c.postSubnet(ctx, "read_state", subnetID, data)
}
// Status returns the status of the IC.
func (c Client) Status() (*Status, error) {
raw, err := c.get("/api/v2/status")
if err != nil {
return nil, err
}
var status Status
return &status, cbor.Unmarshal(raw, &status)
}
func (c Client) get(path string) ([]byte, error) {
c.logger.Printf("[CLIENT] GET %s", c.url(path))
resp, err := c.client.Get(c.url(path))
if err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
}
func (c Client) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/cbor")
return req, nil
}
func (c Client) post(ctx context.Context, path string, canisterID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/canister/%s/%s", canisterID.Encode(), path))
c.logger.Printf("[CLIENT] POST %s", u)
req, err := c.newRequest(ctx, "POST", u, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
return io.ReadAll(resp.Body)
default:
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("(%d) %s: %s", resp.StatusCode, resp.Status, body)
}
}
func (c Client) postSubnet(ctx context.Context, path string, subnetID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/subnet/%s/%s", subnetID.Encode(), path))
c.logger.Printf("[CLIENT] POST %s", u)
req, err := c.newRequest(ctx, "POST", u, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
return io.ReadAll(resp.Body)
default:
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("(%d) %s: %s", resp.StatusCode, resp.Status, body)
}
}
func (c Client) url(p string) string {
u := *c.config.Host
u.Path = path.Join(u.Path, p)
return u.String()
}
// ClientConfig is the configuration for a client.
type ClientConfig struct {
Host *url.URL
}
type preprocessingError struct {
// The reject code.
RejectCode uint64 `cbor:"reject_code"`
// A textual diagnostic message.
Message string `cbor:"reject_message"`
// An optional implementation-specific textual error code.
ErrorCode string `cbor:"error_code"`
}