Skip to content

Commit

Permalink
Update outgoing-http-requests to functional syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Sep 26, 2023
1 parent 4229748 commit b551467
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# "examples/generics",
# "examples/motoko_examples/superheroes", # blocked by recursive
# "examples/motoko_examples/whoami", # blocked by postUpgrade
# "examples/outgoing_http_requests",
# "examples/rejections",
# "examples/robust_imports",
# "examples/run_time_errors",
Expand Down Expand Up @@ -111,6 +110,7 @@ jobs:
"examples/notify_raw",
"examples/null_example",
"examples/optional_types",
"examples/outgoing_http_requests",
"examples/pre_and_post_upgrade",
"examples/primitive_types",
"examples/principal",
Expand Down
3 changes: 2 additions & 1 deletion examples/outgoing_http_requests/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/outgoing_http_requests/outgoing_http_requests.wasm.gz",
"wasm": ".azle/outgoing_http_requests/outgoing_http_requests.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/outgoing_http_requests",
"node_compatibility": true
Expand Down
24 changes: 12 additions & 12 deletions examples/outgoing_http_requests/src/index.did
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type rec_1 = record {value:text; name:text};
type rec_0 = record {status:nat; body:vec nat8; headers:vec rec_1};
type rec_3 = record {value:text; name:text};
type rec_2 = record {status:nat; body:vec nat8; headers:vec rec_3};
type rec_6 = record {value:text; name:text};
type rec_5 = record {status:nat; body:vec nat8; headers:vec rec_6};
type rec_4 = record {context:vec nat8; response:rec_5};
type rec_8 = record {value:text; name:text};
type rec_7 = record {status:nat; body:vec nat8; headers:vec rec_8};
type rec_52 = record {value:text; name:text};
type rec_51 = record {status:nat; body:vec nat8; headers:vec rec_52};
type rec_54 = record {value:text; name:text};
type rec_53 = record {status:nat; body:vec nat8; headers:vec rec_54};
type rec_57 = record {value:text; name:text};
type rec_56 = record {status:nat; body:vec nat8; headers:vec rec_57};
type rec_55 = record {context:vec nat8; response:rec_56};
type rec_59 = record {value:text; name:text};
type rec_58 = record {status:nat; body:vec nat8; headers:vec rec_59};
service: () -> {
xkcd: () -> (rec_0);
xkcdRaw: () -> (rec_2);
xkcdTransform: (rec_4) -> (rec_7) query;
xkcd: () -> (rec_51);
xkcdRaw: () -> (rec_53);
xkcdTransform: (rec_55) -> (rec_58) query;
}
49 changes: 27 additions & 22 deletions examples/outgoing_http_requests/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import {
ic,
Manual,
None,
Principal,
query,
update,
Service,
Some,
None,
Manual
update
} from 'azle';
import {
HttpResponse,
HttpTransformArgs,
managementCanister
} from 'azle/canisters/management';

export default class extends Service {
@update([], HttpResponse)
async xkcd(): Promise<HttpResponse> {
export default Service({
xkcd: update([], HttpResponse, async () => {
return await ic.call(managementCanister.http_request, {
args: [
{
Expand All @@ -28,22 +27,27 @@ export default class extends Service {
headers: [],
body: None,
transform: Some({
function: [ic.id(), 'xkcdTransform'],
function: [ic.id(), 'xkcdTransform'] as [
Principal,
string
],
context: Uint8Array.from([])
})
}
],
cycles: 50_000_000n
});
}
}),

// TODO the replica logs give some concerning output: https://forum.dfinity.org/t/fix-me-in-http-outcalls-call-raw/19435
@update([], HttpResponse, { manual: true })
async xkcdRaw(): Promise<Manual<HttpResponse>> {
const httpResponse = await ic.callRaw(
Principal.fromText('aaaaa-aa'),
'http_request',
ic.candidEncode(`
xkcdRaw: update(
[],
Manual(HttpResponse),
async () => {
const httpResponse = await ic.callRaw(
Principal.fromText('aaaaa-aa'),
'http_request',
ic.candidEncode(`
(
record {
url = "https://xkcd.com/642/info.0.json";
Expand All @@ -57,17 +61,18 @@ export default class extends Service {
}
)
`),
50_000_000n
);
50_000_000n
);

ic.replyRaw(httpResponse);
}
ic.replyRaw(httpResponse);
},
{ manual: true }
),

@query([HttpTransformArgs], HttpResponse)
xkcdTransform(args: HttpTransformArgs): HttpResponse {
xkcdTransform: query([HttpTransformArgs], HttpResponse, (args) => {
return {
...args.response,
headers: []
};
}
}
})
});

0 comments on commit b551467

Please sign in to comment.