@@ -1,44 +1,37 @@
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
9+import Network.API.Mandrill.Orphans()
1810import Test.QuickCheck
1911import Text.Email.Validate
12+import Data.Char
13+import Data.Maybe
14+import Data.Time
15+import Control.Applicative
16+import Data.Time (ParseTime)
2017#if MIN_VERSION_time(1,5,0)
21−import Data.Time.Format (TimeLocale, defaultTimeLocale)
18+import Data.Time.Format (TimeLocale, defaultTimeLocale)
2219#else
23−import System.Locale (TimeLocale, defaultTimeLocale)
20+import System.Locale (TimeLocale, defaultTimeLocale)
2421#endif
22+import qualified Data.ByteString as B
23+import qualified Data.ByteString.Base64 as Base64
24+import qualified Data.Text as T
25+import qualified Data.Text.Encoding as TL
26+import qualified Data.Text.Lazy as TL
27+import Control.Lens
28+import Data.Monoid
2529import Data.Aeson
26−import Data.Aeson.TH
2730import 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
31+import Data.Aeson.TH
32+import qualified Text.Blaze.Html as Blaze
4033import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
34+
4235
4336timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
4437#if MIN_VERSION_time(1,5,0)
@@ -49,11 +42,11 @@ timeParse = parseTime
4942
5043--------------------------------------------------------------------------------
5144data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
45+ _merr_status :: !T.Text
46+ , _merr_code :: !Int
47+ , _merr_name :: !T.Text
5548 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
49+ } deriving Show
5750
5851makeLenses ''MandrillError
5952deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +71,6 @@ data MandrillRejectReason = RR_HardBounce
7871 | RR_InvalidSender
7972 | RR_Invalid
8073 | RR_TestModeLimit
81− | RR_Unsigned
8274 | RR_Rule deriving Show
8375
8476deriveJSON defaultOptions {
@@ -91,7 +83,7 @@ deriveJSON defaultOptions {
9183-- which can be either a success or a failure.
9284data MandrillResponse k =
9385 MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
86+ | MandrillFailure MandrillError deriving Show
9587
9688instance FromJSON k => FromJSON (MandrillResponse k) where
9789 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -109,27 +101,14 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
109101deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
110102
111103
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−
125104--------------------------------------------------------------------------------
126105-- | An array of recipient information.
127106data MandrillRecipient = MandrillRecipient {
128− _mrec_email :: MandrillEmail
107+ _mrec_email :: EmailAddress
129108 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
109+ , _mrec_name :: Maybe T.Text
131110 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
111+ , _mrec_type :: Maybe MandrillRecipientTag
133112 -- ^ The header type to use for the recipient.
134113 -- defaults to "to" if not provided
135114 } deriving Show
@@ -138,11 +117,11 @@ makeLenses ''MandrillRecipient
138117deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
139118
140119newRecipient :: EmailAddress -> MandrillRecipient
141−newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
120+newRecipient email = MandrillRecipient email Nothing Nothing
142121
143122instance Arbitrary MandrillRecipient where
144123 arbitrary = pure MandrillRecipient {
145− _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
124+ _mrec_email = fromJust (emailAddress "test@example.com")
146125 , _mrec_name = Nothing
147126 , _mrec_type = Nothing
148127 }
@@ -158,11 +137,6 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
158137mkMandrillHtml :: Blaze.Html -> MandrillHtml
159138mkMandrillHtml = MandrillHtml
160139
161−#if MIN_VERSION_base(4,11,0)
162−instance Semigroup MandrillHtml where
163− MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164−#endif
165−
166140instance Monoid MandrillHtml where
167141 mempty = MandrillHtml mempty
168142 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -185,23 +159,17 @@ type MandrillTags = T.Text
185159
186160
187161--------------------------------------------------------------------------------
188−type MandrillHeaders = Object
162+type MandrillHeaders = Value
189163
190164
191165--------------------------------------------------------------------------------
166+type MandrillVars = Value
192167
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
200168
201169--------------------------------------------------------------------------------
202170data MandrillMergeVars = MandrillMergeVars {
203171 _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
172+ , _mmvr_vars :: [MandrillVars]
205173 } deriving Show
206174
207175makeLenses ''MandrillMergeVars
@@ -209,33 +177,29 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209177
210178--------------------------------------------------------------------------------
211179data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
180+ _mmdt_rcpt :: !T.Text
181+ , _mmdt_values :: MandrillVars
214182 } deriving Show
215183
216184makeLenses ''MandrillMetadata
217185deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
218186
219187
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
188+newtype Base64ByteString = B64BS B.ByteString deriving Show
226189
227190instance ToJSON Base64ByteString where
228− toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229− toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
191+ toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
230192
231193instance FromJSON Base64ByteString where
232− parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
194+ parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
195+ Left err -> fail err
196+ Right rs -> return $ B64BS rs
233197 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
234198
235199--------------------------------------------------------------------------------
236200data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
201+ _mwct_type :: !T.Text
202+ , _mwct_name :: !T.Text
239203 -- ^ [for images] the Content ID of the image
240204 -- - use <img src="cid:THIS_VALUE"> to reference the image
241205 -- in your HTML content
@@ -248,67 +212,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
248212--------------------------------------------------------------------------------
249213-- | The information on the message to send
250214data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
215+ _mmsg_html :: MandrillHtml
252216 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
217+ , _mmsg_text :: Maybe T.Text
254218 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
219+ , _mmsg_subject :: !T.Text
256220 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
221+ , _mmsg_from_email :: EmailAddress
258222 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
223+ , _mmsg_from_name :: Maybe T.Text
260224 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
225+ , _mmsg_to :: [MandrillRecipient]
262226 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
227+ , _mmsg_headers :: MandrillHeaders
264228 -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
229+ , _mmsg_important :: Maybe Bool
266230 -- ^ whether or not this message is important, and should be delivered ahead
267231 -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
232+ , _mmsg_track_opens :: Maybe Bool
269233 -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
234+ , _mmsg_track_clicks :: Maybe Bool
271235 -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
236+ , _mmsg_auto_text :: Maybe Bool
273237 -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
238+ , _mmsg_auto_html :: Maybe Bool
275239 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
240+ , _mmsg_inline_css :: Maybe Bool
277241 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278242 -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
243+ , _mmsg_url_strip_qs :: Maybe Bool
280244 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
245+ , _mmsg_preserve_recipients :: Maybe Bool
282246 -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
247+ , _mmsg_view_content_link :: Maybe Bool
284248 -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
249+ , _mmsg_bcc_address :: Maybe T.Text
286250 -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
251+ , _mmsg_tracking_domain :: Maybe T.Text
288252 -- ^ 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
253+ , _mmsg_signing_domain :: Maybe Bool
254+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291255 -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
256+ , _mmsg_return_path_domain :: Maybe Bool
293257 -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
258+ , _mmsg_merge :: Maybe Bool
295259 -- ^ whether to evaluate merge tags in the message.
296260 -- Will automatically be set to true if either merge_vars
297261 -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
262+ , _mmsg_global_merge_vars :: [MandrillVars]
299263 -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
264+ , _mmsg_merge_vars :: [MandrillMergeVars]
301265 -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
266+ , _mmsg_tags :: [MandrillTags]
303267 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304268 -- though we only store the first 100 we see, so this should not be unique
305269 -- or change frequently. Tags should be 50 characters or less.
306270 -- Any tags starting with an underscore are reserved for internal use
307271 -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
272+ , _mmsg_subaccount :: Maybe T.Text
309273 -- ^ the unique id of a subaccount for this message
310274 -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
275+ , _mmsg_google_analytics_domains :: [T.Text]
312276 -- ^ an array of strings indicating for which any matching URLs
313277 -- will automatically have Google Analytics parameters appended
314278 -- to their query string automatically.
@@ -316,17 +280,17 @@ data MandrillMessage = MandrillMessage {
316280 -- ^ optional string indicating the value to set for the utm_campaign
317281 -- tracking parameter. If this isn't provided the email's from address
318282 -- will be used instead.
319− , _mmsg_metadata :: Object
283+ , _mmsg_metadata :: MandrillVars
320284 -- ^ metadata an associative array of user metadata. Mandrill will store
321285 -- this metadata and make it available for retrieval.
322286 -- In addition, you can select up to 10 metadata fields to index
323287 -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
288+ , _mmsg_recipient_metadata :: [MandrillMetadata]
325289 -- ^ Per-recipient metadata that will override the global values
326290 -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
291+ , _mmsg_attachments :: [MandrillWebContent]
328292 -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
293+ , _mmsg_images :: [MandrillWebContent]
330294 -- ^ an array of embedded images to add to the message
331295 } deriving Show
332296
@@ -337,10 +301,10 @@ instance Arbitrary MandrillMessage where
337301 arbitrary = MandrillMessage <$> arbitrary
338302 <*> pure Nothing
339303 <*> pure "Test Subject"
340− <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
304+ <*> pure (fromJust $ emailAddress "sender@example.com")
341305 <*> pure Nothing
342306 <*> resize 2 arbitrary
343− <*> pure H.empty
307+ <*> pure emptyObject
344308 <*> pure Nothing
345309 <*> pure Nothing
346310 <*> pure Nothing
@@ -361,7 +325,7 @@ instance Arbitrary MandrillMessage where
361325 <*> pure Nothing
362326 <*> pure []
363327 <*> pure Nothing
364− <*> pure H.empty
328+ <*> pure emptyObject
365329 <*> pure []
366330 <*> pure []
367331 <*> pure []