git.iko.soy/typeable/mandrill
on dbd48ca help sign in
26 files changed+205−830
since splitsnapshots
unifiedsplit
.gitignore−3 .hub+1−1 .travis.yml+8−10 README.md+3−83 default.nix−24 examples/Send.hs+1−1 mandrill-nightly.yml−10 mandrill.cabal+7−14 src/Network/API/Mandrill.hs+28−91 src/Network/API/Mandrill/HTTP.hs+10−24 src/Network/API/Mandrill/Inbound.hs−78 src/Network/API/Mandrill/Messages.hs+6−26 src/Network/API/Mandrill/Messages/Types.hs+1−14 src/Network/API/Mandrill/Orphans.hs+17 src/Network/API/Mandrill/Senders.hs−45 src/Network/API/Mandrill/Settings.hs+7−17 src/Network/API/Mandrill/Trans.hs+3−15 src/Network/API/Mandrill/Types.hs+76−136 src/Network/API/Mandrill/Users.hs+6−6 src/Network/API/Mandrill/Users/Types.hs+1−1 src/Network/API/Mandrill/Webhooks.hs−70 stack.yaml−5 test/Main.hs+7−14 test/Online.hs+11−29 test/RawData.hs+4−59 test/Tests.hs+8−54
.gitignore−3
@@ -1,6 +1,3 @@
11dist
22.cabal-sandbox
33cabal.sandbox.config
4−shell.nix
5−.stack-work
6−tags
.hub+1−1
@@ -1 +1 @@
1−mandrill-7.10.1
1+mandrill
.travis.yml+8−10
@@ -5,17 +5,15 @@ notifications:
55 email: false
66
77env:
8+ - GHCVER=7.6.2 CABALVER=1.16
89 - GHCVER=7.6.3 CABALVER=1.16
910 - GHCVER=7.8.2 CABALVER=1.18
1011 - GHCVER=7.8.3 CABALVER=1.18
11− - GHCVER=7.10.2 CABALVER=1.22
12− - GHCVER=8.0.1 CABALVER=1.22
13− - GHCVER=head CABALVER=1.22
12+ - GHCVER=head CABALVER=1.20
1413
1514matrix:
1615 allow_failures:
17− - env: GHCVER=8.0.1 CABALVER=1.22
18− - env: GHCVER=head CABALVER=1.22
16+ - env: GHCVER=head CABALVER=1.20
1917
2018before_install:
2119 - travis_retry sudo add-apt-repository -y ppa:hvr/ghc
@@ -23,15 +21,17 @@ before_install:
2321 - travis_retry sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER
2422 - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
2523 - cabal --version
24+ - cabal update
25+ - cabal install hpc-coveralls
2626
2727install:
2828 - cabal update
2929 - cabal install --only-dependencies --enable-tests
3030
3131script:
32− - cabal configure --enable-tests -v2 --enable-library-coverage
32+ - cabal configure --enable-tests -v2
3333 - cabal build
34− - cabal test --show-details=always
34+ - cabal test
3535 - cabal check
3636 - cabal sdist
3737
@@ -46,6 +46,4 @@ script:
4646 fi
4747
4848after_script:
49− - cabal install hpc-coveralls
50− - export PATH=/opt/ghc/$GHCVER/bin:$HOME/.cabal/bin:$PATH
51− - hpc-coveralls --coverage-mode=StrictlyFullLines mandrill-tests
49+ - hpc-coveralls mandrill-tests
README.md+3−83
@@ -1,82 +1,15 @@
11[![Build Status](https://travis-ci.org/adinapoli/mandrill.svg?branch=master)](https://travis-ci.org/adinapoli/mandrill)
2−[![Coverage Status](https://img.shields.io/coveralls/adinapoli/mandrill.svg)](https://coveralls.io/r/adinapoli/mandrill)
2+[![Coverage Status](https://img.shields.io/coveralls/adinapoli/mandrill.svg)](https://coveralls.io/r/adinapoli/mandrill)A
33
44# Haskell Client for the Mandrill JSON API
55
66This module implement a low-level, 1:1 mapping API to
77the [Mandrill](http://mandrillapp.com) transactional email service.
88
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−
769# Example
7710
7811This package was built with pragmatism and reuse in mind. This means
79−this API comes in two flavours: an IO-based and an handy monad transformer
12+this API comes in two flavours: an IO-based and an handy monad transformers
8013which can be plugged in your stack of choice.
8114Example:
8215
@@ -91,7 +24,7 @@ main = do
9124 Left err -> print $ "Invalid email!" ++ show err
9225 Right addr -> runMandrill "MYTOKENHERE" $ do
9326 let msg = "<p>My Html</p>"
94− res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
27+ res <- sendEmail (newTextMessage addr addr "Hello" msg)
9528 case res of
9629 MandrillSuccess k -> liftIO (print k)
9730 MandrillFailure f -> liftIO (print f)
@@ -105,17 +38,9 @@ main = do
10538 + ~~ping.json~~ (as **doesn't return valid json!**)
10639 + ping2.json
10740 + senders.json
108−
10941 - [Messages call](https://mandrillapp.com/api/docs/messages.JSON.html)
11042 + send.json
11143
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−
11944# Testing online API
12045
12146To test the online API, first build the package with tests enabled:
@@ -135,8 +60,3 @@ And finally execute the testsuite:
13560```
13661cabal test
13762```
138−
139−# Contributions
140−This library scratches my own itches, but please fork away!
141−Pull requests are encouraged to implement the part of the API
142−you need.
default.nixdeleted−24
@@ -1,24 +0,0 @@
1−# This file was auto-generated by cabal2nix. Please do NOT edit manually!
2−
3−{ cabal, aeson, base64Bytestring, blazeHtml, emailValidate, lens
4−, mtl, QuickCheck, rawStringsQq, tasty, tastyHunit, tastyQuickcheck
5−, text, time, wreq
6−}:
7−
8−cabal.mkDerivation (self: {
9− pname = "mandrill";
10− version = "0.1.0.3";
11− src = ./.;
12− buildDepends = [
13− aeson base64Bytestring blazeHtml emailValidate lens mtl QuickCheck
14− text time wreq
15− ];
16− testDepends = [
17− aeson QuickCheck rawStringsQq tasty tastyHunit tastyQuickcheck text
18− ];
19− meta = {
20− description = "Library for interfacing with the Mandrill JSON API";
21− license = self.stdenv.lib.licenses.mit;
22− platforms = self.ghc.meta.platforms;
23− };
24−})
examples/Send.hs+1−1
@@ -7,7 +7,7 @@ main = case validate "foo@example.com" of
77 Left err -> print $ "Invalid email!" ++ show err
88 Right addr -> runMandrill "SUPER-SECRET-TOKEN" $ do
99 let msg = "<p>My Html</p>"
10− res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
10+ res <- sendEmail (newTextMessage addr addr "Hello" msg)
1111 case res of
1212 MandrillSuccess k -> liftIO (print k)
1313 MandrillFailure f -> liftIO (print f)
mandrill-nightly.ymldeleted−10
@@ -1,10 +0,0 @@
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+7−14
@@ -1,5 +1,5 @@
11name: mandrill
2−version: 0.5.3.5
2+version: 0.1.0.0
33synopsis: Library for interfacing with the Mandrill JSON API
44description: Pure Haskell client for the Mandrill JSON API
55license: MIT
@@ -8,7 +8,6 @@ author: Alfredo Di Napoli
88maintainer: alfredo.dinapoli@gmail.com
99category: Network
1010build-type: Simple
11−tested-with: GHC == 7.4, GHC == 7.6, GHC == 7.8, GHC == 7.10.2
1211cabal-version: >=1.10
1312
1413source-repository head
@@ -25,36 +24,30 @@ library
2524 Network.API.Mandrill.Users.Types
2625 Network.API.Mandrill.Messages
2726 Network.API.Mandrill.Messages.Types
28− Network.API.Mandrill.Inbound
29− Network.API.Mandrill.Webhooks
30− Network.API.Mandrill.Senders
3127 other-modules:
3228 Network.API.Mandrill.Utils
3329 Network.API.Mandrill.HTTP
30+ Network.API.Mandrill.Orphans
3431 -- other-extensions:
3532 build-depends:
3633 base >=4.6 && < 5
3734 , containers >= 0.5.0.0
3835 , bytestring >= 0.9.0
3936 , base64-bytestring >= 1.0.0.1
40− , text >= 1.0.0.0 && < 1.3
41− , http-types >= 0.8.0
42− , http-client >= 0.3.0
43− , http-client-tls >= 0.2.0.0
44− , aeson >= 0.7.0.3 && < 1.5.0.0
45− , microlens-th >= 0.4.0.0
37+ , text >= 1.0.0.0 && < 1.2
38+ , wreq >= 0.1.0.1
39+ , aeson >= 0.7.0.3 && < 0.8
40+ , lens >= 4.1 && < 4.6
4641 , blaze-html >= 0.5.0.0
47− , QuickCheck >= 2.6 && < 3.0
42+ , QuickCheck >= 2.6 && < 2.8
4843 , mtl < 3.0
4944 , time
5045 , email-validate >= 1.0.0
5146 , old-locale
52− , unordered-containers
5347 hs-source-dirs:
5448 src
5549 default-language:
5650 Haskell2010
57−
5851 ghc-options:
5952 -funbox-strict-fields
6053
src/Network/API/Mandrill.hs+28−91
@@ -1,72 +1,37 @@
1−{-| This package is an attempt to expose the Mandrill JSON API in pure Haskell.
2− To do that, the library API comes in two flavours:
3−
4− * An IO-based, low-level 1:1 mapping of the JSON API,
5− as described on <https://mandrillapp.com/api/docs/ the website>.
6− * A handy monad transformer which can be plugged in your stack of choice.
7−-}
81
92module Network.API.Mandrill (
103 module M
114 , sendEmail
12− , sendTextEmail
135 , emptyMessage
146 , newTextMessage
157 , newHtmlMessage
16− , newTemplateMessage
178 , liftIO
18−
19− -- * Appendix: Example Usage
20− -- $exampleusage
219 ) where
2210
23−import Control.Monad.Reader
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
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
33−import Text.Blaze.Html
34−import Text.Email.Validate
35−
36−{- $exampleusage
37−
38−The API was designed to allow to get you started as quickly as possible:
11+import Control.Monad.Reader
12+import Control.Lens
13+import Data.Time
14+import Text.Blaze.Html
15+import Network.API.Mandrill.Types as M
16+import Network.API.Mandrill.Messages as M
17+import Network.API.Mandrill.Messages.Types as M
18+import Network.API.Mandrill.Trans as M
19+import Data.Monoid
20+import Text.Email.Validate
21+import qualified Data.Text as T
22+import qualified Data.Aeson as JSON
3923
40−> {-# LANGUAGE OverloadedStrings #-}
41−> import Text.Email.Validate
42−> import Network.API.Mandrill
43−>
44−> main :: IO ()
45−> main = do
46−> case validate "foo@example.com" of
47−> Left err -> print $ "Invalid email!" ++ show err
48−> Right addr -> runMandrill "MYTOKENHERE" $ do
49−> let msg = "<p>My Html</p>"
50−> res <- sendEmail (newTextMessage addr [addr] "Hello" msg)
51−> case res of
52−> MandrillSuccess k -> liftIO (print k)
53−> MandrillFailure f -> liftIO (print f)
54−
55−-}
5624
5725--------------------------------------------------------------------------------
58−-- | Builds an empty message, given only the email of the sender and
59−-- the emails of the receiver. Please note that the "Subject" will be empty,
60−-- so you need to use either @newTextMessage@ or @newHtmlMessage@ to populate it.
61−emptyMessage :: EmailAddress -> [EmailAddress] -> MandrillMessage
26+emptyMessage :: EmailAddress -> EmailAddress -> MandrillMessage
6227emptyMessage f t = MandrillMessage {
6328 _mmsg_html = mempty
6429 , _mmsg_text = Nothing
6530 , _mmsg_subject = T.empty
66− , _mmsg_from_email = (MandrillEmail f)
31+ , _mmsg_from_email = f
6732 , _mmsg_from_name = Nothing
68− , _mmsg_to = map newRecipient t
69− , _mmsg_headers = H.empty
33+ , _mmsg_to = [newRecipient t]
34+ , _mmsg_headers = JSON.Null
7035 , _mmsg_important = Nothing
7136 , _mmsg_track_opens = Nothing
7237 , _mmsg_track_clicks = Nothing
@@ -87,71 +52,43 @@ emptyMessage f t = MandrillMessage {
8752 , _mmsg_subaccount = Nothing
8853 , _mmsg_google_analytics_domains = []
8954 , _mmsg_google_analytics_campaign = Nothing
90− , _mmsg_metadata = H.empty
55+ , _mmsg_metadata = JSON.Null
9156 , _mmsg_recipient_metadata = []
9257 , _mmsg_attachments = []
9358 , _mmsg_images = []
9459 }
9560
9661
62+
9763--------------------------------------------------------------------------------
98−-- | Create a new HTML message.
9964newHtmlMessage :: EmailAddress
100− -- ^ Sender email
101− -> [EmailAddress]
102− -- ^ Receivers email
65+ -> EmailAddress
10366 -> T.Text
104− -- ^ Subject
10567 -> Html
106− -- ^ The HTML body
10768 -> MandrillMessage
10869newHtmlMessage f t subj html = let body = mkMandrillHtml html in
109− (emptyMessage f t) { _mmsg_html = body, _mmsg_subject = subj }
70+ ((mmsg_html .~ body) . (mmsg_subject .~ subj)) $ (emptyMessage f t)
11071
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 }
12172
12273--------------------------------------------------------------------------------
123−-- | Create a new textual message. By default Mandrill doesn't require you
124−-- to specify the @mmsg_text@ when sending out the JSON Payload, and this
125−-- function ensure it will be present.
12674newTextMessage :: EmailAddress
127− -- ^ Sender email
128− -> [EmailAddress]
129− -- ^ Receivers email
75+ -> EmailAddress
13076 -> T.Text
131− -- ^ Subject
13277 -> T.Text
133− -- ^ The body, as normal text.
13478 -> MandrillMessage
13579newTextMessage f t subj txt = let body = unsafeMkMandrillHtml txt in
136− (emptyMessage f t) {
137− _mmsg_html = body
138− , _mmsg_text = Just txt
139− , _mmsg_subject = subj
140− }
80+ ((mmsg_html .~ body) .
81+ (mmsg_text .~ Just txt) .
82+ (mmsg_subject .~ subj)) (emptyMessage f t)
14183
14284
14385--------------------------------------------------------------------------------
144−-- | The simplest way to use the API. All you need to provide is a valid
145−-- 'MandrillMessage' and this function will send an email inside a
146−-- 'MandrillT' transformer. You are not forced to use the 'MandrillT' context
147−-- though. Have a look at "Network.API.Mandrill.Messages" for an IO-based,
148−-- low level function for sending email.
14986sendEmail :: MonadIO m
15087 => MandrillMessage
15188 -> MandrillT m (MandrillResponse [MessagesResponse])
15289sendEmail msg = do
153− (key, mgr) <- ask
154− liftIO $ send key msg (Just True) Nothing Nothing (Just mgr)
90+ key <- ask
91+ liftIO $ send key msg (Just True) Nothing Nothing
15592
15693
15794--------------------------------------------------------------------------------
@@ -159,6 +96,6 @@ sendTextEmail :: MonadIO m
15996 => MandrillMessage
16097 -> MandrillT m (MandrillResponse [MessagesResponse])
16198sendTextEmail msg = do
162− (key, mgr) <- ask
99+ key <- ask
163100 now <- liftIO getCurrentTime
164− liftIO $ send key msg (Just True) Nothing (Just now) (Just mgr)
101+ liftIO $ send key msg (Just True) Nothing (Just now)
src/Network/API/Mandrill/HTTP.hs+10−24
@@ -1,33 +1,19 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Network.API.Mandrill.HTTP where
33
4−import Control.Applicative
5−import Data.Aeson
6−import Data.Monoid
7−import qualified Data.Text as T
8−import Network.API.Mandrill.Settings
9−import Network.API.Mandrill.Types
10−import Network.HTTP.Client
11−import Network.HTTP.Client.TLS
12−import Network.HTTP.Types
4+import Network.API.Mandrill.Settings
5+import Network.API.Mandrill.Types
6+import qualified Data.Text as T
7+import Data.Monoid
8+import Data.Aeson
9+import Network.Wreq
10+import Control.Lens
1311
1412toMandrillResponse :: (MandrillEndpoint ep, FromJSON a, ToJSON rq)
1513 => ep
1614 -> rq
17− -> Maybe Manager
1815 -> IO (MandrillResponse a)
19−toMandrillResponse ep rq mbMgr = do
16+toMandrillResponse ep rq = do
2017 let fullUrl = mandrillUrl <> toUrl ep
21− rq' <- parseUrl (T.unpack fullUrl)
22− let headers = [(hContentType, "application/json")]
23− let jsonBody = encode rq
24− let req = rq' {
25− method = "POST"
26− , requestHeaders = headers
27− , requestBody = RequestBodyLBS jsonBody
28− }
29− mgr <- maybe (newManager tlsManagerSettings) return mbMgr
30− res <- responseBody <$> httpLbs req mgr
31− case eitherDecode res of
32− Left e -> fail e
33− Right v -> return v
18+ res <- asJSON =<< post (T.unpack fullUrl) (toJSON rq)
19+ return $ res ^. responseBody
src/Network/API/Mandrill/Inbound.hsdeleted−78
@@ -1,78 +0,0 @@
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+6−26
@@ -1,13 +1,12 @@
11
22module Network.API.Mandrill.Messages where
33
4−import qualified Data.Text as T
5−import Data.Time
6−import Network.API.Mandrill.HTTP
4+import Network.API.Mandrill.Types
75import Network.API.Mandrill.Messages.Types
86import Network.API.Mandrill.Settings
9−import Network.API.Mandrill.Types
10−import Network.HTTP.Client
7+import Network.API.Mandrill.HTTP
8+import Data.Time
9+import qualified Data.Text as T
1110
1211--------------------------------------------------------------------------------
1312-- | Send a new transactional message through Mandrill
@@ -21,25 +20,6 @@ send :: MandrillKey
2120 -- ^ ip_pool
2221 -> Maybe UTCTime
2322 -- ^ send_at
24− -> Maybe Manager
2523 -> IO (MandrillResponse [MessagesResponse])
26−send 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)
24+send k msg async ip_pool send_at =
25+ toMandrillResponse MessagesSend (MessagesSendRq k msg async ip_pool send_at)
src/Network/API/Mandrill/Messages/Types.hs+1−14
@@ -5,12 +5,12 @@ module Network.API.Mandrill.Messages.Types where
55import Data.Char
66import Data.Time
77import qualified Data.Text as T
8+import Control.Lens
89import Control.Monad
910import Data.Monoid
1011import Data.Aeson
1112import Data.Aeson.Types
1213import Data.Aeson.TH
13−import Lens.Micro.TH (makeLenses)
1414
1515import Network.API.Mandrill.Types
1616
@@ -27,19 +27,6 @@ data MessagesSendRq = MessagesSendRq {
2727makeLenses ''MessagesSendRq
2828deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MessagesSendRq
2929
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
4330
4431--------------------------------------------------------------------------------
4532data MessagesResponse = MessagesResponse {
src/Network/API/Mandrill/Orphans.hsnew file+17
@@ -0,0 +1,17 @@
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.hsdeleted−45
@@ -1,45 +0,0 @@
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+7−17
@@ -15,26 +15,16 @@ data MandrillCalls =
1515 -- Messages API
1616 | MessagesSend
1717 | MessagesSendTemplate
18− | MessagesSearch
19− -- Inbound API
20− | RoutesAdd
21− | DomainsAdd
22− -- Senders API
23− | VerifyDomain
24−
25− deriving Show
18+ | MessagesSearch deriving Show
2619
2720class MandrillEndpoint ep where
2821 toUrl :: ep -> T.Text
2922
3023instance MandrillEndpoint MandrillCalls where
31− toUrl UsersInfo = "users/info.json"
32− toUrl UsersPing = "users/ping.json"
33− toUrl UsersPing2 = "users/ping2.json"
34− toUrl UsersSenders = "users/senders.json"
35− toUrl MessagesSend = "messages/send.json"
24+ toUrl UsersInfo = "users/info.json"
25+ toUrl UsersPing = "users/ping.json"
26+ toUrl UsersPing2 = "users/ping2.json"
27+ toUrl UsersSenders = "users/senders.json"
28+ toUrl MessagesSend = "messages/send.json"
3629 toUrl MessagesSendTemplate = "messages/send-template.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"
30+ toUrl MessagesSearch = "messages/search.json"
src/Network/API/Mandrill/Trans.hs+3−15
@@ -4,26 +4,14 @@ module Network.API.Mandrill.Trans where
44import Control.Monad.Reader
55import Control.Applicative
66import Network.API.Mandrill.Types
7−import Network.HTTP.Client
8−import Network.HTTP.Client.TLS
97
10−
11−--------------------------------------------------------------------------------
128newtype MandrillT m a = MandrillT {
13− runMandrillT :: ReaderT (MandrillKey, Manager) m a
14− } deriving (MonadTrans, MonadReader (MandrillKey, Manager),
9+ runMandrillT :: ReaderT MandrillKey m a
10+ } deriving (MonadTrans, MonadReader MandrillKey,
1511 Functor, Applicative, Monad, MonadIO)
1612
17−
18−--------------------------------------------------------------------------------
19−type Mandrill = MandrillT IO
20−
21−
22−--------------------------------------------------------------------------------
2313runMandrill :: MonadIO m
2414 => MandrillKey
2515 -> MandrillT m a
2616 -> m a
27−runMandrill key action = do
28− mgr <- liftIO $ newManager tlsManagerSettings
29− runReaderT (runMandrillT action) (key, mgr)
17+runMandrill key action = runReaderT (runMandrillT action) key
src/Network/API/Mandrill/Types.hs+76−136
@@ -1,59 +1,39 @@
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
8+import Network.API.Mandrill.Orphans()
189import Test.QuickCheck
1910import 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
11+import Data.Char
12+import Data.Maybe
13+import Data.Time
14+import Control.Applicative
15+import System.Locale (defaultTimeLocale)
16+import qualified Data.ByteString as B
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
2523import Data.Aeson
26−import Data.Aeson.TH
2724import 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
25+import Data.Aeson.TH
26+import qualified Text.Blaze.Html as Blaze
4027import qualified Text.Blaze.Html.Renderer.Text as Blaze
41−import qualified Text.Email.Validate as TEV
4228
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
4929
5030--------------------------------------------------------------------------------
5131data MandrillError = MandrillError {
52− _merr_status :: !T.Text
53− , _merr_code :: !Int
54− , _merr_name :: !T.Text
32+ _merr_status :: !T.Text
33+ , _merr_code :: !Int
34+ , _merr_name :: !T.Text
5535 , _merr_message :: !T.Text
56− } deriving (Show, Eq)
36+ } deriving Show
5737
5838makeLenses ''MandrillError
5939deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillError
@@ -78,7 +58,6 @@ data MandrillRejectReason = RR_HardBounce
7858 | RR_InvalidSender
7959 | RR_Invalid
8060 | RR_TestModeLimit
81− | RR_Unsigned
8261 | RR_Rule deriving Show
8362
8463deriveJSON defaultOptions {
@@ -91,7 +70,7 @@ deriveJSON defaultOptions {
9170-- which can be either a success or a failure.
9271data MandrillResponse k =
9372 MandrillSuccess k
94− | MandrillFailure MandrillError deriving (Show, Eq, Functor, Foldable, Traversable)
73+ | MandrillFailure MandrillError deriving Show
9574
9675instance FromJSON k => FromJSON (MandrillResponse k) where
9776 parseJSON v = case (parseMaybe parseJSON v) :: Maybe k of
@@ -109,27 +88,14 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
10988deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
11089
11190
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−
12591--------------------------------------------------------------------------------
12692-- | An array of recipient information.
12793data MandrillRecipient = MandrillRecipient {
128− _mrec_email :: MandrillEmail
94+ _mrec_email :: EmailAddress
12995 -- ^ The email address of the recipient
130− , _mrec_name :: Maybe T.Text
96+ , _mrec_name :: Maybe T.Text
13197 -- ^ The optional display name to use for the recipient
132− , _mrec_type :: Maybe MandrillRecipientTag
98+ , _mrec_type :: Maybe MandrillRecipientTag
13399 -- ^ The header type to use for the recipient.
134100 -- defaults to "to" if not provided
135101 } deriving Show
@@ -138,11 +104,11 @@ makeLenses ''MandrillRecipient
138104deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
139105
140106newRecipient :: EmailAddress -> MandrillRecipient
141−newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
107+newRecipient email = MandrillRecipient email Nothing Nothing
142108
143109instance Arbitrary MandrillRecipient where
144110 arbitrary = pure MandrillRecipient {
145− _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
111+ _mrec_email = fromJust (emailAddress "test@example.com")
146112 , _mrec_name = Nothing
147113 , _mrec_type = Nothing
148114 }
@@ -158,11 +124,6 @@ unsafeMkMandrillHtml = MandrillHtml . Blaze.preEscapedToHtml
158124mkMandrillHtml :: Blaze.Html -> MandrillHtml
159125mkMandrillHtml = MandrillHtml
160126
161−#if MIN_VERSION_base(4,11,0)
162−instance Semigroup MandrillHtml where
163− MandrillHtml m1 <> MandrillHtml m2 = MandrillHtml (m1 <> m2)
164−#endif
165−
166127instance Monoid MandrillHtml where
167128 mempty = MandrillHtml mempty
168129 mappend (MandrillHtml m1) (MandrillHtml m2) = MandrillHtml (m1 <> m2)
@@ -174,7 +135,7 @@ instance ToJSON MandrillHtml where
174135 toJSON (MandrillHtml h) = String . TL.toStrict . Blaze.renderHtml $ h
175136
176137instance FromJSON MandrillHtml where
177− parseJSON (String h) = return $ MandrillHtml (Blaze.preEscapedToHtml h)
138+ parseJSON (String h) = return $ MandrillHtml (Blaze.toHtml h)
178139 parseJSON v = typeMismatch "Expecting a String for MandrillHtml" v
179140
180141instance Arbitrary MandrillHtml where
@@ -185,23 +146,17 @@ type MandrillTags = T.Text
185146
186147
187148--------------------------------------------------------------------------------
188−type MandrillHeaders = Object
149+type MandrillHeaders = Value
189150
190151
191152--------------------------------------------------------------------------------
153+type MandrillVars = Value
192154
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
200155
201156--------------------------------------------------------------------------------
202157data MandrillMergeVars = MandrillMergeVars {
203158 _mmvr_rcpt :: !T.Text
204− , _mmvr_vars :: [MergeVar]
159+ , _mmvr_vars :: [MandrillVars]
205160 } deriving Show
206161
207162makeLenses ''MandrillMergeVars
@@ -209,33 +164,29 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMergeVars
209164
210165--------------------------------------------------------------------------------
211166data MandrillMetadata = MandrillMetadata {
212− _mmdt_rcpt :: !T.Text
213− , _mmdt_values :: Object
167+ _mmdt_rcpt :: !T.Text
168+ , _mmdt_values :: MandrillVars
214169 } deriving Show
215170
216171makeLenses ''MandrillMetadata
217172deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
218173
219174
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
175+newtype Base64ByteString = B64BS B.ByteString deriving Show
226176
227177instance ToJSON Base64ByteString where
228− toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
229− toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
178+ toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
230179
231180instance FromJSON Base64ByteString where
232− parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
181+ parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
182+ Left err -> fail err
183+ Right rs -> return $ B64BS rs
233184 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
234185
235186--------------------------------------------------------------------------------
236187data MandrillWebContent = MandrillWebContent {
237− _mwct_type :: !T.Text
238− , _mwct_name :: !T.Text
188+ _mwct_type :: !T.Text
189+ , _mwct_name :: !T.Text
239190 -- ^ [for images] the Content ID of the image
240191 -- - use <img src="cid:THIS_VALUE"> to reference the image
241192 -- in your HTML content
@@ -248,67 +199,67 @@ deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillWebContent
248199--------------------------------------------------------------------------------
249200-- | The information on the message to send
250201data MandrillMessage = MandrillMessage {
251− _mmsg_html :: MandrillHtml
202+ _mmsg_html :: MandrillHtml
252203 -- ^ The full HTML content to be sent
253− , _mmsg_text :: Maybe T.Text
204+ , _mmsg_text :: Maybe T.Text
254205 -- ^ Optional full text content to be sent
255− , _mmsg_subject :: !T.Text
206+ , _mmsg_subject :: !T.Text
256207 -- ^ The message subject
257− , _mmsg_from_email :: MandrillEmail
208+ , _mmsg_from_email :: EmailAddress
258209 -- ^ The sender email address
259− , _mmsg_from_name :: Maybe T.Text
210+ , _mmsg_from_name :: Maybe T.Text
260211 -- ^ Optional from name to be used
261− , _mmsg_to :: [MandrillRecipient]
212+ , _mmsg_to :: [MandrillRecipient]
262213 -- ^ A list of recipient information
263− , _mmsg_headers :: MandrillHeaders
214+ , _mmsg_headers :: MandrillHeaders
264215 -- ^ optional extra headers to add to the message (most headers are allowed)
265− , _mmsg_important :: Maybe Bool
216+ , _mmsg_important :: Maybe Bool
266217 -- ^ whether or not this message is important, and should be delivered ahead
267218 -- of non-important messages
268− , _mmsg_track_opens :: Maybe Bool
219+ , _mmsg_track_opens :: Maybe Bool
269220 -- ^ whether or not to turn on open tracking for the message
270− , _mmsg_track_clicks :: Maybe Bool
221+ , _mmsg_track_clicks :: Maybe Bool
271222 -- ^ whether or not to turn on click tracking for the message
272− , _mmsg_auto_text :: Maybe Bool
223+ , _mmsg_auto_text :: Maybe Bool
273224 -- ^ whether or not to automatically generate a text part for messages that are not given text
274− , _mmsg_auto_html :: Maybe Bool
225+ , _mmsg_auto_html :: Maybe Bool
275226 -- ^ whether or not to automatically generate an HTML part for messages that are not given HTML
276− , _mmsg_inline_css :: Maybe Bool
227+ , _mmsg_inline_css :: Maybe Bool
277228 -- ^ whether or not to automatically inline all CSS styles provided in the message HTML
278229 -- - only for HTML documents less than 256KB in size
279− , _mmsg_url_strip_qs :: Maybe Bool
230+ , _mmsg_url_strip_qs :: Maybe Bool
280231 -- ^ whether or not to strip the query string from URLs when aggregating tracked URL data
281− , _mmsg_preserve_recipients :: Maybe Bool
232+ , _mmsg_preserve_recipients :: Maybe Bool
282233 -- ^ whether or not to expose all recipients in to "To" header for each email
283− , _mmsg_view_content_link :: Maybe Bool
234+ , _mmsg_view_content_link :: Maybe Bool
284235 -- ^ set to false to remove content logging for sensitive emails
285− , _mmsg_bcc_address :: Maybe T.Text
236+ , _mmsg_bcc_address :: Maybe T.Text
286237 -- ^ an optional address to receive an exact copy of each recipient's email
287− , _mmsg_tracking_domain :: Maybe T.Text
238+ , _mmsg_tracking_domain :: Maybe T.Text
288239 -- ^ 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
240+ , _mmsg_signing_domain :: Maybe Bool
241+ -- ^ a custom domain to use for SPF/DKIM signing instead of mandrill
291242 -- (for "via" or "on behalf of" in email clients)
292− , _mmsg_return_path_domain :: Maybe Bool
243+ , _mmsg_return_path_domain :: Maybe Bool
293244 -- ^ a custom domain to use for the messages's return-path
294− , _mmsg_merge :: Maybe Bool
245+ , _mmsg_merge :: Maybe Bool
295246 -- ^ whether to evaluate merge tags in the message.
296247 -- Will automatically be set to true if either merge_vars
297248 -- or global_merge_vars are provided.
298− , _mmsg_global_merge_vars :: [MergeVar]
249+ , _mmsg_global_merge_vars :: [MandrillVars]
299250 -- ^ global merge variables to use for all recipients. You can override these per recipient.
300− , _mmsg_merge_vars :: [MandrillMergeVars]
251+ , _mmsg_merge_vars :: [MandrillMergeVars]
301252 -- ^ per-recipient merge variables, which override global merge variables with the same name.
302− , _mmsg_tags :: [MandrillTags]
253+ , _mmsg_tags :: [MandrillTags]
303254 -- ^ an array of string to tag the message with. Stats are accumulated using tags,
304255 -- though we only store the first 100 we see, so this should not be unique
305256 -- or change frequently. Tags should be 50 characters or less.
306257 -- Any tags starting with an underscore are reserved for internal use
307258 -- and will cause errors.
308− , _mmsg_subaccount :: Maybe T.Text
259+ , _mmsg_subaccount :: Maybe T.Text
309260 -- ^ the unique id of a subaccount for this message
310261 -- - must already exist or will fail with an error
311− , _mmsg_google_analytics_domains :: [T.Text]
262+ , _mmsg_google_analytics_domains :: [T.Text]
312263 -- ^ an array of strings indicating for which any matching URLs
313264 -- will automatically have Google Analytics parameters appended
314265 -- to their query string automatically.
@@ -316,17 +267,17 @@ data MandrillMessage = MandrillMessage {
316267 -- ^ optional string indicating the value to set for the utm_campaign
317268 -- tracking parameter. If this isn't provided the email's from address
318269 -- will be used instead.
319− , _mmsg_metadata :: Object
270+ , _mmsg_metadata :: MandrillVars
320271 -- ^ metadata an associative array of user metadata. Mandrill will store
321272 -- this metadata and make it available for retrieval.
322273 -- In addition, you can select up to 10 metadata fields to index
323274 -- and make searchable using the Mandrill search api.
324− , _mmsg_recipient_metadata :: [MandrillMetadata]
275+ , _mmsg_recipient_metadata :: [MandrillMetadata]
325276 -- ^ Per-recipient metadata that will override the global values
326277 -- specified in the metadata parameter.
327− , _mmsg_attachments :: [MandrillWebContent]
278+ , _mmsg_attachments :: [MandrillWebContent]
328279 -- ^ an array of supported attachments to add to the message
329− , _mmsg_images :: [MandrillWebContent]
280+ , _mmsg_images :: [MandrillWebContent]
330281 -- ^ an array of embedded images to add to the message
331282 } deriving Show
332283
@@ -337,10 +288,10 @@ instance Arbitrary MandrillMessage where
337288 arbitrary = MandrillMessage <$> arbitrary
338289 <*> pure Nothing
339290 <*> pure "Test Subject"
340− <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
291+ <*> pure (fromJust $ emailAddress "sender@example.com")
341292 <*> pure Nothing
342293 <*> resize 2 arbitrary
343− <*> pure H.empty
294+ <*> pure emptyObject
344295 <*> pure Nothing
345296 <*> pure Nothing
346297 <*> pure Nothing
@@ -361,24 +312,13 @@ instance Arbitrary MandrillMessage where
361312 <*> pure Nothing
362313 <*> pure []
363314 <*> pure Nothing
364− <*> pure H.empty
315+ <*> pure emptyObject
365316 <*> pure []
366317 <*> pure []
367318 <*> pure []
368319
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−
379320--------------------------------------------------------------------------------
380321type MandrillKey = T.Text
381−type MandrillTemplate = T.Text
382322
383323newtype MandrillDate = MandrillDate {
384324 fromMandrillDate :: UTCTime
@@ -389,6 +329,6 @@ instance ToJSON MandrillDate where
389329
390330instance FromJSON MandrillDate where
391331 parseJSON = withText "MandrillDate" $ \t ->
392− case timeParse defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
332+ case parseTime defaultTimeLocale "%Y-%m-%d %I:%M:%S%Q" (T.unpack t) of
393333 Just d -> pure $ MandrillDate d
394334 _ -> fail "could not parse Mandrill date"
src/Network/API/Mandrill/Users.hs+6−6
@@ -5,28 +5,28 @@ import Network.API.Mandrill.Settings
55import Network.API.Mandrill.HTTP
66import Network.API.Mandrill.Types
77import Network.API.Mandrill.Users.Types
8−import Network.HTTP.Client
8+import Data.Aeson
99
1010
1111--------------------------------------------------------------------------------
1212-- | Return the information about the API-connected user
13−info :: MandrillKey -> Maybe Manager -> IO (MandrillResponse UsersInfoResponse)
13+info :: MandrillKey -> IO (MandrillResponse UsersInfoResponse)
1414info key = toMandrillResponse UsersInfo (UsersRq key)
1515
1616
1717--------------------------------------------------------------------------------
1818-- | Validate an API key and respond to a ping
19−ping :: MandrillKey -> Maybe Manager -> IO (MandrillResponse UsersPingResponse)
20−ping _ _ = fail "users/ping.json doesn't return valid JSON, thus is not implemented yet."
19+ping :: MandrillKey -> IO (MandrillResponse UsersPingResponse)
20+ping _ = fail "users/ping.json doesn't return valid JSON, thus is not implemented yet."
2121
2222
2323--------------------------------------------------------------------------------
2424-- | Validate an API key and respond to a ping (anal JSON parser version)
25−ping2 :: MandrillKey -> Maybe Manager -> IO (MandrillResponse UsersPing2Response)
25+ping2 :: MandrillKey -> IO (MandrillResponse UsersPing2Response)
2626ping2 key = toMandrillResponse UsersPing2 (UsersRq key)
2727
2828
2929--------------------------------------------------------------------------------
3030-- | Return the senders that have tried to use this account, both verified and unverified
31−senders :: MandrillKey -> Maybe Manager -> IO (MandrillResponse [UsersSendersResponse])
31+senders :: MandrillKey -> IO (MandrillResponse [UsersSendersResponse])
3232senders key = toMandrillResponse UsersSenders (UsersRq key)
src/Network/API/Mandrill/Users/Types.hs+1−1
@@ -5,12 +5,12 @@ module Network.API.Mandrill.Users.Types where
55import Data.Char
66import Data.Time
77import qualified Data.Text as T
8+import Control.Lens
89import Control.Monad
910import Data.Monoid
1011import Data.Aeson
1112import Data.Aeson.Types
1213import Data.Aeson.TH
13−import Lens.Micro.TH (makeLenses)
1414
1515import Network.API.Mandrill.Types
1616
src/Network/API/Mandrill/Webhooks.hsdeleted−70
@@ -1,70 +0,0 @@
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.yamldeleted−5
@@ -1,5 +0,0 @@
1−flags: {}
2−packages:
3−- '.'
4−extra-deps: []
5−resolver: lts-11.13
test/Main.hs+7−14
@@ -1,14 +1,14 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Main where
33
4+import System.Environment
45import Data.Monoid
5−import qualified Data.Text as T
6+import Tests
67import Online
7−import System.Environment
88import Test.Tasty
99import Test.Tasty.HUnit
1010import Test.Tasty.QuickCheck
11−import Tests
11+import qualified Data.Text as T
1212
1313----------------------------------------------------------------------
1414withQuickCheckDepth :: TestName -> Int -> [TestTree] -> TestTree
@@ -23,12 +23,10 @@ onlineTestsIfEnabled = do
2323 Nothing -> return []
2424 Just k -> return [
2525 testGroup "Mandrill online tests" [
26− testCase "users/info.json" (testOnlineUsersInfo k)
27− , testCase "users/ping2.json" (testOnlineUsersPing2 k)
28− , testCase "users/senders.json" (testOnlineUsersSenders k)
29− , testCase "messages/send.json" (testOnlineMessagesSend k)
30− , testCase "inbound/addDomain.json" (testOnlineDomainAdd k)
31− , testCase "inbound/addRoute.json" (testOnlineRouteAdd k)
26+ testCase "users/info.json" (testOnlineUsersInfo k)
27+ , testCase "users/ping2.json" (testOnlineUsersPing2 k)
28+ , testCase "users/senders.json" (testOnlineUsersSenders k)
29+ , testCase "messages/send.json" (testOnlineMessagesSend k)
3230 ]]
3331
3432----------------------------------------------------------------------
@@ -41,10 +39,5 @@ main = do
4139 testCase "users/info.json API parsing" testUsersInfo
4240 , testCase "users/senders.json API parsing" testUsersSenders
4341 , 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
4942 ]
5043 ]
test/Online.hs+11−29
@@ -1,41 +1,40 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Online where
33
4−import Data.Aeson
5−import qualified Data.ByteString.Char8 as C8
4+import Test.QuickCheck
5+import Test.Tasty.HUnit
6+import Text.RawString.QQ
67import Data.Either
8+import Data.Aeson
79import Network.API.Mandrill.Messages.Types
810import Network.API.Mandrill.Users.Types
11+import qualified Data.ByteString.Char8 as C8
912import RawData
10−import Test.QuickCheck
11−import Test.Tasty.HUnit
12−import Text.RawString.QQ
1313
14−import qualified Network.API.Mandrill.Inbound as API
15−import qualified Network.API.Mandrill.Messages as API
1614import Network.API.Mandrill.Types
17−import qualified Network.API.Mandrill.Users as API
15+import qualified Network.API.Mandrill.Messages as API
16+import qualified Network.API.Mandrill.Users as API
1817
1918--
2019-- Users calls
2120--
2221testOnlineUsersInfo :: MandrillKey -> Assertion
2322testOnlineUsersInfo k = do
24− res <- API.info k Nothing
23+ res <- API.info k
2524 case res of
2625 MandrillSuccess _ -> return ()
2726 MandrillFailure e -> fail $ "users/info.json " ++ show e
2827
2928testOnlineUsersPing2 :: MandrillKey -> Assertion
3029testOnlineUsersPing2 k = do
31− res <- API.ping2 k Nothing
30+ res <- API.ping2 k
3231 case res of
3332 MandrillSuccess _ -> return ()
3433 MandrillFailure e -> fail $ "users/ping2.json " ++ show e
3534
3635testOnlineUsersSenders :: MandrillKey -> Assertion
3736testOnlineUsersSenders k = do
38− res <- API.senders k Nothing
37+ res <- API.senders k
3938 case res of
4039 MandrillSuccess _ -> return ()
4140 MandrillFailure e -> fail $ "users/senders.json " ++ show e
@@ -47,24 +46,7 @@ testOnlineUsersSenders k = do
4746testOnlineMessagesSend :: MandrillKey -> Assertion
4847testOnlineMessagesSend k = do
4948 msg <- generate arbitrary
50− res <- API.send k msg Nothing Nothing Nothing Nothing
49+ res <- API.send k msg Nothing Nothing Nothing
5150 case res of
5251 MandrillSuccess _ -> return ()
5352 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+4−59
@@ -1,10 +1,10 @@
11{-# LANGUAGE QuasiQuotes #-}
22module RawData where
33
4−import Data.Either
5−import Test.QuickCheck
6−import Test.Tasty.HUnit
7−import Text.RawString.QQ
4+import Test.QuickCheck
5+import Test.Tasty.HUnit
6+import Text.RawString.QQ
7+import Data.Either
88
99usersInfoData :: String
1010usersInfoData = [r|
@@ -200,58 +200,3 @@ sendData = [r|
200200 "send_at": "2014-08-17T14:23:02.954Z"
201201}
202202|]
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+8−54
@@ -1,17 +1,13 @@
11{-# LANGUAGE CPP #-}
22module Tests where
33
4−import Data.Aeson
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
13−import RawData
14−import Test.Tasty.HUnit
4+import Test.Tasty.HUnit
5+import Data.Either
6+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
10+import RawData
1511
1612#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 706
1713isRight :: Either a b -> Bool
@@ -20,7 +16,7 @@ isRight _ = True
2016#endif
2117
2218testMessagesSend :: Assertion
23−testMessagesSend =
19+testMessagesSend =
2420 assertBool ("send.json: Parsing failed! " ++ show parsePayload)
2521 (isRight parsePayload)
2622 where
@@ -44,45 +40,3 @@ testUsersSenders = do
4440 where
4541 parsePayload :: Either String UsersSendersResponse
4642 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