@@ -3,17 +3,22 @@
33{-# LANGUAGE OverloadedStrings #-}
44module Network.API.Mandrill.Types where
55
6+import Network.API.Mandrill.Utils
7+import Data.Char
8+import Data.Time
69import qualified Data.Text as T
710import qualified Data.Text.Lazy as TL
8−import Control.Lens
9−import Control.Monad
10−import Data.Monoid
11−import Data.Aeson
12−import Data.Aeson.Types
13−import Data.Aeson.TH
11+import Control.Lens
12+import Control.Monad
13+import Data.Monoid
14+import Data.Aeson
15+import Data.Aeson.Types
16+import Data.Aeson.TH
1417import qualified Text.Blaze.Html as Blaze
1518import qualified Text.Blaze.Html.Renderer.Text as Blaze
1619
20+
21+--------------------------------------------------------------------------------
1722data MandrillError = MandrillError {
1823 _merr_status :: !T.Text
1924 , _merr_code :: !Int
@@ -24,16 +29,51 @@ data MandrillError = MandrillError {
2429makeLenses ''MandrillError
2530deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
2631
32+
33+--------------------------------------------------------------------------------
34+data MandrillEmailStatus = ES_Sent
35+ | ES_Queued
36+ | ES_Scheduled
37+ | ES_Rejected
38+ | ES_Invalid deriving Show
39+
40+deriveJSON defaultOptions { constructorTagModifier = map toLower . drop 3 } ''MandrillEmailStatus
41+
42+
43+--------------------------------------------------------------------------------
44+data MandrillRejectReason = RR_HardBounce
45+ | RR_SoftBounce
46+ | RR_Spam
47+ | RR_Unsub
48+ | RR_Custom
49+ | RR_InvalidSender
50+ | RR_Invalid
51+ | RR_TestModeLimit
52+ | RR_Rule deriving Show
53+
54+deriveJSON defaultOptions {
55+ constructorTagModifier = modRejectReason . drop 3
56+ } ''MandrillRejectReason
57+
58+
59+--------------------------------------------------------------------------------
2760data MandrillResult = MandrillResult {
2861 _mres_email :: !T.Text
29− , _mres_status :: !T.Text
62+ -- ^ The email address of the recipient
63+ , _mres_status :: MandrillEmailStatus
64+ -- ^ The sending status of the recipient
65+ , _mres_reject_reason :: Maybe MandrillRejectReason
66+ -- ^ The reason for the rejection if the recipient status is "rejected"
3067 , _mres__id :: !T.Text
31− , _mres_reject_reason :: Maybe T.Text
68+ -- ^ The message's unique id
3269 } deriving Show
3370
3471makeLenses ''MandrillResult
3572deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillResult
3673
74+--------------------------------------------------------------------------------
75+-- | The main datatypes which models the response from the Mandrill API,
76+-- which can be either a success or a failure.
3777data MandrillResponse =
3878 MandrillSuccess [MandrillResult]
3979 | MandrillFailure MandrillError deriving Show
@@ -47,20 +87,38 @@ instance FromJSON MandrillResponse where
4787 Nothing -> fail $ show o <> " is not a valid MandrillError"
4888 parseJSON v = typeMismatch "Expected an Object or an Array for MandrillResponse" v
4989
90+
91+--------------------------------------------------------------------------------
92+data MandrillRecipientTag = To | Cc | Bcc deriving Show
93+
94+deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
95+
96+
97+--------------------------------------------------------------------------------
98+-- | An array of recipient information.
5099data MandrillRecipient = MandrillRecipient {
51100 _mrec_email :: !T.Text
52− , _mrec_name :: !T.Text
53− , _mrec_type :: !T.Text
101+ -- ^ The email address of the recipient
102+ , _mrec_name :: Maybe T.Text
103+ -- ^ The optional display name to use for the recipient
104+ , _mrec_type :: Maybe MandrillRecipientTag
105+ -- ^ The header type to use for the recipient.
106+ -- defaults to "to" if not provided
54107 } deriving Show
55108
56109makeLenses ''MandrillRecipient
57110deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
58111
112+
113+--------------------------------------------------------------------------------
59114newtype MandrillHtml = MandrillHtml Blaze.Html
60115
61116mkMandrillHtml :: T.Text -> MandrillHtml
62117mkMandrillHtml = MandrillHtml . Blaze.toHtml
63118
119+mkMandrillHtml' :: Blaze.Html -> MandrillHtml
120+mkMandrillHtml' = MandrillHtml
121+
64122instance Show MandrillHtml where
65123 show (MandrillHtml h) = show $ Blaze.renderHtml h
66124
@@ -71,26 +129,44 @@ instance FromJSON MandrillHtml where
71129 parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
72130 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
73131
132+
133+--------------------------------------------------------------------------------
134+-- | The information on the message to send
74135data MandrillMessage = MandrillMessage {
75136 _mmsg_html :: MandrillHtml
76− , _mmsg_text :: !T.Text
137+ -- ^ The full HTML content to be sent
138+ , _mmsg_text :: Maybe T.Text
139+ -- ^ Optional full text content to be sent
77140 , _mmsg_subject :: !T.Text
141+ -- ^ The message subject
78142 , _mmsg_from_email :: !T.Text
79− , _mmsg_from_name :: !T.Text
143+ -- ^ The sender email address
144+ , _mmsg_from_name :: Maybe T.Text
145+ -- ^ Optional from name to be used
80146 , _mmsg_to :: [MandrillRecipient]
147+ -- ^ A list of recipient information
81148 } deriving Show
82149
83150makeLenses ''MandrillMessage
84151deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
85152
153+
154+--------------------------------------------------------------------------------
86155-- * Send payloads
156+--------------------------------------------------------------------------------
87157
158+
159+--------------------------------------------------------------------------------
88160type MandrillKey = T.Text
89161
162+
163+--------------------------------------------------------------------------------
90164data MandrillSendRq = MandrillSendRq {
91165 _msrq_key :: MandrillKey
92166 , _msrq_message :: MandrillMessage
93− , _msrq_async :: Bool
167+ , _msrq_async :: Maybe Bool
168+ , _msrq_ip_pool :: Maybe T.Text
169+ , _msrq_send_at :: Maybe UTCTime
94170 } deriving Show
95171
96172makeLenses ''MandrillSendRq