Current section

34 Versions

Jump to

Compare versions

13 files changed
+156 additions
-186 deletions
  @@ -2,31 +2,6 @@
2 2
3 3 Types and functions for HTTP clients and servers!
4 4
5 - ## HTTP Service Example
6 -
7 - ```gleam
8 - import gleam/http/elli
9 - import gleam/http/response.{type Response}
10 - import gleam/http/request.{type Request}
11 - import gleam/bytes_tree.{type BytesTree}
12 -
13 - // Define a HTTP service
14 - //
15 - pub fn my_service(_request: Request(t)) -> Response(BytesTree) {
16 - let body = bytes_tree.from_string("Hello, world!")
17 -
18 - response.new(200)
19 - |> response.prepend_header("made-with", "Gleam")
20 - |> response.set_body(body)
21 - }
22 -
23 - // Start it on port 3000 using the Elli web server
24 - //
25 - pub fn main() {
26 - elli.become(my_service, on_port: 3000)
27 - }
28 - ```
29 -
30 5 ## Server adapters
31 6
32 7 In the example above the Elli Erlang web server is used to run the Gleam HTTP
  @@ -1,5 +1,5 @@
1 1 name = "gleam_http"
2 - version = "4.0.0"
2 + version = "4.1.0"
3 3 licences = ["Apache-2.0"]
4 4 description = "Types and functions for Gleam HTTP clients and servers"
5 5 gleam = ">= 1.0.0"
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"gleam_http">>}.
2 2 {<<"app">>, <<"gleam_http">>}.
3 - {<<"version">>, <<"4.0.0">>}.
3 + {<<"version">>, <<"4.1.0">>}.
4 4 {<<"description">>, <<"Types and functions for Gleam HTTP clients and servers"/utf8>>}.
5 5 {<<"licenses">>, [<<"Apache-2.0">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -105,8 +105,8 @@ pub fn parse(cookie_string: String) -> List(#(String, String)) {
105 105 Ok(#(key, value)) -> {
106 106 let key = string.trim(key)
107 107 let value = string.trim(value)
108 - use _ <- result.then(check_token(key))
109 - use _ <- result.then(check_token(value))
108 + use _ <- result.try(check_token(key))
109 + use _ <- result.try(check_token(value))
110 110 Ok(#(key, value))
111 111 }
112 112 Error(Nil) -> Error(Nil)
  @@ -10,7 +10,7 @@ import gleam/uri.{type Uri, Uri}
10 10 ///
11 11 /// The body of the request is parameterised. The HTTP server or client you are
12 12 /// using will have a particular set of types it supports for the body.
13 - ///
13 + ///
14 14 pub type Request(body) {
15 15 Request(
16 16 method: Method,
  @@ -42,12 +42,12 @@ pub fn to_uri(request: Request(a)) -> Uri {
42 42 /// Construct a request from a URI.
43 43 ///
44 44 pub fn from_uri(uri: Uri) -> Result(Request(String), Nil) {
45 - use scheme <- result.then(
45 + use scheme <- result.try(
46 46 uri.scheme
47 47 |> option.unwrap("")
48 48 |> http.scheme_from_string,
49 49 )
50 - use host <- result.then(
50 + use host <- result.try(
51 51 uri.host
52 52 |> option.to_result(Nil),
53 53 )
  @@ -216,7 +216,7 @@ pub fn new() -> Request(String) {
216 216 pub fn to(url: String) -> Result(Request(String), Nil) {
217 217 url
218 218 |> uri.parse
219 - |> result.then(from_uri)
219 + |> result.try(from_uri)
220 220 }
221 221
222 222 /// Set the scheme (protocol) of the request.
Loading more files…