git.iko.soy/typeable/mandrill
on master help sign in
Modify `Base64ByteString` type to support already-encoded payloads

This patch allows the user to pass his own already-encoded B64 bytestrings to be used as attachments.

commit9c70c134e7bd75744975d7ac74424118816cdd6b authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateSun 30 Aug 2015, 15:45 (11 years ago) parent66180b8
3 files changed+18−6
README.md+8 mandrill.cabal+1−1 src/Network/API/Mandrill/Types.hs+9−5
README.md+8
@@ -6,6 +6,14 @@
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.4.0.0
12+
13+* Modified the `Base64ByteString` type to accept another constructor. This
14+ allows the user to pass already-encoded Base64 strings which might be coming
15+ upstream.
16+
917# Example
1018
1119This package was built with pragmatism and reuse in mind. This means
mandrill.cabal+1−1
@@ -1,5 +1,5 @@
11name: mandrill
2−version: 0.3.0.0
2+version: 0.4.0.0
33synopsis: Library for interfacing with the Mandrill JSON API
44description: Pure Haskell client for the Mandrill JSON API
55license: MIT
src/Network/API/Mandrill/Types.hs+9−5
@@ -196,15 +196,19 @@ makeLenses ''MandrillMetadata
196196deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillMetadata
197197
198198
199−newtype Base64ByteString = B64BS B.ByteString deriving Show
199+data Base64ByteString =
200+ EncodedB64BS B.ByteString
201+ -- ^ An already-encoded Base64 ByteString.
202+ | PlainBS B.ByteString
203+ -- ^ A plain Base64 ByteString which requires encoding.
204+ deriving Show
200205
201206instance ToJSON Base64ByteString where
202− toJSON (B64BS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
207+ toJSON (PlainBS bs) = String . TL.decodeUtf8 . Base64.encode $ bs
208+ toJSON (EncodedB64BS bs) = String . TL.decodeUtf8 $ bs
203209
204210instance FromJSON Base64ByteString where
205− parseJSON (String v) = case Base64.decode (TL.encodeUtf8 v) of
206− Left err -> fail err
207− Right rs -> return $ B64BS rs
211+ parseJSON (String v) = pure $ EncodedB64BS (TL.encodeUtf8 v)
208212 parseJSON rest = typeMismatch "Base64ByteString must be a String." rest
209213
210214--------------------------------------------------------------------------------