Packages

A purely Gleam Discord library built from the ground up with stratus.

Current section

Files

Jump to
glyph src glyph@internal@network@rest.erl
Raw

src/glyph@internal@network@rest.erl

-module(glyph@internal@network@rest).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([set_bot_user_agent/2, set_authorization/3, new/3, set_content_type/2, get/3, post/3]).
-export_type([token_type/0, user_agent/0, rest_client/0]).
-type token_type() :: bearer | bot.
-type user_agent() :: {user_agent, binary(), binary()}.
-type rest_client() :: {rest_client, gleam@http@request:request(binary())}.
-spec token_type_to_string(token_type()) -> binary().
token_type_to_string(Token_type) ->
case Token_type of
bearer ->
<<"Bearer"/utf8>>;
bot ->
<<"Bot"/utf8>>
end.
-spec user_agent_to_string(user_agent()) -> binary().
user_agent_to_string(User_agent) ->
<<<<<<<<"DiscordBot("/utf8, (erlang:element(2, User_agent))/binary>>/binary,
", "/utf8>>/binary,
(erlang:element(3, User_agent))/binary>>/binary,
")"/utf8>>.
-spec set_bot_user_agent(gleam@http@request:request(binary()), user_agent()) -> gleam@http@request:request(binary()).
set_bot_user_agent(R, User_agent) ->
_pipe = R,
gleam@http@request:set_header(
_pipe,
<<"User-Agent"/utf8>>,
user_agent_to_string(User_agent)
).
-spec set_authorization(
gleam@http@request:request(binary()),
token_type(),
binary()
) -> gleam@http@request:request(binary()).
set_authorization(R, Token_type, Token) ->
_pipe = R,
gleam@http@request:set_header(
_pipe,
<<"Authorization"/utf8>>,
<<<<(token_type_to_string(Token_type))/binary, " "/utf8>>/binary,
Token/binary>>
).
-spec new(token_type(), binary(), user_agent()) -> rest_client().
new(Token_type, Token, User_agent) ->
Req = begin
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_host(_pipe, <<"discord.com"/utf8>>),
_pipe@2 = gleam@http@request:set_scheme(_pipe@1, https),
_pipe@3 = set_bot_user_agent(_pipe@2, User_agent),
set_authorization(_pipe@3, Token_type, Token)
end,
{rest_client, Req}.
-spec set_content_type(gleam@http@request:request(binary()), binary()) -> gleam@http@request:request(binary()).
set_content_type(R, Content_type) ->
_pipe = R,
gleam@http@request:set_header(_pipe, <<"content-type"/utf8>>, Content_type).
-spec send(rest_client(), gleam@http:method(), binary(), binary(), binary()) -> {ok,
gleam@http@response:response(binary())} |
{error, gleam@hackney:error()}.
send(Rest, Method, Endpoint, Content_type, Body) ->
_pipe = erlang:element(2, Rest),
_pipe@1 = gleam@http@request:set_method(_pipe, Method),
_pipe@2 = gleam@http@request:set_path(
_pipe@1,
<<<<"/api/v"/utf8, "10"/utf8>>/binary, Endpoint/binary>>
),
_pipe@3 = gleam@http@request:set_header(
_pipe@2,
<<"content-type"/utf8>>,
Content_type
),
_pipe@4 = gleam@http@request:set_body(_pipe@3, Body),
gleam@hackney:send(_pipe@4).
-spec get(rest_client(), binary(), binary()) -> {ok,
gleam@http@response:response(binary())} |
{error, gleam@hackney:error()}.
get(Rest, Endpoint, Body) ->
send(Rest, get, Endpoint, <<"application/json"/utf8>>, Body).
-spec post(rest_client(), binary(), binary()) -> {ok,
gleam@http@response:response(binary())} |
{error, gleam@hackney:error()}.
post(Rest, Endpoint, Body) ->
send(Rest, post, Endpoint, <<"application/json"/utf8>>, Body).