Compare commits

...

2 Commits

Author SHA1 Message Date
Jesse Brault
9ffcf68695 Some more http sketching. 2025-01-14 14:31:09 -06:00
Jesse Brault
114a16e1e8 More http sketching. 2025-01-14 13:55:16 -06:00

View File

@ -26,3 +26,55 @@ fn main = jsonClient(
get '/greeting'
map || ( it.body.greeting )
fold println
pub int HttpReqOpts {
headers: Map<String, Any>
}
pub int GetOpts : HttpReqOpts
pub int HttpClient {
baseUrl: String
fn get(path: String, opts?: GetOpts): IOEither<HttpRequestError, HttpResponse>
fn post(path: String, body: Any, opts: PostOpts): IOEither<HttpRequestError, HttpResponse>
}
pub int JsonClient : HttpClient
pub int JsonClientOpts {
baseUrl: String
fn getAuth(#[self] client: JsonClient) -> HttpClientAuth
}
pub int HttpClientAuth = (reqBuilder: HttpRequest::Builder) -> Void
impl : JsonClient {
baseUrl: String
fld auth: HttpClientAuth
ctor (opts: JsonClientOpts) {
self.baseUrl = opts.baseUrl
self.auth = opts.getAuth(self)
}
impl fn get(path, opts) = IOEither::of || (
HttpRequest::builder() tap || {
url = baseUrl + path
method = 'GET'
headers = opts?.headers ?: [:]
cookies = opts?.cookies ?: [:]
auth(self)
}()
)
}
pub fn jsonClient(opts: JsonClientOpts): JsonClient = JsonClientImpl(opts)
pub fn bearerToken(tokenGetter: (#[self] client: JsonClient) -> IOEither<HttpRequestError, String>) {
|client: JsonClient| {
let token = tokenGetter.hydrate(self: client)().unwrap()
|reqBuilder: HttpRequest::Builder| {
reqBuilder.headers['Authorization'] = "Bearer $token"
}
}
}