From 2d42e6428895e4b8be1ee3a624711918725d8f97 Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Sat, 25 Jul 2026 23:32:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Begin=20sketching=20out=20an=20x?= =?UTF-8?q?rpc=20client=20module.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/at/xrpc.gleam | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/at/xrpc.gleam diff --git a/src/at/xrpc.gleam b/src/at/xrpc.gleam new file mode 100644 index 0000000..8effe6a --- /dev/null +++ b/src/at/xrpc.gleam @@ -0,0 +1,76 @@ +// IMPORTS --------------------------------------------------------------------- + +import at/nsid.{type Nsid} +import gleam/http.{Get, Https, Post} +import gleam/http/request.{type Request, Request} +import gleam/option.{type Option, None, Some} +import gleam/uri.{type Uri} + +// TYPES ----------------------------------------------------------------------- + +/// +/// +pub type Client { + Client(host: String, port: Option(Int)) +} + +/// +/// +pub type Error { + XrpcError(error: String, message: Option(String)) +} + +// CONSTRUCTORS ---------------------------------------------------------------- + +/// +/// +pub fn new(uri: Uri) -> Result(Client, Nil) { + case uri.host { + None -> Error(Nil) + Some(host) -> Ok(Client(host:, port: uri.port)) + } +} + +/// +/// +pub fn query( + client: Client, + path: Nsid, + parameters: List(#(String, String)), +) -> Request(String) { + Request( + method: Get, + headers: [], + body: "", + scheme: Https, + host: client.host, + port: client.port, + path: "/xrpc/" <> nsid.to_string(path), + query: case parameters { + [] -> option.None + _ -> option.Some(uri.query_to_string(parameters)) + }, + ) +} + +/// +/// +pub fn procedure( + client: Client, + path: Nsid, + parameters: List(#(String, String)), +) -> Request(String) { + Request( + method: Post, + headers: [], + body: "", + scheme: Https, + host: client.host, + port: client.port, + path: "/xrpx/" <> nsid.to_string(path), + query: case parameters { + [] -> option.None + _ -> option.Some(uri.query_to_string(parameters)) + }, + ) +} -- 2.51.2