git.iko.soy/typeable/mandrill
on master help sign in
.gitignore+2
since splitsnapshots
unifiedsplit
@@ -2,3 +2,5 @@ dist
2.cabal-sandbox
2.cabal-sandbox
3cabal.sandbox.config
3cabal.sandbox.config
4shell.nix
4shell.nix
5+.stack-work
6+tags
.hub+1−1
@@ -1 +1 @@
1−mandrill
1+mandrill-7.10.1
.travis.yml+6−4
@@ -5,15 +5,17 @@ notifications:
5 email: false
5 email: false
6
6
7env:
7env:
8− - GHCVER=7.6.2 CABALVER=1.16
9 - GHCVER=7.6.3 CABALVER=1.16
8 - GHCVER=7.6.3 CABALVER=1.16
10 - GHCVER=7.8.2 CABALVER=1.18
9 - GHCVER=7.8.2 CABALVER=1.18
11 - GHCVER=7.8.3 CABALVER=1.18
10 - GHCVER=7.8.3 CABALVER=1.18
11+ - GHCVER=7.10.2 CABALVER=1.22
12+ - GHCVER=8.0.1 CABALVER=1.22
12− - GHCVER=head CABALVER=1.20
13+ - GHCVER=head CABALVER=1.22
13
14
14matrix:
15matrix:
15 allow_failures:
16 allow_failures:
17+ - env: GHCVER=8.0.1 CABALVER=1.22
16− - env: GHCVER=head CABALVER=1.20
18+ - env: GHCVER=head CABALVER=1.22
17
19
18before_install:
20before_install:
19 - travis_retry sudo add-apt-repository -y ppa:hvr/ghc
21 - travis_retry sudo add-apt-repository -y ppa:hvr/ghc
@@ -46,4 +48,4 @@ script:
46after_script:
48after_script:
47 - cabal install hpc-coveralls
49 - cabal install hpc-coveralls
48 - export PATH=/opt/ghc/$GHCVER/bin:$HOME/.cabal/bin:$PATH
50 - export PATH=/opt/ghc/$GHCVER/bin:$HOME/.cabal/bin:$PATH
49− - hpc-coveralls --coverage-mode=StrictlyFullLines mandrill-tests
51+ - hpc-coveralls --coverage-mode=StrictlyFullLines mandrill-tests
README.md+75
@@ -6,6 +6,73 @@
6This module implement a low-level, 1:1 mapping API to
6This module implement a low-level, 1:1 mapping API to
7the [Mandrill](http://mandrillapp.com) transactional email service.
7the [Mandrill](http://mandrillapp.com) transactional email service.
8
8
9+# Changelog
10+
11+## Version 0.5.3.5
12+
13+* Replaced dependency on lens with microlens (Courtesy of @tom-bop)
14+
15+## Version 0.5.3.3
16+
17+* Relaxed upper bound on Aeson (see: fpco/stackage/issues/3337)
18+
19+## Version 0.5.3.2
20+
21+* Relaxed upper bound on Aeson (see: fpco/stackage/issues/2449)
22+
23+## Version 0.5.3.1
24+
25+* Relaxed upper bound on Aeson (see: fpco/stackage/issues/2177 )
26+
27+## Version 0.5.3.0
28+
29+* Added support for webhooks (thanks to @mwotton).
30+
31+## Version 0.5.2.3
32+
33+* Added support for aeson-1.0.0.0 by relaxing its upper bound.
34+
35+## Version 0.5.2.2
36+
37+* Added support for QuickCheck-2.9 by relaxing its upper bound.
38+
39+## Version 0.5.2.1
40+
41+* Added `Functor`, `Foldable` and `Traversable` instances to `MandrillResponse` (Courtesy of @dredozubov)
42+
43+## Version 0.5.2.0
44+
45+* Added inbound calls (Courtesy of @mwotton)
46+
47+## Version 0.5.1.0
48+
49+* Relaxed the constraint on `aeson` to allow `0.11.0.0`.
50+
51+## Version 0.5.0.0
52+
53+* Changed the `MandrillHeaders` type synonym from `Value` to `Object`.
54+
55+* Changed the `mmsg_metadata` and `mmdt_values` fields from `MandrillVars` to `Object`.
56+
57+* Changed the `mmsg_global_merge_vars` and `mmvr_vars` fields from `[MandrillVars]` to `[MergeVar]`
58+
59+* Added the `MergeVar` data type:
60+
61+```haskell
62+data MergeVar = MergeVar {
63+ _mv_name :: !Text
64+ , _mv_content :: Value
65+ }
66+```
67+
68+* Removed the `MandrillVars` type synonym.
69+
70+## Version 0.4.0.0
71+
72+* Modified the `Base64ByteString` type to accept another constructor. This
73+ allows the user to pass already-encoded Base64 strings which might be coming
74+ upstream.
75+
9# Example
76# Example
10
77
11This package was built with pragmatism and reuse in mind. This means
78This package was built with pragmatism and reuse in mind. This means
@@ -38,9 +105,17 @@ main = do
38 + ~~ping.json~~ (as **doesn't return valid json!**)
105 + ~~ping.json~~ (as **doesn't return valid json!**)
39 + ping2.json
106 + ping2.json
40 + senders.json
107 + senders.json
108+
41 - [Messages call](https://mandrillapp.com/api/docs/messages.JSON.html)
109 - [Messages call](https://mandrillapp.com/api/docs/messages.JSON.html)
42 + send.json
110 + send.json
43
111
112+ - [Senders call](https://mandrillapp.com/api/docs/senders.JSON.html)
113+ + verify-domain.json
114+
115+ - [Inbound call](https://mandrillapp.com/api/docs/inbound.JSON.html)
116+ + add-route.json
117+ + add-domain.json
118+
44# Testing online API
119# Testing online API
45
120
46To test the online API, first build the package with tests enabled:
121To test the online API, first build the package with tests enabled:
mandrill-nightly.ymlnew file+10
@@ -0,0 +1,10 @@
1+flags:
2+ semigroups:
3+ bytestring-builder: false
4+packages:
5+- '.'
6+extra-deps:
7+ - aeson-1.3.0.0
8+ - text-1.2.3.0
9+ - semigroups-0.18.2
10+resolver: lts-9.1
mandrill.cabal+10−6
@@ -1,5 +1,5 @@
1name: mandrill
1name: mandrill
2−version: 0.2.0.0
2+version: 0.5.3.5
3synopsis: Library for interfacing with the Mandrill JSON API
3synopsis: Library for interfacing with the Mandrill JSON API
4description: Pure Haskell client for the Mandrill JSON API
4description: Pure Haskell client for the Mandrill JSON API
5license: MIT
5license: MIT
@@ -8,7 +8,7 @@ author: Alfredo Di Napoli
8maintainer: alfredo.dinapoli@gmail.com
8maintainer: alfredo.dinapoli@gmail.com
9category: Network
9category: Network
10build-type: Simple
10build-type: Simple
11−tested-with: GHC == 7.4, GHC == 7.6, GHC == 7.8
11+tested-with: GHC == 7.4, GHC == 7.6, GHC == 7.8, GHC == 7.10.2
12cabal-version: >=1.10
12cabal-version: >=1.10
13
13
14source-repository head
14source-repository head
@@ -25,10 +25,12 @@ library
25 Network.API.Mandrill.Users.Types
25 Network.API.Mandrill.Users.Types
26 Network.API.Mandrill.Messages
26 Network.API.Mandrill.Messages
27 Network.API.Mandrill.Messages.Types
27 Network.API.Mandrill.Messages.Types
28+ Network.API.Mandrill.Inbound
29+ Network.API.Mandrill.Webhooks
30+ Network.API.Mandrill.Senders
28 other-modules:
31 other-modules:
29 Network.API.Mandrill.Utils
32 Network.API.Mandrill.Utils
30 Network.API.Mandrill.HTTP
33 Network.API.Mandrill.HTTP
31− Network.API.Mandrill.Orphans
32 -- other-extensions:
34 -- other-extensions:
33 build-depends:
35 build-depends:
34 base >=4.6 && < 5
36 base >=4.6 && < 5
@@ -39,18 +41,20 @@ library
39 , http-types >= 0.8.0
41 , http-types >= 0.8.0
40 , http-client >= 0.3.0
42 , http-client >= 0.3.0
41 , http-client-tls >= 0.2.0.0
43 , http-client-tls >= 0.2.0.0
42− , aeson >= 0.7.0.3 && < 0.9
44+ , aeson >= 0.7.0.3 && < 1.5.0.0
43− , lens >= 4.0
45+ , microlens-th >= 0.4.0.0
44 , blaze-html >= 0.5.0.0
46 , blaze-html >= 0.5.0.0
45− , QuickCheck >= 2.6 && < 2.8
47+ , QuickCheck >= 2.6 && < 3.0
46 , mtl < 3.0
48 , mtl < 3.0
47 , time
49 , time
48 , email-validate >= 1.0.0
50 , email-validate >= 1.0.0
49 , old-locale
51 , old-locale
52+ , unordered-containers
50 hs-source-dirs:
53 hs-source-dirs:
51 src
54 src
52 default-language:
55 default-language:
53 Haskell2010
56 Haskell2010
57+
54 ghc-options:
58 ghc-options:
55 -funbox-strict-fields
59 -funbox-strict-fields
56
60
src/Network/API/Mandrill.hs+35−21
@@ -9,27 +9,29 @@
9module Network.API.Mandrill (
9module Network.API.Mandrill (
10 module M
10 module M
11 , sendEmail
11 , sendEmail
12+ , sendTextEmail
12 , emptyMessage
13 , emptyMessage
13 , newTextMessage
14 , newTextMessage
14 , newHtmlMessage
15 , newHtmlMessage
16+ , newTemplateMessage
15 , liftIO
17 , liftIO
16
18
17 -- * Appendix: Example Usage
19 -- * Appendix: Example Usage
18 -- $exampleusage
20 -- $exampleusage
19 ) where
21 ) where
20
22
21−import Control.Monad.Reader
23+import Control.Monad.Reader
22−import Control.Lens
24+import qualified Data.Aeson as JSON
25+import qualified Data.HashMap.Strict as H
26+import Data.Monoid
27+import qualified Data.Text as T
23−import Data.Time
28+import Data.Time
29+import Network.API.Mandrill.Messages as M
30+import Network.API.Mandrill.Messages.Types as M
31+import Network.API.Mandrill.Trans as M
32+import Network.API.Mandrill.Types as M
24−import Text.Blaze.Html
33+import Text.Blaze.Html
25−import Network.API.Mandrill.Types as M
26−import Network.API.Mandrill.Messages as M
27−import Network.API.Mandrill.Messages.Types as M
28−import Network.API.Mandrill.Trans as M
29−import Data.Monoid
30−import Text.Email.Validate
34+import Text.Email.Validate
31−import qualified Data.Text as T
32−import qualified Data.Aeson as JSON
33
35
34{- $exampleusage
36{- $exampleusage
35
37
@@ -38,7 +40,7 @@ The API was designed to allow to get you started as quickly as possible:
38> {-# LANGUAGE OverloadedStrings #-}
40> {-# LANGUAGE OverloadedStrings #-}
39> import Text.Email.Validate
41> import Text.Email.Validate
40> import Network.API.Mandrill
42> import Network.API.Mandrill
41−>
43+>
42> main :: IO ()
44> main :: IO ()
43> main = do
45> main = do
44> case validate "foo@example.com" of
46> case validate "foo@example.com" of
@@ -61,10 +63,10 @@ emptyMessage f t = MandrillMessage {
61 _mmsg_html = mempty
63 _mmsg_html = mempty
62 , _mmsg_text = Nothing
64 , _mmsg_text = Nothing
63 , _mmsg_subject = T.empty
65 , _mmsg_subject = T.empty
64− , _mmsg_from_email = f
66+ , _mmsg_from_email = (MandrillEmail f)
65 , _mmsg_from_name = Nothing
67 , _mmsg_from_name = Nothing
66 , _mmsg_to = map newRecipient t
68 , _mmsg_to = map newRecipient t
67− , _mmsg_headers = JSON.Null
69+ , _mmsg_headers = H.empty
68 , _mmsg_important = Nothing
70 , _mmsg_important = Nothing
69 , _mmsg_track_opens = Nothing
71 , _mmsg_track_opens = Nothing
70 , _mmsg_track_clicks = Nothing
72 , _mmsg_track_clicks = Nothing
@@ -85,7 +87,7 @@ emptyMessage f t = MandrillMessage {
85 , _mmsg_subaccount = Nothing
87 , _mmsg_subaccount = Nothing
86 , _mmsg_google_analytics_domains = []
88 , _mmsg_google_analytics_domains = []
87 , _mmsg_google_analytics_campaign = Nothing
89 , _mmsg_google_analytics_campaign = Nothing
88− , _mmsg_metadata = JSON.Null
90+ , _mmsg_metadata = H.empty
89 , _mmsg_recipient_metadata = []
91 , _mmsg_recipient_metadata = []
90 , _mmsg_attachments = []
92 , _mmsg_attachments = []
91 , _mmsg_images = []
93 , _mmsg_images = []
@@ -104,8 +106,18 @@ newHtmlMessage :: EmailAddress
104 -- ^ The HTML body
106 -- ^ The HTML body
105 -> MandrillMessage
107 -> MandrillMessage
106newHtmlMessage f t subj html = let body = mkMandrillHtml html in
108newHtmlMessage f t subj html = let body = mkMandrillHtml html in
107− ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t)
109+ (emptyMessage f t) { _mmsg_html = body, _mmsg_subject = subj }
108
110
111+--------------------------------------------------------------------------------
112+-- | Create a new template message (no HTML).
113+newTemplateMessage :: EmailAddress
114+ -- ^ Sender email
115+ -> [EmailAddress]
116+ -- ^ Receivers email
117+ -> T.Text
118+ -- ^ Subject
119+ -> MandrillMessage
120+newTemplateMessage f t subj = (emptyMessage f t) { _mmsg_subject = subj }
109
121
110--------------------------------------------------------------------------------
122--------------------------------------------------------------------------------
111-- | Create a new textual message. By default Mandrill doesn't require you
123-- | Create a new textual message. By default Mandrill doesn't require you
@@ -121,9 +133,11 @@ newTextMessage :: EmailAddress
121 -- ^ The body, as normal text.
133 -- ^ The body, as normal text.
122 -> MandrillMessage
134 -> MandrillMessage
123newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
135newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
124− ((mmsg_html .~ body) .
136+ (emptyMessage f t) {
137+ _mmsg_html = body
125− (mmsg_text .~ Just txt) .
138+ , _mmsg_text = Just txt
126− (mmsg_subject .~ subj)) (emptyMessage f t)
139+ , _mmsg_subject = subj
140+ }
127
141
128
142
129--------------------------------------------------------------------------------
143--------------------------------------------------------------------------------
@@ -131,7 +145,7 @@ newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
131-- 'MandrillMessage' and this function will send an email inside a
145-- 'MandrillMessage' and this function will send an email inside a
132-- 'MandrillT' transformer. You are not forced to use the 'MandrillT' context
146-- 'MandrillT' transformer. You are not forced to use the 'MandrillT' context
133-- though. Have a look at "Network.API.Mandrill.Messages" for an IO-based,
147-- though. Have a look at "Network.API.Mandrill.Messages" for an IO-based,
134−-- low lever function for sending email.
148+-- low level function for sending email.
135sendEmail :: MonadIO m
149sendEmail :: MonadIO m
136 => MandrillMessage
150 => MandrillMessage
137 -> MandrillT m (MandrillResponse [MessagesResponse])
151 -> MandrillT m (MandrillResponse [MessagesResponse])
src/Network/API/Mandrill/HTTP.hs+9−9
@@ -1,15 +1,15 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Network.API.Mandrill.HTTP where
2module Network.API.Mandrill.HTTP where
3
3
4−import Network.API.Mandrill.Settings
4+import Control.Applicative
5−import Network.API.Mandrill.Types
5+import Data.Aeson
6+import Data.Monoid
6−import qualified Data.Text as T
7+import qualified Data.Text as T
7−import Data.Monoid
8+import Network.API.Mandrill.Settings
8−import Data.Aeson
9−import Control.Applicative
10−import Network.HTTP.Types
9+import Network.API.Mandrill.Types
11−import Network.HTTP.Client
10+import Network.HTTP.Client
12−import Network.HTTP.Client.TLS
11+import Network.HTTP.Client.TLS
12+import Network.HTTP.Types
13
13
14toMandrillResponse :: (MandrillEndpoint ep, FromJSON a, ToJSON rq)
14toMandrillResponse :: (MandrillEndpoint ep, FromJSON a, ToJSON rq)
15 => ep
15 => ep
src/Network/API/Mandrill/Inbound.hsnew file+78
@@ -0,0 +1,78 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+module Network.API.Mandrill.Inbound where
3+import Data.Aeson (FromJSON, ToJSON, parseJSON,
4+ toJSON)
5+import Data.Aeson.TH (defaultOptions, deriveJSON)
6+import Data.Aeson.Types (fieldLabelModifier)
7+import Data.Text (Text)
8+import Lens.Micro.TH (makeLenses)
9+import Network.API.Mandrill.HTTP (toMandrillResponse)
10+import Network.API.Mandrill.Settings
11+import Network.API.Mandrill.Types
12+import Network.HTTP.Client (Manager)
13+
14+data DomainAddRq =
15+ DomainAddRq
16+ { _darq_key :: MandrillKey
17+ , _darq_domain :: Text
18+ } deriving Show
19+
20+makeLenses ''DomainAddRq
21+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''DomainAddRq
22+
23+data DomainAddResponse =
24+ DomainAddResponse
25+ { _dares_domain :: Text
26+ , _dares_created_at :: MandrillDate
27+ , _dares_valid_mx :: Bool
28+ } deriving Show
29+
30+
31+
32+makeLenses ''DomainAddResponse
33+deriveJSON defaultOptions { fieldLabelModifier = drop 7 } ''DomainAddResponse
34+
35+data RouteAddResponse =
36+ RouteAddResponse
37+ { _rares_id :: Text
38+ , _rares_pattern :: Text
39+ , _rares_url :: Text
40+ } deriving Show
41+
42+
43+
44+makeLenses ''RouteAddResponse
45+deriveJSON defaultOptions { fieldLabelModifier = drop 7 } ''RouteAddResponse
46+
47+data RouteAddRq =
48+ RouteAddRq
49+ { _rarq_key :: Text
50+ , _rarq_domain :: Text
51+ , _rarq_pattern :: Text
52+ , _rarq_url :: Text
53+ } deriving Show
54+
55+makeLenses ''RouteAddRq
56+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''RouteAddRq
57+
58+addDomain :: MandrillKey
59+ -- ^ The API key
60+ -> Text
61+ -- ^ The domain to add
62+ -> Maybe Manager
63+ -> IO (MandrillResponse DomainAddResponse)
64+addDomain k dom = toMandrillResponse DomainsAdd (DomainAddRq k dom)
65+
66+
67+
68+addRoute :: MandrillKey
69+ -- ^ The API key
70+ -> Text
71+ -- ^ The domain to add
72+ -> Text
73+ -- ^ the pattern including wildcards
74+ -> Text
75+ -- ^ URL to forward to
76+ -> Maybe Manager
77+ -> IO (MandrillResponse RouteAddResponse)
78+addRoute k dom pattern forward = toMandrillResponse RoutesAdd (RouteAddRq k dom pattern forward)
src/Network/API/Mandrill/Messages.hs+23−4
@@ -1,13 +1,13 @@
1
1
2module Network.API.Mandrill.Messages where
2module Network.API.Mandrill.Messages where
3
3
4+import qualified Data.Text as T
5+import Data.Time
4−import Network.API.Mandrill.Types
6+import Network.API.Mandrill.HTTP
5import Network.API.Mandrill.Messages.Types
7import Network.API.Mandrill.Messages.Types
6import Network.API.Mandrill.Settings
8import Network.API.Mandrill.Settings
7−import Network.API.Mandrill.HTTP
9+import Network.API.Mandrill.Types
8import Network.HTTP.Client
10import Network.HTTP.Client
9−import Data.Time
10−import qualified Data.Text as T
11
11
12--------------------------------------------------------------------------------
12--------------------------------------------------------------------------------
13-- | Send a new transactional message through Mandrill
13-- | Send a new transactional message through Mandrill
@@ -24,3 +24,22 @@ send :: MandrillKey
24 -> Maybe Manager
24 -> Maybe Manager
25 -> IO (MandrillResponse [MessagesResponse])
25 -> IO (MandrillResponse [MessagesResponse])
26send k msg async ip_pool send_at = toMandrillResponse MessagesSend (MessagesSendRq k msg async ip_pool send_at)
26send k msg async ip_pool send_at = toMandrillResponse MessagesSend (MessagesSendRq k msg async ip_pool send_at)
27+
28+-- | Send a new transactional message through Mandrill using a template
29+sendTemplate :: MandrillKey
30+ -- ^ The API key
31+ -> MandrillTemplate
32+ -- ^ The template name
33+ -> [MandrillTemplateContent]
34+ -- ^ Template content for 'editable regions'
35+ -> MandrillMessage
36+ -- ^ The email message
37+ -> Maybe Bool
38+ -- ^ Enable a background sending mode that is optimized for bulk sending
39+ -> Maybe T.Text
40+ -- ^ ip_pool
41+ -> Maybe UTCTime
42+ -- ^ send_at
43+ -> Maybe Manager
44+ -> IO (MandrillResponse [MessagesResponse])
45+sendTemplate k template content msg async ip_pool send_at = toMandrillResponse MessagesSendTemplate (MessagesSendTemplateRq k template content msg async ip_pool send_at)
src/Network/API/Mandrill/Messages/Types.hs+14−1
@@ -5,12 +5,12 @@ module Network.API.Mandrill.Messages.Types where
5import Data.Char
5import Data.Char
6import Data.Time
6import Data.Time
7import qualified Data.Text as T
7import qualified Data.Text as T
8−import Control.Lens
9import Control.Monad
8import Control.Monad
10import Data.Monoid
9import Data.Monoid
11import Data.Aeson
10import Data.Aeson
12import Data.Aeson.Types
11import Data.Aeson.Types
13import Data.Aeson.TH
12import Data.Aeson.TH
13+import Lens.Micro.TH (makeLenses)
14
14
15import Network.API.Mandrill.Types
15import Network.API.Mandrill.Types
16
16
@@ -27,6 +27,19 @@ data MessagesSendRq = MessagesSendRq {
27makeLenses ''MessagesSendRq
27makeLenses ''MessagesSendRq
28deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MessagesSendRq
28deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MessagesSendRq
29
29
30+--------------------------------------------------------------------------------
31+data MessagesSendTemplateRq = MessagesSendTemplateRq {
32+ _mstrq_key :: MandrillKey
33+ , _mstrq_template_name :: T.Text
34+ , _mstrq_template_content :: [MandrillTemplateContent]
35+ , _mstrq_message :: MandrillMessage
36+ , _mstrq_async :: Maybe Bool
37+ , _mstrq_ip_pool :: Maybe T.Text
38+ , _mstrq_send_at :: Maybe UTCTime
39+ } deriving Show
40+
41+makeLenses ''MessagesSendTemplateRq
42+deriveJSON defaultOptions { fieldLabelModifier = drop 7 } ''MessagesSendTemplateRq
30
43
31--------------------------------------------------------------------------------
44--------------------------------------------------------------------------------
32data MessagesResponse = MessagesResponse {
45data MessagesResponse = MessagesResponse {
src/Network/API/Mandrill/Orphans.hsdeleted−17
@@ -1,17 +0,0 @@
1−
2−module Network.API.Mandrill.Orphans where
3−
4−import Data.Aeson
5−import Data.Aeson.Types
6−import Text.Email.Validate
7−import qualified Data.Text.Encoding as TE
8−
9−
10−instance ToJSON EmailAddress where
11− toJSON = String . TE.decodeUtf8 . toByteString
12−
13−instance FromJSON EmailAddress where
14− parseJSON (String s) = case validate (TE.encodeUtf8 s) of
15− Left err -> fail err
16− Right v -> return v
17− parseJSON o = typeMismatch "Expecting a String for EmailAddress." o
src/Network/API/Mandrill/Senders.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+module Network.API.Mandrill.Senders where
3+import Data.Aeson (FromJSON, ToJSON, parseJSON,
4+ toJSON)
5+import Data.Aeson.TH (defaultOptions, deriveJSON)
6+import Data.Aeson.Types (Value (..), fieldLabelModifier)
7+import Data.Text (Text)
8+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
9+import Lens.Micro.TH (makeLenses)
10+import Network.API.Mandrill.HTTP (toMandrillResponse)
11+import Network.API.Mandrill.Settings
12+import Network.API.Mandrill.Types
13+import Network.HTTP.Client (Manager)
14+import qualified Text.Email.Validate as TEV
15+
16+data VerifyDomainRq =
17+ VerifyDomainRq
18+ { _vdrq_key :: MandrillKey
19+ , _vdrq_domain :: Text
20+ , _vdrq_mailbox :: Text
21+ } deriving Show
22+
23+makeLenses ''VerifyDomainRq
24+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''VerifyDomainRq
25+
26+data VerifyDomainResponse =
27+ VerifyDomainResponse
28+ { _vdres_status :: Text
29+ , _vdres_domain :: Text
30+ , _vdres_email :: MandrillEmail
31+ } deriving Show
32+
33+makeLenses ''VerifyDomainResponse
34+deriveJSON defaultOptions { fieldLabelModifier = drop 7 } ''VerifyDomainResponse
35+
36+
37+verifyDomain :: MandrillKey
38+ -- ^ The API key
39+ -> TEV.EmailAddress
40+ -- ^ Email address to use for verification
41+ -> Maybe Manager
42+ -> IO (MandrillResponse VerifyDomainResponse)
43+verifyDomain k email =
44+ toMandrillResponse VerifyDomain
45+ (VerifyDomainRq k (decodeUtf8 $ TEV.domainPart email) (decodeUtf8 $ TEV.localPart email))
src/Network/API/Mandrill/Settings.hs+17−7
@@ -15,16 +15,26 @@ data MandrillCalls =
15 -- Messages API
15 -- Messages API
16 | MessagesSend
16 | MessagesSend
17 | MessagesSendTemplate
17 | MessagesSendTemplate
18− | MessagesSearch deriving Show
18+ | MessagesSearch
19+ -- Inbound API
20+ | RoutesAdd
21+ | DomainsAdd
22+ -- Senders API
23+ | VerifyDomain
24+
25+ deriving Show
19
26
20class MandrillEndpoint ep where
27class MandrillEndpoint ep where
21 toUrl :: ep -> T.Text
28 toUrl :: ep -> T.Text
22
29
23instance MandrillEndpoint MandrillCalls where
30instance MandrillEndpoint MandrillCalls where
24− toUrl UsersInfo = "users/info.json"
31+ toUrl UsersInfo = "users/info.json"
25− toUrl UsersPing = "users/ping.json"
32+ toUrl UsersPing = "users/ping.json"
26− toUrl UsersPing2 = "users/ping2.json"
33+ toUrl UsersPing2 = "users/ping2.json"
27− toUrl UsersSenders = "users/senders.json"
34+ toUrl UsersSenders = "users/senders.json"
28− toUrl MessagesSend = "messages/send.json"
35+ toUrl MessagesSend = "messages/send.json"
29 toUrl MessagesSendTemplate = "messages/send-template.json"
36 toUrl MessagesSendTemplate = "messages/send-template.json"
30− toUrl MessagesSearch = "messages/search.json"
37+ toUrl MessagesSearch = "messages/search.json"
38+ toUrl DomainsAdd = "inbound/add-domain.json"
39+ toUrl RoutesAdd = "inbound/add-route.json"
40+ toUrl VerifyDomain = "senders/verify-domain.json"
src/Network/API/Mandrill/Types.hs+135−75
@@ -1,39 +1,59 @@
1−{-# LANGUAGE TemplateHaskell #-}
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveFoldable #-}
3+{-# LANGUAGE DeriveFunctor #-}
4+{-# LANGUAGE DeriveTraversable #-}
5+{-# LANGUAGE OverloadedStrings #-}
2−{-# LANGUAGE RankNTypes #-}
6+{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
4−{-# LANGUAGE OverloadedStrings #-}
8+{-# LANGUAGE TemplateHaskell #-}
5module Network.API.Mandrill.Types where
9module Network.API.Mandrill.Types where
6
10
7−import Network.API.Mandrill.Utils
11+import Control.Applicative
8−import Network.API.Mandrill.Orphans()
12+import Control.Monad (mzero)
9−import Test.QuickCheck
10−import Text.Email.Validate
11import Data.Char
13import Data.Char
12import Data.Maybe
14import Data.Maybe
13import Data.Time
15import Data.Time
14−import Control.Applicative
16+import Lens.Micro.TH (makeLenses)
17+import Network.API.Mandrill.Utils
18+import Test.QuickCheck
19+import Text.Email.Validate
20+#if MIN_VERSION_time(1,5,0)
21+import Data.Time.Format (TimeLocale, defaultTimeLocale)
22+#else
15−import System.Locale (defaultTimeLocale)
23+import System.Locale (TimeLocale, defaultTimeLocale)
16−import qualified Data.ByteString as B
24+#endif
17−import qualified Data.ByteString.Base64 as Base64
18−import qualified Data.Text as T
19−import qualified Data.Text.Encoding as TL
20−import qualified Data.Text.Lazy as TL
21−import Control.Lens
22−import Data.Monoid
23import Data.Aeson
25import Data.Aeson
24−import Data.Aeson.Types
25import Data.Aeson.TH
26import Data.Aeson.TH
27+import 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
26−import qualified Text.Blaze.Html as Blaze
39+import qualified Text.Blaze.Html as Blaze
27import qualified Text.Blaze.Html.Renderer.Text as Blaze
40import qualified Text.Blaze.Html.Renderer.Text as Blaze
41+import qualified Text.Email.Validate as TEV
28
42
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
29
49
30--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
31data MandrillError = MandrillError {
51data MandrillError = MandrillError {
32− _merr_status :: !T.Text
52+ _merr_status :: !T.Text
33− , _merr_code :: !Int
53+ , _merr_code :: !Int
34− , _merr_name :: !T.Text
54+ , _merr_name :: !T.Text
35 , _merr_message :: !T.Text
55 , _merr_message :: !T.Text
36− } deriving Show
56+ } deriving (Show, Eq)
37
57
38makeLenses ''MandrillError
58makeLenses ''MandrillError
39deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
59deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -58,6 +78,7 @@ data MandrillRejectReason = RR_HardBounce
58 | RR_InvalidSender
78 | RR_InvalidSender
59 | RR_Invalid
79 | RR_Invalid
60 | RR_TestModeLimit
80 | RR_TestModeLimit
81+ | RR_Unsigned
61 | RR_Rule deriving Show
82 | RR_Rule deriving Show
62
83
63deriveJSON defaultOptions {
84deriveJSON defaultOptions {
@@ -70,7 +91,7 @@ deriveJSON defaultOptions {
70-- which can be either a success or a failure.
91-- which can be either a success or a failure.
71data MandrillResponse k =
92data MandrillResponse k =
72 MandrillSuccess k
93 MandrillSuccess k
73− | MandrillFailure MandrillError deriving Show
94+ | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
74
95
75instance FromJSON k => FromJSON (MandrillResponse k) where
96instance FromJSON k => FromJSON (MandrillResponse k) where
76 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
97 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -88,14 +109,27 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
88deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
109deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
89
110
90
111
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+
91--------------------------------------------------------------------------------
125--------------------------------------------------------------------------------
92-- | An array of recipient information.
126-- | An array of recipient information.
93data MandrillRecipient = MandrillRecipient {
127data MandrillRecipient = MandrillRecipient {
94− _mrec_email :: EmailAddress
128+ _mrec_email :: MandrillEmail
95 -- ^ The email address of the recipient
129 -- ^ The email address of the recipient
96− , _mrec_name :: Maybe T.Text
130+ , _mrec_name :: Maybe T.Text
97 -- ^ The optional display name to use for the recipient
131 -- ^ The optional display name to use for the recipient
98− , _mrec_type :: Maybe MandrillRecipientTag
132+ , _mrec_type :: Maybe MandrillRecipientTag
99 -- ^ The header type to use for the recipient.
133 -- ^ The header type to use for the recipient.
100 -- defaults to "to" if not provided
134 -- defaults to "to" if not provided
101 } deriving Show
135 } deriving Show
@@ -104,11 +138,11 @@ makeLenses ''MandrillRecipient
104deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
138deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
105
139
106newRecipient :: EmailAddress -> MandrillRecipient
140newRecipient :: EmailAddress -> MandrillRecipient
107−newRecipient email = MandrillRecipient email Nothing Nothing
141+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
108
142
109instance Arbitrary MandrillRecipient where
143instance Arbitrary MandrillRecipient where
110 arbitrary = pure MandrillRecipient {
144 arbitrary = pure MandrillRecipient {
111− _mrec_email = fromJust (emailAddress "test@example.com")
145+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
112 , _mrec_name = Nothing
146 , _mrec_name = Nothing
113 , _mrec_type = Nothing
147 , _mrec_type = Nothing
114 }
148 }
@@ -124,6 +158,11 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
124mkMandrillHtml :: Blaze.Html -> MandrillHtml
158mkMandrillHtml :: Blaze.Html -> MandrillHtml
125mkMandrillHtml = MandrillHtml
159mkMandrillHtml = MandrillHtml
126
160
161+#if MIN_VERSION_base(4,11,0)
162+instance Semigroup MandrillHtml where
163+ MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164+#endif
165+
127instance Monoid MandrillHtml where
166instance Monoid MandrillHtml where
128 mempty = MandrillHtml mempty
167 mempty = MandrillHtml mempty
129 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
168 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -146,17 +185,23 @@ type MandrillTags = T.Text
146
185
147
186
148--------------------------------------------------------------------------------
187--------------------------------------------------------------------------------
149−type MandrillHeaders = Value
188+type MandrillHeaders = Object
150
189
151
190
152--------------------------------------------------------------------------------
191--------------------------------------------------------------------------------
153−type MandrillVars = Value
154
192
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
155
200
156--------------------------------------------------------------------------------
201--------------------------------------------------------------------------------
157data MandrillMergeVars = MandrillMergeVars {
202data MandrillMergeVars = MandrillMergeVars {
158 _mmvr_rcpt :: !T.Text
203 _mmvr_rcpt :: !T.Text
159− , _mmvr_vars :: [MandrillVars]
204+ , _mmvr_vars :: [MergeVar]
160 } deriving Show
205 } deriving Show
161
206
162makeLenses ''MandrillMergeVars
207makeLenses ''MandrillMergeVars
@@ -164,29 +209,33 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
164
209
165--------------------------------------------------------------------------------
210--------------------------------------------------------------------------------
166data MandrillMetadata = MandrillMetadata {
211data MandrillMetadata = MandrillMetadata {
167− _mmdt_rcpt :: !T.Text
212+ _mmdt_rcpt :: !T.Text
168− , _mmdt_values :: MandrillVars
213+ , _mmdt_values :: Object
169 } deriving Show
214 } deriving Show
170
215
171makeLenses ''MandrillMetadata
216makeLenses ''MandrillMetadata
172deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
217deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
173
218
174
219
175−newtype Base64ByteString = B64BS B.ByteString deriving Show
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
176
226
177instance ToJSON Base64ByteString where
227instance ToJSON Base64ByteString where
178− toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
228+ toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229+ toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
179
230
180instance FromJSON Base64ByteString where
231instance FromJSON Base64ByteString where
181− parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
232+ parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
182− Left err -> fail err
183− Right rs -> return $ B64BS rs
184 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
233 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
185
234
186--------------------------------------------------------------------------------
235--------------------------------------------------------------------------------
187data MandrillWebContent = MandrillWebContent {
236data MandrillWebContent = MandrillWebContent {
188− _mwct_type :: !T.Text
237+ _mwct_type :: !T.Text
189− , _mwct_name :: !T.Text
238+ , _mwct_name :: !T.Text
190 -- ^ [for images] the Content ID of the image
239 -- ^ [for images] the Content ID of the image
191 -- - use <img src="cid:THIS_VALUE"> to reference the image
240 -- - use <img src="cid:THIS_VALUE"> to reference the image
192 -- in your HTML content
241 -- in your HTML content
@@ -199,67 +248,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
199--------------------------------------------------------------------------------
248--------------------------------------------------------------------------------
200-- | The information on the message to send
249-- | The information on the message to send
201data MandrillMessage = MandrillMessage {
250data MandrillMessage = MandrillMessage {
202− _mmsg_html :: MandrillHtml
251+ _mmsg_html :: MandrillHtml
203 -- ^ The full HTML content to be sent
252 -- ^ The full HTML content to be sent
204− , _mmsg_text :: Maybe T.Text
253+ , _mmsg_text :: Maybe T.Text
205 -- ^ Optional full text content to be sent
254 -- ^ Optional full text content to be sent
206− , _mmsg_subject :: !T.Text
255+ , _mmsg_subject :: !T.Text
207 -- ^ The message subject
256 -- ^ The message subject
208− , _mmsg_from_email :: EmailAddress
257+ , _mmsg_from_email :: MandrillEmail
209 -- ^ The sender email address
258 -- ^ The sender email address
210− , _mmsg_from_name :: Maybe T.Text
259+ , _mmsg_from_name :: Maybe T.Text
211 -- ^ Optional from name to be used
260 -- ^ Optional from name to be used
212− , _mmsg_to :: [MandrillRecipient]
261+ , _mmsg_to :: [MandrillRecipient]
213 -- ^ A list of recipient information
262 -- ^ A list of recipient information
214− , _mmsg_headers :: MandrillHeaders
263+ , _mmsg_headers :: MandrillHeaders
215 -- ^ optional extra headers to add to the message (most headers are allowed)
264 -- ^ optional extra headers to add to the message (most headers are allowed)
216− , _mmsg_important :: Maybe Bool
265+ , _mmsg_important :: Maybe Bool
217 -- ^ whether or not this message is important, and should be delivered ahead
266 -- ^ whether or not this message is important, and should be delivered ahead
218 -- of non-important messages
267 -- of non-important messages
219− , _mmsg_track_opens :: Maybe Bool
268+ , _mmsg_track_opens :: Maybe Bool
220 -- ^ whether or not to turn on open tracking for the message
269 -- ^ whether or not to turn on open tracking for the message
221− , _mmsg_track_clicks :: Maybe Bool
270+ , _mmsg_track_clicks :: Maybe Bool
222 -- ^ whether or not to turn on click tracking for the message
271 -- ^ whether or not to turn on click tracking for the message
223− , _mmsg_auto_text :: Maybe Bool
272+ , _mmsg_auto_text :: Maybe Bool
224 -- ^ whether or not to automatically generate a text part for messages that are not given text
273 -- ^ whether or not to automatically generate a text part for messages that are not given text
225− , _mmsg_auto_html :: Maybe Bool
274+ , _mmsg_auto_html :: Maybe Bool
226 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
275 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
227− , _mmsg_inline_css :: Maybe Bool
276+ , _mmsg_inline_css :: Maybe Bool
228 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
277 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
229 -- - only for HTML documents less than 256KB in size
278 -- - only for HTML documents less than 256KB in size
230− , _mmsg_url_strip_qs :: Maybe Bool
279+ , _mmsg_url_strip_qs :: Maybe Bool
231 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
280 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
232− , _mmsg_preserve_recipients :: Maybe Bool
281+ , _mmsg_preserve_recipients :: Maybe Bool
233 -- ^ whether or not to expose all recipients in to "To" header for each email
282 -- ^ whether or not to expose all recipients in to "To" header for each email
234− , _mmsg_view_content_link :: Maybe Bool
283+ , _mmsg_view_content_link :: Maybe Bool
235 -- ^ set to false to remove content logging for sensitive emails
284 -- ^ set to false to remove content logging for sensitive emails
236− , _mmsg_bcc_address :: Maybe T.Text
285+ , _mmsg_bcc_address :: Maybe T.Text
237 -- ^ an optional address to receive an exact copy of each recipient's email
286 -- ^ an optional address to receive an exact copy of each recipient's email
238− , _mmsg_tracking_domain :: Maybe T.Text
287+ , _mmsg_tracking_domain :: Maybe T.Text
239 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
288 -- ^ a custom domain to use for tracking opens and clicks instead of mandrillapp.com
240− , _mmsg_signing_domain :: Maybe Bool
289+ , _mmsg_signing_domain :: Maybe Bool
241− -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
290+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
242 -- (for "via" or "on behalf of" in email clients)
291 -- (for "via" or "on behalf of" in email clients)
243− , _mmsg_return_path_domain :: Maybe Bool
292+ , _mmsg_return_path_domain :: Maybe Bool
244 -- ^ a custom domain to use for the messages's return-path
293 -- ^ a custom domain to use for the messages's return-path
245− , _mmsg_merge :: Maybe Bool
294+ , _mmsg_merge :: Maybe Bool
246 -- ^ whether to evaluate merge tags in the message.
295 -- ^ whether to evaluate merge tags in the message.
247 -- Will automatically be set to true if either merge_vars
296 -- Will automatically be set to true if either merge_vars
248 -- or global_merge_vars are provided.
297 -- or global_merge_vars are provided.
249− , _mmsg_global_merge_vars :: [MandrillVars]
298+ , _mmsg_global_merge_vars :: [MergeVar]
250 -- ^ global merge variables to use for all recipients. You can override these per recipient.
299 -- ^ global merge variables to use for all recipients. You can override these per recipient.
251− , _mmsg_merge_vars :: [MandrillMergeVars]
300+ , _mmsg_merge_vars :: [MandrillMergeVars]
252 -- ^ per-recipient merge variables, which override global merge variables with the same name.
301 -- ^ per-recipient merge variables, which override global merge variables with the same name.
253− , _mmsg_tags :: [MandrillTags]
302+ , _mmsg_tags :: [MandrillTags]
254 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
303 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
255 -- though we only store the first 100 we see, so this should not be unique
304 -- though we only store the first 100 we see, so this should not be unique
256 -- or change frequently. Tags should be 50 characters or less.
305 -- or change frequently. Tags should be 50 characters or less.
257 -- Any tags starting with an underscore are reserved for internal use
306 -- Any tags starting with an underscore are reserved for internal use
258 -- and will cause errors.
307 -- and will cause errors.
259− , _mmsg_subaccount :: Maybe T.Text
308+ , _mmsg_subaccount :: Maybe T.Text
260 -- ^ the unique id of a subaccount for this message
309 -- ^ the unique id of a subaccount for this message
261 -- - must already exist or will fail with an error
310 -- - must already exist or will fail with an error
262− , _mmsg_google_analytics_domains :: [T.Text]
311+ , _mmsg_google_analytics_domains :: [T.Text]
263 -- ^ an array of strings indicating for which any matching URLs
312 -- ^ an array of strings indicating for which any matching URLs
264 -- will automatically have Google Analytics parameters appended
313 -- will automatically have Google Analytics parameters appended
265 -- to their query string automatically.
314 -- to their query string automatically.
@@ -267,17 +316,17 @@ data MandrillMessage = MandrillMessage {
267 -- ^ optional string indicating the value to set for the utm_campaign
316 -- ^ optional string indicating the value to set for the utm_campaign
268 -- tracking parameter. If this isn't provided the email's from address
317 -- tracking parameter. If this isn't provided the email's from address
269 -- will be used instead.
318 -- will be used instead.
270− , _mmsg_metadata :: MandrillVars
319+ , _mmsg_metadata :: Object
271 -- ^ metadata an associative array of user metadata. Mandrill will store
320 -- ^ metadata an associative array of user metadata. Mandrill will store
272 -- this metadata and make it available for retrieval.
321 -- this metadata and make it available for retrieval.
273 -- In addition, you can select up to 10 metadata fields to index
322 -- In addition, you can select up to 10 metadata fields to index
274 -- and make searchable using the Mandrill search api.
323 -- and make searchable using the Mandrill search api.
275− , _mmsg_recipient_metadata :: [MandrillMetadata]
324+ , _mmsg_recipient_metadata :: [MandrillMetadata]
276 -- ^ Per-recipient metadata that will override the global values
325 -- ^ Per-recipient metadata that will override the global values
277 -- specified in the metadata parameter.
326 -- specified in the metadata parameter.
278− , _mmsg_attachments :: [MandrillWebContent]
327+ , _mmsg_attachments :: [MandrillWebContent]
279 -- ^ an array of supported attachments to add to the message
328 -- ^ an array of supported attachments to add to the message
280− , _mmsg_images :: [MandrillWebContent]
329+ , _mmsg_images :: [MandrillWebContent]
281 -- ^ an array of embedded images to add to the message
330 -- ^ an array of embedded images to add to the message
282 } deriving Show
331 } deriving Show
283
332
@@ -288,10 +337,10 @@ instance Arbitrary MandrillMessage where
288 arbitrary = MandrillMessage <$> arbitrary
337 arbitrary = MandrillMessage <$> arbitrary
289 <*> pure Nothing
338 <*> pure Nothing
290 <*> pure "Test Subject"
339 <*> pure "Test Subject"
291− <*> pure (fromJust $ emailAddress "sender@example.com")
340+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
292 <*> pure Nothing
341 <*> pure Nothing
293 <*> resize 2 arbitrary
342 <*> resize 2 arbitrary
294− <*> pure emptyObject
343+ <*> pure H.empty
295 <*> pure Nothing
344 <*> pure Nothing
296 <*> pure Nothing
345 <*> pure Nothing
297 <*> pure Nothing
346 <*> pure Nothing
@@ -312,13 +361,24 @@ instance Arbitrary MandrillMessage where
312 <*> pure Nothing
361 <*> pure Nothing
313 <*> pure []
362 <*> pure []
314 <*> pure Nothing
363 <*> pure Nothing
315− <*> pure emptyObject
364+ <*> pure H.empty
316 <*> pure []
365 <*> pure []
317 <*> pure []
366 <*> pure []
318 <*> pure []
367 <*> pure []
319
368
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+
320--------------------------------------------------------------------------------
379--------------------------------------------------------------------------------
321type MandrillKey = T.Text
380type MandrillKey = T.Text
381+type MandrillTemplate = T.Text
322
382
323newtype MandrillDate = MandrillDate {
383newtype MandrillDate = MandrillDate {
324 fromMandrillDate :: UTCTime
384 fromMandrillDate :: UTCTime
@@ -329,6 +389,6 @@ instance ToJSON MandrillDate where
329
389
330instance FromJSON MandrillDate where
390instance FromJSON MandrillDate where
331 parseJSON = withText "MandrillDate" $ \t ->
391 parseJSON = withText "MandrillDate" $ \t ->
332− case parseTime defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
392+ case timeParse defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
333 Just d -> pure $ MandrillDate d
393 Just d -> pure $ MandrillDate d
334 _ -> fail "could not parse Mandrill date"
394 _ -> fail "could not parse Mandrill date"
src/Network/API/Mandrill/Users/Types.hs+1−1
@@ -5,12 +5,12 @@ module Network.API.Mandrill.Users.Types where
5import Data.Char
5import Data.Char
6import Data.Time
6import Data.Time
7import qualified Data.Text as T
7import qualified Data.Text as T
8−import Control.Lens
9import Control.Monad
8import Control.Monad
10import Data.Monoid
9import Data.Monoid
11import Data.Aeson
10import Data.Aeson
12import Data.Aeson.Types
11import Data.Aeson.Types
13import Data.Aeson.TH
12import Data.Aeson.TH
13+import Lens.Micro.TH (makeLenses)
14
14
15import Network.API.Mandrill.Types
15import Network.API.Mandrill.Types
16
16
src/Network/API/Mandrill/Webhooks.hsnew file+70
@@ -0,0 +1,70 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE TemplateHaskell #-}
3+module Network.API.Mandrill.Webhooks where
4+import Control.Applicative (pure)
5+import Control.Monad (mzero)
6+import Data.Aeson (FromJSON, ToJSON, parseJSON,
7+ toJSON)
8+import Data.Aeson (Value (String))
9+import Data.Aeson.TH (defaultOptions, deriveJSON)
10+import Data.Aeson.Types (fieldLabelModifier)
11+import Data.Set (Set)
12+import Data.Text (Text)
13+import qualified Data.Text as T
14+import Lens.Micro.TH (makeLenses)
15+import Network.API.Mandrill.HTTP (toMandrillResponse)
16+import Network.API.Mandrill.Settings
17+import Network.API.Mandrill.Types
18+import Network.HTTP.Client (Manager)
19+
20+data EventHook
21+ = EventSent
22+ | EventDeferred
23+ | EventHardBounced
24+ | EventSoftBounced
25+ | EventOpened
26+ | EventClicked
27+ | EventMarkedAsSpam
28+ | EventUnsubscribed
29+ | EventRejected
30+ deriving (Ord,Eq)
31+
32+instance Show EventHook where
33+ show e = case e of
34+ EventSent -> "send"
35+ EventDeferred -> "deferral"
36+ EventSoftBounced -> "soft_bounce"
37+ EventHardBounced -> "hard_bounce"
38+ EventOpened -> "open"
39+ EventClicked -> "click"
40+ EventMarkedAsSpam -> "spam"
41+ EventUnsubscribed -> "unsub"
42+ EventRejected -> "reject"
43+instance FromJSON EventHook where
44+ parseJSON (String s) =
45+ case s of
46+ "send" -> pure EventSent
47+ "deferral" -> pure EventDeferred
48+ "soft_bounce" -> pure EventSoftBounced
49+ "hard_bounce" -> pure EventHardBounced
50+ "open" -> pure EventOpened
51+ "click" -> pure EventClicked
52+ "spam" -> pure EventMarkedAsSpam
53+ "reject" -> pure EventRejected
54+ "unsub" -> pure EventUnsubscribed
55+ x -> fail ("can't parse " ++ show x)
56+
57+
58+instance ToJSON EventHook where
59+ toJSON e = String (T.pack $ show e)
60+
61+data WebhookAddRq =
62+ WebhookAddRq
63+ { _warq_key :: MandrillKey
64+ , _warq_url :: Text
65+ , _warq_description :: Text
66+ , _warq_events :: Set EventHook
67+ } deriving Show
68+
69+makeLenses ''WebhookAddRq
70+deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''WebhookAddRq
stack.yamlnew file+5
@@ -0,0 +1,5 @@
1+flags: {}
2+packages:
3+- '.'
4+extra-deps: []
5+resolver: lts-11.13
test/Main.hs+14−7
@@ -1,14 +1,14 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Main where
2module Main where
3
3
4−import System.Environment
5import Data.Monoid
4import Data.Monoid
6−import Tests
5+import qualified Data.Text as T
7import Online
6import Online
7+import System.Environment
8import Test.Tasty
8import Test.Tasty
9import Test.Tasty.HUnit
9import Test.Tasty.HUnit
10import Test.Tasty.QuickCheck
10import Test.Tasty.QuickCheck
11−import qualified Data.Text as T
11+import Tests
12
12
13----------------------------------------------------------------------
13----------------------------------------------------------------------
14withQuickCheckDepth :: TestName -> Int -> [TestTree] -> TestTree
14withQuickCheckDepth :: TestName -> Int -> [TestTree] -> TestTree
@@ -23,10 +23,12 @@ onlineTestsIfEnabled = do
23 Nothing -> return []
23 Nothing -> return []
24 Just k -> return [
24 Just k -> return [
25 testGroup "Mandrill online tests" [
25 testGroup "Mandrill online tests" [
26− testCase "users/info.json" (testOnlineUsersInfo k)
26+ testCase "users/info.json" (testOnlineUsersInfo k)
27− , testCase "users/ping2.json" (testOnlineUsersPing2 k)
27+ , testCase "users/ping2.json" (testOnlineUsersPing2 k)
28− , testCase "users/senders.json" (testOnlineUsersSenders k)
28+ , testCase "users/senders.json" (testOnlineUsersSenders k)
29− , testCase "messages/send.json" (testOnlineMessagesSend k)
29+ , testCase "messages/send.json" (testOnlineMessagesSend k)
30+ , testCase "inbound/addDomain.json" (testOnlineDomainAdd k)
31+ , testCase "inbound/addRoute.json" (testOnlineRouteAdd k)
30 ]]
32 ]]
31
33
32----------------------------------------------------------------------
34----------------------------------------------------------------------
@@ -39,5 +41,10 @@ main = do
39 testCase "users/info.json API parsing" testUsersInfo
41 testCase "users/info.json API parsing" testUsersInfo
40 , testCase "users/senders.json API parsing" testUsersSenders
42 , testCase "users/senders.json API parsing" testUsersSenders
41 , testCase "messages/send.json API parsing" testMessagesSend
43 , testCase "messages/send.json API parsing" testMessagesSend
44+ , testCase "messages/send.json API response parsing" testMessagesResponseRejected
45+ , testCase "inbound/add-route.json API response parsing" testRouteAdd
46+ , testCase "inbound/add-domain.json API response parsing" testDomainAdd
47+ , testCase "senders/verify-domain.json API response parsing" testVerifyDomain
48+ , testCase "webhooks/add.json API response parsing" testWebhookAdd
42 ]
49 ]
43 ]
50 ]
test/Online.hs+25−7
@@ -1,19 +1,20 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Online where
2module Online where
3
3
4−import Test.QuickCheck
5−import Test.Tasty.HUnit
6−import Text.RawString.QQ
7−import Data.Either
8import Data.Aeson
4import Data.Aeson
5+import qualified Data.ByteString.Char8 as C8
6+import Data.Either
9import Network.API.Mandrill.Messages.Types
7import Network.API.Mandrill.Messages.Types
10import Network.API.Mandrill.Users.Types
8import Network.API.Mandrill.Users.Types
11−import qualified Data.ByteString.Char8 as C8
12import RawData
9import RawData
10+import Test.QuickCheck
11+import Test.Tasty.HUnit
12+import Text.RawString.QQ
13
13
14+import qualified Network.API.Mandrill.Inbound as API
15+import qualified Network.API.Mandrill.Messages as API
14import Network.API.Mandrill.Types
16import Network.API.Mandrill.Types
15−import qualified Network.API.Mandrill.Messages as API
17+import qualified Network.API.Mandrill.Users as API
16−import qualified Network.API.Mandrill.Users as API
17
18
18--
19--
19-- Users calls
20-- Users calls
@@ -50,3 +51,20 @@ testOnlineMessagesSend k = do
50 case res of
51 case res of
51 MandrillSuccess _ -> return ()
52 MandrillSuccess _ -> return ()
52 MandrillFailure e -> fail $ "messages/send.json " ++ show e
53 MandrillFailure e -> fail $ "messages/send.json " ++ show e
54+
55+--
56+-- Inbound calls
57+--
58+testOnlineDomainAdd :: MandrillKey -> Assertion
59+testOnlineDomainAdd k = do
60+ res <- API.addDomain k "foobar.com" Nothing
61+ case res of
62+ MandrillSuccess a -> print a
63+ MandrillFailure e -> fail $ "inbound/add-domain.json " ++ show e
64+
65+testOnlineRouteAdd :: MandrillKey -> Assertion
66+testOnlineRouteAdd k = do
67+ res <- API.addRoute k "foobar.com" "mail-*" "http://requestb.in/19a4frw1" Nothing
68+ case res of
69+ MandrillSuccess a -> print a
70+ MandrillFailure e -> fail $ "inbound/add-domain.json " ++ show e
test/RawData.hs+59−4
@@ -1,10 +1,10 @@
1{-# LANGUAGE QuasiQuotes #-}
1{-# LANGUAGE QuasiQuotes #-}
2module RawData where
2module RawData where
3
3
4+import Data.Either
4−import Test.QuickCheck
5+import Test.QuickCheck
5−import Test.Tasty.HUnit
6+import Test.Tasty.HUnit
6−import Text.RawString.QQ
7+import Text.RawString.QQ
7−import Data.Either
8
8
9usersInfoData :: String
9usersInfoData :: String
10usersInfoData = [r|
10usersInfoData = [r|
@@ -200,3 +200,58 @@ sendData = [r|
200 "send_at": "2014-08-17T14:23:02.954Z"
200 "send_at": "2014-08-17T14:23:02.954Z"
201}
201}
202|]
202|]
203+
204+
205+messagesResponseRejected = [r|
206+[ { "email" : "foo@bar.com",
207+ "status" : "rejected",
208+ "_id" : "abc123",
209+ "reject_reason": "unsigned"
210+ }
211+]
212+|]
213+
214+
215+domainAdd = [r|
216+{
217+ "domain": "inbound.example.com",
218+ "created_at": "2013-01-01 15:30:27",
219+ "valid_mx": true
220+}
221+|]
222+
223+
224+routeAdd= [r|
225+{
226+ "id": "7.23",
227+ "pattern": "mailbox-*",
228+ "url": "http://example.com/webhook-url"
229+}
230+|]
231+
232+domainVerify = [r|
233+{
234+ "status": "example status",
235+ "domain": "example domain",
236+ "email": "email@domain.com"
237+}
238+|]
239+
240+webhookAdd = [r|
241+{
242+ "key": "example key",
243+ "url": "http://example/webhook-url",
244+ "description": "My Example Webhook",
245+ "events": [
246+ "send",
247+ "deferral",
248+ "hard_bounce",
249+ "soft_bounce",
250+ "open",
251+ "click",
252+ "spam",
253+ "unsub",
254+ "reject"
255+ ]
256+}
257+|]
test/Tests.hs+54−8
@@ -1,13 +1,17 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2module Tests where
2module Tests where
3
3
4−import Test.Tasty.HUnit
5−import Data.Either
6−import Data.Aeson
4+import Data.Aeson
7−import Network.API.Mandrill.Messages.Types
8−import Network.API.Mandrill.Users.Types
9−import qualified Data.ByteString.Char8 as C8
5+import qualified Data.ByteString.Char8 as C8
6+import Data.Either
7+import Network.API.Mandrill.Inbound
8+import Network.API.Mandrill.Messages.Types
9+import Network.API.Mandrill.Senders
10+import Network.API.Mandrill.Types
11+import Network.API.Mandrill.Users.Types
12+import Network.API.Mandrill.Webhooks
10−import RawData
13+import RawData
14+import Test.Tasty.HUnit
11
15
12#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 706
16#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 706
13isRight :: Either a b -> Bool
17isRight :: Either a b -> Bool
@@ -16,7 +20,7 @@ isRight _ = True
16#endif
20#endif
17
21
18testMessagesSend :: Assertion
22testMessagesSend :: Assertion
19−testMessagesSend =
23+testMessagesSend =
20 assertBool ("send.json: Parsing failed! " ++ show parsePayload)
24 assertBool ("send.json: Parsing failed! " ++ show parsePayload)
21 (isRight parsePayload)
25 (isRight parsePayload)
22 where
26 where
@@ -40,3 +44,45 @@ testUsersSenders = do
40 where
44 where
41 parsePayload :: Either String UsersSendersResponse
45 parsePayload :: Either String UsersSendersResponse
42 parsePayload = eitherDecodeStrict . C8.pack $ usersSendersData
46 parsePayload = eitherDecodeStrict . C8.pack $ usersSendersData
47+
48+testMessagesResponseRejected :: Assertion
49+testMessagesResponseRejected = do
50+ assertBool ("send.json response: Parsing failed" ++ show parsePayload)
51+ (isRight parsePayload)
52+ where
53+ parsePayload :: Either String (MandrillResponse [MessagesResponse])
54+ parsePayload = eitherDecodeStrict . C8.pack $ messagesResponseRejected
55+
56+testDomainAdd :: Assertion
57+testDomainAdd =
58+ assertBool ("inbound/add-domain.json (response): parsing failed" ++ show parsePayload)
59+ (isRight parsePayload)
60+ where
61+ parsePayload :: Either String DomainAddResponse
62+ parsePayload = eitherDecodeStrict . C8.pack $ domainAdd
63+
64+
65+testRouteAdd :: Assertion
66+testRouteAdd =
67+ assertBool ("inbound/add-route.json (response): parsing failed" ++ show parsePayload)
68+ (isRight parsePayload)
69+ where
70+ parsePayload :: Either String RouteAddResponse
71+ parsePayload = eitherDecodeStrict . C8.pack $ routeAdd
72+
73+testVerifyDomain :: Assertion
74+testVerifyDomain =
75+ assertBool ("senders/verify-domain.json" ++ show parsePayload)
76+ (isRight parsePayload)
77+ where
78+ parsePayload :: Either String VerifyDomainResponse
79+ parsePayload = eitherDecodeStrict . C8.pack $ domainVerify
80+
81+
82+testWebhookAdd :: Assertion
83+testWebhookAdd =
84+ assertBool ("webhooks/add.json (request): parsing failed" ++ show parsePayload)
85+ (isRight parsePayload)
86+ where
87+ parsePayload :: Either String WebhookAddRq
88+ parsePayload = eitherDecodeStrict . C8.pack $ webhookAdd