{-# LANGUAGE OverloadedStrings #-}
module Network.Api.Postmark.Core (
email,
emails,
emailWithTemplate,
request
) where
import qualified Data.ByteString.Lazy as BL
import Data.Aeson
import Data.Text.Encoding
import Network.Api.Support
import Network.Api.Postmark.Data
import Network.Api.Postmark.Request
import Network.Api.Postmark.Response
import Network.Api.Postmark.Settings
import Network.HTTP.Client.TLS
import Network.HTTP.Types
email :: Email -> PostmarkRequest' Sent
email e = PostmarkRequest POST "/email" $ setJson e
emails :: [Email] -> PostmarkRequest' [Sent]
emails es = PostmarkRequest POST "/email/batch" $ setJson es
emailWithTemplate :: EmailWithTemplate -> PostmarkRequest' Sent
emailWithTemplate ewt = PostmarkRequest POST "/email/withTemplate" $ setJson ewt
request :: PostmarkSettings -> PostmarkRequest e a -> IO (PostmarkResponse e a)
request settings (PostmarkRequest stdmethod url transform) =
runRequest tlsManagerSettings stdmethod (apiUrl settings <> url) (
addHeader ("Accept", "application/json") <>
addHeader ("X-Postmark-Server-Token", encodeUtf8 $ apiToken settings) <>
transform
) (basicResponder responder)
responder :: (FromJSON e, FromJSON a) => Int -> BL.ByteString -> PostmarkResponse e a
responder 200 body = parseBodyWith body (syntaxErr 200 body) (formatErr 200 body) PostmarkSuccess
responder 401 _ = PostmarkUnauthorized
responder 422 body = parseBodyWith body (syntaxErr 422 body) (formatErr 422 body) PostmarkFailure
responder 500 body = PostmarkUnexpected ServerError 500 (Just . toText $ body) Nothing
responder c body = PostmarkUnexpected UnexpectedResponseCode c (Just . toText $ body) Nothing