Skip to content

Commit

Permalink
Add streaming example (blocked on dfinity/agent-rs#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Bloom-dfinity committed Mar 17, 2022
1 parent 1b847d3 commit d61e0cb
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions motoko/http_counter/src/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Text "mo:base/Text";
import Array "mo:base/Array";
import Option "mo:base/Option";
import Prim "mo:prim";
import Prelude "mo:base/Prelude";


actor HttpCounter {
Expand All @@ -13,10 +14,8 @@ actor HttpCounter {
};

type Token = {
key: Text;
content_encoding: Text;
index: Nat;
sha256: ?Blob;
// Add whatever fields you'd like
arbitrary_data: Text;
};

type CallbackStrategy = {
Expand Down Expand Up @@ -52,23 +51,35 @@ actor HttpCounter {
stable var counter = 0;

public query func http_request(req : HttpRequest) : async HttpResponse {
switch (req.method, not Option.isNull(Array.find(req.headers, isGzip))) {
case ("GET", false) {{
switch (req.method, not Option.isNull(Array.find(req.headers, isGzip)), req.url) {
case ("GET", false, "/stream") {{
status_code = 200;
headers = [ ("content-type", "text/plain") ];
body = Text.encodeUtf8("Counter");
streaming_strategy = ?#Callback({
callback = http_streaming;
token = {
arbitrary_data = "start";
}
});
upgrade = ?false;
}};
case ("GET", false, _) {{
status_code = 200;
headers = [ ("content-type", "text/plain") ];
body = Text.encodeUtf8("Counter is " # Nat.toText(counter) # "\n");
body = Text.encodeUtf8("Counter is " # Nat.toText(counter) # "\n" # req.url # "\n");
streaming_strategy = null;
upgrade = null;
}};
case ("GET", true) {{
case ("GET", true, _) {{
status_code = 200;
headers = [ ("content-type", "text/plain"), ("content-encoding", "gzip") ];
body = "\1f\8b\08\00\98\02\1b\62\00\03\2b\2c\4d\2d\aa\e4\02\00\d6\80\2b\05\06\00\00\00";
streaming_strategy = null;
upgrade = null;
}};

case ("POST", _) {{
case ("POST", _, _) {{
status_code = 204;
headers = [];
body = "";
Expand Down Expand Up @@ -117,4 +128,23 @@ actor HttpCounter {
}};
}
};

public query func http_streaming(token : Token) : async StreamingCallbackHttpResponse {
switch (token.arbitrary_data) {
case "start" {{
body = Text.encodeUtf8(" is ");
token = ?{arbitrary_data = "next"};
}};
case "next" {{
body = Text.encodeUtf8(Nat.toText(counter));
token = ?{arbitrary_data = "last"};

}};
case "last" {{
body = Text.encodeUtf8(" streaming\n");
token = null;
}};
case _ { Prelude.unreachable() };
}
};
};

0 comments on commit d61e0cb

Please sign in to comment.