Current section
Files
Jump to
Current section
Files
src/sendgriddle.erl
-module(sendgriddle).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/sendgriddle.gleam").
-export([mail_send_request/2, mail_send_response/1]).
-export_type([email/0, email_content/0, send_grid_error/0, error_object/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.
-type email() :: {email,
list(binary()),
binary(),
binary(),
binary(),
email_content()}.
-type email_content() :: {text_content, binary()} |
{rich_content, binary(), binary()}.
-type send_grid_error() :: {send_grid_error,
gleam@option:option(binary()),
list(error_object())} |
{unexpected_response_error, gleam@http@response:response(binary())}.
-type error_object() :: {error_object,
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-file("src/sendgriddle.gleam", 62).
?DOC(" A request to send email over SendGrid's v3 Web API.\n").
-spec mail_send_request(email(), binary()) -> gleam@http@request:request(binary()).
mail_send_request(Email, Api_key) ->
{email, To, Sender_email, Sender_name, Subject, Content} = Email,
Make_email = fun(Email@1) ->
gleam@json:object([{<<"email"/utf8>>, gleam@json:string(Email@1)}])
end,
Recipients = gleam@json:object(
[{<<"to"/utf8>>, gleam@json:array(To, Make_email)}]
),
Personalizations = gleam@json:preprocessed_array([Recipients]),
From = gleam@json:object(
[{<<"email"/utf8>>, gleam@json:string(Sender_email)},
{<<"name"/utf8>>, gleam@json:string(Sender_name)}]
),
Make_content = fun(Content@1, Content_type) ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(Content_type)},
{<<"value"/utf8>>, gleam@json:string(Content@1)}]
)
end,
Content@2 = case Content of
{text_content, Text} ->
gleam@json:preprocessed_array(
[Make_content(Text, <<"text/plain"/utf8>>)]
);
{rich_content, Html, Text@1} ->
gleam@json:preprocessed_array(
[Make_content(Text@1, <<"text/plain"/utf8>>),
Make_content(Html, <<"text/html"/utf8>>)]
)
end,
Body = gleam@json:object(
[{<<"personalizations"/utf8>>, Personalizations},
{<<"from"/utf8>>, From},
{<<"subject"/utf8>>, gleam@json:string(Subject)},
{<<"content"/utf8>>, Content@2}]
),
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_host(_pipe@1, <<"api.sendgrid.com"/utf8>>),
_pipe@3 = gleam@http@request:set_path(_pipe@2, <<"/v3/mail/send"/utf8>>),
_pipe@4 = gleam@http@request:set_body(_pipe@3, gleam@json:to_string(Body)),
_pipe@5 = gleam@http@request:prepend_header(
_pipe@4,
<<"authorization"/utf8>>,
<<"Bearer "/utf8, Api_key/binary>>
),
gleam@http@request:prepend_header(
_pipe@5,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
).
-file("src/sendgriddle.gleam", 146).
-spec field_decoder() -> gleam@dynamic@decode:decoder(gleam@option:option(binary())).
field_decoder() ->
gleam@dynamic@decode:map(
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Field_value) -> case Field_value of
{some, <<"null"/utf8>>} ->
none;
_ ->
Field_value
end end
).
-file("src/sendgriddle.gleam", 135).
-spec error_object_decoder() -> gleam@dynamic@decode:decoder(error_object()).
error_object_decoder() ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:optional_field(
<<"field"/utf8>>,
none,
field_decoder(),
fun(Field) ->
gleam@dynamic@decode:optional_field(
<<"help"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Help) ->
gleam@dynamic@decode:success(
{error_object, Message, Field, Help}
)
end
)
end
)
end
).
-file("src/sendgriddle.gleam", 125).
-spec sendgrid_error_decoder() -> gleam@dynamic@decode:decoder(send_grid_error()).
sendgrid_error_decoder() ->
gleam@dynamic@decode:optional_field(
<<"id"/utf8>>,
none,
gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Field@0) -> {some, Field@0} end
),
fun(Id) ->
gleam@dynamic@decode:optional_field(
<<"errors"/utf8>>,
[],
gleam@dynamic@decode:list(error_object_decoder()),
fun(Errors) ->
gleam@dynamic@decode:success({send_grid_error, Id, Errors})
end
)
end
).
-file("src/sendgriddle.gleam", 112).
?DOC(
" Parse a response to a mail-send request, to determine whether or not it\n"
" was successful.\n"
).
-spec mail_send_response(gleam@http@response:response(binary())) -> {ok, nil} |
{error, send_grid_error()}.
mail_send_response(Response) ->
case erlang:element(2, Response) of
N when (200 =< N) andalso (N =< 299) ->
{ok, nil};
_ ->
case gleam@json:parse(
erlang:element(4, Response),
sendgrid_error_decoder()
) of
{ok, Errors} ->
{error, Errors};
{error, _} ->
{error, {unexpected_response_error, Response}}
end
end.