-module(discord_gleam@http@request). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/discord_gleam/http/request.gleam"). -export([new/2, new_with_body/3, new_auth/3, new_auth_with_body/4, new_auth_with_header/4, extract_ratelimit_error/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(" This module contains functions to create http requests to discord\n"). -file("src/discord_gleam/http/request.gleam", 11). ?DOC(" Create a base request to discord\n"). -spec new(gleam@http:method(), binary()) -> gleam@http@request:request(binary()). new(Method, Path) -> _pipe = gleam@http@request:new(), _pipe@1 = gleam@http@request:set_method(_pipe, Method), _pipe@2 = gleam@http@request:set_host(_pipe@1, <<"discord.com"/utf8>>), _pipe@3 = gleam@http@request:set_path( _pipe@2, <<"/api/v10"/utf8, Path/binary>> ), _pipe@4 = gleam@http@request:prepend_header( _pipe@3, <<"accept"/utf8>>, <<"application/json"/utf8>> ), gleam@http@request:prepend_header( _pipe@4, <<"User-Agent"/utf8>>, <<"DiscordBot (https://github.com/cyteon/discord_gleam, 3.0.0)"/utf8>> ). -file("src/discord_gleam/http/request.gleam", 24). ?DOC(" Create an unauthenticated request, with a body\n"). -spec new_with_body(gleam@http:method(), binary(), binary()) -> gleam@http@request:request(binary()). new_with_body(Method, Path, Data) -> _pipe = new(Method, Path), _pipe@1 = gleam@http@request:set_body(_pipe, Data), gleam@http@request:prepend_header( _pipe@1, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ). -file("src/discord_gleam/http/request.gleam", 35). ?DOC(" Create an authenticated request\n"). -spec new_auth(gleam@http:method(), binary(), binary()) -> gleam@http@request:request(binary()). new_auth(Method, Path, Token) -> _pipe = new(Method, Path), gleam@http@request:prepend_header( _pipe, <<"Authorization"/utf8>>, <<"Bot "/utf8, Token/binary>> ). -file("src/discord_gleam/http/request.gleam", 45). ?DOC(" Create an authenticated request, with a body\n"). -spec new_auth_with_body(gleam@http:method(), binary(), binary(), binary()) -> gleam@http@request:request(binary()). new_auth_with_body(Method, Path, Token, Data) -> _pipe = new(Method, Path), _pipe@1 = gleam@http@request:prepend_header( _pipe, <<"Authorization"/utf8>>, <<"Bot "/utf8, Token/binary>> ), _pipe@2 = gleam@http@request:set_body(_pipe@1, Data), gleam@http@request:prepend_header( _pipe@2, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ). -file("src/discord_gleam/http/request.gleam", 58). ?DOC(" Create an authenticated request with a custom header\n"). -spec new_auth_with_header( gleam@http:method(), binary(), binary(), {binary(), binary()} ) -> gleam@http@request:request(binary()). new_auth_with_header(Method, Path, Token, Header) -> _pipe = new_auth(Method, Path, Token), gleam@http@request:set_header( _pipe, erlang:element(1, Header), erlang:element(2, Header) ). -file("src/discord_gleam/http/request.gleam", 69). ?DOC(" Used to exctract the 429 error from a request\n"). -spec extract_ratelimit_error(gleam@http@response:response(binary())) -> discord_gleam@internal@error:discord_error(). extract_ratelimit_error(Resp) -> Global = case gleam@list:key_find( erlang:element(3, Resp), <<"x-ratelimit-global"/utf8>> ) of {ok, _} -> true; {error, _} -> false end, case gleam@list:key_find(erlang:element(3, Resp), <<"retry-after"/utf8>>) of {ok, Retry_after} -> case gleam_stdlib:parse_float(Retry_after) of {ok, Retry_after@1} -> {ratelimit_error, Retry_after@1, Global}; {error, _} -> {api_error, erlang:element(2, Resp), erlang:element(4, Resp)} end; {error, _} -> {api_error, erlang:element(2, Resp), erlang:element(4, Resp)} end.