Current section
Files
Jump to
Current section
Files
src/dove.erl
-module(dove).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([connect/3, request/5]).
-export_type([method/0, request_option/1, body/0]).
-type method() :: get |
put |
post |
head |
patch |
trace |
delete |
connect |
options.
-type request_option(HNV) :: {body, body()} |
{headers, list({binary(), binary()})} |
{query_params, list({binary(), binary()})} |
{response_decoder,
fun((gleam@dynamic:dynamic_()) -> {ok, HNV} |
{error, list(gleam@dynamic:decode_error())})}.
-type body() :: {json, binary()} | {plain_text, binary()}.
-spec connect(binary(), integer(), integer()) -> {ok,
dove@client:connection(any())} |
{error, gleam@otp@actor:start_error()}.
connect(Host, Port, Timeout) ->
dove@client:connect(Host, Port, Timeout).
-spec get_query_params(list(request_option(any()))) -> list({binary(), binary()}).
get_query_params(Options) ->
case gleam@list:find_map(Options, fun(Opt) -> case Opt of
{query_params, Params} ->
{ok, Params};
_ ->
{error, nil}
end end) of
{ok, Params@1} ->
Params@1;
{error, nil} ->
[]
end.
-spec get_decoder(list(request_option(HOG))) -> gleam@option:option(fun((gleam@dynamic:dynamic_()) -> {ok,
HOG} |
{error, list(gleam@dynamic:decode_error())})).
get_decoder(Options) ->
case gleam@list:find_map(Options, fun(Opt) -> case Opt of
{response_decoder, Decoder} ->
{ok, Decoder};
_ ->
{error, nil}
end end) of
{ok, Decoder@1} ->
{some, Decoder@1};
{error, nil} ->
none
end.
-spec get_headers(list(request_option(any()))) -> list({binary(), binary()}).
get_headers(Options) ->
case gleam@list:find_map(Options, fun(Opt) -> case Opt of
{headers, Headers} ->
{ok, Headers};
_ ->
{error, nil}
end end) of
{ok, Headers@1} ->
Headers@1;
{error, nil} ->
[]
end.
-spec get_body(list(request_option(any()))) -> gleam@option:option(body()).
get_body(Options) ->
case gleam@list:find_map(Options, fun(Opt) -> case Opt of
{body, Body} ->
{ok, Body};
_ ->
{error, nil}
end end) of
{ok, Body@1} ->
{some, Body@1};
{error, nil} ->
none
end.
-spec request(
dove@client:connection(HNX),
method(),
binary(),
list(request_option(HNX)),
integer()
) -> {ok,
gleam@erlang@process:subject({ok, dove@response:response(HNX)} |
{error, dove@error:error()})} |
{error, dove@error:error()}.
request(Conn, Method, Path, Options, Timeout) ->
_assert_subject = gleam@list:key_find(
[{get, <<"GET"/utf8>>},
{put, <<"PUT"/utf8>>},
{post, <<"POST"/utf8>>},
{head, <<"HEAD"/utf8>>},
{patch, <<"PATCH"/utf8>>},
{trace, <<"TRACE"/utf8>>},
{delete, <<"DELETE"/utf8>>},
{connect, <<"CONNECT"/utf8>>},
{options, <<"OPTIONS"/utf8>>}],
Method
),
{ok, Method@1} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"dove"/utf8>>,
function => <<"request"/utf8>>,
line => 46})
end,
Query_params = get_query_params(Options),
Headers = get_headers(Options),
Body = get_body(Options),
Decoder = get_decoder(Options),
{Body@3, Headers@1} = case Body of
{some, {json, Body@1}} ->
{{some, Body@1},
gleam@list:append(
Headers,
[{<<"content-type"/utf8>>, <<"application/json"/utf8>>},
{<<"content-length"/utf8>>,
begin
_pipe = Body@1,
_pipe@1 = gleam@string:length(_pipe),
gleam@int:to_string(_pipe@1)
end}]
)};
{some, {plain_text, Body@2}} ->
{{some, Body@2},
gleam@list:append(
Headers,
[{<<"content-type"/utf8>>, <<"text/plain"/utf8>>},
{<<"content-length"/utf8>>,
begin
_pipe@2 = Body@2,
_pipe@3 = gleam@string:length(_pipe@2),
gleam@int:to_string(_pipe@3)
end}]
)};
none ->
{none, Headers}
end,
gleam@result:then(
dove@request:encode(
erlang:element(2, Conn),
Method@1,
Path,
Query_params,
Headers@1,
Body@3
),
fun(Request) ->
Subject = gleam@erlang@process:new_subject(),
gleam@erlang@process:send(
erlang:element(3, Conn),
{request, Request, Subject, Decoder, Timeout}
),
{ok, Subject}
end
).