module Wire.AWS where

import Amazonka (Env, runResourceT)
import Amazonka.Core.Lens.Internal qualified as AWS
import Amazonka.Send as AWS
import Amazonka.Types qualified as AWS
import Control.Lens
import Imports
import Network.HTTP.Client
import Polysemy
import Polysemy.Input

sendCatch ::
  ( Member (Input Amazonka.Env) r,
    Member (Embed IO) r,
    AWS.AWSRequest req,
    Typeable req,
    Typeable (AWS.AWSResponse req)
  ) =>
  req ->
  Sem r (Either AWS.Error (AWS.AWSResponse req))
sendCatch :: forall (r :: EffectRow) req.
(Member (Input Env) r, Member (Embed IO) r, AWSRequest req,
 Typeable req, Typeable (AWSResponse req)) =>
req -> Sem r (Either Error (AWSResponse req))
sendCatch req
req = do
  Env
env <- Sem r Env
forall i (r :: EffectRow). Member (Input i) r => Sem r i
input
  IO (Either Error (AWSResponse req))
-> Sem r (Either Error (AWSResponse req))
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed (IO (Either Error (AWSResponse req))
 -> Sem r (Either Error (AWSResponse req)))
-> (req -> IO (Either Error (AWSResponse req)))
-> req
-> Sem r (Either Error (AWSResponse req))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting (First Error) SomeException Error
-> IO (AWSResponse req) -> IO (Either Error (AWSResponse req))
forall (m :: * -> *) a r.
MonadCatch m =>
Getting (First a) SomeException a -> m r -> m (Either a r)
AWS.trying Getting (First Error) SomeException Error
forall a. AsError a => Prism' a Error
Prism' SomeException Error
AWS._Error (IO (AWSResponse req) -> IO (Either Error (AWSResponse req)))
-> (req -> IO (AWSResponse req))
-> req
-> IO (Either Error (AWSResponse req))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResourceT IO (AWSResponse req) -> IO (AWSResponse req)
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (ResourceT IO (AWSResponse req) -> IO (AWSResponse req))
-> (req -> ResourceT IO (AWSResponse req))
-> req
-> IO (AWSResponse req)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env -> req -> ResourceT IO (AWSResponse req)
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a, Typeable a,
 Typeable (AWSResponse a)) =>
Env -> a -> m (AWSResponse a)
AWS.send Env
env (req -> Sem r (Either Error (AWSResponse req)))
-> req -> Sem r (Either Error (AWSResponse req))
forall a b. (a -> b) -> a -> b
$ req
req

canRetry :: Either AWS.Error a -> Bool
canRetry :: forall a. Either Error a -> Bool
canRetry (Right a
_) = Bool
False
canRetry (Left Error
e) = case Error
e of
  AWS.TransportError (HttpExceptionRequest Request
_ HttpExceptionContent
ResponseTimeout) -> Bool
True
  AWS.ServiceError ServiceError
se | ServiceError
se ServiceError
-> Getting ErrorCode ServiceError ErrorCode -> ErrorCode
forall s a. s -> Getting a s a -> a
^. Getting ErrorCode ServiceError ErrorCode
Lens' ServiceError ErrorCode
AWS.serviceError_code ErrorCode -> ErrorCode -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> ErrorCode
AWS.ErrorCode Text
"RequestThrottled" -> Bool
True
  Error
_ -> Bool
False