-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.roc
97 lines (82 loc) · 3.01 KB
/
Request.roc
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
95
96
97
module [
Request,
Method,
from_method,
method,
method_str,
headers,
uri,
body,
timeout,
with_method,
with_headers,
add_header,
with_uri,
with_body,
with_timeout,
]
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
Method : [OPTIONS, GET, POST, PUT, DELETE, HEAD, TRACE, CONNECT, PATCH, Unknown Str]
Request := {
method : Method,
headers : List (Str, Str),
uri : Str,
body : List U8,
timeout_ms : [TimeoutMilliseconds U64, NoTimeout], # TODO change to Duration once that's in builtins
}
from_method : Method -> Request
from_method = \initial_method ->
@Request(
{
method: initial_method,
headers: [],
uri: "",
body: [],
timeout_ms: NoTimeout,
},
)
method : Request -> Method
method = \@Request(req) -> req.method
method_str : Request -> Str
method_str = \@Request(req) ->
when req.method is
OPTIONS -> "OPTIONS"
GET -> "GET"
POST -> "POST"
PUT -> "PUT"
DELETE -> "DELETE"
HEAD -> "HEAD"
TRACE -> "TRACE"
CONNECT -> "CONNECT"
PATCH -> "PATCH"
Unknown str -> str
headers : Request -> List (Str, Str)
headers = \@Request(req) -> req.headers
body : Request -> List U8
body = \@Request(req) -> req.body
uri : Request -> Str
uri = \@Request(req) -> req.uri
timeout : Request -> [TimeoutMilliseconds U64, NoTimeout]
timeout = \@Request(req) -> req.timeout_ms
with_method : Request, Method -> Request
with_method = \@Request(req), new_method -> @Request({ req & method: new_method })
## Set the request's exact list of [HTTP headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields).
##
## The HTTP spec [allows](https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2) multiple headers to
## have the same name. However, some recipients may not interpret this the way you would hope
## they would, so it's generally best to make all the header names unique.
with_headers : Request, List (Str, Str) -> Request
with_headers = \@Request(req), new_headers -> @Request({ req & headers: new_headers })
## Add a header to the request's list of [HTTP headers](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields).
##
## The HTTP spec [allows](https://www.rfc-editor.org/rfc/rfc7230#section-3.2.2) multiple headers to
## have the same name. However, some recipients may not interpret this the way you would hope
## they would, so it's generally best to make all the header names unique.
add_header : Request, Str, Str -> Request
add_header = \@Request(req), name, value -> @Request({ req & headers: List.append(req.headers, (name, value)) })
with_uri : Request, Str -> Request
with_uri = \@Request(req), new_uri -> @Request({ req & uri: new_uri })
with_body : Request, List U8 -> Request
with_body = \@Request(req), new_body -> @Request({ req & body: new_body })
with_timeout : Request, [TimeoutMilliseconds U64, NoTimeout] -> Request
with_timeout = \@Request(req), timeout_ms -> @Request({ req & timeout_ms })