From a4c4357b31a2cfb513c29a0724b985234e43bbdf Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Tue, 14 Jan 2025 11:02:34 -0600 Subject: [PATCH 1/4] Small change to string.dm. --- dm_std_lib/std/core/string.dm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dm_std_lib/std/core/string.dm b/dm_std_lib/std/core/string.dm index 6344807..c00552d 100644 --- a/dm_std_lib/std/core/string.dm +++ b/dm_std_lib/std/core/string.dm @@ -1,5 +1,8 @@ ns std::core -pub int String +pub int String { + bytes: Array +} -impl StringImpl(fld bytes: Array) : String +#[internal] +impl StringImpl(bytes) : String From f6c4d634ab026e400f038dd8bcd385d496f13bc2 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Tue, 14 Jan 2025 11:09:39 -0600 Subject: [PATCH 2/4] More curl sketching. --- sketching/curl.dms | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sketching/curl.dms b/sketching/curl.dms index 1b0519b..7ab2e94 100644 --- a/sketching/curl.dms +++ b/sketching/curl.dms @@ -13,3 +13,16 @@ fn main = jsonClient() map || ( it.body.accessToken ) fold println () + +# Better: main which returns IO (a Callable?) automatically calls the IO +# Also, JsonClient can "login" using various methods, such as a Bearer token + +use std::http::{jsonClient, bearerToken} + +fn main = jsonClient( + baseUrl: 'http:/localhost:1234', + auth: bearerToken || ( post('/login', username: 'test', password: 'test') map || ( it.body.accessToken ) ) +) + get '/greeting' + map || ( it.body.greeting ) + fold println From 114a16e1e86c163ddf7f6286b2894ea95ee18906 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Tue, 14 Jan 2025 13:55:16 -0600 Subject: [PATCH 3/4] More http sketching. --- sketching/curl.dms | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/sketching/curl.dms b/sketching/curl.dms index 7ab2e94..7e92147 100644 --- a/sketching/curl.dms +++ b/sketching/curl.dms @@ -26,3 +26,50 @@ fn main = jsonClient( get '/greeting' map || ( it.body.greeting ) fold println + +pub int HttpReqOpts { + headers: Map +} + +pub int GetOpts : HttpReqOpts + +pub int HttpClient { + baseUrl: String + fn get(path: String, opts?: GetOpts): IOEither + fn post(path: String, body: Any, opts: PostOpts): IOEither +} + +pub int JsonClient : HttpClient + +pub int JsonClientOpts { + baseUrl: String + auth: HttpClientAuth +} + +pub int HttpClientAuth = (reqBuilder: HttpRequest::Builder) -> Void + +impl : JsonClient { + + baseUrl: String + fld auth: HttpClientAuth + + ctor (opts: JsonClientOpts) { + self.{baseUrl, auth} = opts + } + + 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): HttpClientAuth { + // todo +} \ No newline at end of file From 9ffcf68695be31ac99182ee52e8098955dfefacb Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Tue, 14 Jan 2025 14:31:09 -0600 Subject: [PATCH 4/4] Some more http sketching. --- sketching/curl.dms | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sketching/curl.dms b/sketching/curl.dms index 7e92147..2ff17b8 100644 --- a/sketching/curl.dms +++ b/sketching/curl.dms @@ -43,18 +43,18 @@ pub int JsonClient : HttpClient pub int JsonClientOpts { baseUrl: String - auth: HttpClientAuth + 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, auth} = opts + self.baseUrl = opts.baseUrl + self.auth = opts.getAuth(self) } impl fn get(path, opts) = IOEither::of || ( @@ -70,6 +70,11 @@ impl : JsonClient { pub fn jsonClient(opts: JsonClientOpts): JsonClient = JsonClientImpl(opts) -pub fn bearerToken(tokenGetter: (#[self] client: JsonClient) -> IOEither): HttpClientAuth { - // todo +pub fn bearerToken(tokenGetter: (#[self] client: JsonClient) -> IOEither) { + |client: JsonClient| { + let token = tokenGetter.hydrate(self: client)().unwrap() + |reqBuilder: HttpRequest::Builder| { + reqBuilder.headers['Authorization'] = "Bearer $token" + } + } } \ No newline at end of file