Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/body_bytes.roc
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request
import http.Response

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(POST)
.with_uri("https://example.com/messages")
.with_body("hello".to_utf8())

response = Response.from_status(201)
.with_body("created".to_utf8())

Stdout.line!("request body: ${Str.inspect(request.body())}")?
echo!("request body: ${Str.inspect(request.body())}\n")

Stdout.line!("response body: ${Str.inspect(response.body())}")?
echo!("response body: ${Str.inspect(response.body())}\n")

Ok({})
}
7 changes: 2 additions & 5 deletions examples/custom_method.roc
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(Unknown("PROPFIND"))
.with_uri("https://dav.example.com/docs")

Stdout.line!("custom method: ${request.method_str()}")?
echo!("custom method: ${request.method_str()}\n")

Ok({})
}
7 changes: 2 additions & 5 deletions examples/headers.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request

Expand All @@ -17,8 +15,7 @@ ParsedHeaders : {
parse_headers : Str -> Try(ParsedHeaders, [BadHeader, MissingRequiredField(Str)])
parse_headers = |raw| Encoding.HttpHeader.parse(raw)

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(GET)
.with_uri("https://api.example.com/items")
.with_headers([
Expand All @@ -27,7 +24,7 @@ main! = |_args| {
{ name: "X-Trace-Id", value: "demo-456" },
])

Stdout.line!(Str.inspect(request.headers()))?
echo!("${Str.inspect(request.headers())}\n")

Ok({})
}
Expand Down
7 changes: 2 additions & 5 deletions examples/hello_request.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(GET)
.with_uri("https://example.com/widgets")

Stdout.line!("${request.method_str()} ${request.uri()}")?
echo!("${request.method_str()} ${request.uri()}\n")
Ok({})
}
7 changes: 2 additions & 5 deletions examples/mock_send.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request
import http.Response
Expand All @@ -18,14 +16,13 @@ mock_send = |request|
Response.from_status(405)
}

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(GET)
.with_uri("https://example.com/offline")

response = mock_send(request)

Stdout.line!("mock status: ${response.status().to_str()}")?
echo!("mock status: ${response.status().to_str()}\n")

Ok({})
}
11 changes: 4 additions & 7 deletions examples/response.roc
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Response

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
response = Response.from_status(404)
.add_header("Content-Type", "text/plain")
.with_body("not found".to_utf8())

Stdout.line!("status: ${response.status().to_str()}")?
Stdout.line!("headers: ${response.headers().len().to_str()}")?
Stdout.line!("body bytes: ${response.body().len().to_str()}")?
echo!("status: ${response.status().to_str()}\n")
echo!("headers: ${response.headers().len().to_str()}\n")
echo!("body bytes: ${response.body().len().to_str()}\n")

Ok({})
}
7 changes: 2 additions & 5 deletions examples/router.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request
import http.Response
Expand Down Expand Up @@ -111,15 +109,14 @@ route = |request|
_ => json_error(404, "not found")
}

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
request = Request.from_method(POST)
.with_uri("/widgets")
.with_body("{\"name\":\"washer\",\"quantity\":5}".to_utf8())

response = route(request)

Stdout.line!("route status: ${response.status().to_str()} ${body_str(response)}")?
echo!("route status: ${response.status().to_str()} ${body_str(response)}\n")

Ok({})
}
Expand Down
7 changes: 2 additions & 5 deletions examples/tests.roc
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Header
import http.Method
import http.Request
import http.Response

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
Stdout.line!("Run `roc test examples/tests.roc` to exercise the http package examples.")?
main! = |_| {
echo!("Run `roc test examples/tests.roc` to exercise the http package examples.\n")
Ok({})
}

Expand Down
9 changes: 3 additions & 6 deletions examples/timeouts.roc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
app [main!] {
pf: platform "https://github.com/lukewilliamboswell/roc-platform-template-zig/releases/download/1.0.0/AnZoxzoGPtSGQ15EQh6pBeeaHJ7aizP9MQhK81dES3Uq.tar.zst",
http: "../package/main.roc",
}

import pf.Stdout
import http.Method
import http.Request

Expand All @@ -14,14 +12,13 @@ timeout_to_str = |request|
TimeoutMilliseconds(ms) => "${ms.to_str()}ms"
}

main! : List(Str) => Try({}, [Exit(I32), StdoutErr(Str), ..])
main! = |_args| {
main! = |_| {
no_timeout = Request.from_method(GET)
.with_uri("https://example.com/stream")
bounded = no_timeout.with_timeout(TimeoutMilliseconds(1500))

Stdout.line!("default: ${timeout_to_str(no_timeout)}")?
Stdout.line!("bounded: ${timeout_to_str(bounded)}")?
echo!("default: ${timeout_to_str(no_timeout)}\n")
echo!("bounded: ${timeout_to_str(bounded)}\n")

Ok({})
}
Loading