@@ -1,59 +1,38 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE DeriveFoldable #-}
3−{-# LANGUAGE DeriveFunctor #-}
4−{-# LANGUAGE DeriveTraversable #-}
5−{-# LANGUAGE OverloadedStrings #-}
6−{-# LANGUAGE RankNTypes #-}
1+{-# LANGUAGE TemplateHaskell #-}
2+{-# LANGUAGE RankNTypes #-}
73{-# LANGUAGE ScopedTypeVariables #-}
8−{-# LANGUAGE TemplateHaskell #-}
4+{-# LANGUAGE OverloadedStrings #-}
95module Network.API.Mandrill.Types where
106
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)
177import Network.API.Mandrill.Utils
188import 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 Data.Char
10+import Data.Time
11+import Control.Applicative
12+import System.Locale (defaultTimeLocale)
13+import Data.Map (Map)
14+import qualified Data.Map as Map
15+import qualified Data.ByteString as B
16+import qualified Data.ByteString.Base64 as Base64
17+import qualified Data.Text as T
18+import qualified Data.Text.Encoding as TL
19+import qualified Data.Text.Lazy as TL
20+import Control.Lens
21+import Data.Monoid
2522import Data.Aeson
26−import Data.Aeson.TH
2723import 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
24+import Data.Aeson.TH
25+import qualified Text.Blaze.Html as Blaze
4026import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
4227
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
4928
5029--------------------------------------------------------------------------------
5130data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
31+ _merr_status :: !T.Text
32+ , _merr_code :: !Int
33+ , _merr_name :: !T.Text
5534 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
35+ } deriving Show
5736
5837makeLenses ''MandrillError
5938deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +57,6 @@ data MandrillRejectReason = RR_HardBounce
7857 | RR_InvalidSender
7958 | RR_Invalid
8059 | RR_TestModeLimit
81− | RR_Unsigned
8260 | RR_Rule deriving Show
8361
8462deriveJSON defaultOptions {
@@ -91,7 +69,7 @@ deriveJSON defaultOptions {
9169-- which can be either a success or a failure.
9270data MandrillResponse k =
9371 MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
72+ | MandrillFailure MandrillError deriving Show
9573
9674instance FromJSON k => FromJSON (MandrillResponse k) where
9775 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -109,27 +87,14 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
10987deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
11088
11189
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−
12590--------------------------------------------------------------------------------
12691-- | An array of recipient information.
12792data MandrillRecipient = MandrillRecipient {
128− _mrec_email :: MandrillEmail
93+ _mrec_email :: !T.Text
12994 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
95+ , _mrec_name :: Maybe T.Text
13196 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
97+ , _mrec_type :: Maybe MandrillRecipientTag
13398 -- ^ The header type to use for the recipient.
13499 -- defaults to "to" if not provided
135100 } deriving Show
@@ -137,35 +102,17 @@ data MandrillRecipient = MandrillRecipient {
137102makeLenses ''MandrillRecipient
138103deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
139104
140−newRecipient :: EmailAddress -> MandrillRecipient
141−newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
142−
143105instance 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− }
106+ arbitrary = pure $ MandrillRecipient "test@example.com" Nothing Nothing
149107
150108--------------------------------------------------------------------------------
151109newtype MandrillHtml = MandrillHtml Blaze.Html
152110
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
111+mkMandrillHtml :: T.Text -> MandrillHtml
112+mkMandrillHtml = MandrillHtml . Blaze.toHtml
165113
166−instance Monoid MandrillHtml where
167− mempty = MandrillHtml mempty
168− mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
114+mkMandrillHtml' :: Blaze.Html -> MandrillHtml
115+mkMandrillHtml' = MandrillHtml
169116
170117instance Show MandrillHtml where
171118 show (MandrillHtml h) = show $ Blaze.renderHtml h
@@ -174,7 +121,7 @@ instance ToJSON MandrillHtml where
174121 toJSON (MandrillHtml h) = String . TL.toStrict . Blaze.renderHtml $ h
175122
176123instance FromJSON MandrillHtml where
177− parseJSON (String h) = return $ MandrillHtml (Blaze.preEscapedToHtml h)
124+ parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
178125 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
179126
180127instance Arbitrary MandrillHtml where
@@ -185,23 +132,17 @@ type MandrillTags = T.Text
185132
186133
187134--------------------------------------------------------------------------------
188−type MandrillHeaders = Object
135+type MandrillHeaders = Value
189136
190137
191138--------------------------------------------------------------------------------
139+type MandrillVars = Value
192140
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
200141
201142--------------------------------------------------------------------------------
202143data MandrillMergeVars = MandrillMergeVars {
203144 _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
145+ , _mmvr_vars :: [MandrillVars]
205146 } deriving Show
206147
207148makeLenses ''MandrillMergeVars
@@ -209,33 +150,29 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209150
210151--------------------------------------------------------------------------------
211152data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
153+ _mmdt_rcpt :: !T.Text
154+ , _mmdt_values :: MandrillVars
214155 } deriving Show
215156
216157makeLenses ''MandrillMetadata
217158deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
218159
219160
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
161+newtype Base64ByteString = B64BS B.ByteString deriving Show
226162
227163instance ToJSON Base64ByteString where
228− toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229− toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
164+ toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
230165
231166instance FromJSON Base64ByteString where
232− parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
167+ parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
168+ Left err -> fail err
169+ Right rs -> return $ B64BS rs
233170 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
234171
235172--------------------------------------------------------------------------------
236173data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
174+ _mwct_type :: !T.Text
175+ , _mwct_name :: !T.Text
239176 -- ^ [for images] the Content ID of the image
240177 -- - use <img src="cid:THIS_VALUE"> to reference the image
241178 -- in your HTML content
@@ -248,67 +185,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
248185--------------------------------------------------------------------------------
249186-- | The information on the message to send
250187data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
188+ _mmsg_html :: MandrillHtml
252189 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
190+ , _mmsg_text :: Maybe T.Text
254191 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
192+ , _mmsg_subject :: !T.Text
256193 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
194+ , _mmsg_from_email :: !T.Text
258195 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
196+ , _mmsg_from_name :: Maybe T.Text
260197 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
198+ , _mmsg_to :: [MandrillRecipient]
262199 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
200+ , _mmsg_headers :: MandrillHeaders
264201 -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
202+ , _mmsg_important :: Maybe Bool
266203 -- ^ whether or not this message is important, and should be delivered ahead
267204 -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
205+ , _mmsg_track_opens :: Maybe Bool
269206 -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
207+ , _mmsg_track_clicks :: Maybe Bool
271208 -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
209+ , _mmsg_auto_text :: Maybe Bool
273210 -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
211+ , _mmsg_auto_html :: Maybe Bool
275212 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
213+ , _mmsg_inline_css :: Maybe Bool
277214 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278215 -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
216+ , _mmsg_url_strip_qs :: Maybe Bool
280217 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
218+ , _mmsg_preserve_recipients :: Maybe Bool
282219 -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
220+ , _mmsg_view_content_link :: Maybe Bool
284221 -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
222+ , _mmsg_bcc_address :: Maybe T.Text
286223 -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
224+ , _mmsg_tracking_domain :: Maybe T.Text
288225 -- ^ 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
226+ , _mmsg_signing_domain :: Maybe Bool
227+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291228 -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
229+ , _mmsg_return_path_domain :: Maybe Bool
293230 -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
231+ , _mmsg_merge :: Maybe Bool
295232 -- ^ whether to evaluate merge tags in the message.
296233 -- Will automatically be set to true if either merge_vars
297234 -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
235+ , _mmsg_global_merge_vars :: [MandrillVars]
299236 -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
237+ , _mmsg_merge_vars :: [MandrillMergeVars]
301238 -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
239+ , _mmsg_tags :: [MandrillTags]
303240 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304241 -- though we only store the first 100 we see, so this should not be unique
305242 -- or change frequently. Tags should be 50 characters or less.
306243 -- Any tags starting with an underscore are reserved for internal use
307244 -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
245+ , _mmsg_subaccount :: Maybe T.Text
309246 -- ^ the unique id of a subaccount for this message
310247 -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
248+ , _mmsg_google_analytics_domains :: [T.Text]
312249 -- ^ an array of strings indicating for which any matching URLs
313250 -- will automatically have Google Analytics parameters appended
314251 -- to their query string automatically.
@@ -316,17 +253,17 @@ data MandrillMessage = MandrillMessage {
316253 -- ^ optional string indicating the value to set for the utm_campaign
317254 -- tracking parameter. If this isn't provided the email's from address
318255 -- will be used instead.
319− , _mmsg_metadata :: Object
256+ , _mmsg_metadata :: MandrillVars
320257 -- ^ metadata an associative array of user metadata. Mandrill will store
321258 -- this metadata and make it available for retrieval.
322259 -- In addition, you can select up to 10 metadata fields to index
323260 -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
261+ , _mmsg_recipient_metadata :: [MandrillMetadata]
325262 -- ^ Per-recipient metadata that will override the global values
326263 -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
264+ , _mmsg_attachments :: [MandrillWebContent]
328265 -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
266+ , _mmsg_images :: [MandrillWebContent]
330267 -- ^ an array of embedded images to add to the message
331268 } deriving Show
332269
@@ -337,10 +274,10 @@ instance Arbitrary MandrillMessage where
337274 arbitrary = MandrillMessage <$> arbitrary
338275 <*> pure Nothing
339276 <*> pure "Test Subject"
340− <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
277+ <*> pure "sender@example.com"
341278 <*> pure Nothing
342279 <*> resize 2 arbitrary
343− <*> pure H.empty
280+ <*> pure emptyObject
344281 <*> pure Nothing
345282 <*> pure Nothing
346283 <*> pure Nothing
@@ -361,24 +298,13 @@ instance Arbitrary MandrillMessage where
361298 <*> pure Nothing
362299 <*> pure []
363300 <*> pure Nothing
364− <*> pure H.empty
301+ <*> pure emptyObject
365302 <*> pure []
366303 <*> pure []
367304 <*> pure []
368305
369−--------------------------------------------------------------------------------
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
375−
376−makeLenses ''MandrillTemplateContent
377−deriveJSON defaultOptions { fieldLabelModifier = drop 5 } ''MandrillTemplateContent
378−
379306--------------------------------------------------------------------------------
380307type MandrillKey = T.Text
381−type MandrillTemplate = T.Text
382308
383309newtype MandrillDate = MandrillDate {
384310 fromMandrillDate :: UTCTime
@@ -389,6 +315,6 @@ instance ToJSON MandrillDate where
389315
390316instance FromJSON MandrillDate where
391317 parseJSON = withText "MandrillDate" $ \t ->
392− case timeParse defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
318+ case parseTime defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
393319 Just d -> pure $ MandrillDate d
394320 _ -> fail "could not parse Mandrill date"