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)) + }, + ) +}