Skip to content

Commit

Permalink
http_counter example updated and passing
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 13, 2023
1 parent 0a174ce commit f881868
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
# "examples/guard_functions",
# "examples/ic_api",
# "examples/inline_types",
# "examples/motoko_examples/http_counter",
# "examples/motoko_examples/persistent-storage",
# "examples/motoko_examples/phone-book",
# "examples/motoko_examples/simple-to-do",
Expand Down Expand Up @@ -177,6 +176,7 @@ jobs:
"examples/motoko_examples/factorial",
"examples/motoko_examples/hello",
"examples/motoko_examples/hello-world",
"examples/motoko_examples/http_counter",
"examples/motoko_examples/minimal-counter-dapp",
"examples/motoko_examples/quicksort",
"examples/notify_raw",
Expand Down
52 changes: 29 additions & 23 deletions examples/motoko_examples/http_counter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/motoko_examples/http_counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"test": "AZLE_REBUILD=true ts-node --transpile-only --ignore=false test/test.ts"
},
"dependencies": {
"azle": "0.17.1",
"encode-utf8": "2.0.0"
"azle": "0.17.1"
},
"devDependencies": {
"@dfinity/agent": "^0.19.0",
"ts-node": "^10.9.1"
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
50 changes: 22 additions & 28 deletions examples/motoko_examples/http_counter/src/index.did
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
type CallbackStrategy = record {
token : Token;
callback : func (Token) -> (StreamingCallbackHttpResponse) query;
};
type HttpRequest = record {
url : text;
method : text;
body : vec nat8;
headers : vec record { text; text };
};
type HttpResponse = record {
body : vec nat8;
headers : vec record { text; text };
upgrade : opt bool;
streaming_strategy : opt StreamingStrategy;
status_code : nat16;
};
type StreamingCallbackHttpResponse = record {
token : opt Token;
body : vec nat8;
};
type StreamingStrategy = variant { Callback : CallbackStrategy };
type Token = record { arbitrary_data : text };
service : () -> {
http_request : (HttpRequest) -> (HttpResponse) query;
http_request_update : (HttpRequest) -> (HttpResponse);
http_streaming : (Token) -> (StreamingCallbackHttpResponse) query;
}
type rec_0 = record {url:text; method:text; body:vec nat8; headers:vec record {text; text}};
type rec_6 = record {arbitrary_data:text};
type rec_5 = record {arbitrary_data:text};
type rec_4 = record {token:opt rec_5; body:vec nat8};
type rec_3 = record {token:rec_6; callback:func (text) -> (rec_4) query};
type rec_2 = variant {Callback:rec_3};
type rec_1 = record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt rec_2; status_code:nat16};
type rec_7 = record {url:text; method:text; body:vec nat8; headers:vec record {text; text}};
type rec_13 = record {arbitrary_data:text};
type rec_12 = record {arbitrary_data:text};
type rec_11 = record {token:opt rec_12; body:vec nat8};
type rec_10 = record {token:rec_13; callback:func (text) -> (rec_11) query};
type rec_9 = variant {Callback:rec_10};
type rec_8 = record {body:vec nat8; headers:vec record {text; text}; upgrade:opt bool; streaming_strategy:opt rec_9; status_code:nat16};
type rec_14 = record {arbitrary_data:text};
type rec_16 = record {arbitrary_data:text};
type rec_15 = record {token:opt rec_16; body:vec nat8};
service: () -> {
http_request: (rec_0) -> (rec_1) query;
http_request_update: (rec_7) -> (rec_8);
http_streaming: (rec_14) -> (rec_15) query;
}
111 changes: 54 additions & 57 deletions examples/motoko_examples/http_counter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
blob,
ic,
init,
nat,
nat16,
Opt,
Expand All @@ -11,19 +12,21 @@ import {
Variant,
Vec,
update,
record,
text,
candid,
func,
variant,
Some,
None,
bool,
Service
} from 'azle';
import encodeUtf8 from 'encode-utf8';

@record
class Token extends Record {
// add whatever fields you'd like
@candid(text)
arbitrary_data: text;
}

class StreamingCallbackHttpResponse extends Record {
@candid(blob)
body: blob;
Expand All @@ -32,14 +35,9 @@ class StreamingCallbackHttpResponse extends Record {
token: Opt<Token>;
}

@record
class Token extends Record {
// add whatever fields you'd like
@candid(text)
arbitrary_data: text;
}
@func([text], StreamingCallbackHttpResponse, 'query')
class Callback {}

@record
class CallbackStrategy extends Record {
@candid(Callback)
callback: Callback;
Expand All @@ -48,10 +46,6 @@ class CallbackStrategy extends Record {
token: Token;
}

@func([text], StreamingCallbackHttpResponse, 'query')
class Callback {}

@variant
class StreamingStrategy extends Variant {
@candid(CallbackStrategy)
Callback?: CallbackStrategy;
Expand All @@ -60,7 +54,6 @@ class StreamingStrategy extends Variant {
type HeaderField = [text, text];
const HeaderField = Tuple(text, text);

@record
class HttpResponse extends Record {
@candid(nat16)
status_code: nat16;
Expand All @@ -78,7 +71,6 @@ class HttpResponse extends Record {
upgrade: Opt<bool>;
}

@record
class HttpRequest extends Record {
@candid(text)
method: text;
Expand All @@ -93,22 +85,14 @@ class HttpRequest extends Record {
body: blob;
}

let stableStorage = new StableBTreeMap<text, nat>(0, 25, 1_000);

stableStorage.insert('counter', 0n);

function isGzip(x: HeaderField): boolean {
return (
x[0].toLowerCase() === 'accept-encoding' &&
x[1].toLowerCase().includes('gzip')
);
}
export default class extends Service {
stableStorage = new StableBTreeMap<text, nat>(text, nat, 0);

function encode(string: string): blob {
return new Uint8Array(encodeUtf8(string));
}
@init([])
init() {
this.stableStorage.insert('counter', 0n);
}

export default class extends Service {
@query([HttpRequest], HttpResponse)
http_request(req: HttpRequest): HttpResponse {
console.log('Hello from http_request');
Expand All @@ -131,15 +115,17 @@ export default class extends Service {
upgrade: Some(false)
};
}

const counterOpt = this.stableStorage.get('counter');
const counter =
counterOpt.length === 0
? ic.trap('counter does not exist')
: counterOpt[0];

return {
status_code: 200,
headers: [['content-type', 'text/plain']],
body: encode(
`Counter is ${match(stableStorage.get('counter'), {
Some: (x) => x,
None: () => 0n
})}\n${req.url}\n`
),
body: encode(`Counter is ${counter}\n${req.url}\n`),
streaming_strategy: None,
upgrade: None
};
Expand Down Expand Up @@ -187,26 +173,25 @@ export default class extends Service {
@update([HttpRequest], HttpResponse)
http_request_update(req: HttpRequest): HttpResponse {
if (req.method === 'POST') {
const counter = match(stableStorage.get('counter'), {
Some: (x) => x,
None: () => 0n
});
const counterOpt = this.stableStorage.get('counter');
const counter =
counterOpt.length === 0
? ic.trap('counter does not exist')
: counterOpt[0];

stableStorage.insert('counter', counter + 1n);
this.stableStorage.insert('counter', counter + 1n);

if (req.headers.find(isGzip) === undefined) {
const counterOpt = this.stableStorage.get('counter');
const counter =
counterOpt.length === 0
? ic.trap('counter does not exist')
: counterOpt[0];

return {
status_code: 201,
headers: [['content-type', 'text/plain']],
body: encode(
`Counter updated to ${match(
stableStorage.get('counter'),
{
Some: (x) => x,
None: () => 0n
}
)}\n`
),
body: encode(`Counter updated to ${counter}\n`),
streaming_strategy: None,
upgrade: None
};
Expand Down Expand Up @@ -252,13 +237,14 @@ export default class extends Service {
};
}
case 'next': {
const counterOpt = this.stableStorage.get('counter');
const counter =
counterOpt.length === 0
? ic.trap('counter does not exist')
: counterOpt[0];

return {
body: encode(
`${match(stableStorage.get('counter'), {
Some: (x) => x,
None: () => 0n
})}`
),
body: encode(`${counter}`),
token: Some({ arbitrary_data: 'last' })
};
}
Expand All @@ -274,3 +260,14 @@ export default class extends Service {
}
}
}

function isGzip(x: HeaderField): boolean {
return (
x[0].toLowerCase() === 'accept-encoding' &&
x[1].toLowerCase().includes('gzip')
);
}

function encode(string: string): blob {
return Buffer.from(string, 'utf-8');
}

0 comments on commit f881868

Please sign in to comment.