@@ -1,4 +1,5 @@
1{-# LANGUAGE TemplateHaskell #-}
1{-# LANGUAGE TemplateHaskell #-}
2+{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
4module Network.API.Mandrill.Types where
5module Network.API.Mandrill.Types where
@@ -6,6 +7,8 @@ module Network.API.Mandrill.Types where
6import Network.API.Mandrill.Utils
7import Network.API.Mandrill.Utils
7import Data.Char
8import Data.Char
8import Data.Time
9import Data.Time
10+import Control.Applicative
11+import System.Locale (defaultTimeLocale)
9import qualified Data.Text as T
12import qualified Data.Text as T
10import qualified Data.Text.Lazy as TL
13import qualified Data.Text.Lazy as TL
11import Control.Lens
14import Control.Lens
@@ -56,32 +59,17 @@ deriveJSON defaultOptions {
56 } ''MandrillRejectReason
59 } ''MandrillRejectReason
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
71−makeLenses ''MandrillResult
72−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillResult
74--------------------------------------------------------------------------------
62--------------------------------------------------------------------------------
75-- | The main datatypes which models the response from the Mandrill API,
63-- | The main datatypes which models the response from the Mandrill API,
76-- which can be either a success or a failure.
64-- which can be either a success or a failure.
77−data MandrillResponse =
65+data MandrillResponse k =
78− MandrillSuccess [MandrillResult]
66+ MandrillSuccess [k]
79 | MandrillFailure MandrillError deriving Show
67 | MandrillFailure MandrillError deriving Show
81−instance FromJSON MandrillResponse where
69+instance FromJSON k => FromJSON (MandrillResponse k) where
82− parseJSON a@(Array _) = case (parseMaybe parseJSON a) :: Maybe [MandrillResult] of
70+ parseJSON a@(Array _) = case (parseMaybe parseJSON a) :: Maybe [k] of
83 Just r -> return $ MandrillSuccess r
71 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"
85 parseJSON o@(Object _) = case (parseMaybe parseJSON o) :: Maybe MandrillError of
73 parseJSON o@(Object _) = case (parseMaybe parseJSON o) :: Maybe MandrillError of
86 Just e -> return $ MandrillFailure e
74 Just e -> return $ MandrillFailure e
87 Nothing -> fail $ show o <> " is not a valid MandrillError"
75 Nothing -> fail $ show o <> " is not a valid MandrillError"
@@ -151,23 +139,18 @@ makeLenses ''MandrillMessage
151deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
139deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMessage
154−--------------------------------------------------------------------------------
156−--------------------------------------------------------------------------------
159--------------------------------------------------------------------------------
142--------------------------------------------------------------------------------
160type MandrillKey = T.Text
143type MandrillKey = T.Text
162−
145+newtype MandrillDate = MandrillDate {
163−--------------------------------------------------------------------------------
146+ fromMandrillDate :: UTCTime
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
170 } deriving Show
147 } deriving Show
172−makeLenses ''MandrillSendRq
149+instance ToJSON MandrillDate where
173−deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillSendRq
150+ toJSON = toJSON . fromMandrillDate
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"