@@ -1,36 +1,44 @@
1−{-# LANGUAGE TemplateHaskell #-}
1+{-# LANGUAGE CPP #-}
2−{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveFoldable #-}
3+{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE DeriveTraversable #-}
5+{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE RankNTypes #-}
6+{-# LANGUAGE RankNTypes #-}
4{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE OverloadedStrings #-}
8+{-# LANGUAGE TemplateHaskell #-}
6module Network.API.Mandrill.Types where
9module Network.API.Mandrill.Types where
8−import Network.API.Mandrill.Utils
11+import Control.Applicative
9−import Test.QuickCheck
12+import Control.Monad (mzero)
10−import Text.Email.Validate
11import Data.Char
13import Data.Char
12import Data.Maybe
14import Data.Maybe
13import Data.Time
15import Data.Time
14−import Control.Applicative
16+import Lens.Micro.TH (makeLenses)
17+import Network.API.Mandrill.Utils
18+import Test.QuickCheck
19+import Text.Email.Validate
15#if MIN_VERSION_time(1,5,0)
20#if MIN_VERSION_time(1,5,0)
16−import Data.Time.Format (TimeLocale, defaultTimeLocale)
21+import Data.Time.Format (TimeLocale, defaultTimeLocale)
18−import System.Locale (TimeLocale, defaultTimeLocale)
23+import System.Locale (TimeLocale, defaultTimeLocale)
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
28import Data.Aeson
25import Data.Aeson
29−import Data.Aeson.Types
30import Data.Aeson.TH
26import Data.Aeson.TH
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)
32+import Data.Traversable
34+import qualified Data.HashMap.Strict as H
36+import qualified Data.Text as T
37+import qualified Data.Text.Encoding as TL
38+import qualified Data.Text.Lazy as TL
31−import qualified Text.Blaze.Html as Blaze
39+import qualified Text.Blaze.Html as Blaze
32import qualified Text.Blaze.Html.Renderer.Text as Blaze
40import qualified Text.Blaze.Html.Renderer.Text as Blaze
33−
41+import qualified Text.Email.Validate as TEV
35timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
43timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
36#if MIN_VERSION_time(1,5,0)
44#if MIN_VERSION_time(1,5,0)
@@ -41,11 +49,11 @@ timeParse = parseTime
42--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
43data MandrillError = MandrillError {
51data MandrillError = MandrillError {
44− _merr_status :: !T.Text
52+ _merr_status :: !T.Text
45− , _merr_code :: !Int
53+ , _merr_code :: !Int
46− , _merr_name :: !T.Text
54+ , _merr_name :: !T.Text
47 , _merr_message :: !T.Text
55 , _merr_message :: !T.Text
48− } deriving Show
56+ } deriving (Show, Eq)
50makeLenses ''MandrillError
58makeLenses ''MandrillError
51deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
59deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -83,7 +91,7 @@ deriveJSON defaultOptions {
83-- which can be either a success or a failure.
91-- which can be either a success or a failure.
84data MandrillResponse k =
92data MandrillResponse k =
85 MandrillSuccess k
93 MandrillSuccess k
86− | MandrillFailure MandrillError deriving Show
94+ | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
88instance FromJSON k => FromJSON (MandrillResponse k) where
96instance FromJSON k => FromJSON (MandrillResponse k) where
89 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
97 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -119,9 +127,9 @@ instance FromJSON MandrillEmail where
119data MandrillRecipient = MandrillRecipient {
127data MandrillRecipient = MandrillRecipient {
120 _mrec_email :: MandrillEmail
128 _mrec_email :: MandrillEmail
121 -- ^ The email address of the recipient
129 -- ^ The email address of the recipient
122− , _mrec_name :: Maybe T.Text
130+ , _mrec_name :: Maybe T.Text
123 -- ^ The optional display name to use for the recipient
131 -- ^ The optional display name to use for the recipient
124− , _mrec_type :: Maybe MandrillRecipientTag
132+ , _mrec_type :: Maybe MandrillRecipientTag
125 -- ^ The header type to use for the recipient.
133 -- ^ The header type to use for the recipient.
126 -- defaults to "to" if not provided
134 -- defaults to "to" if not provided
127 } deriving Show
135 } deriving Show
@@ -150,6 +158,11 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
150mkMandrillHtml :: Blaze.Html -> MandrillHtml
158mkMandrillHtml :: Blaze.Html -> MandrillHtml
151mkMandrillHtml = MandrillHtml
159mkMandrillHtml = MandrillHtml
161+#if MIN_VERSION_base(4,11,0)
162+instance Semigroup MandrillHtml where
163+ MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
153instance Monoid MandrillHtml where
166instance Monoid MandrillHtml where
154 mempty = MandrillHtml mempty
167 mempty = MandrillHtml mempty
155 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
168 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -196,7 +209,7 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
197--------------------------------------------------------------------------------
210--------------------------------------------------------------------------------
198data MandrillMetadata = MandrillMetadata {
211data MandrillMetadata = MandrillMetadata {
199− _mmdt_rcpt :: !T.Text
212+ _mmdt_rcpt :: !T.Text
200 , _mmdt_values :: Object
213 , _mmdt_values :: Object
201 } deriving Show
214 } deriving Show
@@ -221,8 +234,8 @@ instance FromJSON Base64ByteString where
222--------------------------------------------------------------------------------
235--------------------------------------------------------------------------------
223data MandrillWebContent = MandrillWebContent {
236data MandrillWebContent = MandrillWebContent {
224− _mwct_type :: !T.Text
237+ _mwct_type :: !T.Text
225− , _mwct_name :: !T.Text
238+ , _mwct_name :: !T.Text
226 -- ^ [for images] the Content ID of the image
239 -- ^ [for images] the Content ID of the image
227 -- - use <img src="cid:THIS_VALUE"> to reference the image
240 -- - use <img src="cid:THIS_VALUE"> to reference the image
228 -- in your HTML content
241 -- in your HTML content
@@ -235,67 +248,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
235--------------------------------------------------------------------------------
248--------------------------------------------------------------------------------
236-- | The information on the message to send
249-- | The information on the message to send
237data MandrillMessage = MandrillMessage {
250data MandrillMessage = MandrillMessage {
238− _mmsg_html :: MandrillHtml
251+ _mmsg_html :: MandrillHtml
239 -- ^ The full HTML content to be sent
252 -- ^ The full HTML content to be sent
240− , _mmsg_text :: Maybe T.Text
253+ , _mmsg_text :: Maybe T.Text
241 -- ^ Optional full text content to be sent
254 -- ^ Optional full text content to be sent
242− , _mmsg_subject :: !T.Text
255+ , _mmsg_subject :: !T.Text
243 -- ^ The message subject
256 -- ^ The message subject
244− , _mmsg_from_email :: MandrillEmail
257+ , _mmsg_from_email :: MandrillEmail
245 -- ^ The sender email address
258 -- ^ The sender email address
246− , _mmsg_from_name :: Maybe T.Text
259+ , _mmsg_from_name :: Maybe T.Text
247 -- ^ Optional from name to be used
260 -- ^ Optional from name to be used
248− , _mmsg_to :: [MandrillRecipient]
261+ , _mmsg_to :: [MandrillRecipient]
249 -- ^ A list of recipient information
262 -- ^ A list of recipient information
250− , _mmsg_headers :: MandrillHeaders
263+ , _mmsg_headers :: MandrillHeaders
251 -- ^ optional extra headers to add to the message (most headers are allowed)
264 -- ^ optional extra headers to add to the message (most headers are allowed)
252− , _mmsg_important :: Maybe Bool
265+ , _mmsg_important :: Maybe Bool
253 -- ^ whether or not this message is important, and should be delivered ahead
266 -- ^ whether or not this message is important, and should be delivered ahead
254 -- of non-important messages
267 -- of non-important messages
255− , _mmsg_track_opens :: Maybe Bool
268+ , _mmsg_track_opens :: Maybe Bool
256 -- ^ whether or not to turn on open tracking for the message
269 -- ^ whether or not to turn on open tracking for the message
257− , _mmsg_track_clicks :: Maybe Bool
270+ , _mmsg_track_clicks :: Maybe Bool
258 -- ^ whether or not to turn on click tracking for the message
271 -- ^ whether or not to turn on click tracking for the message
259− , _mmsg_auto_text :: Maybe Bool
272+ , _mmsg_auto_text :: Maybe Bool
260 -- ^ whether or not to automatically generate a text part for messages that are not given text
273 -- ^ whether or not to automatically generate a text part for messages that are not given text
261− , _mmsg_auto_html :: Maybe Bool
274+ , _mmsg_auto_html :: Maybe Bool
262 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
275 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
263− , _mmsg_inline_css :: Maybe Bool
276+ , _mmsg_inline_css :: Maybe Bool
264 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
277 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
265 -- - only for HTML documents less than 256KB in size
278 -- - only for HTML documents less than 256KB in size
266− , _mmsg_url_strip_qs :: Maybe Bool
279+ , _mmsg_url_strip_qs :: Maybe Bool
267 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
280 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
268− , _mmsg_preserve_recipients :: Maybe Bool
281+ , _mmsg_preserve_recipients :: Maybe Bool
269 -- ^ whether or not to expose all recipients in to "To" header for each email
282 -- ^ whether or not to expose all recipients in to "To" header for each email
270− , _mmsg_view_content_link :: Maybe Bool
283+ , _mmsg_view_content_link :: Maybe Bool
271 -- ^ set to false to remove content logging for sensitive emails
284 -- ^ set to false to remove content logging for sensitive emails
272− , _mmsg_bcc_address :: Maybe T.Text
285+ , _mmsg_bcc_address :: Maybe T.Text
273 -- ^ an optional address to receive an exact copy of each recipient's email
286 -- ^ an optional address to receive an exact copy of each recipient's email
274− , _mmsg_tracking_domain :: Maybe T.Text
287+ , _mmsg_tracking_domain :: Maybe T.Text
275 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
288 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
276− , _mmsg_signing_domain :: Maybe Bool
289+ , _mmsg_signing_domain :: Maybe Bool
277 -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
290 -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
278 -- (for "via" or "on behalf of" in email clients)
291 -- (for "via" or "on behalf of" in email clients)
279− , _mmsg_return_path_domain :: Maybe Bool
292+ , _mmsg_return_path_domain :: Maybe Bool
280 -- ^ a custom domain to use for the messages's return-path
293 -- ^ a custom domain to use for the messages's return-path
281− , _mmsg_merge :: Maybe Bool
294+ , _mmsg_merge :: Maybe Bool
282 -- ^ whether to evaluate merge tags in the message.
295 -- ^ whether to evaluate merge tags in the message.
283 -- Will automatically be set to true if either merge_vars
296 -- Will automatically be set to true if either merge_vars
284 -- or global_merge_vars are provided.
297 -- or global_merge_vars are provided.
285− , _mmsg_global_merge_vars :: [MergeVar]
298+ , _mmsg_global_merge_vars :: [MergeVar]
286 -- ^ global merge variables to use for all recipients. You can override these per recipient.
299 -- ^ global merge variables to use for all recipients. You can override these per recipient.
287− , _mmsg_merge_vars :: [MandrillMergeVars]
300+ , _mmsg_merge_vars :: [MandrillMergeVars]
288 -- ^ per-recipient merge variables, which override global merge variables with the same name.
301 -- ^ per-recipient merge variables, which override global merge variables with the same name.
289− , _mmsg_tags :: [MandrillTags]
302+ , _mmsg_tags :: [MandrillTags]
290 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
303 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
291 -- though we only store the first 100 we see, so this should not be unique
304 -- though we only store the first 100 we see, so this should not be unique
292 -- or change frequently. Tags should be 50 characters or less.
305 -- or change frequently. Tags should be 50 characters or less.
293 -- Any tags starting with an underscore are reserved for internal use
306 -- Any tags starting with an underscore are reserved for internal use
294 -- and will cause errors.
307 -- and will cause errors.
295− , _mmsg_subaccount :: Maybe T.Text
308+ , _mmsg_subaccount :: Maybe T.Text
296 -- ^ the unique id of a subaccount for this message
309 -- ^ the unique id of a subaccount for this message
297 -- - must already exist or will fail with an error
310 -- - must already exist or will fail with an error
298− , _mmsg_google_analytics_domains :: [T.Text]
311+ , _mmsg_google_analytics_domains :: [T.Text]
299 -- ^ an array of strings indicating for which any matching URLs
312 -- ^ an array of strings indicating for which any matching URLs
300 -- will automatically have Google Analytics parameters appended
313 -- will automatically have Google Analytics parameters appended
301 -- to their query string automatically.
314 -- to their query string automatically.
@@ -303,17 +316,17 @@ data MandrillMessage = MandrillMessage {
303 -- ^ optional string indicating the value to set for the utm_campaign
316 -- ^ optional string indicating the value to set for the utm_campaign
304 -- tracking parameter. If this isn't provided the email's from address
317 -- tracking parameter. If this isn't provided the email's from address
305 -- will be used instead.
318 -- will be used instead.
306− , _mmsg_metadata :: Object
319+ , _mmsg_metadata :: Object
307 -- ^ metadata an associative array of user metadata. Mandrill will store
320 -- ^ metadata an associative array of user metadata. Mandrill will store
308 -- this metadata and make it available for retrieval.
321 -- this metadata and make it available for retrieval.
309 -- In addition, you can select up to 10 metadata fields to index
322 -- In addition, you can select up to 10 metadata fields to index
310 -- and make searchable using the Mandrill search api.
323 -- and make searchable using the Mandrill search api.
311− , _mmsg_recipient_metadata :: [MandrillMetadata]
324+ , _mmsg_recipient_metadata :: [MandrillMetadata]
312 -- ^ Per-recipient metadata that will override the global values
325 -- ^ Per-recipient metadata that will override the global values
313 -- specified in the metadata parameter.
326 -- specified in the metadata parameter.
314− , _mmsg_attachments :: [MandrillWebContent]
327+ , _mmsg_attachments :: [MandrillWebContent]
315 -- ^ an array of supported attachments to add to the message
328 -- ^ an array of supported attachments to add to the message
316− , _mmsg_images :: [MandrillWebContent]
329+ , _mmsg_images :: [MandrillWebContent]
317 -- ^ an array of embedded images to add to the message
330 -- ^ an array of embedded images to add to the message
318 } deriving Show
331 } deriving Show