module Aws.Ses.Commands.SendRawEmail
    ( SendRawEmail(..)
    , SendRawEmailResponse(..)
    ) where

import Data.Text (Text)
import Data.Typeable
import Control.Applicative ((<$>))
import qualified Data.ByteString.Char8 as BS
import Text.XML.Cursor (($//))
import qualified Data.Text.Encoding as T

import Aws.Core
import Aws.Ses.Core

-- | Send a raw e-mail message.
data SendRawEmail =
    SendRawEmail
      { SendRawEmail -> [EmailAddress]
srmDestinations :: [EmailAddress]
      , SendRawEmail -> RawMessage
srmRawMessage   :: RawMessage
      , SendRawEmail -> Maybe Sender
srmSource       :: Maybe Sender
      }
    deriving (SendRawEmail -> SendRawEmail -> Bool
(SendRawEmail -> SendRawEmail -> Bool)
-> (SendRawEmail -> SendRawEmail -> Bool) -> Eq SendRawEmail
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SendRawEmail -> SendRawEmail -> Bool
$c/= :: SendRawEmail -> SendRawEmail -> Bool
== :: SendRawEmail -> SendRawEmail -> Bool
$c== :: SendRawEmail -> SendRawEmail -> Bool
Eq, Eq SendRawEmail
Eq SendRawEmail
-> (SendRawEmail -> SendRawEmail -> Ordering)
-> (SendRawEmail -> SendRawEmail -> Bool)
-> (SendRawEmail -> SendRawEmail -> Bool)
-> (SendRawEmail -> SendRawEmail -> Bool)
-> (SendRawEmail -> SendRawEmail -> Bool)
-> (SendRawEmail -> SendRawEmail -> SendRawEmail)
-> (SendRawEmail -> SendRawEmail -> SendRawEmail)
-> Ord SendRawEmail
SendRawEmail -> SendRawEmail -> Bool
SendRawEmail -> SendRawEmail -> Ordering
SendRawEmail -> SendRawEmail -> SendRawEmail
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: SendRawEmail -> SendRawEmail -> SendRawEmail
$cmin :: SendRawEmail -> SendRawEmail -> SendRawEmail
max :: SendRawEmail -> SendRawEmail -> SendRawEmail
$cmax :: SendRawEmail -> SendRawEmail -> SendRawEmail
>= :: SendRawEmail -> SendRawEmail -> Bool
$c>= :: SendRawEmail -> SendRawEmail -> Bool
> :: SendRawEmail -> SendRawEmail -> Bool
$c> :: SendRawEmail -> SendRawEmail -> Bool
<= :: SendRawEmail -> SendRawEmail -> Bool
$c<= :: SendRawEmail -> SendRawEmail -> Bool
< :: SendRawEmail -> SendRawEmail -> Bool
$c< :: SendRawEmail -> SendRawEmail -> Bool
compare :: SendRawEmail -> SendRawEmail -> Ordering
$ccompare :: SendRawEmail -> SendRawEmail -> Ordering
$cp1Ord :: Eq SendRawEmail
Ord, Int -> SendRawEmail -> ShowS
[SendRawEmail] -> ShowS
SendRawEmail -> String
(Int -> SendRawEmail -> ShowS)
-> (SendRawEmail -> String)
-> ([SendRawEmail] -> ShowS)
-> Show SendRawEmail
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SendRawEmail] -> ShowS
$cshowList :: [SendRawEmail] -> ShowS
show :: SendRawEmail -> String
$cshow :: SendRawEmail -> String
showsPrec :: Int -> SendRawEmail -> ShowS
$cshowsPrec :: Int -> SendRawEmail -> ShowS
Show, Typeable)

-- | ServiceConfiguration: 'SesConfiguration'
instance SignQuery SendRawEmail where
    type ServiceConfiguration SendRawEmail = SesConfiguration
    signQuery :: SendRawEmail
-> ServiceConfiguration SendRawEmail queryType
-> SignatureData
-> SignedQuery
signQuery SendRawEmail {[EmailAddress]
Maybe Sender
RawMessage
srmSource :: Maybe Sender
srmRawMessage :: RawMessage
srmDestinations :: [EmailAddress]
srmSource :: SendRawEmail -> Maybe Sender
srmRawMessage :: SendRawEmail -> RawMessage
srmDestinations :: SendRawEmail -> [EmailAddress]
..} =
        [(ByteString, ByteString)]
-> SesConfiguration queryType -> SignatureData -> SignedQuery
forall qt.
[(ByteString, ByteString)]
-> SesConfiguration qt -> SignatureData -> SignedQuery
sesSignQuery ([(ByteString, ByteString)]
 -> SesConfiguration queryType -> SignatureData -> SignedQuery)
-> [(ByteString, ByteString)]
-> SesConfiguration queryType
-> SignatureData
-> SignedQuery
forall a b. (a -> b) -> a -> b
$ (ByteString
"Action", ByteString
"SendRawEmail") (ByteString, ByteString)
-> [(ByteString, ByteString)] -> [(ByteString, ByteString)]
forall a. a -> [a] -> [a]
:
                       [[(ByteString, ByteString)]] -> [(ByteString, ByteString)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [(ByteString, ByteString)]
destinations
                              , RawMessage -> [(ByteString, ByteString)]
forall a. SesAsQuery a => a -> [(ByteString, ByteString)]
sesAsQuery RawMessage
srmRawMessage
                              , Maybe Sender -> [(ByteString, ByteString)]
forall a. SesAsQuery a => a -> [(ByteString, ByteString)]
sesAsQuery Maybe Sender
srmSource
                              ]
      where
        destinations :: [(ByteString, ByteString)]
destinations = [ByteString] -> [ByteString] -> [(ByteString, ByteString)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Int -> ByteString
enumMember   (Int -> ByteString) -> [Int] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Int
1..] :: [Int]))
                           (EmailAddress -> ByteString
T.encodeUtf8 (EmailAddress -> ByteString) -> [EmailAddress] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>  [EmailAddress]
srmDestinations)
        enumMember :: Int -> ByteString
enumMember   = ByteString -> ByteString -> ByteString
BS.append ByteString
"Destinations.member." (ByteString -> ByteString)
-> (Int -> ByteString) -> Int -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
BS.pack (String -> ByteString) -> (Int -> String) -> Int -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show

-- | The response sent back by Amazon SES after a
-- 'SendRawEmail' command.
data SendRawEmailResponse =
    SendRawEmailResponse { SendRawEmailResponse -> EmailAddress
srmrMessageId :: Text }
    deriving (SendRawEmailResponse -> SendRawEmailResponse -> Bool
(SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> (SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> Eq SendRawEmailResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c/= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
== :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c== :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
Eq, Eq SendRawEmailResponse
Eq SendRawEmailResponse
-> (SendRawEmailResponse -> SendRawEmailResponse -> Ordering)
-> (SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> (SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> (SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> (SendRawEmailResponse -> SendRawEmailResponse -> Bool)
-> (SendRawEmailResponse
    -> SendRawEmailResponse -> SendRawEmailResponse)
-> (SendRawEmailResponse
    -> SendRawEmailResponse -> SendRawEmailResponse)
-> Ord SendRawEmailResponse
SendRawEmailResponse -> SendRawEmailResponse -> Bool
SendRawEmailResponse -> SendRawEmailResponse -> Ordering
SendRawEmailResponse
-> SendRawEmailResponse -> SendRawEmailResponse
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: SendRawEmailResponse
-> SendRawEmailResponse -> SendRawEmailResponse
$cmin :: SendRawEmailResponse
-> SendRawEmailResponse -> SendRawEmailResponse
max :: SendRawEmailResponse
-> SendRawEmailResponse -> SendRawEmailResponse
$cmax :: SendRawEmailResponse
-> SendRawEmailResponse -> SendRawEmailResponse
>= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c>= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
> :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c> :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
<= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c<= :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
< :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
$c< :: SendRawEmailResponse -> SendRawEmailResponse -> Bool
compare :: SendRawEmailResponse -> SendRawEmailResponse -> Ordering
$ccompare :: SendRawEmailResponse -> SendRawEmailResponse -> Ordering
$cp1Ord :: Eq SendRawEmailResponse
Ord, Int -> SendRawEmailResponse -> ShowS
[SendRawEmailResponse] -> ShowS
SendRawEmailResponse -> String
(Int -> SendRawEmailResponse -> ShowS)
-> (SendRawEmailResponse -> String)
-> ([SendRawEmailResponse] -> ShowS)
-> Show SendRawEmailResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SendRawEmailResponse] -> ShowS
$cshowList :: [SendRawEmailResponse] -> ShowS
show :: SendRawEmailResponse -> String
$cshow :: SendRawEmailResponse -> String
showsPrec :: Int -> SendRawEmailResponse -> ShowS
$cshowsPrec :: Int -> SendRawEmailResponse -> ShowS
Show, Typeable)


instance ResponseConsumer SendRawEmail SendRawEmailResponse where
    type ResponseMetadata SendRawEmailResponse = SesMetadata
    responseConsumer :: Request
-> SendRawEmail
-> IORef (ResponseMetadata SendRawEmailResponse)
-> HTTPResponseConsumer SendRawEmailResponse
responseConsumer Request
_ SendRawEmail
_ =
      (Cursor -> Response SesMetadata SendRawEmailResponse)
-> IORef SesMetadata -> HTTPResponseConsumer SendRawEmailResponse
forall a.
(Cursor -> Response SesMetadata a)
-> IORef SesMetadata -> HTTPResponseConsumer a
sesResponseConsumer ((Cursor -> Response SesMetadata SendRawEmailResponse)
 -> IORef SesMetadata -> HTTPResponseConsumer SendRawEmailResponse)
-> (Cursor -> Response SesMetadata SendRawEmailResponse)
-> IORef SesMetadata
-> HTTPResponseConsumer SendRawEmailResponse
forall a b. (a -> b) -> a -> b
$ \Cursor
cursor -> do
        EmailAddress
messageId <- String -> [EmailAddress] -> Response SesMetadata EmailAddress
forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"MessageId not found" ([EmailAddress] -> Response SesMetadata EmailAddress)
-> [EmailAddress] -> Response SesMetadata EmailAddress
forall a b. (a -> b) -> a -> b
$ Cursor
cursor Cursor -> (Cursor -> [EmailAddress]) -> [EmailAddress]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$// EmailAddress -> Cursor -> [EmailAddress]
elContent EmailAddress
"MessageId"
        SendRawEmailResponse -> Response SesMetadata SendRawEmailResponse
forall (m :: * -> *) a. Monad m => a -> m a
return (EmailAddress -> SendRawEmailResponse
SendRawEmailResponse EmailAddress
messageId)


instance Transaction SendRawEmail SendRawEmailResponse where

instance AsMemoryResponse SendRawEmailResponse where
    type MemoryResponse SendRawEmailResponse = SendRawEmailResponse
    loadToMemory :: SendRawEmailResponse
-> ResourceT IO (MemoryResponse SendRawEmailResponse)
loadToMemory = SendRawEmailResponse
-> ResourceT IO (MemoryResponse SendRawEmailResponse)
forall (m :: * -> *) a. Monad m => a -> m a
return