-module(telega@client). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([set_fetch_client/2, set_max_retry_attempts/2, set_tg_api_url/2, get_api_url/1, new_post_request/3, new_get_request/3, fetch/2, new/1]). -export_type([telegram_client/0, telegram_api_request/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -opaque telegram_client() :: {telegram_client, binary(), integer(), binary(), fun((gleam@http@request:request(binary())) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()})}. -opaque telegram_api_request() :: {telegram_api_post_request, binary(), binary()} | {telegram_api_get_request, binary(), gleam@option:option(list({binary(), binary()}))}. -file("src/telega/client.gleam", 58). ?DOC(" Set the HTTP client to use.\n"). -spec set_fetch_client( telegram_client(), fun((gleam@http@request:request(binary())) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()}) ) -> telegram_client(). set_fetch_client(Client, Fetch_client) -> _record = Client, {telegram_client, erlang:element(2, _record), erlang:element(3, _record), erlang:element(4, _record), Fetch_client}. -file("src/telega/client.gleam", 62). -spec set_max_retry_attempts(telegram_client(), integer()) -> telegram_client(). set_max_retry_attempts(Client, Max_retry_attempts) -> _record = Client, {telegram_client, erlang:element(2, _record), Max_retry_attempts, erlang:element(4, _record), erlang:element(5, _record)}. -file("src/telega/client.gleam", 69). -spec set_tg_api_url(telegram_client(), binary()) -> telegram_client(). set_tg_api_url(Client, Tg_api_url) -> _record = Client, {telegram_client, erlang:element(2, _record), erlang:element(3, _record), Tg_api_url, erlang:element(5, _record)}. -file("src/telega/client.gleam", 73). -spec fetch_httpc_adapter(gleam@http@request:request(binary())) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()}. fetch_httpc_adapter(Req) -> _pipe = gleam@httpc:send(Req), gleam@result:map_error( _pipe, fun(Error) -> {fetch_error, gleam_stdlib:identity(Error)} end ). -file("src/telega/client.gleam", 143). -spec set_query( gleam@http@request:request(AMXV), gleam@option:option(list({binary(), binary()})) ) -> gleam@http@request:request(AMXV). set_query(Api_request, Query) -> case Query of none -> Api_request; {some, Query@1} -> gleam@http@request:set_query(Api_request, Query@1) end. -file("src/telega/client.gleam", 120). -spec api_to_request(telegram_api_request()) -> {ok, gleam@http@request:request(binary())} | {error, telega@error:telega_error()}. api_to_request(Api_request) -> _pipe@7 = case Api_request of {telegram_api_get_request, Url, Query} -> _pipe = gleam@http@request:to(Url), gleam@result:map(_pipe, fun(Req) -> _pipe@1 = Req, _pipe@2 = gleam@http@request:set_method(_pipe@1, get), set_query(_pipe@2, Query) end); {telegram_api_post_request, Url@1, Body} -> _pipe@3 = gleam@http@request:to(Url@1), gleam@result:map(_pipe@3, fun(Req@1) -> _pipe@4 = Req@1, _pipe@5 = gleam@http@request:set_body(_pipe@4, Body), _pipe@6 = gleam@http@request:set_method(_pipe@5, post), gleam@http@request:set_header( _pipe@6, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ) end) end, gleam@result:map_error(_pipe@7, fun(_) -> api_to_request_convert_error end). -file("src/telega/client.gleam", 150). -spec get_api_url(telegram_client()) -> binary(). get_api_url(Client) -> erlang:element(4, Client). -file("src/telega/client.gleam", 171). -spec build_url(telegram_client(), binary()) -> binary(). build_url(Client, Path) -> <<<<<<(erlang:element(4, Client))/binary, (erlang:element(2, Client))/binary>>/binary, "/"/utf8>>/binary, Path/binary>>. -file("src/telega/client.gleam", 159). -spec new_post_request(telegram_client(), binary(), binary()) -> telegram_api_request(). new_post_request(Client, Path, Body) -> {telegram_api_post_request, build_url(Client, Path), Body}. -file("src/telega/client.gleam", 163). -spec new_get_request( telegram_client(), binary(), gleam@option:option(list({binary(), binary()})) ) -> telegram_api_request(). new_get_request(Client, Path, Query) -> {telegram_api_get_request, build_url(Client, Path), Query}. -file("src/telega/client.gleam", 88). -spec send_with_retry( fun((gleam@http@request:request(binary())) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()}), gleam@http@request:request(binary()), integer() ) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()}. send_with_retry(Fetch_client, Api_request, Retries) -> Response = Fetch_client(Api_request), case Retries of 0 -> Response; _ -> case Response of {ok, Response@1} -> case erlang:element(2, Response@1) of 429 -> telega@internal@log:warn( <<"Telegram API throttling, HTTP 429 'Too Many Requests'"/utf8>> ), gleam_erlang_ffi:sleep(1000), send_with_retry( Fetch_client, Api_request, Retries - 1 ); _ -> {ok, Response@1} end; {error, _} -> gleam_erlang_ffi:sleep(1000), send_with_retry(Fetch_client, Api_request, Retries - 1) end end. -file("src/telega/client.gleam", 79). -spec fetch(telegram_api_request(), telegram_client()) -> {ok, gleam@http@response:response(binary())} | {error, telega@error:telega_error()}. fetch(Api_request, Client) -> gleam@result:'try'( api_to_request(Api_request), fun(Api_request@1) -> send_with_retry( erlang:element(5, Client), Api_request@1, erlang:element(3, Client) ) end ). -file("src/telega/client.gleam", 48). ?DOC(" Create a new Telegram client. It uses `httpc` as a default HTTP client.\n"). -spec new(binary()) -> telegram_client(). new(Token) -> {telegram_client, Token, 3, <<"https://api.telegram.org/bot"/utf8>>, fun fetch_httpc_adapter/1}.