Packages

A wrapper for ZeptoMail's transactional email API

Current section

Files

Jump to
zeptomail src zeptomail.erl
Raw

src/zeptomail.erl

-module(zeptomail).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/zeptomail.gleam").
-export([email_request/2, decode_email_response/1]).
-export_type([email/0, addressee/0, body/0, api_data/0, api_datum/0, api_error/0, api_error_detail/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,
addressee(),
list(addressee()),
list(addressee()),
list(addressee()),
list(addressee()),
body(),
binary()}.
-type addressee() :: {addressee, binary(), binary()}.
-type body() :: {text_body, binary()} | {html_body, binary()}.
-type api_data() :: {api_data, binary(), binary(), binary(), list(api_datum())}.
-type api_datum() :: {api_datum, binary(), binary()}.
-type api_error() :: {api_error, binary(), binary(), list(api_error_detail())} |
{unexpected_response, gleam@json:decode_error()}.
-type api_error_detail() :: {api_error_detail,
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-file("src/zeptomail.gleam", 80).
-spec encode_addressee(addressee()) -> gleam@json:json().
encode_addressee(Addressee) ->
gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(erlang:element(2, Addressee))},
{<<"address"/utf8>>,
gleam@json:string(erlang:element(3, Addressee))}]
).
-file("src/zeptomail.gleam", 58).
-spec encode_email(email()) -> gleam@json:json().
encode_email(Email) ->
Bodykind = case erlang:element(7, Email) of
{text_body, _} ->
<<"textbody"/utf8>>;
{html_body, _} ->
<<"htmlbody"/utf8>>
end,
Addressee_array = fun(Addressees) ->
gleam@json:array(
Addressees,
fun(A) ->
gleam@json:object(
[{<<"email_address"/utf8>>, encode_addressee(A)}]
)
end
)
end,
gleam@json:object(
[{<<"from"/utf8>>, encode_addressee(erlang:element(2, Email))},
{<<"to"/utf8>>, Addressee_array(erlang:element(3, Email))},
{<<"cc"/utf8>>, Addressee_array(erlang:element(4, Email))},
{<<"bcc"/utf8>>, Addressee_array(erlang:element(5, Email))},
{<<"reply_to"/utf8>>,
gleam@json:array(
erlang:element(6, Email),
fun encode_addressee/1
)},
{<<"subject"/utf8>>, gleam@json:string(erlang:element(8, Email))},
{Bodykind,
gleam@json:string(erlang:element(2, erlang:element(7, Email)))}]
).
-file("src/zeptomail.gleam", 11).
?DOC(" Create a HTTP request to send an email via the ZeptoMail API\n").
-spec email_request(email(), binary()) -> gleam@http@request:request(binary()).
email_request(Email, Api_token) ->
Body = begin
_pipe = Email,
_pipe@1 = encode_email(_pipe),
gleam@json:to_string(_pipe@1)
end,
_pipe@2 = gleam@http@request:new(),
_pipe@3 = gleam@http@request:set_method(_pipe@2, post),
_pipe@4 = gleam@http@request:set_host(_pipe@3, <<"api.zeptomail.com"/utf8>>),
_pipe@5 = gleam@http@request:set_path(_pipe@4, <<"/v1.1/email"/utf8>>),
_pipe@6 = gleam@http@request:set_body(_pipe@5, Body),
gleam@http@request:prepend_header(
_pipe@6,
<<"authorization"/utf8>>,
Api_token
).
-file("src/zeptomail.gleam", 113).
-spec api_datum_decoder() -> gleam@dynamic@decode:decoder(api_datum()).
api_datum_decoder() ->
gleam@dynamic@decode:field(
<<"code"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Code) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:success({api_datum, Code, Message})
end
)
end
).
-file("src/zeptomail.gleam", 101).
-spec api_data_decoder() -> gleam@dynamic@decode:decoder(api_data()).
api_data_decoder() ->
gleam@dynamic@decode:field(
<<"object"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Object) ->
gleam@dynamic@decode:field(
<<"request_id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Request_id) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:field(
<<"data"/utf8>>,
gleam@dynamic@decode:list(api_datum_decoder()),
fun(Data) ->
gleam@dynamic@decode:success(
{api_data,
Object,
Request_id,
Message,
Data}
)
end
)
end
)
end
)
end
).
-file("src/zeptomail.gleam", 149).
-spec api_error_detail_decoder() -> gleam@dynamic@decode:decoder(api_error_detail()).
api_error_detail_decoder() ->
gleam@dynamic@decode:field(
<<"code"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Code) ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:optional_field(
<<"target"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Target) ->
gleam@dynamic@decode:optional_field(
<<"target_value"/utf8>>,
none,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Target_value) ->
gleam@dynamic@decode:success(
{api_error_detail,
Code,
Message,
Target,
Target_value}
)
end
)
end
)
end
)
end
).
-file("src/zeptomail.gleam", 129).
-spec api_error_decoder() -> gleam@dynamic@decode:decoder(api_error()).
api_error_decoder() ->
gleam@dynamic@decode:subfield(
[<<"error"/utf8>>, <<"code"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Code) ->
gleam@dynamic@decode:subfield(
[<<"error"/utf8>>, <<"message"/utf8>>],
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:subfield(
[<<"error"/utf8>>, <<"details"/utf8>>],
gleam@dynamic@decode:list(api_error_detail_decoder()),
fun(Details) ->
gleam@dynamic@decode:success(
{api_error, Code, Message, Details}
)
end
)
end
)
end
).
-file("src/zeptomail.gleam", 26).
?DOC(" Create a HTTP request to send an email via the ZeptoMail API\n").
-spec decode_email_response(gleam@http@response:response(binary())) -> {ok,
api_data()} |
{error, api_error()}.
decode_email_response(Response) ->
case (erlang:element(2, Response) >= 200) andalso (erlang:element(
2,
Response
)
< 300) of
true ->
_pipe = erlang:element(4, Response),
_pipe@1 = gleam@json:parse(_pipe, api_data_decoder()),
gleam@result:map_error(
_pipe@1,
fun(Field@0) -> {unexpected_response, Field@0} end
);
false ->
_pipe@2 = erlang:element(4, Response),
_pipe@3 = gleam@json:parse(_pipe@2, api_error_decoder()),
_pipe@4 = gleam@result:map_error(
_pipe@3,
fun(Field@0) -> {unexpected_response, Field@0} end
),
gleam@result:then(_pipe@4, fun(Field@0) -> {error, Field@0} end)
end.