-
Notifications
You must be signed in to change notification settings - Fork 0
/
transparent_http.gleam
99 lines (92 loc) · 3.03 KB
/
transparent_http.gleam
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
98
99
// Copyright 2023 Harry Bairstow
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import gleam/http.{Get}
import gleam/http/request.{Request}
import gleam/http/response.{Response}
import gleam/option.{None, Option}
pub type TransparentHttpBuilder(body, error) {
TransparentHttpBuilder(
/// Function that is applied to the `base_request` before being passed
/// to the `req` function
setup_request: Option(fn(Request(body)) -> Request(body)),
/// Base request for contant values e.g. auth headers
base_request: Option(Request(body)),
/// Initial value for request body, e.g. `""`
initial_req_body: body,
/// Function that sends a request and gets a response
sender: fn(Request(body)) -> Result(Response(body), error),
)
}
pub type TransparentHttp(body, error) {
TransparentHttp(
setup_request: fn(Request(body)) -> Request(body),
base_request: Request(body),
sender: fn(Request(body)) -> Result(Response(body), error),
)
}
pub fn new_builder(
sender: fn(Request(body)) -> Result(Response(body), error),
initial_request_body: body,
) -> TransparentHttpBuilder(body, error) {
TransparentHttpBuilder(
setup_request: None,
base_request: None,
sender: sender,
initial_req_body: initial_request_body,
)
}
/// Creates a default TransparentHttp client
pub fn default(
sender: fn(Request(body)) -> Result(Response(body), error),
initial_request_body: body,
) -> TransparentHttp(body, error) {
new(new_builder(sender, initial_request_body))
}
/// Creates a new TransparentHttp client, taking overrides from the
/// TransparentHttpBuilder
pub fn new(
builder: TransparentHttpBuilder(body, error),
) -> TransparentHttp(body, error) {
TransparentHttp(
setup_request: option.unwrap(builder.setup_request, or: fn(r) { r }),
base_request: option.unwrap(
builder.base_request,
Request(
method: Get,
headers: [],
body: builder.initial_req_body,
scheme: http.Https,
host: "localhost",
port: option.None,
path: "",
query: option.None,
),
),
sender: builder.sender,
)
}
/// Sends a request via the client's `sender`, the flow is as follows:
/// 1. Take client's `base_request`
/// 2. Pass to the `setup_request` function
/// 3. Pass to the `request` parameter
/// 4. Return the reponse from `sender`
pub fn req(
client: TransparentHttp(body, error),
request: fn(Request(body)) -> Request(body),
) -> Result(Response(body), error) {
client.base_request
|> client.setup_request
|> request
|> client.sender
}