Packages

Glibsql is a library for interacting with a hosted libSQL database such as [Turso](https://turso.tech).

Current section

Files

Jump to
glibsql src glibsql@http.erl
Raw

src/glibsql@http.erl

-module(glibsql@http).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_request/0, with_database/2, with_organization/2, with_host/2, with_path/2, with_token/2, with_statement/2, clear_statements/1, with_baton/2, build/1]).
-export_type([statement/0, request/0]).
-type statement() :: {execute_statement, binary()} | close_statement.
-opaque request() :: {request,
binary(),
binary(),
binary(),
binary(),
binary(),
list(statement()),
gleam@option:option(binary())}.
-spec new_request() -> request().
new_request() ->
{request,
<<""/utf8>>,
<<""/utf8>>,
<<"turso.io"/utf8>>,
<<"/v2/pipeline"/utf8>>,
<<""/utf8>>,
[],
none}.
-spec with_database(request(), binary()) -> request().
with_database(Request, Database) ->
erlang:setelement(2, Request, Database).
-spec with_organization(request(), binary()) -> request().
with_organization(Request, Organization) ->
erlang:setelement(3, Request, Organization).
-spec with_host(request(), binary()) -> request().
with_host(Request, Host) ->
erlang:setelement(4, Request, Host).
-spec with_path(request(), binary()) -> request().
with_path(Request, Path) ->
erlang:setelement(5, Request, Path).
-spec with_token(request(), binary()) -> request().
with_token(Request, Token) ->
erlang:setelement(6, Request, Token).
-spec with_statement(request(), statement()) -> request().
with_statement(Request, Statement) ->
erlang:setelement(7, Request, [Statement | erlang:element(7, Request)]).
-spec clear_statements(request()) -> request().
clear_statements(Request) ->
erlang:setelement(7, Request, []).
-spec with_baton(request(), binary()) -> request().
with_baton(Request, Baton) ->
erlang:setelement(8, Request, {some, Baton}).
-spec build_json(request()) -> binary().
build_json(Req) ->
Statements = begin
_pipe = lists:reverse(erlang:element(7, Req)),
gleam@list:map(_pipe, fun(Stmt) -> case Stmt of
{execute_statement, Sql} ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"execute"/utf8>>)},
{<<"stmt"/utf8>>,
gleam@json:object(
[{<<"sql"/utf8>>,
gleam@json:string(Sql)}]
)}]
);
close_statement ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"close"/utf8>>)}]
)
end end)
end,
_pipe@1 = gleam@json:object(
[{<<"baton"/utf8>>,
gleam@json:nullable(
erlang:element(8, Req),
fun gleam@json:string/1
)},
{<<"requests"/utf8>>, gleam@json:preprocessed_array(Statements)}]
),
gleam@json:to_string(_pipe@1).
-spec build(request()) -> gleam@http@request:request(binary()).
build(Request) ->
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_scheme(_pipe@1, https),
_pipe@3 = gleam@http@request:set_host(
_pipe@2,
<<<<<<<<(erlang:element(2, Request))/binary, "-"/utf8>>/binary,
(erlang:element(3, Request))/binary>>/binary,
"."/utf8>>/binary,
(erlang:element(4, Request))/binary>>
),
_pipe@4 = gleam@http@request:set_path(_pipe@3, erlang:element(5, Request)),
_pipe@5 = gleam@http@request:set_header(
_pipe@4,
<<"Authorization"/utf8>>,
gleam@string:join(
[<<"Bearer"/utf8>>, erlang:element(6, Request)],
<<" "/utf8>>
)
),
_pipe@6 = gleam@http@request:set_header(
_pipe@5,
<<"Content-Type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@7 = gleam@http@request:set_header(
_pipe@6,
<<"Accept"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@8 = gleam@http@request:set_header(
_pipe@7,
<<"User-Agent"/utf8>>,
<<"glibsql/0.3.0"/utf8>>
),
gleam@http@request:set_body(_pipe@8, build_json(Request)).