@@ -1,44 +1,34 @@
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
2014#if MIN_VERSION_time(1,5,0)
21−import Data.Time.Format (TimeLocale, defaultTimeLocale)
15+import Data.Time.Format (TimeLocale, defaultTimeLocale)
2216#else
23−import System.Locale (TimeLocale, defaultTimeLocale)
17+import System.Locale (TimeLocale, defaultTimeLocale)
2418#endif
19+import qualified Data.ByteString as B
20+import qualified Data.ByteString.Base64 as Base64
21+import qualified Data.Text as T
22+import qualified Data.Text.Encoding as TL
23+import qualified Data.Text.Lazy as TL
24+import Control.Lens
25+import Data.Monoid
2526import Data.Aeson
26−import Data.Aeson.TH
2727import 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
28+import Data.Aeson.TH
29+import qualified Text.Blaze.Html as Blaze
4030import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
31+
4232
4333timeParse :: ParseTime t => TimeLocale -> String -> String -> Maybe t
4434#if MIN_VERSION_time(1,5,0)
@@ -49,11 +39,11 @@ timeParse = parseTime
4939
5040--------------------------------------------------------------------------------
5141data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
42+ _merr_status :: !T.Text
43+ , _merr_code :: !Int
44+ , _merr_name :: !T.Text
5545 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
46+ } deriving Show
5747
5848makeLenses ''MandrillError
5949deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +68,6 @@ data MandrillRejectReason = RR_HardBounce
7868 | RR_InvalidSender
7969 | RR_Invalid
8070 | RR_TestModeLimit
81− | RR_Unsigned
8271 | RR_Rule deriving Show
8372
8473deriveJSON defaultOptions {
@@ -91,7 +80,7 @@ deriveJSON defaultOptions {
9180-- which can be either a success or a failure.
9281data MandrillResponse k =
9382 MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
83+ | MandrillFailure MandrillError deriving Show
9584
9685instance FromJSON k => FromJSON (MandrillResponse k) where
9786 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -127,9 +116,9 @@ instance FromJSON MandrillEmail where
127116data MandrillRecipient = MandrillRecipient {
128117 _mrec_email :: MandrillEmail
129118 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
119+ , _mrec_name :: Maybe T.Text
131120 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
121+ , _mrec_type :: Maybe MandrillRecipientTag
133122 -- ^ The header type to use for the recipient.
134123 -- defaults to "to" if not provided
135124 } deriving Show
@@ -158,11 +147,6 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
158147mkMandrillHtml :: Blaze.Html -> MandrillHtml
159148mkMandrillHtml = MandrillHtml
160149
161−#if MIN_VERSION_base(4,11,0)
162−instance Semigroup MandrillHtml where
163− MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164−#endif
165−
166150instance Monoid MandrillHtml where
167151 mempty = MandrillHtml mempty
168152 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -185,23 +169,17 @@ type MandrillTags = T.Text
185169
186170
187171--------------------------------------------------------------------------------
188−type MandrillHeaders = Object
172+type MandrillHeaders = Value
189173
190174
191175--------------------------------------------------------------------------------
176+type MandrillVars = Value
192177
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
200178
201179--------------------------------------------------------------------------------
202180data MandrillMergeVars = MandrillMergeVars {
203181 _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
182+ , _mmvr_vars :: [MandrillVars]
205183 } deriving Show
206184
207185makeLenses ''MandrillMergeVars
@@ -209,33 +187,29 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209187
210188--------------------------------------------------------------------------------
211189data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
190+ _mmdt_rcpt :: !T.Text
191+ , _mmdt_values :: MandrillVars
214192 } deriving Show
215193
216194makeLenses ''MandrillMetadata
217195deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
218196
219197
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
198+newtype Base64ByteString = B64BS B.ByteString deriving Show
226199
227200instance ToJSON Base64ByteString where
228− toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229− toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
201+ toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
230202
231203instance FromJSON Base64ByteString where
232− parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
204+ parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
205+ Left err -> fail err
206+ Right rs -> return $ B64BS rs
233207 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
234208
235209--------------------------------------------------------------------------------
236210data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
211+ _mwct_type :: !T.Text
212+ , _mwct_name :: !T.Text
239213 -- ^ [for images] the Content ID of the image
240214 -- - use <img src="cid:THIS_VALUE"> to reference the image
241215 -- in your HTML content
@@ -248,67 +222,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
248222--------------------------------------------------------------------------------
249223-- | The information on the message to send
250224data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
225+ _mmsg_html :: MandrillHtml
252226 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
227+ , _mmsg_text :: Maybe T.Text
254228 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
229+ , _mmsg_subject :: !T.Text
256230 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
231+ , _mmsg_from_email :: MandrillEmail
258232 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
233+ , _mmsg_from_name :: Maybe T.Text
260234 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
235+ , _mmsg_to :: [MandrillRecipient]
262236 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
237+ , _mmsg_headers :: MandrillHeaders
264238 -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
239+ , _mmsg_important :: Maybe Bool
266240 -- ^ whether or not this message is important, and should be delivered ahead
267241 -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
242+ , _mmsg_track_opens :: Maybe Bool
269243 -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
244+ , _mmsg_track_clicks :: Maybe Bool
271245 -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
246+ , _mmsg_auto_text :: Maybe Bool
273247 -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
248+ , _mmsg_auto_html :: Maybe Bool
275249 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
250+ , _mmsg_inline_css :: Maybe Bool
277251 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278252 -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
253+ , _mmsg_url_strip_qs :: Maybe Bool
280254 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
255+ , _mmsg_preserve_recipients :: Maybe Bool
282256 -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
257+ , _mmsg_view_content_link :: Maybe Bool
284258 -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
259+ , _mmsg_bcc_address :: Maybe T.Text
286260 -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
261+ , _mmsg_tracking_domain :: Maybe T.Text
288262 -- ^ 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
263+ , _mmsg_signing_domain :: Maybe Bool
264+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291265 -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
266+ , _mmsg_return_path_domain :: Maybe Bool
293267 -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
268+ , _mmsg_merge :: Maybe Bool
295269 -- ^ whether to evaluate merge tags in the message.
296270 -- Will automatically be set to true if either merge_vars
297271 -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
272+ , _mmsg_global_merge_vars :: [MandrillVars]
299273 -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
274+ , _mmsg_merge_vars :: [MandrillMergeVars]
301275 -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
276+ , _mmsg_tags :: [MandrillTags]
303277 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304278 -- though we only store the first 100 we see, so this should not be unique
305279 -- or change frequently. Tags should be 50 characters or less.
306280 -- Any tags starting with an underscore are reserved for internal use
307281 -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
282+ , _mmsg_subaccount :: Maybe T.Text
309283 -- ^ the unique id of a subaccount for this message
310284 -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
285+ , _mmsg_google_analytics_domains :: [T.Text]
312286 -- ^ an array of strings indicating for which any matching URLs
313287 -- will automatically have Google Analytics parameters appended
314288 -- to their query string automatically.
@@ -316,17 +290,17 @@ data MandrillMessage = MandrillMessage {
316290 -- ^ optional string indicating the value to set for the utm_campaign
317291 -- tracking parameter. If this isn't provided the email's from address
318292 -- will be used instead.
319− , _mmsg_metadata :: Object
293+ , _mmsg_metadata :: MandrillVars
320294 -- ^ metadata an associative array of user metadata. Mandrill will store
321295 -- this metadata and make it available for retrieval.
322296 -- In addition, you can select up to 10 metadata fields to index
323297 -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
298+ , _mmsg_recipient_metadata :: [MandrillMetadata]
325299 -- ^ Per-recipient metadata that will override the global values
326300 -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
301+ , _mmsg_attachments :: [MandrillWebContent]
328302 -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
303+ , _mmsg_images :: [MandrillWebContent]
330304 -- ^ an array of embedded images to add to the message
331305 } deriving Show
332306
@@ -340,7 +314,7 @@ instance Arbitrary MandrillMessage where
340314 <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
341315 <*> pure Nothing
342316 <*> resize 2 arbitrary
343− <*> pure H.empty
317+ <*> pure emptyObject
344318 <*> pure Nothing
345319 <*> pure Nothing
346320 <*> pure Nothing
@@ -361,7 +335,7 @@ instance Arbitrary MandrillMessage where
361335 <*> pure Nothing
362336 <*> pure []
363337 <*> pure Nothing
364− <*> pure H.empty
338+ <*> pure emptyObject
365339 <*> pure []
366340 <*> pure []
367341 <*> pure []