-- |
-- Module      : Amazonka.Response
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
--
-- Functions contained in this module fully consume the body and thus close
-- the connection. This is needed to avoid hitting this issue:
-- <https://github.com/brendanhay/amazonka/issues/490>.
--
-- The only exception is 'receiveBody', which passes a streaming response
-- body to a callback and thus is not allowed to close the connection. Users
-- of streaming functions are advised to be careful and consume the response
-- body manually if they want the connection to be closed promptly.
--
-- Note that using 'runResourceT' will always close the connection.
module Amazonka.Response
  ( receiveNull,
    receiveEmpty,
    receiveXMLWrapper,
    receiveXML,
    receiveJSON,
    receiveBytes,
    receiveBody,
  )
where

import Amazonka.Data
import Amazonka.Prelude
import Amazonka.Types
import qualified Control.Monad.Trans.Except as Except
import Control.Monad.Trans.Resource (liftResourceT)
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.Conduit ()
import qualified Data.Conduit as Conduit
import qualified Data.Conduit.Binary as Conduit.Binary
import qualified Network.HTTP.Client as Client
import Network.HTTP.Types (ResponseHeaders)
import qualified Text.XML as XML

receiveNull ::
  MonadResource m =>
  AWSResponse a ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveNull :: AWSResponse a
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveNull AWSResponse a
rs Logger
_ =
  (ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) a.
MonadResource m =>
(ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
stream ((ClientResponse ()
  -> Int
  -> ResponseHeaders
  -> ClientBody
  -> m (Either String (AWSResponse a)))
 -> Service
 -> Proxy a
 -> ClientResponse ClientBody
 -> m (Either Error (ClientResponse (AWSResponse a))))
-> (ClientResponse ()
    -> Int
    -> ResponseHeaders
    -> ClientBody
    -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall a b. (a -> b) -> a -> b
$ \ClientResponse ()
r Int
_ ResponseHeaders
_ ClientBody
_ ->
    IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ClientResponse () -> IO ()
forall a. Response a -> IO ()
Client.responseClose ClientResponse ()
r) m ()
-> m (Either String (AWSResponse a))
-> m (Either String (AWSResponse a))
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Either String (AWSResponse a) -> m (Either String (AWSResponse a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AWSResponse a -> Either String (AWSResponse a)
forall a b. b -> Either a b
Right AWSResponse a
rs)

receiveEmpty ::
  MonadResource m =>
  (Int -> ResponseHeaders -> () -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveEmpty :: (Int -> ResponseHeaders -> () -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveEmpty Int -> ResponseHeaders -> () -> Either String (AWSResponse a)
f Logger
_ =
  (ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) a.
MonadResource m =>
(ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
stream ((ClientResponse ()
  -> Int
  -> ResponseHeaders
  -> ClientBody
  -> m (Either String (AWSResponse a)))
 -> Service
 -> Proxy a
 -> ClientResponse ClientBody
 -> m (Either Error (ClientResponse (AWSResponse a))))
-> (ClientResponse ()
    -> Int
    -> ResponseHeaders
    -> ClientBody
    -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall a b. (a -> b) -> a -> b
$ \ClientResponse ()
r Int
s ResponseHeaders
h ClientBody
_ ->
    IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ClientResponse () -> IO ()
forall a. Response a -> IO ()
Client.responseClose ClientResponse ()
r) m ()
-> m (Either String (AWSResponse a))
-> m (Either String (AWSResponse a))
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Either String (AWSResponse a) -> m (Either String (AWSResponse a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ResponseHeaders -> () -> Either String (AWSResponse a)
f Int
s ResponseHeaders
h ())

receiveXMLWrapper ::
  MonadResource m =>
  Text ->
  (Int -> ResponseHeaders -> [XML.Node] -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveXMLWrapper :: Text
-> (Int
    -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveXMLWrapper Text
n Int -> ResponseHeaders -> [Node] -> Either String (AWSResponse a)
f = (Int -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveXML (\Int
s ResponseHeaders
h [Node]
x -> [Node]
x [Node] -> Text -> Either String [Node]
forall a. FromXML a => [Node] -> Text -> Either String a
.@ Text
n Either String [Node]
-> ([Node] -> Either String (AWSResponse a))
-> Either String (AWSResponse a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> ResponseHeaders -> [Node] -> Either String (AWSResponse a)
f Int
s ResponseHeaders
h)

receiveXML ::
  MonadResource m =>
  (Int -> ResponseHeaders -> [XML.Node] -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveXML :: (Int -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveXML = (ByteStringLazy -> Either String [Node])
-> (Int
    -> ResponseHeaders -> [Node] -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) b a.
MonadResource m =>
(ByteStringLazy -> Either String b)
-> (Int -> ResponseHeaders -> b -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
deserialise ByteStringLazy -> Either String [Node]
forall a. FromXML a => ByteStringLazy -> Either String a
decodeXML

receiveJSON ::
  MonadResource m =>
  (Int -> ResponseHeaders -> Aeson.Object -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveJSON :: (Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveJSON = (ByteStringLazy -> Either String Object)
-> (Int
    -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) b a.
MonadResource m =>
(ByteStringLazy -> Either String b)
-> (Int -> ResponseHeaders -> b -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
deserialise ByteStringLazy -> Either String Object
forall a. FromJSON a => ByteStringLazy -> Either String a
Aeson.eitherDecode'

receiveBytes ::
  MonadResource m =>
  (Int -> ResponseHeaders -> ByteString -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveBytes :: (Int
 -> ResponseHeaders -> ByteString -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveBytes = (ByteStringLazy -> Either String ByteString)
-> (Int
    -> ResponseHeaders -> ByteString -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) b a.
MonadResource m =>
(ByteStringLazy -> Either String b)
-> (Int -> ResponseHeaders -> b -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
deserialise (ByteString -> Either String ByteString
forall a b. b -> Either a b
Right (ByteString -> Either String ByteString)
-> (ByteStringLazy -> ByteString)
-> ByteStringLazy
-> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteStringLazy -> ByteString
LBS.toStrict)

receiveBody ::
  MonadResource m =>
  (Int -> ResponseHeaders -> ResponseBody -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
receiveBody :: (Int
 -> ResponseHeaders
 -> ResponseBody
 -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
receiveBody Int
-> ResponseHeaders -> ResponseBody -> Either String (AWSResponse a)
f Logger
_ =
  (ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall (m :: * -> *) a.
MonadResource m =>
(ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
stream ((ClientResponse ()
  -> Int
  -> ResponseHeaders
  -> ClientBody
  -> m (Either String (AWSResponse a)))
 -> Service
 -> Proxy a
 -> ClientResponse ClientBody
 -> m (Either Error (ClientResponse (AWSResponse a))))
-> (ClientResponse ()
    -> Int
    -> ResponseHeaders
    -> ClientBody
    -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
forall a b. (a -> b) -> a -> b
$ \ClientResponse ()
_ Int
s ResponseHeaders
h ClientBody
x ->
    Either String (AWSResponse a) -> m (Either String (AWSResponse a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
-> ResponseHeaders -> ResponseBody -> Either String (AWSResponse a)
f Int
s ResponseHeaders
h (ClientBody -> ResponseBody
ResponseBody ClientBody
x))

-- | Deserialise an entire response body, such as an XML or JSON payload.
deserialise ::
  MonadResource m =>
  (ByteStringLazy -> Either String b) ->
  (Int -> ResponseHeaders -> b -> Either String (AWSResponse a)) ->
  Logger ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
deserialise :: (ByteStringLazy -> Either String b)
-> (Int -> ResponseHeaders -> b -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
deserialise ByteStringLazy -> Either String b
reader Int -> ResponseHeaders -> b -> Either String (AWSResponse a)
parser Logger
logger Service {Maybe Seconds
ByteString
Signer
Retry
Abbrev
Status -> Bool
Status -> ResponseHeaders -> ByteStringLazy -> Error
Region -> Endpoint
$sel:_serviceRetry:Service :: Service -> Retry
$sel:_serviceError:Service :: Service -> Status -> ResponseHeaders -> ByteStringLazy -> Error
$sel:_serviceCheck:Service :: Service -> Status -> Bool
$sel:_serviceTimeout:Service :: Service -> Maybe Seconds
$sel:_serviceEndpoint:Service :: Service -> Region -> Endpoint
$sel:_serviceEndpointPrefix:Service :: Service -> ByteString
$sel:_serviceVersion:Service :: Service -> ByteString
$sel:_serviceSigningName:Service :: Service -> ByteString
$sel:_serviceSigner:Service :: Service -> Signer
$sel:_serviceAbbrev:Service :: Service -> Abbrev
_serviceRetry :: Retry
_serviceError :: Status -> ResponseHeaders -> ByteStringLazy -> Error
_serviceCheck :: Status -> Bool
_serviceTimeout :: Maybe Seconds
_serviceEndpoint :: Region -> Endpoint
_serviceEndpointPrefix :: ByteString
_serviceVersion :: ByteString
_serviceSigningName :: ByteString
_serviceSigner :: Signer
_serviceAbbrev :: Abbrev
..} Proxy a
_ ClientResponse ClientBody
rs =
  ExceptT Error m (ClientResponse (AWSResponse a))
-> m (Either Error (ClientResponse (AWSResponse a)))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
Except.runExceptT (ExceptT Error m (ClientResponse (AWSResponse a))
 -> m (Either Error (ClientResponse (AWSResponse a))))
-> ExceptT Error m (ClientResponse (AWSResponse a))
-> m (Either Error (ClientResponse (AWSResponse a)))
forall a b. (a -> b) -> a -> b
$ do
    let status :: Status
status = ClientResponse ClientBody -> Status
forall body. Response body -> Status
Client.responseStatus ClientResponse ClientBody
rs
        headers :: ResponseHeaders
headers = ClientResponse ClientBody -> ResponseHeaders
forall body. Response body -> ResponseHeaders
Client.responseHeaders ClientResponse ClientBody
rs

    ByteStringLazy
body <- ClientBody -> ExceptT Error m ByteStringLazy
forall (m :: * -> *).
MonadResource m =>
ClientBody -> m ByteStringLazy
sinkLBS (ClientResponse ClientBody -> ClientBody
forall body. Response body -> body
Client.responseBody ClientResponse ClientBody
rs)

    Bool -> ExceptT Error m () -> ExceptT Error m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Status -> Bool
_serviceCheck Status
status) (ExceptT Error m () -> ExceptT Error m ())
-> ExceptT Error m () -> ExceptT Error m ()
forall a b. (a -> b) -> a -> b
$
      Error -> ExceptT Error m ()
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
Except.throwE (Status -> ResponseHeaders -> ByteStringLazy -> Error
_serviceError Status
status ResponseHeaders
headers ByteStringLazy
body)

    IO () -> ExceptT Error m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT Error m ())
-> (ByteStringBuilder -> IO ())
-> ByteStringBuilder
-> ExceptT Error m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Logger
logger LogLevel
Trace (ByteStringBuilder -> ExceptT Error m ())
-> ByteStringBuilder -> ExceptT Error m ()
forall a b. (a -> b) -> a -> b
$
      ByteStringLazy -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build (ByteStringLazy
"[Raw Response Body] {\n" ByteStringLazy -> ByteStringLazy -> ByteStringLazy
forall a. Semigroup a => a -> a -> a
<> ByteStringLazy
body ByteStringLazy -> ByteStringLazy -> ByteStringLazy
forall a. Semigroup a => a -> a -> a
<> ByteStringLazy
"\n}")

    case ByteStringLazy -> Either String b
reader ByteStringLazy
body Either String b
-> (b -> Either String (AWSResponse a))
-> Either String (AWSResponse a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> ResponseHeaders -> b -> Either String (AWSResponse a)
parser (Status -> Int
forall a. Enum a => a -> Int
fromEnum Status
status) ResponseHeaders
headers of
      Right AWSResponse a
ok -> ClientResponse (AWSResponse a)
-> ExceptT Error m (ClientResponse (AWSResponse a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AWSResponse a
ok AWSResponse a
-> ClientResponse ClientBody -> ClientResponse (AWSResponse a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ClientResponse ClientBody
rs)
      Left String
err ->
        Error -> ExceptT Error m (ClientResponse (AWSResponse a))
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
Except.throwE (Error -> ExceptT Error m (ClientResponse (AWSResponse a)))
-> Error -> ExceptT Error m (ClientResponse (AWSResponse a))
forall a b. (a -> b) -> a -> b
$
          SerializeError -> Error
SerializeError (Abbrev
-> Status -> Maybe ByteStringLazy -> String -> SerializeError
SerializeError' Abbrev
_serviceAbbrev Status
status (ByteStringLazy -> Maybe ByteStringLazy
forall a. a -> Maybe a
Just ByteStringLazy
body) String
err)

-- | Stream a raw response body, such as an S3 object payload.
stream ::
  MonadResource m =>
  ( ClientResponse () ->
    Int ->
    ResponseHeaders ->
    ClientBody ->
    m (Either String (AWSResponse a))
  ) ->
  Service ->
  Proxy a ->
  ClientResponse ClientBody ->
  m (Either Error (ClientResponse (AWSResponse a)))
stream :: (ClientResponse ()
 -> Int
 -> ResponseHeaders
 -> ClientBody
 -> m (Either String (AWSResponse a)))
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
stream ClientResponse ()
-> Int
-> ResponseHeaders
-> ClientBody
-> m (Either String (AWSResponse a))
parser Service {Maybe Seconds
ByteString
Signer
Retry
Abbrev
Status -> Bool
Status -> ResponseHeaders -> ByteStringLazy -> Error
Region -> Endpoint
_serviceRetry :: Retry
_serviceError :: Status -> ResponseHeaders -> ByteStringLazy -> Error
_serviceCheck :: Status -> Bool
_serviceTimeout :: Maybe Seconds
_serviceEndpoint :: Region -> Endpoint
_serviceEndpointPrefix :: ByteString
_serviceVersion :: ByteString
_serviceSigningName :: ByteString
_serviceSigner :: Signer
_serviceAbbrev :: Abbrev
$sel:_serviceRetry:Service :: Service -> Retry
$sel:_serviceError:Service :: Service -> Status -> ResponseHeaders -> ByteStringLazy -> Error
$sel:_serviceCheck:Service :: Service -> Status -> Bool
$sel:_serviceTimeout:Service :: Service -> Maybe Seconds
$sel:_serviceEndpoint:Service :: Service -> Region -> Endpoint
$sel:_serviceEndpointPrefix:Service :: Service -> ByteString
$sel:_serviceVersion:Service :: Service -> ByteString
$sel:_serviceSigningName:Service :: Service -> ByteString
$sel:_serviceSigner:Service :: Service -> Signer
$sel:_serviceAbbrev:Service :: Service -> Abbrev
..} Proxy a
_ ClientResponse ClientBody
rs =
  ExceptT Error m (ClientResponse (AWSResponse a))
-> m (Either Error (ClientResponse (AWSResponse a)))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
Except.runExceptT (ExceptT Error m (ClientResponse (AWSResponse a))
 -> m (Either Error (ClientResponse (AWSResponse a))))
-> ExceptT Error m (ClientResponse (AWSResponse a))
-> m (Either Error (ClientResponse (AWSResponse a)))
forall a b. (a -> b) -> a -> b
$ do
    let status :: Status
status = ClientResponse ClientBody -> Status
forall body. Response body -> Status
Client.responseStatus ClientResponse ClientBody
rs
        headers :: ResponseHeaders
headers = ClientResponse ClientBody -> ResponseHeaders
forall body. Response body -> ResponseHeaders
Client.responseHeaders ClientResponse ClientBody
rs
        body :: ClientBody
body = ClientResponse ClientBody -> ClientBody
forall body. Response body -> body
Client.responseBody ClientResponse ClientBody
rs

    Bool -> ExceptT Error m () -> ExceptT Error m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Status -> Bool
_serviceCheck Status
status) (ExceptT Error m () -> ExceptT Error m ())
-> ExceptT Error m () -> ExceptT Error m ()
forall a b. (a -> b) -> a -> b
$ do
      ByteStringLazy
lazy <- ClientBody -> ExceptT Error m ByteStringLazy
forall (m :: * -> *).
MonadResource m =>
ClientBody -> m ByteStringLazy
sinkLBS ClientBody
body
      Error -> ExceptT Error m ()
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
Except.throwE (Status -> ResponseHeaders -> ByteStringLazy -> Error
_serviceError Status
status ResponseHeaders
headers ByteStringLazy
lazy)

    m (Either String (AWSResponse a))
-> ExceptT Error m (Either String (AWSResponse a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ClientResponse ()
-> Int
-> ResponseHeaders
-> ClientBody
-> m (Either String (AWSResponse a))
parser (() () -> ClientResponse ClientBody -> ClientResponse ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ClientResponse ClientBody
rs) (Status -> Int
forall a. Enum a => a -> Int
fromEnum Status
status) ResponseHeaders
headers ClientBody
body) ExceptT Error m (Either String (AWSResponse a))
-> (Either String (AWSResponse a)
    -> ExceptT Error m (ClientResponse (AWSResponse a)))
-> ExceptT Error m (ClientResponse (AWSResponse a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Right AWSResponse a
ok -> ClientResponse (AWSResponse a)
-> ExceptT Error m (ClientResponse (AWSResponse a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AWSResponse a
ok AWSResponse a
-> ClientResponse ClientBody -> ClientResponse (AWSResponse a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ClientResponse ClientBody
rs)
      Left String
err ->
        Error -> ExceptT Error m (ClientResponse (AWSResponse a))
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
Except.throwE (Error -> ExceptT Error m (ClientResponse (AWSResponse a)))
-> Error -> ExceptT Error m (ClientResponse (AWSResponse a))
forall a b. (a -> b) -> a -> b
$
          SerializeError -> Error
SerializeError (Abbrev
-> Status -> Maybe ByteStringLazy -> String -> SerializeError
SerializeError' Abbrev
_serviceAbbrev Status
status Maybe ByteStringLazy
forall a. Maybe a
Nothing String
err)

sinkLBS :: MonadResource m => ClientBody -> m ByteStringLazy
sinkLBS :: ClientBody -> m ByteStringLazy
sinkLBS ClientBody
bdy = ResourceT IO ByteStringLazy -> m ByteStringLazy
forall (m :: * -> *) a. MonadResource m => ResourceT IO a -> m a
liftResourceT (ClientBody
bdy ClientBody
-> ConduitT ByteString Void (ResourceT IO) ByteStringLazy
-> ResourceT IO ByteStringLazy
forall (m :: * -> *) a r.
Monad m =>
ConduitT () a m () -> ConduitT a Void m r -> m r
`Conduit.connect` ConduitT ByteString Void (ResourceT IO) ByteStringLazy
forall (m :: * -> *) o.
Monad m =>
ConduitT ByteString o m ByteStringLazy
Conduit.Binary.sinkLbs)