|
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE RecordWildCards #-} |
| 4 | + |
| 5 | +module Network.API.SendGrid.Types where |
| 6 | + |
| 7 | +import Data.Aeson (pairs, (.=), fromEncoding, Encoding) |
| 8 | +import Data.ByteString (ByteString) |
| 9 | +import Data.ByteString.Builder as B (toLazyByteString) |
| 10 | +import Data.ByteString.Lazy as BSL (toStrict) |
| 11 | +import Data.CaseInsensitive (foldedCase) |
| 12 | +import Data.DList as D (toList, fromList) |
| 13 | +import Data.List.NonEmpty as NE (NonEmpty, toList) |
| 14 | +import Data.Maybe (maybeToList) |
| 15 | +import Data.Monoid ((<>)) |
| 16 | +import Data.Text as T (Text, pack, unpack) |
| 17 | +import Data.Text.Encoding (decodeUtf8) |
| 18 | +import Data.These (These(..)) |
| 19 | +import Data.Time (UTCTime(..), rfc822DateFormat, defaultTimeLocale, formatTime) |
| 20 | +import Network.HTTP.Client (RequestBody(RequestBodyBS)) |
| 21 | +import Network.HTTP.Client.MultipartFormData (partFileRequestBody) |
| 22 | +import Network.HTTP.Types.Header (Header) |
| 23 | +import Network.Wreq |
| 24 | +import Network.Wreq.Types (Postable(..)) |
| 25 | +import Text.Blaze.Html (Html) |
| 26 | +import Text.Blaze.Html.Renderer.Utf8 (renderHtml) |
| 27 | +import Text.Email.Validate as E (EmailAddress, toByteString) |
| 28 | + |
| 29 | +data NamedEmail |
| 30 | + = NamedEmail |
| 31 | + { neEmail :: EmailAddress |
| 32 | + , neName :: Text |
| 33 | + } deriving (Eq, Show) |
| 34 | + |
| 35 | +data File |
| 36 | + = File |
| 37 | + { fileName :: Text |
| 38 | + , fileContent :: ByteString |
| 39 | + } deriving (Eq, Show) |
| 40 | + |
| 41 | +data Content |
| 42 | + = Content |
| 43 | + { contentFile :: File |
| 44 | + , contentId :: Text |
| 45 | + } deriving (Eq, Show) |
| 46 | + |
| 47 | +data SendEmail |
| 48 | + = SendEmail |
| 49 | + { sendTo :: Either (NonEmpty NamedEmail) (NonEmpty EmailAddress) |
| 50 | + , sendSubject :: Text |
| 51 | + , sendBody :: These Html Text |
| 52 | + , sendFrom :: EmailAddress |
| 53 | + , sendCc :: Maybe (Either (NonEmpty NamedEmail) (NonEmpty EmailAddress)) |
| 54 | + , sendBcc :: Maybe (Either (NonEmpty NamedEmail) (NonEmpty EmailAddress)) |
| 55 | + , sendFromName :: Maybe Text |
| 56 | + , sendReplyTo :: Maybe EmailAddress |
| 57 | + , sendDate :: Maybe UTCTime |
| 58 | + -- Don't duplicate files from `sendContent` here |
| 59 | + , sendFiles :: Maybe (NonEmpty File) |
| 60 | + , sendContent :: Maybe (NonEmpty Content) |
| 61 | + , sendHeaders :: Maybe (NonEmpty Header) |
| 62 | + } |
| 63 | +-- Can't derive (`Eq` or `Show`) because of `Html` |
| 64 | + |
| 65 | +mkSendEmail :: Either (NonEmpty NamedEmail) (NonEmpty EmailAddress) -> Text -> These Html Text -> EmailAddress -> SendEmail |
| 66 | +mkSendEmail to subject body from |
| 67 | + = SendEmail |
| 68 | + { sendTo = to |
| 69 | + , sendSubject = subject |
| 70 | + , sendBody = body |
| 71 | + , sendFrom = from |
| 72 | + , sendCc = Nothing |
| 73 | + , sendBcc = Nothing |
| 74 | + , sendFromName = Nothing |
| 75 | + , sendReplyTo = Nothing |
| 76 | + , sendDate = Nothing |
| 77 | + , sendFiles = Nothing |
| 78 | + , sendContent = Nothing |
| 79 | + , sendHeaders = Nothing |
| 80 | + } |
| 81 | + |
| 82 | +emailsToParts :: Text -> Text -> Either (NonEmpty NamedEmail) (NonEmpty EmailAddress) -> [Part] |
| 83 | +emailsToParts emailKey nameKey (Left namedEmails) = |
| 84 | + flip foldMap namedEmails |
| 85 | + (\NamedEmail{..} -> |
| 86 | + [ partBS emailKey $ E.toByteString neEmail |
| 87 | + , partText nameKey neName |
| 88 | + ]) |
| 89 | +emailsToParts emailKey _ (Right emails) = |
| 90 | + partBS emailKey . E.toByteString <$> NE.toList emails |
| 91 | + |
| 92 | +instance Postable SendEmail where |
| 93 | + postPayload = postPayload . sendEmailToParts |
| 94 | + |
| 95 | +sendEmailToParts :: SendEmail -> [Part] |
| 96 | +sendEmailToParts SendEmail{..} = |
| 97 | + D.toList $ foldMap D.fromList |
| 98 | + [ toParts |
| 99 | + , [subjectPart] |
| 100 | + , bodyParts |
| 101 | + , [fromPart] |
| 102 | + , ccParts |
| 103 | + , bccParts |
| 104 | + , fromNamePart |
| 105 | + , replyToPart |
| 106 | + , datePart |
| 107 | + , headerPart |
| 108 | + , fileParts |
| 109 | + , contentParts |
| 110 | + ] |
| 111 | + where |
| 112 | + fileToPart File{..} = partFileRequestBody ("files[" <> fileName <> "]") (T.unpack fileName) (RequestBodyBS fileContent) |
| 113 | + contentParts = maybe [] (foldMap contentToParts . NE.toList) sendContent |
| 114 | + where |
| 115 | + contentToParts (Content file@File{..} contentId) = |
| 116 | + [fileToPart file, partText ("content[" <> fileName <> "]") contentId] |
| 117 | + fileParts = maybe [] (map fileToPart . NE.toList) sendFiles |
| 118 | + toParts = emailsToParts "to[]" "toname[]" sendTo |
| 119 | + fromPart = partBS "from" $ E.toByteString sendFrom |
| 120 | + ccParts = maybe [] (emailsToParts "cc[]" "ccname[]") sendCc |
| 121 | + bccParts = maybe [] (emailsToParts "bcc[]" "bccname[]") sendBcc |
| 122 | + fromNamePart = maybeToList $ partText "fromname" <$> sendFromName |
| 123 | + replyToPart = maybeToList $ partBS "replyto" . E.toByteString <$> sendReplyTo |
| 124 | + datePart = maybeToList $ partText "date" . T.pack . formatTime defaultTimeLocale rfc822DateFormat <$> sendDate |
| 125 | + subjectPart = partText "subject" sendSubject |
| 126 | + headerPart = maybeToList $ partBS "headers" . encodingToByteString . encodeHeaders . NE.toList <$> sendHeaders |
| 127 | + bodyParts = |
| 128 | + case sendBody of |
| 129 | + This html -> [partBS "html" . BSL.toStrict $ renderHtml html] |
| 130 | + That text -> [partText "text" text] |
| 131 | + These html text -> [partBS "html" . BSL.toStrict $ renderHtml html, partText "text" text] |
| 132 | + |
| 133 | +encodingToByteString :: Encoding -> ByteString |
| 134 | +encodingToByteString = BSL.toStrict . B.toLazyByteString . fromEncoding |
| 135 | + |
| 136 | +encodeHeaders :: [Header] -> Encoding |
| 137 | +encodeHeaders = pairs . foldMap (\(key, value) -> decodeUtf8 (foldedCase key) .= decodeUtf8 value) |
0 commit comments