-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
96 lines (88 loc) · 2.44 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
package agent
import (
"bytes"
"fmt"
"io"
"net/http"
)
type Client struct {
client http.Client
host string
}
func NewClient(host string) Client {
return Client{
client: http.Client{},
host: host,
}
}
func (c *Client) Status() (Status, error) {
raw, err := c.get("/api/v2/status")
if err != nil {
return Status{}, err
}
status := new(Status)
err = status.UnmarshalCBOR(raw)
return *status, err
}
func (c *Client) query(canisterId string, data []byte) ([]byte, error) {
buffer := bytes.NewBuffer(data)
endpoint := c.host + "/api/v2/canister/" + canisterId + "/query"
//fmt.Println("post url:", endpoint)
resp, err := c.client.Post(endpoint, "application/cbor", buffer)
if err != nil {
fmt.Println("error:", err)
return nil, err
} else if resp.StatusCode != 200 {
//fmt.Println(
// "status:", resp.Status, "\n",
// "StatusCode:", resp.StatusCode, "\n",
// "Proto:", resp.Proto, "\n",
// "ProtoMajor:", resp.ProtoMajor, "\n",
// "ProtoMinor:", resp.ProtoMinor, "\n",
// "Header:", resp.Header, "\n",
// "Body:", resp.Body, "\n",
// "ContentLength:", resp.ContentLength, "\n",
// "TransferEncoding:", resp.TransferEncoding, "\n",
// "Request:", resp.Request,
//)
return nil, fmt.Errorf("fail to post ic with status: %v", resp.Status)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func (c *Client) call(canisterId string, reqId RequestID, data []byte) (RequestID, error) {
buffer := bytes.NewBuffer(data)
endpoint := c.host + "/api/v2/canister/" + canisterId + "/call"
//fmt.Println("endpoint:", endpoint)
contentType := "application/cbor"
resp, err := c.client.Post(endpoint, contentType, buffer)
if err != nil {
return reqId, err
}
if resp.StatusCode != 202 {
fmt.Println(resp)
return reqId, fmt.Errorf("fail to call ic with status: %v", resp.Status)
}
return reqId, nil
}
func (c *Client) readState(canisterId string, data []byte) ([]byte, error) {
buffer := bytes.NewBuffer(data)
endpoint := c.host + "/api/v2/canister/" + canisterId + "/read_state"
contentType := "application/cbor"
resp, err := c.client.Post(endpoint, contentType, buffer)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return []byte{}, fmt.Errorf("fail to read state with status: %v", resp.Status)
}
return io.ReadAll(resp.Body)
}
func (c Client) get(path string) ([]byte, error) {
a := c.host + path
resp, err := c.client.Get(a)
if err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
}