-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
196 lines (185 loc) · 5.11 KB
/
request.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
package pocketic
import (
"bytes"
"encoding/json"
"fmt"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/principal"
"io"
"net/http"
)
var headers = func() http.Header {
return http.Header{
"content-type": []string{"application/json"},
"processing-timeout-ms": []string{"300000"},
}
}
func newRequest(method, url string, body any) (*http.Request, error) {
var bodyBytes io.Reader
if body != nil {
raw, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyBytes = bytes.NewBuffer(raw)
}
req, err := http.NewRequest(method, url, bodyBytes)
if err != nil {
return nil, err
}
req.Header = headers()
return req, nil
}
// AwaitCall awaits an update call submitted previously by `submit_call_with_effective_principal`.
func (pic PocketIC) AwaitCall(messageID MessageID) ([]byte, error) {
var resp result[wasmResult[[]byte]]
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/await_ingress_message", pic.InstanceURL()),
messageID,
&resp,
); err != nil {
return nil, err
}
if resp.Err != nil {
return nil, resp.Err
}
if resp.Ok.Reject != nil {
return nil, resp.Ok.Reject
}
return *resp.Ok.Reply, nil
}
// ExecuteCall executes an update call on a canister.
func (pic PocketIC) ExecuteCall(
canisterID principal.Principal,
effectivePrincipal EffectivePrincipal,
sender principal.Principal,
method string,
payload []byte,
) ([]byte, error) {
var resp rawCanisterResult
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/execute_ingress_message", pic.InstanceURL()),
rawCanisterCall{
CanisterID: canisterID.Raw,
EffectivePrincipal: effectivePrincipal,
Method: method,
Payload: payload,
Sender: sender.Raw,
},
&resp,
); err != nil {
return nil, err
}
if resp.Err != nil {
return nil, resp.Err
}
if resp.Ok.Reject != nil {
return nil, resp.Ok.Reject
}
return *resp.Ok.Reply, nil
}
// QueryCall executes a query call on a canister.
func (pic PocketIC) QueryCall(canisterID principal.Principal, sender principal.Principal, method string, args []any, ret []any) error {
payload, err := idl.Marshal(args)
if err != nil {
return err
}
raw, err := pic.canisterCall("read/query", canisterID, new(EffectivePrincipalNone), sender, method, payload)
if err != nil {
return err
}
if err := idl.Unmarshal(raw, ret); err != nil {
return err
}
return nil
}
// SubmitCall submits an update call (without executing it immediately).
func (pic PocketIC) SubmitCall(
canisterID principal.Principal,
sender principal.Principal,
method string,
payload []byte,
) (*MessageID, error) {
return pic.SubmitCallWithEP(
canisterID,
new(EffectivePrincipalNone),
sender,
method,
payload,
)
}
// SubmitCallWithEP submits an update call with a provided effective principal (without executing it immediately).
func (pic PocketIC) SubmitCallWithEP(
canisterID principal.Principal,
effectivePrincipal EffectivePrincipal,
sender principal.Principal,
method string,
payload []byte,
) (*MessageID, error) {
var resp rawSubmitIngressResult
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/submit_ingress_message", pic.InstanceURL()),
rawCanisterCall{
CanisterID: canisterID.Raw,
EffectivePrincipal: effectivePrincipal,
Method: method,
Payload: payload,
Sender: sender.Raw,
},
&resp,
); err != nil {
return nil, err
}
if resp.Err != nil {
return nil, resp.Err
}
return resp.Ok, nil
}
// UpdateCall executes an update call on a canister.
func (pic PocketIC) UpdateCall(canisterID principal.Principal, sender principal.Principal, method string, args []any, ret []any) error {
payload, err := idl.Marshal(args)
if err != nil {
return err
}
raw, err := pic.updateCallWithEP(canisterID, &EffectivePrincipalCanisterID{CanisterID: canisterID.Raw}, sender, method, payload)
if err != nil {
return err
}
return idl.Unmarshal(raw, ret)
}
// canisterCall calls the canister endpoint with the provided arguments.
func (pic PocketIC) canisterCall(endpoint string, canisterID principal.Principal, effectivePrincipal EffectivePrincipal, sender principal.Principal, method string, payload []byte) ([]byte, error) {
var resp rawCanisterResult
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/%s", pic.InstanceURL(), endpoint),
rawCanisterCall{
CanisterID: canisterID.Raw,
EffectivePrincipal: effectivePrincipal,
Method: method,
Payload: payload,
Sender: sender.Raw,
},
&resp,
); err != nil {
return nil, err
}
if resp.Err != nil {
return nil, resp.Err
}
if resp.Ok.Reject != nil {
return nil, resp.Ok.Reject
}
return *resp.Ok.Reply, nil
}
// updateCallWithEP calls SubmitCallWithEP and AwaitCall in sequence.
func (pic PocketIC) updateCallWithEP(canisterID principal.Principal, effectivePrincipal EffectivePrincipal, sender principal.Principal, method string, payload []byte) ([]byte, error) {
messageID, err := pic.SubmitCallWithEP(canisterID, effectivePrincipal, sender, method, payload)
if err != nil {
return nil, err
}
return pic.AwaitCall(*messageID)
}