Repository navigation
Expand file tree
/
Copy pathhttp-client.roc
More file actions
94 lines (79 loc) · 3.1 KB
/
Copy pathhttp-client.roc
File metadata and controls
94 lines (79 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
## Send HTTP requests and handle UTF-8, JSON, and transport failures.
app [main!] {
http: "https://github.com/roc-lang/http/releases/download/1.0.0/6ZUwqYhCS8PU9Mo6MF7oV82ET2o7KYb57CLKDq4cq4sS.tar.zst",
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.24.0/AEjfyaMFFbh8FJrkkHJy68riVNPr3Qp6c6PawWQjBwMH.tar.zst",
}
import pf.OsStr
import pf.Http
import pf.Stdout
import http.Request
import http.Response
# To run this example, first start the test server in another terminal:
#
# cd ci/rust_http_server && cargo run --release
#
# then:
#
# roc build examples/http-client.roc
# ./examples/http-client
#
# The URLs below say `localhost` rather than `127.0.0.1` on purpose. An IP
# literal never reaches a resolver, so only a host name exercises the platform's
# name-resolution path; examples/http-simple.roc covers the IP literal.
main! : List(OsStr) => Try({}, _)
main! = |_args| run_demo!()
run_demo! : () => Try({}, _)
run_demo! = || {
utf8 = Http.get_utf8!("http://localhost:9000/utf8test") ? |err| GetUtf8Failed(err)
write_line!("I received '${utf8}' from the server.")?
request = Request.from_method(GET).with_uri("http://localhost:9000/utf8test")
response = Http.send!(request) ? |err| SendFailed(err)
status = U16.to_str(Response.status(response))
decoded : { foo : Str }
decoded = Http.get!("http://localhost:9000") ? |err| GetJsonFailed(err)
echo_request = Request.from_method(POST).with_uri("http://localhost:9000/echo-json")
echo_response = Http.send_json!(echo_request, { foo: "Hello Json!" }) ? |err| SendJsonFailed(err)
echoed : { foo : Str }
echoed = Http.decode_json_response(echo_response) ? |err| EchoedJsonDecodeFailed(err)
write_line!("The json I received was: { foo: \"${decoded.foo}\" }")?
write_line!("send! returned status ${status}.")?
write_line!("send_json! echoed: { foo: \"${echoed.foo}\" }.")?
reject_invalid_json!()?
reject_invalid_utf8!()?
reject_invalid_url!()?
Ok({})
}
reject_invalid_url! : () => Try({}, _)
reject_invalid_url! = || {
result = Http.send!(Request.from_method(GET).with_uri("ftp://example.com"))
match result {
Err(InvalidUrl(UnsupportedScheme("ftp"))) => write_line!("invalid request URL was rejected.")
Err(err) => Err(InvalidUrlRejectedWithWrongError(err))
Ok(_) => Err(InvalidUrlUnexpectedlySucceeded)
}
}
reject_invalid_json! : () => Try({}, _)
reject_invalid_json! = || {
result : Try({ foo : Str }, _)
result = Http.get!("http://localhost:9000/invalid-json")
match result {
Err(JsonErr(_)) => write_line!("invalid JSON was rejected.")
Err(err) => Err(InvalidJsonRejectedWithWrongError(err))
Ok(_) => Err(InvalidJsonUnexpectedlySucceeded)
}
}
reject_invalid_utf8! : () => Try({}, _)
reject_invalid_utf8! = || {
result = Http.get_utf8!("http://localhost:9000/invalid-utf8")
match result {
Err(BadBody(_)) => write_line!("invalid UTF-8 was rejected.")
Err(err) => Err(InvalidUtf8RejectedWithWrongError(err))
Ok(_) => Err(InvalidUtf8UnexpectedlySucceeded)
}
}
write_line! : Str => Try({}, _)
write_line! = |message|
match Stdout.line!(message) {
Ok(value) => Ok(value)
Err(StdoutErr(err)) => Err(StdoutErr(err))
}