From b55146735220aa51d86239a7eb32fc5e65667e94 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 15:27:11 -0600 Subject: [PATCH] Update outgoing-http-requests to functional syntax --- .github/workflows/test.yml | 2 +- examples/outgoing_http_requests/dfx.json | 3 +- examples/outgoing_http_requests/src/index.did | 24 ++++----- examples/outgoing_http_requests/src/index.ts | 49 ++++++++++--------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1b6932397f..d41d35d99f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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", @@ -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", diff --git a/examples/outgoing_http_requests/dfx.json b/examples/outgoing_http_requests/dfx.json index a142c3f081..1f2b061611 100644 --- a/examples/outgoing_http_requests/dfx.json +++ b/examples/outgoing_http_requests/dfx.json @@ -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 diff --git a/examples/outgoing_http_requests/src/index.did b/examples/outgoing_http_requests/src/index.did index 9447f44407..b329d2a5dd 100644 --- a/examples/outgoing_http_requests/src/index.did +++ b/examples/outgoing_http_requests/src/index.did @@ -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; } diff --git a/examples/outgoing_http_requests/src/index.ts b/examples/outgoing_http_requests/src/index.ts index 7f65abad58..e41d514cb0 100644 --- a/examples/outgoing_http_requests/src/index.ts +++ b/examples/outgoing_http_requests/src/index.ts @@ -1,12 +1,12 @@ import { ic, + Manual, + None, Principal, query, - update, Service, Some, - None, - Manual + update } from 'azle'; import { HttpResponse, @@ -14,9 +14,8 @@ import { managementCanister } from 'azle/canisters/management'; -export default class extends Service { - @update([], HttpResponse) - async xkcd(): Promise { +export default Service({ + xkcd: update([], HttpResponse, async () => { return await ic.call(managementCanister.http_request, { args: [ { @@ -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> { - 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"; @@ -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: [] }; - } -} + }) +});