-module(glibsql). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new_http_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, http_request/0]). -type statement() :: {execute_statement, binary()} | close_statement. -opaque http_request() :: {http_request, binary(), binary(), binary(), binary(), binary(), list(statement()), gleam@option:option(binary())}. -spec new_http_request() -> http_request(). new_http_request() -> {http_request, <<""/utf8>>, <<""/utf8>>, <<"turso.io"/utf8>>, <<"/v2/pipeline"/utf8>>, <<""/utf8>>, [], none}. -spec with_database(http_request(), binary()) -> http_request(). with_database(Request, Database) -> erlang:setelement(2, Request, Database). -spec with_organization(http_request(), binary()) -> http_request(). with_organization(Request, Organization) -> erlang:setelement(3, Request, Organization). -spec with_host(http_request(), binary()) -> http_request(). with_host(Request, Host) -> erlang:setelement(4, Request, Host). -spec with_path(http_request(), binary()) -> http_request(). with_path(Request, Path) -> erlang:setelement(5, Request, Path). -spec with_token(http_request(), binary()) -> http_request(). with_token(Request, Token) -> erlang:setelement(6, Request, Token). -spec with_statement(http_request(), statement()) -> http_request(). with_statement(Request, Statement) -> erlang:setelement(7, Request, [Statement | erlang:element(7, Request)]). -spec clear_statements(http_request()) -> http_request(). clear_statements(Request) -> erlang:setelement(7, Request, []). -spec with_baton(http_request(), binary()) -> http_request(). with_baton(Request, Baton) -> erlang:setelement(8, Request, {some, Baton}). -spec build_json(http_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(http_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.2.0"/utf8>> ), gleam@http@request:set_body(_pipe@8, build_json(Request)).