@@ -1,44 +1,36 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveFoldable #-}
3−{-# LANGUAGE DeriveFunctor #-}
4−{-# LANGUAGE DeriveTraversable #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE TemplateHaskell #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE RankNTypes #-}
74{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TemplateHaskell #-}
5+{-# LANGUAGE OverloadedStrings #-}
96module Network.API.Mandrill.Types where
107
11−import Control.Applicative
12−import Control.Monad (mzero)
13−import Data.Char
14−import Data.Maybe
15−import Data.Time
16−import Lens.Micro.TH (makeLenses)
178import Network.API.Mandrill.Utils
189import Test.QuickCheck
1910import Text.Email.Validate
11+import Data.Char
12+import Data.Maybe
13+import Data.Time
14+import Control.Applicative
2015#if MIN_VERSION_time(1,5,0)
21−import Data.Time.Format (TimeLocale, defaultTimeLocale)
16+import Data.Time.Format (TimeLocale, defaultTimeLocale)
2217#else
23−import System.Locale (TimeLocale, defaultTimeLocale)
18+import System.Locale (TimeLocale, defaultTimeLocale)
2419#endif
20+import qualified Data.ByteString as B
21+import qualified Data.ByteString.Base64 as Base64
22+import qualified Data.HashMap.Strict as H
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
2528import Data.Aeson
26−import Data.Aeson.TH
2729import 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
30+import Data.Aeson.TH
31+import qualified Text.Blaze.Html as Blaze
4032import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
33+
4234
4335timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
4436#if MIN_VERSION_time(1,5,0)
@@ -49,11 +41,11 @@ timeParse = parseTime
4941
5042--------------------------------------------------------------------------------
5143data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
44+ _merr_status :: !T.Text
45+ , _merr_code :: !Int
46+ , _merr_name :: !T.Text
5547 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
48+ } deriving Show
5749
5850makeLenses ''MandrillError
5951deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +70,6 @@ data MandrillRejectReason = RR_HardBounce
7870 | RR_InvalidSender
7971 | RR_Invalid
8072 | RR_TestModeLimit
81− | RR_Unsigned
8273 | RR_Rule deriving Show
8374
8475deriveJSON defaultOptions {
@@ -91,7 +82,7 @@ deriveJSON defaultOptions {
9182-- which can be either a success or a failure.
9283data MandrillResponse k =
9384 MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
85+ | MandrillFailure MandrillError deriving Show
9586
9687instance FromJSON k => FromJSON (MandrillResponse k) where
9788 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -127,9 +118,9 @@ instance FromJSON MandrillEmail where
127118data MandrillRecipient = MandrillRecipient {
128119 _mrec_email :: MandrillEmail
129120 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
121+ , _mrec_name :: Maybe T.Text
131122 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
123+ , _mrec_type :: Maybe MandrillRecipientTag
133124 -- ^ The header type to use for the recipient.
134125 -- defaults to "to" if not provided
135126 } deriving Show
@@ -158,11 +149,6 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
158149mkMandrillHtml :: Blaze.Html -> MandrillHtml
159150mkMandrillHtml = MandrillHtml
160151
161−#if MIN_VERSION_base(4,11,0)
162−instance Semigroup MandrillHtml where
163− MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164−#endif
165−
166152instance Monoid MandrillHtml where
167153 mempty = MandrillHtml mempty
168154 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -189,19 +175,13 @@ type MandrillHeaders = Object
189175
190176
191177--------------------------------------------------------------------------------
178+type MandrillVars = Value
192179
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
200180
201181--------------------------------------------------------------------------------
202182data MandrillMergeVars = MandrillMergeVars {
203183 _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
184+ , _mmvr_vars :: [MandrillVars]
205185 } deriving Show
206186
207187makeLenses ''MandrillMergeVars
@@ -209,8 +189,8 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209189
210190--------------------------------------------------------------------------------
211191data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
192+ _mmdt_rcpt :: !T.Text
193+ , _mmdt_values :: MandrillVars
214194 } deriving Show
215195
216196makeLenses ''MandrillMetadata
@@ -234,8 +214,8 @@ instance FromJSON Base64ByteString where
234214
235215--------------------------------------------------------------------------------
236216data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
217+ _mwct_type :: !T.Text
218+ , _mwct_name :: !T.Text
239219 -- ^ [for images] the Content ID of the image
240220 -- - use <img src="cid:THIS_VALUE"> to reference the image
241221 -- in your HTML content
@@ -248,67 +228,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
248228--------------------------------------------------------------------------------
249229-- | The information on the message to send
250230data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
231+ _mmsg_html :: MandrillHtml
252232 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
233+ , _mmsg_text :: Maybe T.Text
254234 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
235+ , _mmsg_subject :: !T.Text
256236 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
237+ , _mmsg_from_email :: MandrillEmail
258238 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
239+ , _mmsg_from_name :: Maybe T.Text
260240 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
241+ , _mmsg_to :: [MandrillRecipient]
262242 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
243+ , _mmsg_headers :: MandrillHeaders
264244 -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
245+ , _mmsg_important :: Maybe Bool
266246 -- ^ whether or not this message is important, and should be delivered ahead
267247 -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
248+ , _mmsg_track_opens :: Maybe Bool
269249 -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
250+ , _mmsg_track_clicks :: Maybe Bool
271251 -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
252+ , _mmsg_auto_text :: Maybe Bool
273253 -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
254+ , _mmsg_auto_html :: Maybe Bool
275255 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
256+ , _mmsg_inline_css :: Maybe Bool
277257 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278258 -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
259+ , _mmsg_url_strip_qs :: Maybe Bool
280260 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
261+ , _mmsg_preserve_recipients :: Maybe Bool
282262 -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
263+ , _mmsg_view_content_link :: Maybe Bool
284264 -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
265+ , _mmsg_bcc_address :: Maybe T.Text
286266 -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
267+ , _mmsg_tracking_domain :: Maybe T.Text
288268 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
289− , _mmsg_signing_domain :: Maybe Bool
269+ , _mmsg_signing_domain :: Maybe Bool
290270 -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291271 -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
272+ , _mmsg_return_path_domain :: Maybe Bool
293273 -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
274+ , _mmsg_merge :: Maybe Bool
295275 -- ^ whether to evaluate merge tags in the message.
296276 -- Will automatically be set to true if either merge_vars
297277 -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
278+ , _mmsg_global_merge_vars :: [MandrillVars]
299279 -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
280+ , _mmsg_merge_vars :: [MandrillMergeVars]
301281 -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
282+ , _mmsg_tags :: [MandrillTags]
303283 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304284 -- though we only store the first 100 we see, so this should not be unique
305285 -- or change frequently. Tags should be 50 characters or less.
306286 -- Any tags starting with an underscore are reserved for internal use
307287 -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
288+ , _mmsg_subaccount :: Maybe T.Text
309289 -- ^ the unique id of a subaccount for this message
310290 -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
291+ , _mmsg_google_analytics_domains :: [T.Text]
312292 -- ^ an array of strings indicating for which any matching URLs
313293 -- will automatically have Google Analytics parameters appended
314294 -- to their query string automatically.
@@ -316,17 +296,17 @@ data MandrillMessage = MandrillMessage {
316296 -- ^ optional string indicating the value to set for the utm_campaign
317297 -- tracking parameter. If this isn't provided the email's from address
318298 -- will be used instead.
319− , _mmsg_metadata :: Object
299+ , _mmsg_metadata :: MandrillVars
320300 -- ^ metadata an associative array of user metadata. Mandrill will store
321301 -- this metadata and make it available for retrieval.
322302 -- In addition, you can select up to 10 metadata fields to index
323303 -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
304+ , _mmsg_recipient_metadata :: [MandrillMetadata]
325305 -- ^ Per-recipient metadata that will override the global values
326306 -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
307+ , _mmsg_attachments :: [MandrillWebContent]
328308 -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
309+ , _mmsg_images :: [MandrillWebContent]
330310 -- ^ an array of embedded images to add to the message
331311 } deriving Show
332312
@@ -361,7 +341,7 @@ instance Arbitrary MandrillMessage where
361341 <*> pure Nothing
362342 <*> pure []
363343 <*> pure Nothing
364− <*> pure H.empty
344+ <*> pure emptyObject
365345 <*> pure []
366346 <*> pure []
367347 <*> pure []