Current section
Files
Jump to
Current section
Files
src/gleesend@emails.erl
-module(gleesend@emails).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([with_bcc/2, with_cc/2, with_reply_to/2, with_html/2, with_text/2, create_email/4, to_request/1, to_response/2]).
-export_type([resend_email/0, send_email_error/0, success_response/0]).
-type resend_email() :: {resend_email,
gleesend:resend(),
binary(),
list(binary()),
binary(),
gleam@option:option(list(binary())),
gleam@option:option(list(binary())),
gleam@option:option(list(binary())),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type send_email_error() :: bad_request_error | parse_response_body_error.
-type success_response() :: {success_response, binary()}.
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 23).
-spec with_bcc(resend_email(), list(binary())) -> resend_email().
with_bcc(Resend, Bcc) ->
erlang:setelement(6, Resend, {some, Bcc}).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 27).
-spec with_cc(resend_email(), list(binary())) -> resend_email().
with_cc(Resend, Cc) ->
erlang:setelement(7, Resend, {some, Cc}).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 31).
-spec with_reply_to(resend_email(), list(binary())) -> resend_email().
with_reply_to(Resend, Reply_to) ->
erlang:setelement(8, Resend, {some, Reply_to}).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 35).
-spec with_html(resend_email(), binary()) -> resend_email().
with_html(Resend, Html) ->
erlang:setelement(9, Resend, {some, Html}).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 39).
-spec with_text(resend_email(), binary()) -> resend_email().
with_text(Resend, Text) ->
erlang:setelement(10, Resend, {some, Text}).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 43).
-spec create_email(gleesend:resend(), binary(), list(binary()), binary()) -> resend_email().
create_email(Client, From, To, Subject) ->
{resend_email, Client, From, To, Subject, none, none, none, none, none}.
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 72).
-spec to_request(resend_email()) -> gleam@http@request:request(binary()).
to_request(Email) ->
_assert_subject = gleam@http@request:to(
<<"https://api.resend.com/emails"/utf8>>
),
{ok, Request} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleesend/emails"/utf8>>,
function => <<"to_request"/utf8>>,
line => 73})
end,
Body = gleam@json:object(
gleam@list:concat(
[[{<<"from"/utf8>>, gleam@json:string(erlang:element(3, Email))},
{<<"to"/utf8>>,
gleam@json:array(
erlang:element(4, Email),
fun(A) -> gleam@json:string(A) end
)},
{<<"subject"/utf8>>,
gleam@json:string(erlang:element(5, Email))}],
case erlang:element(6, Email) of
{some, Bcc} ->
[{<<"bcc"/utf8>>,
gleam@json:array(
Bcc,
fun(A@1) -> gleam@json:string(A@1) end
)}];
none ->
[]
end,
case erlang:element(7, Email) of
{some, Cc} ->
[{<<"cc"/utf8>>,
gleam@json:array(
Cc,
fun(A@2) -> gleam@json:string(A@2) end
)}];
none ->
[]
end,
case erlang:element(8, Email) of
{some, Reply_to} ->
[{<<"reply_to"/utf8>>,
gleam@json:array(
Reply_to,
fun(A@3) -> gleam@json:string(A@3) end
)}];
none ->
[]
end,
case erlang:element(9, Email) of
{some, Html} ->
[{<<"html"/utf8>>, gleam@json:string(Html)}];
none ->
[]
end,
case erlang:element(10, Email) of
{some, Text} ->
[{<<"text"/utf8>>, gleam@json:string(Text)}];
none ->
[]
end]
)
),
_pipe = Request,
_pipe@1 = gleam@http@request:prepend_header(
_pipe,
<<"accept"/utf8>>,
<<"application/vnd.hmrc.1.0+json"/utf8>>
),
_pipe@2 = gleam@http@request:prepend_header(
_pipe@1,
<<"Content-Type"/utf8>>,
<<"application/json"/utf8>>
),
_pipe@3 = gleam@http@request:prepend_header(
_pipe@2,
<<"Authorization"/utf8>>,
<<"Bearer "/utf8, (erlang:element(2, erlang:element(2, Email)))/binary>>
),
_pipe@4 = gleam@http@request:set_method(_pipe@3, post),
gleam@http@request:set_body(
_pipe@4,
begin
_pipe@5 = Body,
gleam@json:to_string(_pipe@5)
end
).
-file("/home/keii/Coding/kirakira/gleesend/src/gleesend/emails.gleam", 120).
-spec to_response(binary(), integer()) -> {ok, success_response()} |
{error, send_email_error()}.
to_response(Body, Status) ->
Body_decoder = gleam@dynamic:decode1(
fun(Field@0) -> {success_response, Field@0} end,
gleam@dynamic:field(<<"id"/utf8>>, fun gleam@dynamic:string/1)
),
case Status of
200 ->
case gleam@json:decode(Body, Body_decoder) of
{ok, Body@1} ->
{ok, Body@1};
{error, _} ->
{error, parse_response_body_error}
end;
_ ->
{error, bad_request_error}
end.