@@ -1,4 +1,5 @@
11{-# LANGUAGE TemplateHaskell #-}
2+{-# LANGUAGE RankNTypes #-}
23{-# LANGUAGE ScopedTypeVariables #-}
34{-# LANGUAGE OverloadedStrings #-}
45module Network.API.Mandrill.Types where
@@ -6,6 +7,8 @@ module Network.API.Mandrill.Types where
67import Network.API.Mandrill.Utils
78import Data.Char
89import Data.Time
10+import Control.Applicative
11+import System.Locale (defaultTimeLocale)
912import qualified Data.Text as T
1013import qualified Data.Text.Lazy as TL
1114import Control.Lens
@@ -56,32 +59,17 @@ deriveJSON defaultOptions {
5659 } ''MandrillRejectReason
5760
5861
59−--------------------------------------------------------------------------------
60−data MandrillResult = MandrillResult {
61− _mres_email :: !T.Text
62− -- ^ The email address of the recipient
63− , _mres_status :: MandrillEmailStatus
64− -- ^ The sending status of the recipient
65− , _mres_reject_reason :: Maybe MandrillRejectReason
66− -- ^ The reason for the rejection if the recipient status is "rejected"
67− , _mres__id :: !T.Text
68− -- ^ The message's unique id
69− } deriving Show
70−
71−makeLenses ''MandrillResult
72−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillResult
73−
7462--------------------------------------------------------------------------------
7563-- | The main datatypes which models the response from the Mandrill API,
7664-- which can be either a success or a failure.
77−data MandrillResponse =
78− MandrillSuccess [MandrillResult]
65+data MandrillResponse k =
66+ MandrillSuccess [k]
7967 | MandrillFailure MandrillError deriving Show
8068
81−instance FromJSON MandrillResponse where
82− parseJSON a@(Array _) = case (parseMaybe parseJSON a) :: Maybe [MandrillResult] of
69+instance FromJSON k => FromJSON (MandrillResponse k) where
70+ parseJSON a@(Array _) = case (parseMaybe parseJSON a) :: Maybe [k] of
8371 Just r -> return $ MandrillSuccess r
84− Nothing -> fail $ show a <> " is not a valid MandrillResult"
72+ Nothing -> fail $ show a <> " is not a valid MandrillSuccess"
8573 parseJSON o@(Object _) = case (parseMaybe parseJSON o) :: Maybe MandrillError of
8674 Just e -> return $ MandrillFailure e
8775 Nothing -> fail $ show o <> " is not a valid MandrillError"
@@ -151,23 +139,18 @@ makeLenses ''MandrillMessage
151139deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
152140
153141
154−--------------------------------------------------------------------------------
155−-- * Send payloads
156−--------------------------------------------------------------------------------
157−
158−
159142--------------------------------------------------------------------------------
160143type MandrillKey = T.Text
161144
162−
163−--------------------------------------------------------------------------------
164−data MandrillSendRq = MandrillSendRq {
165− _msrq_key :: MandrillKey
166− , _msrq_message :: MandrillMessage
167− , _msrq_async :: Maybe Bool
168− , _msrq_ip_pool :: Maybe T.Text
169− , _msrq_send_at :: Maybe UTCTime
145+newtype MandrillDate = MandrillDate {
146+ fromMandrillDate :: UTCTime
170147 } deriving Show
171148
172−makeLenses ''MandrillSendRq
173−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillSendRq
149+instance ToJSON MandrillDate where
150+ toJSON = toJSON . fromMandrillDate
151+
152+instance FromJSON MandrillDate where
153+ parseJSON = withText "MandrillDate" $ \t ->
154+ case parseTime defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
155+ Just d -> pure $ MandrillDate d
156+ _ -> fail "could not parse Mandrill date"