-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, http_request/0, glibsql_error/0]). -type statement() :: {execute_statement, binary()} | close_statement. -opaque http_request() :: {http_request, gleam@option:option(binary()), gleam@option:option(binary()), gleam@option:option(binary()), gleam@option:option(binary()), gleam@option:option(binary()), list(statement()), gleam@option:option(binary())}. -opaque glibsql_error() :: {missing_property_error, binary()}. -spec new_request() -> http_request(). new_request() -> {http_request, none, none, none, none, none, [], none}. -spec with_database(http_request(), binary()) -> http_request(). with_database(Request, Database) -> erlang:setelement(2, Request, {some, Database}). -spec with_organization(http_request(), binary()) -> http_request(). with_organization(Request, Organization) -> erlang:setelement(3, Request, {some, Organization}). -spec with_host(http_request(), binary()) -> http_request(). with_host(Request, Host) -> erlang:setelement(4, Request, {some, Host}). -spec with_path(http_request(), binary()) -> http_request(). with_path(Request, Path) -> erlang:setelement(5, Request, {some, Path}). -spec with_token(http_request(), binary()) -> http_request(). with_token(Request, Token) -> erlang:setelement(6, Request, {some, 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()) -> {ok, gleam@http@request:request(binary())} | {error, glibsql_error()}. build(Request) -> gleam@result:'try'( gleam@option:to_result( erlang:element(2, Request), {missing_property_error, <<"database"/utf8>>} ), fun(Database) -> gleam@result:'try'( gleam@option:to_result( erlang:element(3, Request), {missing_property_error, <<"organization"/utf8>>} ), fun(Organization) -> Host = gleam@option:unwrap( erlang:element(4, Request), <<"turso.io"/utf8>> ), Path = gleam@option:unwrap( erlang:element(5, Request), <<"/v2/pipeline"/utf8>> ), gleam@result:'try'( gleam@option:to_result( erlang:element(6, Request), {missing_property_error, <<"token"/utf8>>} ), fun(Token) -> {ok, begin _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, <<<<<<<>/binary, Organization/binary>>/binary, "."/utf8>>/binary, Host/binary>> ), _pipe@4 = gleam@http@request:set_path( _pipe@3, Path ), _pipe@5 = gleam@http@request:set_header( _pipe@4, <<"Authorization"/utf8>>, gleam@string:join( [<<"Bearer"/utf8>>, Token], <<" "/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.4.0"/utf8>> ), gleam@http@request:set_body( _pipe@8, build_json(Request) ) end} end ) end ) end ).