@@ -1,36 +1,44 @@
1−{-# LANGUAGE TemplateHaskell #-}
2−{-# LANGUAGE CPP #-}
3−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveFoldable #-}
3+{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE DeriveTraversable #-}
5+{-# LANGUAGE OverloadedStrings #-}
6+{-# LANGUAGE RankNTypes #-}
47{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE OverloadedStrings #-}
8+{-# LANGUAGE TemplateHaskell #-}
69module Network.API.Mandrill.Types where
710
8−import Network.API.Mandrill.Utils
9−import Test.QuickCheck
10−import Text.Email.Validate
11+import Control.Applicative
12+import Control.Monad (mzero)
1113import Data.Char
1214import Data.Maybe
1315import Data.Time
14−import Control.Applicative
15−import Data.Time (ParseTime)
16+import Lens.Micro.TH (makeLenses)
17+import Network.API.Mandrill.Utils
18+import Test.QuickCheck
19+import Text.Email.Validate
1620#if MIN_VERSION_time(1,5,0)
17−import Data.Time.Format (TimeLocale, defaultTimeLocale)
21+import Data.Time.Format (TimeLocale, defaultTimeLocale)
1822#else
19−import System.Locale (TimeLocale, defaultTimeLocale)
23+import System.Locale (TimeLocale, defaultTimeLocale)
2024#endif
21−import qualified Data.ByteString as B
22−import qualified Data.ByteString.Base64 as Base64
23−import qualified Data.Text as T
24−import qualified Data.Text.Encoding as TL
25−import qualified Data.Text.Lazy as TL
26−import Control.Lens
27−import Data.Monoid
2825import Data.Aeson
29−import Data.Aeson.Types
3026import Data.Aeson.TH
31−import qualified Text.Blaze.Html as Blaze
27+import 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
3240import qualified Text.Blaze.Html.Renderer.Text as Blaze
33−
41+import qualified Text.Email.Validate as TEV
3442
3543timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
3644#if MIN_VERSION_time(1,5,0)
@@ -41,11 +49,11 @@ timeParse = parseTime
4149
4250--------------------------------------------------------------------------------
4351data MandrillError = MandrillError {
44− _merr_status :: !T.Text
45− , _merr_code :: !Int
46− , _merr_name :: !T.Text
52+ _merr_status :: !T.Text
53+ , _merr_code :: !Int
54+ , _merr_name :: !T.Text
4755 , _merr_message :: !T.Text
48− } deriving Show
56+ } deriving (Show, Eq)
4957
5058makeLenses ''MandrillError
5159deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -70,6 +78,7 @@ data MandrillRejectReason = RR_HardBounce
7078 | RR_InvalidSender
7179 | RR_Invalid
7280 | RR_TestModeLimit
81+ | RR_Unsigned
7382 | RR_Rule deriving Show
7483
7584deriveJSON defaultOptions {
@@ -82,7 +91,7 @@ deriveJSON defaultOptions {
8291-- which can be either a success or a failure.
8392data MandrillResponse k =
8493 MandrillSuccess k
85− | MandrillFailure MandrillError deriving Show
94+ | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
8695
8796instance FromJSON k => FromJSON (MandrillResponse k) where
8897 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -118,9 +127,9 @@ instance FromJSON MandrillEmail where
118127data MandrillRecipient = MandrillRecipient {
119128 _mrec_email :: MandrillEmail
120129 -- ^ The email address of the recipient
121− , _mrec_name :: Maybe T.Text
130+ , _mrec_name :: Maybe T.Text
122131 -- ^ The optional display name to use for the recipient
123− , _mrec_type :: Maybe MandrillRecipientTag
132+ , _mrec_type :: Maybe MandrillRecipientTag
124133 -- ^ The header type to use for the recipient.
125134 -- defaults to "to" if not provided
126135 } deriving Show
@@ -149,6 +158,11 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
149158mkMandrillHtml :: Blaze.Html -> MandrillHtml
150159mkMandrillHtml = MandrillHtml
151160
161+#if MIN_VERSION_base(4,11,0)
162+instance Semigroup MandrillHtml where
163+ MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164+#endif
165+
152166instance Monoid MandrillHtml where
153167 mempty = MandrillHtml mempty
154168 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -171,17 +185,23 @@ type MandrillTags = T.Text
171185
172186
173187--------------------------------------------------------------------------------
174−type MandrillHeaders = Value
188+type MandrillHeaders = Object
175189
176190
177191--------------------------------------------------------------------------------
178−type MandrillVars = Value
179192
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
180200
181201--------------------------------------------------------------------------------
182202data MandrillMergeVars = MandrillMergeVars {
183203 _mmvr_rcpt :: !T.Text
184− , _mmvr_vars :: [MandrillVars]
204+ , _mmvr_vars :: [MergeVar]
185205 } deriving Show
186206
187207makeLenses ''MandrillMergeVars
@@ -189,29 +209,33 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
189209
190210--------------------------------------------------------------------------------
191211data MandrillMetadata = MandrillMetadata {
192− _mmdt_rcpt :: !T.Text
193− , _mmdt_values :: MandrillVars
212+ _mmdt_rcpt :: !T.Text
213+ , _mmdt_values :: Object
194214 } deriving Show
195215
196216makeLenses ''MandrillMetadata
197217deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
198218
199219
200−newtype Base64ByteString = B64BS B.ByteString deriving Show
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
201226
202227instance ToJSON Base64ByteString where
203− toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
228+ toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229+ toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
204230
205231instance FromJSON Base64ByteString where
206− parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
207− Left err -> fail err
208− Right rs -> return $ B64BS rs
232+ parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
209233 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
210234
211235--------------------------------------------------------------------------------
212236data MandrillWebContent = MandrillWebContent {
213− _mwct_type :: !T.Text
214− , _mwct_name :: !T.Text
237+ _mwct_type :: !T.Text
238+ , _mwct_name :: !T.Text
215239 -- ^ [for images] the Content ID of the image
216240 -- - use <img src="cid:THIS_VALUE"> to reference the image
217241 -- in your HTML content
@@ -224,67 +248,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
224248--------------------------------------------------------------------------------
225249-- | The information on the message to send
226250data MandrillMessage = MandrillMessage {
227− _mmsg_html :: MandrillHtml
251+ _mmsg_html :: MandrillHtml
228252 -- ^ The full HTML content to be sent
229− , _mmsg_text :: Maybe T.Text
253+ , _mmsg_text :: Maybe T.Text
230254 -- ^ Optional full text content to be sent
231− , _mmsg_subject :: !T.Text
255+ , _mmsg_subject :: !T.Text
232256 -- ^ The message subject
233− , _mmsg_from_email :: MandrillEmail
257+ , _mmsg_from_email :: MandrillEmail
234258 -- ^ The sender email address
235− , _mmsg_from_name :: Maybe T.Text
259+ , _mmsg_from_name :: Maybe T.Text
236260 -- ^ Optional from name to be used
237− , _mmsg_to :: [MandrillRecipient]
261+ , _mmsg_to :: [MandrillRecipient]
238262 -- ^ A list of recipient information
239− , _mmsg_headers :: MandrillHeaders
263+ , _mmsg_headers :: MandrillHeaders
240264 -- ^ optional extra headers to add to the message (most headers are allowed)
241− , _mmsg_important :: Maybe Bool
265+ , _mmsg_important :: Maybe Bool
242266 -- ^ whether or not this message is important, and should be delivered ahead
243267 -- of non-important messages
244− , _mmsg_track_opens :: Maybe Bool
268+ , _mmsg_track_opens :: Maybe Bool
245269 -- ^ whether or not to turn on open tracking for the message
246− , _mmsg_track_clicks :: Maybe Bool
270+ , _mmsg_track_clicks :: Maybe Bool
247271 -- ^ whether or not to turn on click tracking for the message
248− , _mmsg_auto_text :: Maybe Bool
272+ , _mmsg_auto_text :: Maybe Bool
249273 -- ^ whether or not to automatically generate a text part for messages that are not given text
250− , _mmsg_auto_html :: Maybe Bool
274+ , _mmsg_auto_html :: Maybe Bool
251275 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
252− , _mmsg_inline_css :: Maybe Bool
276+ , _mmsg_inline_css :: Maybe Bool
253277 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
254278 -- - only for HTML documents less than 256KB in size
255− , _mmsg_url_strip_qs :: Maybe Bool
279+ , _mmsg_url_strip_qs :: Maybe Bool
256280 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
257− , _mmsg_preserve_recipients :: Maybe Bool
281+ , _mmsg_preserve_recipients :: Maybe Bool
258282 -- ^ whether or not to expose all recipients in to "To" header for each email
259− , _mmsg_view_content_link :: Maybe Bool
283+ , _mmsg_view_content_link :: Maybe Bool
260284 -- ^ set to false to remove content logging for sensitive emails
261− , _mmsg_bcc_address :: Maybe T.Text
285+ , _mmsg_bcc_address :: Maybe T.Text
262286 -- ^ an optional address to receive an exact copy of each recipient's email
263− , _mmsg_tracking_domain :: Maybe T.Text
287+ , _mmsg_tracking_domain :: Maybe T.Text
264288 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
265− , _mmsg_signing_domain :: Maybe Bool
266− -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
289+ , _mmsg_signing_domain :: Maybe Bool
290+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
267291 -- (for "via" or "on behalf of" in email clients)
268− , _mmsg_return_path_domain :: Maybe Bool
292+ , _mmsg_return_path_domain :: Maybe Bool
269293 -- ^ a custom domain to use for the messages's return-path
270− , _mmsg_merge :: Maybe Bool
294+ , _mmsg_merge :: Maybe Bool
271295 -- ^ whether to evaluate merge tags in the message.
272296 -- Will automatically be set to true if either merge_vars
273297 -- or global_merge_vars are provided.
274− , _mmsg_global_merge_vars :: [MandrillVars]
298+ , _mmsg_global_merge_vars :: [MergeVar]
275299 -- ^ global merge variables to use for all recipients. You can override these per recipient.
276− , _mmsg_merge_vars :: [MandrillMergeVars]
300+ , _mmsg_merge_vars :: [MandrillMergeVars]
277301 -- ^ per-recipient merge variables, which override global merge variables with the same name.
278− , _mmsg_tags :: [MandrillTags]
302+ , _mmsg_tags :: [MandrillTags]
279303 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
280304 -- though we only store the first 100 we see, so this should not be unique
281305 -- or change frequently. Tags should be 50 characters or less.
282306 -- Any tags starting with an underscore are reserved for internal use
283307 -- and will cause errors.
284− , _mmsg_subaccount :: Maybe T.Text
308+ , _mmsg_subaccount :: Maybe T.Text
285309 -- ^ the unique id of a subaccount for this message
286310 -- - must already exist or will fail with an error
287− , _mmsg_google_analytics_domains :: [T.Text]
311+ , _mmsg_google_analytics_domains :: [T.Text]
288312 -- ^ an array of strings indicating for which any matching URLs
289313 -- will automatically have Google Analytics parameters appended
290314 -- to their query string automatically.
@@ -292,17 +316,17 @@ data MandrillMessage = MandrillMessage {
292316 -- ^ optional string indicating the value to set for the utm_campaign
293317 -- tracking parameter. If this isn't provided the email's from address
294318 -- will be used instead.
295− , _mmsg_metadata :: MandrillVars
319+ , _mmsg_metadata :: Object
296320 -- ^ metadata an associative array of user metadata. Mandrill will store
297321 -- this metadata and make it available for retrieval.
298322 -- In addition, you can select up to 10 metadata fields to index
299323 -- and make searchable using the Mandrill search api.
300− , _mmsg_recipient_metadata :: [MandrillMetadata]
324+ , _mmsg_recipient_metadata :: [MandrillMetadata]
301325 -- ^ Per-recipient metadata that will override the global values
302326 -- specified in the metadata parameter.
303− , _mmsg_attachments :: [MandrillWebContent]
327+ , _mmsg_attachments :: [MandrillWebContent]
304328 -- ^ an array of supported attachments to add to the message
305− , _mmsg_images :: [MandrillWebContent]
329+ , _mmsg_images :: [MandrillWebContent]
306330 -- ^ an array of embedded images to add to the message
307331 } deriving Show
308332
@@ -316,7 +340,7 @@ instance Arbitrary MandrillMessage where
316340 <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
317341 <*> pure Nothing
318342 <*> resize 2 arbitrary
319− <*> pure emptyObject
343+ <*> pure H.empty
320344 <*> pure Nothing
321345 <*> pure Nothing
322346 <*> pure Nothing
@@ -337,7 +361,7 @@ instance Arbitrary MandrillMessage where
337361 <*> pure Nothing
338362 <*> pure []
339363 <*> pure Nothing
340− <*> pure emptyObject
364+ <*> pure H.empty
341365 <*> pure []
342366 <*> pure []
343367 <*> pure []