module Network.AWS.Waiters
(
Acceptor
, Accept (..)
, Wait (..)
, accept
, matchAll
, matchAny
, matchError
, matchStatus
, module Control.Lens
) where
import Control.Lens
import Data.ByteString (ByteString)
import Data.Maybe
import Network.AWS.Data
import Network.AWS.Error
import Network.AWS.Types
import Network.HTTP.Types
type Acceptor a = Request a -> Response' a -> Maybe Accept
data Accept
= AcceptSuccess
| AcceptFailure
| AcceptRetry
deriving (Eq, Show)
instance ToBuilder Accept where
build = \case
AcceptSuccess -> "Success"
AcceptFailure -> "Failure"
AcceptRetry -> "Retry"
data Wait a = Wait
{ _waitName :: !ByteString
, _waitAttempts :: !Int
, _waitDelay :: !Int
, _waitAcceptors :: [Acceptor a]
}
accept :: Wait a -> Acceptor a
accept w rq rs = listToMaybe . mapMaybe (\f -> f rq rs) $ _waitAcceptors w
matchAll :: Eq b => b -> Accept -> Fold (Rs a) b -> Acceptor a
matchAll x a l = match (allOf l (== x)) a
matchAny :: Eq b => b -> Accept -> Fold (Rs a) b -> Acceptor a
matchAny x a l = match (anyOf l (== x)) a
matchStatus :: Int -> Accept -> Acceptor a
matchStatus x a _ = \case
Left (ServiceError _ s _)
| x == statusCode s -> Just a
Right (s, _)
| x == statusCode s -> Just a
_ -> Nothing
matchError :: AWSErrorCode (Er (Sv a)) => ErrorCode -> Accept -> Acceptor a
matchError c a _ = \case
Left (ServiceError _ _ e)
| c == awsErrorCode e -> Just a
_ -> Nothing
match :: (Rs a -> Bool) -> Accept -> Acceptor a
match f a _ = \case
Right (_, rs)
| f rs -> Just a
_ -> Nothing