    module Aws.S3.Commands.GetBucketObjectVersions
    where

    import           Aws.Core
    import           Aws.S3.Core
    import           Control.Applicative
    import           Data.ByteString.Char8 ({- IsString -})
    import           Data.Maybe
    import           Text.XML.Cursor       (($/), (&|), (&//))
    import qualified Data.ByteString.Char8 as B8
    import qualified Data.Text             as T
    import qualified Data.Text.Encoding    as T
    import qualified Data.Traversable
    import           Prelude
    import qualified Network.HTTP.Types    as HTTP
    import qualified Text.XML.Cursor       as Cu
    import qualified Text.XML              as XML

    data GetBucketObjectVersions
        = GetBucketObjectVersions {
            GetBucketObjectVersions -> Bucket
    gbovBucket          :: Bucket
          , GetBucketObjectVersions -> Maybe Bucket
    gbovDelimiter       :: Maybe T.Text
          , GetBucketObjectVersions -> Maybe Bucket
    gbovKeyMarker       :: Maybe T.Text
          , GetBucketObjectVersions -> Maybe Int
    gbovMaxKeys         :: Maybe Int
          , GetBucketObjectVersions -> Maybe Bucket
    gbovPrefix          :: Maybe T.Text
          , GetBucketObjectVersions -> Maybe Bucket
    gbovVersionIdMarker :: Maybe T.Text
          }
        deriving (Int -> GetBucketObjectVersions -> ShowS
    [GetBucketObjectVersions] -> ShowS
    GetBucketObjectVersions -> String
    (Int -> GetBucketObjectVersions -> ShowS)
    -> (GetBucketObjectVersions -> String)
    -> ([GetBucketObjectVersions] -> ShowS)
    -> Show GetBucketObjectVersions
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> GetBucketObjectVersions -> ShowS
    showsPrec :: Int -> GetBucketObjectVersions -> ShowS
    $cshow :: GetBucketObjectVersions -> String
    show :: GetBucketObjectVersions -> String
    $cshowList :: [GetBucketObjectVersions] -> ShowS
    showList :: [GetBucketObjectVersions] -> ShowS
    Show)

    getBucketObjectVersions :: Bucket -> GetBucketObjectVersions
    getBucketObjectVersions :: Bucket -> GetBucketObjectVersions
    getBucketObjectVersions Bucket
    bucket
        = GetBucketObjectVersions {
            gbovBucket :: Bucket
    gbovBucket          = Bucket
    bucket
          , gbovDelimiter :: Maybe Bucket
    gbovDelimiter       = Maybe Bucket
    forall a. Maybe a
    Nothing
          , gbovKeyMarker :: Maybe Bucket
    gbovKeyMarker       = Maybe Bucket
    forall a. Maybe a
    Nothing
          , gbovMaxKeys :: Maybe Int
    gbovMaxKeys         = Maybe Int
    forall a. Maybe a
    Nothing
          , gbovPrefix :: Maybe Bucket
    gbovPrefix          = Maybe Bucket
    forall a. Maybe a
    Nothing
          , gbovVersionIdMarker :: Maybe Bucket
    gbovVersionIdMarker = Maybe Bucket
    forall a. Maybe a
    Nothing
          }

    data GetBucketObjectVersionsResponse
        = GetBucketObjectVersionsResponse {
            GetBucketObjectVersionsResponse -> Bucket
    gbovrName                :: Bucket
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrDelimiter           :: Maybe T.Text
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrKeyMarker           :: Maybe T.Text
          , GetBucketObjectVersionsResponse -> Maybe Int
    gbovrMaxKeys             :: Maybe Int
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrPrefix              :: Maybe T.Text
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrVersionIdMarker     :: Maybe T.Text
          , GetBucketObjectVersionsResponse -> [ObjectVersionInfo]
    gbovrContents            :: [ObjectVersionInfo]
          , GetBucketObjectVersionsResponse -> [Bucket]
    gbovrCommonPrefixes      :: [T.Text]
          , GetBucketObjectVersionsResponse -> Bool
    gbovrIsTruncated         :: Bool
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrNextKeyMarker       :: Maybe T.Text
          , GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrNextVersionIdMarker :: Maybe T.Text
          }
        deriving (Int -> GetBucketObjectVersionsResponse -> ShowS
    [GetBucketObjectVersionsResponse] -> ShowS
    GetBucketObjectVersionsResponse -> String
    (Int -> GetBucketObjectVersionsResponse -> ShowS)
    -> (GetBucketObjectVersionsResponse -> String)
    -> ([GetBucketObjectVersionsResponse] -> ShowS)
    -> Show GetBucketObjectVersionsResponse
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> GetBucketObjectVersionsResponse -> ShowS
    showsPrec :: Int -> GetBucketObjectVersionsResponse -> ShowS
    $cshow :: GetBucketObjectVersionsResponse -> String
    show :: GetBucketObjectVersionsResponse -> String
    $cshowList :: [GetBucketObjectVersionsResponse] -> ShowS
    showList :: [GetBucketObjectVersionsResponse] -> ShowS
    Show)

    -- | ServiceConfiguration: 'S3Configuration'
    instance SignQuery GetBucketObjectVersions where
        type ServiceConfiguration GetBucketObjectVersions = S3Configuration
        signQuery :: forall queryType.
    GetBucketObjectVersions
    -> ServiceConfiguration GetBucketObjectVersions queryType
    -> SignatureData
    -> SignedQuery
    signQuery GetBucketObjectVersions {Maybe Int
    Maybe Bucket
    Bucket
    gbovBucket :: GetBucketObjectVersions -> Bucket
    gbovDelimiter :: GetBucketObjectVersions -> Maybe Bucket
    gbovKeyMarker :: GetBucketObjectVersions -> Maybe Bucket
    gbovMaxKeys :: GetBucketObjectVersions -> Maybe Int
    gbovPrefix :: GetBucketObjectVersions -> Maybe Bucket
    gbovVersionIdMarker :: GetBucketObjectVersions -> Maybe Bucket
    gbovBucket :: Bucket
    gbovDelimiter :: Maybe Bucket
    gbovKeyMarker :: Maybe Bucket
    gbovMaxKeys :: Maybe Int
    gbovPrefix :: Maybe Bucket
    gbovVersionIdMarker :: Maybe Bucket
    ..} = S3Query
    -> S3Configuration queryType -> SignatureData -> SignedQuery
    forall qt.
    S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
    s3SignQuery S3Query {
                                     s3QMethod :: Method
    s3QMethod = Method
    Get
                                   , s3QBucket :: Maybe ByteString
    s3QBucket = ByteString -> Maybe ByteString
    forall a. a -> Maybe a
    Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
    forall a b. (a -> b) -> a -> b
    $ Bucket -> ByteString
    T.encodeUtf8 Bucket
    gbovBucket
                                   , s3QObject :: Maybe ByteString
    s3QObject = Maybe ByteString
    forall a. Maybe a
    Nothing
                                   , s3QSubresources :: Query
    s3QSubresources = [ (ByteString
    "versions", Maybe ByteString
    forall a. Maybe a
    Nothing) ]
                                   , s3QQuery :: Query
    s3QQuery = [Maybe (ByteString, Bucket)] -> Query
    forall a. QueryLike a => a -> Query
    HTTP.toQuery [
                                                  (ByteString
    "delimiter" :: B8.ByteString ,) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbovDelimiter
                                                , (ByteString
    "key-marker",) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbovKeyMarker
                                                , (ByteString
    "max-keys",) (Bucket -> (ByteString, Bucket))
    -> (Int -> Bucket) -> Int -> (ByteString, Bucket)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Bucket
    T.pack (String -> Bucket) -> (Int -> String) -> Int -> Bucket
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> String
    forall a. Show a => a -> String
    show (Int -> (ByteString, Bucket))
    -> Maybe Int -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Int
    gbovMaxKeys
                                                , (ByteString
    "prefix",) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbovPrefix
                                                , (ByteString
    "version-id-marker",) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbovVersionIdMarker
                                                ]
                                   , s3QContentType :: Maybe ByteString
    s3QContentType = Maybe ByteString
    forall a. Maybe a
    Nothing
                                   , s3QContentMd5 :: Maybe (Digest MD5)
    s3QContentMd5 = Maybe (Digest MD5)
    forall a. Maybe a
    Nothing
                                   , s3QAmzHeaders :: RequestHeaders
    s3QAmzHeaders = []
                                   , s3QOtherHeaders :: RequestHeaders
    s3QOtherHeaders = []
                                   , s3QRequestBody :: Maybe RequestBody
    s3QRequestBody = Maybe RequestBody
    forall a. Maybe a
    Nothing
                                   }

    instance ResponseConsumer r GetBucketObjectVersionsResponse where
        type ResponseMetadata GetBucketObjectVersionsResponse = S3Metadata

        responseConsumer :: Request
    -> r
    -> IORef (ResponseMetadata GetBucketObjectVersionsResponse)
    -> HTTPResponseConsumer GetBucketObjectVersionsResponse
    responseConsumer Request
    _ r
    _ = (Cursor -> Response S3Metadata GetBucketObjectVersionsResponse)
    -> IORef S3Metadata
    -> HTTPResponseConsumer GetBucketObjectVersionsResponse
    forall a.
    (Cursor -> Response S3Metadata a)
    -> IORef S3Metadata -> HTTPResponseConsumer a
    s3XmlResponseConsumer Cursor -> Response S3Metadata GetBucketObjectVersionsResponse
    forall {m :: * -> *}.
    MonadThrow m =>
    Cursor -> m GetBucketObjectVersionsResponse
    parse
            where parse :: Cursor -> m GetBucketObjectVersionsResponse
    parse Cursor
    cursor
                      = do name <- String -> [Bucket] -> m Bucket
    forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
    force String
    "Missing Name" ([Bucket] -> m Bucket) -> [Bucket] -> m Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "Name"
                           let delimiter = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "Delimiter"
                           let keyMarker = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "KeyMarker"
                           let versionMarker = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "VersionIdMarker"
                           maxKeys <- Data.Traversable.sequence . listToMaybe $ cursor $/ elContent "MaxKeys" &| textReadInt
                           let truncated = Bool -> (Bucket -> Bool) -> Maybe Bucket -> Bool
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Bool
    True (Bucket -> Bucket -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Bucket
    "false") (Maybe Bucket -> Bool) -> Maybe Bucket -> Bool
    forall a b. (a -> b) -> a -> b
    $ [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "IsTruncated"
                           let nextKeyMarker = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "NextKeyMarker"
                           let nextVersionMarker = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "NextVersionIdMarker"
                           let prefix = [Bucket] -> Maybe Bucket
    forall a. [a] -> Maybe a
    listToMaybe ([Bucket] -> Maybe Bucket) -> [Bucket] -> Maybe Bucket
    forall a b. (a -> b) -> a -> b
    $ Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Cursor -> [Bucket]
    elContent Bucket
    "Prefix"
                           contents <- sequence $ cursor $/ Cu.checkName objectNodeName &| parseObjectVersionInfo
                           let commonPrefixes = Cursor
    cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
    forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
    $/ Bucket -> Axis
    Cu.laxElement Bucket
    "CommonPrefixes" Axis -> (Cursor -> [Bucket]) -> Cursor -> [Bucket]
    forall node a.
    Axis node -> (Cursor node -> [a]) -> Cursor node -> [a]
    &// Cursor -> [Bucket]
    Cu.content
                           return GetBucketObjectVersionsResponse{
                                                    gbovrName                = name
                                                  , gbovrDelimiter           = delimiter
                                                  , gbovrKeyMarker           = keyMarker
                                                  , gbovrMaxKeys             = maxKeys
                                                  , gbovrPrefix              = prefix
                                                  , gbovrVersionIdMarker     = versionMarker
                                                  , gbovrContents            = contents
                                                  , gbovrCommonPrefixes      = commonPrefixes
                                                  , gbovrIsTruncated         = truncated
                                                  , gbovrNextKeyMarker       = nextKeyMarker
                                                  , gbovrNextVersionIdMarker = nextVersionMarker
                                                  }
                  objectNodeName :: Name -> Bool
    objectNodeName Name
    n = let fn :: Bucket
    fn = Bucket -> Bucket
    T.toCaseFold (Bucket -> Bucket) -> Bucket -> Bucket
    forall a b. (a -> b) -> a -> b
    $ Name -> Bucket
    XML.nameLocalName Name
    n
                                      in Bucket
    fn Bucket -> Bucket -> Bool
    forall a. Eq a => a -> a -> Bool
    == Bucket -> Bucket
    T.toCaseFold Bucket
    "Version" Bool -> Bool -> Bool
    || Bucket
    fn Bucket -> Bucket -> Bool
    forall a. Eq a => a -> a -> Bool
    == Bucket -> Bucket
    T.toCaseFold Bucket
    "DeleteMarker"

    instance Transaction GetBucketObjectVersions GetBucketObjectVersionsResponse

    instance IteratedTransaction GetBucketObjectVersions GetBucketObjectVersionsResponse where
        nextIteratedRequest :: GetBucketObjectVersions
    -> GetBucketObjectVersionsResponse -> Maybe GetBucketObjectVersions
    nextIteratedRequest GetBucketObjectVersions
    request GetBucketObjectVersionsResponse
    response
            = case (GetBucketObjectVersionsResponse -> Bool
    gbovrIsTruncated GetBucketObjectVersionsResponse
    response, GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrNextKeyMarker GetBucketObjectVersionsResponse
    response, GetBucketObjectVersionsResponse -> Maybe Bucket
    gbovrNextVersionIdMarker GetBucketObjectVersionsResponse
    response, GetBucketObjectVersionsResponse -> [ObjectVersionInfo]
    gbovrContents GetBucketObjectVersionsResponse
    response) of
                (Bool
    True, Just Bucket
    keyMarker, Just Bucket
    versionMarker, [ObjectVersionInfo]
    _             ) -> GetBucketObjectVersions -> Maybe GetBucketObjectVersions
    forall a. a -> Maybe a
    Just (GetBucketObjectVersions -> Maybe GetBucketObjectVersions)
    -> GetBucketObjectVersions -> Maybe GetBucketObjectVersions
    forall a b. (a -> b) -> a -> b
    $ GetBucketObjectVersions
    request { gbovKeyMarker = Just keyMarker, gbovVersionIdMarker = Just versionMarker }
                (Bool
    True, Maybe Bucket
    Nothing,        Maybe Bucket
    Nothing,            contents :: [ObjectVersionInfo]
    contents@(ObjectVersionInfo
    _:[ObjectVersionInfo]
    _)) -> GetBucketObjectVersions -> Maybe GetBucketObjectVersions
    forall a. a -> Maybe a
    Just (GetBucketObjectVersions -> Maybe GetBucketObjectVersions)
    -> GetBucketObjectVersions -> Maybe GetBucketObjectVersions
    forall a b. (a -> b) -> a -> b
    $ GetBucketObjectVersions
    request { gbovKeyMarker = Just $ oviKey $ last contents, gbovVersionIdMarker = Just $ oviVersionId $ last contents }
                (Bool
    _,    Maybe Bucket
    _,              Maybe Bucket
    _,                  [ObjectVersionInfo]
    _             ) -> Maybe GetBucketObjectVersions
    forall a. Maybe a
    Nothing

    instance ListResponse GetBucketObjectVersionsResponse ObjectVersionInfo where
        listResponse :: GetBucketObjectVersionsResponse -> [ObjectVersionInfo]
    listResponse = GetBucketObjectVersionsResponse -> [ObjectVersionInfo]
    gbovrContents

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