11defmodule CodeCorps.Emails.ExtendedAPI do
2- @ moduledoc """
2+ @ moduledoc ~S """
33 Serves as an extension of the SparkPost API provided by the elixir-sparkpost
44 library, for features the external package does not support yet
55
@@ -9,31 +9,41 @@ defmodule CodeCorps.Emails.ExtendedAPI do
99
1010 use HTTPoison.Base
1111
12+ alias SparkPost . { Transmission , Endpoint }
13+
1214 @ base_url "https://api.sparkpost.com/api/v1"
1315
16+ @ spec process_url ( String . t ) :: String . t
1417 def process_url ( url ) , do: @ base_url <> url
1518
19+ @ spec process_request_headers ( list ) :: list
1620 def process_request_headers ( headers ) do
1721 [
1822 { "Content-Type" , "application/json" } ,
1923 { "Authorization" , System . get_env ( "SPARKPOST_API_KEY" ) }
2024 ] ++ headers
2125 end
2226
27+ @ spec process_request_body ( String . t ) :: map
2328 def process_request_body ( body ) , do: body |> Poison . encode!
29+
30+ @ spec process_response_body ( String . t ) :: map
2431 def process_response_body ( body ) , do: body |> Poison . decode!
2532
33+ @ spec create_template ( map , list , list ) :: { :ok , HTTPoison.Response . t } | { :error , HTTPoison.Error . t }
2634 def create_template ( body \\ % { } , headers \\ [ ] , options \\ [ ] ) do
2735 start ( )
2836 post ( "/templates" , body , headers , options )
2937 end
3038
39+ @ spec update_template ( String . t , map , list , list ) :: { :ok , HTTPoison.Response . t } | { :error , HTTPoison.Error . t }
3140 def update_template ( id , body \\ % { } , headers \\ [ ] , options \\ [ ] ) do
3241 start ( )
3342 put ( "/templates/#{ id } " , body , headers , options )
3443 end
3544
36- def send_transmission ( content ) do
37- SparkPost.Transmission . send ( content )
45+ @ spec send_transmission ( % Transmission { } ) :: % Transmission.Response { } | % Endpoint.Error { }
46+ def send_transmission ( % Transmission { } = transmission ) do
47+ transmission |> SparkPost.Transmission . send
3848 end
3949end
0 commit comments