@@ -1,59 +1,30 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveFoldable #-}
3−{-# LANGUAGE DeriveFunctor #-}
4−{-# LANGUAGE DeriveTraversable #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE TemplateHaskell #-}
72{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TemplateHaskell #-}
3+{-# LANGUAGE OverloadedStrings #-}
94module Network.API.Mandrill.Types where
105
11−import Control.Applicative
12−import Control.Monad (mzero)
6+import Network.API.Mandrill.Utils
137import Data.Char
14−import Data.Maybe
158import Data.Time
16−import Lens.Micro.TH (makeLenses)
17−import Network.API.Mandrill.Utils
18−import Test.QuickCheck
19−import Text.Email.Validate
20−#if MIN_VERSION_time(1,5,0)
21−import Data.Time.Format (TimeLocale, defaultTimeLocale)
22−#else
23−import System.Locale (TimeLocale, defaultTimeLocale)
24−#endif
9+import qualified Data.Text as T
10+import qualified Data.Text.Lazy as TL
11+import Control.Lens
12+import Control.Monad
13+import Data.Monoid
2514import Data.Aeson
26−import Data.Aeson.TH
2715import Data.Aeson.Types
28−import qualified Data.ByteString as B
29−import qualified Data.ByteString.Base64 as Base64
30−#if !MIN_VERSION_base(4,8,0)
31−import Data.Foldable
32−import Data.Traversable
33−#endif
34−import qualified Data.HashMap.Strict as H
35−import Data.Monoid
36−import qualified Data.Text as T
37−import qualified Data.Text.Encoding as TL
38−import qualified Data.Text.Lazy as TL
39−import qualified Text.Blaze.Html as Blaze
16+import Data.Aeson.TH
17+import qualified Text.Blaze.Html as Blaze
4018import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
4219
43−timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
44−#if MIN_VERSION_time(1,5,0)
45−timeParse = parseTimeM True
46−#else
47−timeParse = parseTime
48−#endif
4920
5021--------------------------------------------------------------------------------
5122data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
23+ _merr_status :: !T.Text
24+ , _merr_code :: !Int
25+ , _merr_name :: !T.Text
5526 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
27+ } deriving Show
5728
5829makeLenses ''MandrillError
5930deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +49,6 @@ data MandrillRejectReason = RR_HardBounce
7849 | RR_InvalidSender
7950 | RR_Invalid
8051 | RR_TestModeLimit
81− | RR_Unsigned
8252 | RR_Rule deriving Show
8353
8454deriveJSON defaultOptions {
@@ -86,21 +56,36 @@ deriveJSON defaultOptions {
8656 } ''MandrillRejectReason
8757
8858
59+--------------------------------------------------------------------------------
60+data MandrillResult = MandrillResult {
61+ _mres_email :: !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"
67+ , _mres__id :: !T.Text
68+ -- ^ The message's unique id
69+ } deriving Show
70+
71+makeLenses ''MandrillResult
72+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillResult
73+
8974--------------------------------------------------------------------------------
9075-- | The main datatypes which models the response from the Mandrill API,
9176-- which can be either a success or a failure.
92−data MandrillResponse k =
93− MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
77+data MandrillResponse =
78+ MandrillSuccess [MandrillResult]
79+ | MandrillFailure MandrillError deriving Show
9580
96−instance FromJSON k => FromJSON (MandrillResponse k) where
97− parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
81+instance FromJSON MandrillResponse where
82+ parseJSON a@(Array _) = case (parseMaybe parseJSON a) :: Maybe [MandrillResult] of
9883 Just r -> return $ MandrillSuccess r
99− Nothing -> do
100− -- try to parse it as an error
101− case (parseMaybe parseJSON v) :: Maybe MandrillError of
102− Just e -> return $ MandrillFailure e
103− Nothing -> fail $ show v <> " is neither a MandrillSuccess or a MandrillError."
84+ Nothing -> fail $ show a <> " is not a valid MandrillResult"
85+ parseJSON o@(Object _) = case (parseMaybe parseJSON o) :: Maybe MandrillError of
86+ Just e -> return $ MandrillFailure e
87+ Nothing -> fail $ show o <> " is not a valid MandrillError"
88+ parseJSON v = typeMismatch "Expected an Object or an Array for MandrillResponse" v
10489
10590
10691--------------------------------------------------------------------------------
@@ -109,27 +94,14 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
10994deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
11095
11196
112−--------------------------------------------------------------------------------
113−newtype MandrillEmail = MandrillEmail EmailAddress deriving Show
114−
115−instance ToJSON MandrillEmail where
116− toJSON (MandrillEmail e) = String . TL.decodeUtf8 . toByteString $ e
117−
118−instance FromJSON MandrillEmail where
119− parseJSON (String s) = case validate (TL.encodeUtf8 s) of
120− Left err -> fail err
121− Right v -> return . MandrillEmail $ v
122− parseJSON o = typeMismatch "Expecting a String for MandrillEmail." o
123−
124−
12597--------------------------------------------------------------------------------
12698-- | An array of recipient information.
12799data MandrillRecipient = MandrillRecipient {
128− _mrec_email :: MandrillEmail
100+ _mrec_email :: !T.Text
129101 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
102+ , _mrec_name :: Maybe T.Text
131103 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
104+ , _mrec_type :: Maybe MandrillRecipientTag
133105 -- ^ The header type to use for the recipient.
134106 -- defaults to "to" if not provided
135107 } deriving Show
@@ -137,35 +109,15 @@ data MandrillRecipient = MandrillRecipient {
137109makeLenses ''MandrillRecipient
138110deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
139111
140−newRecipient :: EmailAddress -> MandrillRecipient
141−newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
142−
143−instance Arbitrary MandrillRecipient where
144− arbitrary = pure MandrillRecipient {
145− _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
146− , _mrec_name = Nothing
147− , _mrec_type = Nothing
148− }
149112
150113--------------------------------------------------------------------------------
151114newtype MandrillHtml = MandrillHtml Blaze.Html
152115
153−unsafeMkMandrillHtml :: T.Text -> MandrillHtml
154−unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
155−
156−-- This might be slightly hairy because it violates
157−-- the nice encapsulation that newtypes offer.
158−mkMandrillHtml :: Blaze.Html -> MandrillHtml
159−mkMandrillHtml = MandrillHtml
160−
161−#if MIN_VERSION_base(4,11,0)
162−instance Semigroup MandrillHtml where
163− MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164−#endif
116+mkMandrillHtml :: T.Text -> MandrillHtml
117+mkMandrillHtml = MandrillHtml . Blaze.toHtml
165118
166−instance Monoid MandrillHtml where
167− mempty = MandrillHtml mempty
168− mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
119+mkMandrillHtml' :: Blaze.Html -> MandrillHtml
120+mkMandrillHtml' = MandrillHtml
169121
170122instance Show MandrillHtml where
171123 show (MandrillHtml h) = show $ Blaze.renderHtml h
@@ -174,221 +126,48 @@ instance ToJSON MandrillHtml where
174126 toJSON (MandrillHtml h) = String . TL.toStrict . Blaze.renderHtml $ h
175127
176128instance FromJSON MandrillHtml where
177− parseJSON (String h) = return $ MandrillHtml (Blaze.preEscapedToHtml h)
129+ parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
178130 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
179131
180−instance Arbitrary MandrillHtml where
181− arbitrary = pure $ mkMandrillHtml "<p><b>FooBar</b></p>"
182−
183−--------------------------------------------------------------------------------
184−type MandrillTags = T.Text
185−
186−
187−--------------------------------------------------------------------------------
188−type MandrillHeaders = Object
189−
190−
191−--------------------------------------------------------------------------------
192−
193−data MergeVar = MergeVar {
194− _mv_name :: !T.Text
195− , _mv_content :: Value
196− } deriving Show
197−
198−makeLenses ''MergeVar
199−deriveJSON defaultOptions { fieldLabelModifier = drop 4 } ''MergeVar
200−
201−--------------------------------------------------------------------------------
202−data MandrillMergeVars = MandrillMergeVars {
203− _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
205− } deriving Show
206−
207−makeLenses ''MandrillMergeVars
208−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209−
210−--------------------------------------------------------------------------------
211−data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
214− } deriving Show
215−
216−makeLenses ''MandrillMetadata
217−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
218−
219−
220−data Base64ByteString =
221− EncodedB64BS B.ByteString
222− -- ^ An already-encoded Base64 ByteString.
223− | PlainBS B.ByteString
224− -- ^ A plain Base64 ByteString which requires encoding.
225− deriving Show
226−
227−instance ToJSON Base64ByteString where
228− toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229− toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
230−
231−instance FromJSON Base64ByteString where
232− parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
233− parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
234−
235−--------------------------------------------------------------------------------
236−data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
239− -- ^ [for images] the Content ID of the image
240− -- - use <img src="cid:THIS_VALUE"> to reference the image
241− -- in your HTML content
242− , _mwct_content :: !Base64ByteString
243− } deriving Show
244−
245−makeLenses ''MandrillWebContent
246−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
247132
248133--------------------------------------------------------------------------------
249134-- | The information on the message to send
250135data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
136+ _mmsg_html :: MandrillHtml
252137 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
138+ , _mmsg_text :: Maybe T.Text
254139 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
140+ , _mmsg_subject :: !T.Text
256141 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
142+ , _mmsg_from_email :: !T.Text
258143 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
144+ , _mmsg_from_name :: Maybe T.Text
260145 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
146+ , _mmsg_to :: [MandrillRecipient]
262147 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
264− -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
266− -- ^ whether or not this message is important, and should be delivered ahead
267− -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
269− -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
271− -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
273− -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
275− -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
277− -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278− -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
280− -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
282− -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
284− -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
286− -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
288− -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
289− , _mmsg_signing_domain :: Maybe Bool
290− -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291− -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
293− -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
295− -- ^ whether to evaluate merge tags in the message.
296− -- Will automatically be set to true if either merge_vars
297− -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
299− -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
301− -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
303− -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304− -- though we only store the first 100 we see, so this should not be unique
305− -- or change frequently. Tags should be 50 characters or less.
306− -- Any tags starting with an underscore are reserved for internal use
307− -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
309− -- ^ the unique id of a subaccount for this message
310− -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
312− -- ^ an array of strings indicating for which any matching URLs
313− -- will automatically have Google Analytics parameters appended
314− -- to their query string automatically.
315− , _mmsg_google_analytics_campaign :: Maybe T.Text
316− -- ^ optional string indicating the value to set for the utm_campaign
317− -- tracking parameter. If this isn't provided the email's from address
318− -- will be used instead.
319− , _mmsg_metadata :: Object
320− -- ^ metadata an associative array of user metadata. Mandrill will store
321− -- this metadata and make it available for retrieval.
322− -- In addition, you can select up to 10 metadata fields to index
323− -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
325− -- ^ Per-recipient metadata that will override the global values
326− -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
328− -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
330− -- ^ an array of embedded images to add to the message
331148 } deriving Show
332149
333150makeLenses ''MandrillMessage
334151deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
335152
336−instance Arbitrary MandrillMessage where
337− arbitrary = MandrillMessage <$> arbitrary
338− <*> pure Nothing
339− <*> pure "Test Subject"
340− <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
341− <*> pure Nothing
342− <*> resize 2 arbitrary
343− <*> pure H.empty
344− <*> pure Nothing
345− <*> pure Nothing
346− <*> pure Nothing
347− <*> pure Nothing
348− <*> pure Nothing
349− <*> pure Nothing
350− <*> pure Nothing
351− <*> pure Nothing
352− <*> pure Nothing
353− <*> pure Nothing
354− <*> pure Nothing
355− <*> pure Nothing
356− <*> pure Nothing
357− <*> pure Nothing
358− <*> pure []
359− <*> pure []
360− <*> pure []
361− <*> pure Nothing
362− <*> pure []
363− <*> pure Nothing
364− <*> pure H.empty
365− <*> pure []
366− <*> pure []
367− <*> pure []
368153
369154--------------------------------------------------------------------------------
370−-- | Key value pair for replacing content in templates via 'Editable Regions'
371−data MandrillTemplateContent = MandrillTemplateContent {
372− _mtc_name :: T.Text
373− , _mtc_content :: T.Text
374− } deriving Show
155+-- * Send payloads
156+--------------------------------------------------------------------------------
375157
376−makeLenses ''MandrillTemplateContent
377−deriveJSON defaultOptions { fieldLabelModifier = drop 5 } ''MandrillTemplateContent
378158
379159--------------------------------------------------------------------------------
380160type MandrillKey = T.Text
381−type MandrillTemplate = T.Text
382161
383−newtype MandrillDate = MandrillDate {
384− fromMandrillDate :: UTCTime
385− } deriving Show
386162
387−instance ToJSON MandrillDate where
388− toJSON = toJSON . fromMandrillDate
163+--------------------------------------------------------------------------------
164+data MandrillSendRq = MandrillSendRq {
165+ _msrq_key :: MandrillKey
166+ , _msrq_message :: MandrillMessage
167+ , _msrq_async :: Maybe Bool
168+ , _msrq_ip_pool :: Maybe T.Text
169+ , _msrq_send_at :: Maybe UTCTime
170+ } deriving Show
389171
390−instance FromJSON MandrillDate where
391− parseJSON = withText "MandrillDate" $ \t ->
392− case timeParse defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
393− Just d -> pure $ MandrillDate d
394− _ -> fail "could not parse Mandrill date"
172+makeLenses ''MandrillSendRq
173+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillSendRq