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
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetBucketObjectVersions] -> ShowS
$cshowList :: [GetBucketObjectVersions] -> ShowS
show :: GetBucketObjectVersions -> String
$cshow :: GetBucketObjectVersions -> String
showsPrec :: Int -> GetBucketObjectVersions -> ShowS
$cshowsPrec :: Int -> GetBucketObjectVersions -> ShowS
Show)

getBucketObjectVersions :: Bucket -> GetBucketObjectVersions
getBucketObjectVersions :: Bucket -> GetBucketObjectVersions
getBucketObjectVersions Bucket
bucket
    = GetBucketObjectVersions {
        gbovBucket :: Bucket
gbovBucket          = Bucket
bucket
      , gbovDelimiter :: Maybe Bucket
gbovDelimiter       = forall a. Maybe a
Nothing
      , gbovKeyMarker :: Maybe Bucket
gbovKeyMarker       = forall a. Maybe a
Nothing
      , gbovMaxKeys :: Maybe Int
gbovMaxKeys         = forall a. Maybe a
Nothing
      , gbovPrefix :: Maybe Bucket
gbovPrefix          = forall a. Maybe a
Nothing
      , gbovVersionIdMarker :: Maybe Bucket
gbovVersionIdMarker = 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
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetBucketObjectVersionsResponse] -> ShowS
$cshowList :: [GetBucketObjectVersionsResponse] -> ShowS
show :: GetBucketObjectVersionsResponse -> String
$cshow :: GetBucketObjectVersionsResponse -> String
showsPrec :: Int -> GetBucketObjectVersionsResponse -> ShowS
$cshowsPrec :: Int -> 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
gbovVersionIdMarker :: Maybe Bucket
gbovPrefix :: Maybe Bucket
gbovMaxKeys :: Maybe Int
gbovKeyMarker :: Maybe Bucket
gbovDelimiter :: Maybe Bucket
gbovBucket :: Bucket
gbovVersionIdMarker :: GetBucketObjectVersions -> Maybe Bucket
gbovPrefix :: GetBucketObjectVersions -> Maybe Bucket
gbovMaxKeys :: GetBucketObjectVersions -> Maybe Int
gbovKeyMarker :: GetBucketObjectVersions -> Maybe Bucket
gbovDelimiter :: GetBucketObjectVersions -> Maybe Bucket
gbovBucket :: GetBucketObjectVersions -> Bucket
..} = forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery S3Query {
                                 s3QMethod :: Method
s3QMethod = Method
Get
                               , s3QBucket :: Maybe ByteString
s3QBucket = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Bucket -> ByteString
T.encodeUtf8 Bucket
gbovBucket
                               , s3QObject :: Maybe ByteString
s3QObject = forall a. Maybe a
Nothing
                               , s3QSubresources :: Query
s3QSubresources = [ (ByteString
"versions", forall a. Maybe a
Nothing) ]
                               , s3QQuery :: Query
s3QQuery = forall a. QueryLike a => a -> Query
HTTP.toQuery [
                                              (ByteString
"delimiter" :: B8.ByteString ,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbovDelimiter
                                            , (ByteString
"key-marker",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbovKeyMarker
                                            , (ByteString
"max-keys",) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bucket
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Int
gbovMaxKeys
                                            , (ByteString
"prefix",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbovPrefix
                                            , (ByteString
"version-id-marker",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbovVersionIdMarker
                                            ]
                               , s3QContentType :: Maybe ByteString
s3QContentType = forall a. Maybe a
Nothing
                               , s3QContentMd5 :: Maybe (Digest MD5)
s3QContentMd5 = forall a. Maybe a
Nothing
                               , s3QAmzHeaders :: RequestHeaders
s3QAmzHeaders = []
                               , s3QOtherHeaders :: RequestHeaders
s3QOtherHeaders = []
                               , s3QRequestBody :: Maybe RequestBody
s3QRequestBody = 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
_ = forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer forall {m :: * -> *}.
MonadThrow m =>
Cursor -> m GetBucketObjectVersionsResponse
parse
        where parse :: Cursor -> m GetBucketObjectVersionsResponse
parse Cursor
cursor
                  = do Bucket
name <- forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing Name" forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"Name"
                       let delimiter :: Maybe Bucket
delimiter = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"Delimiter"
                       let keyMarker :: Maybe Bucket
keyMarker = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"KeyMarker"
                       let versionMarker :: Maybe Bucket
versionMarker = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"VersionIdMarker"
                       Maybe Int
maxKeys <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
Data.Traversable.sequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"MaxKeys" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall (m :: * -> *) a. (MonadThrow m, Num a) => Bucket -> m a
textReadInt
                       let truncated :: Bool
truncated = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (forall a. Eq a => a -> a -> Bool
/= Bucket
"false") forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"IsTruncated"
                       let nextKeyMarker :: Maybe Bucket
nextKeyMarker = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"NextKeyMarker"
                       let nextVersionMarker :: Maybe Bucket
nextVersionMarker = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"NextVersionIdMarker"
                       let prefix :: Maybe Bucket
prefix = forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"Prefix"
                       [ObjectVersionInfo]
contents <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall a b. (a -> b) -> a -> b
$ Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ forall b. Boolean b => (Name -> b) -> Axis
Cu.checkName Name -> Bool
objectNodeName forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall (m :: * -> *). MonadThrow m => Cursor -> m ObjectVersionInfo
parseObjectVersionInfo
                       let commonPrefixes :: [Bucket]
commonPrefixes = Cursor
cursor forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Axis
Cu.laxElement Bucket
"CommonPrefixes" forall node a.
Axis node -> (Cursor node -> [a]) -> Cursor node -> [a]
&// Cursor -> [Bucket]
Cu.content
                       forall (m :: * -> *) a. Monad m => a -> m a
return GetBucketObjectVersionsResponse{
                                                gbovrName :: Bucket
gbovrName                = Bucket
name
                                              , gbovrDelimiter :: Maybe Bucket
gbovrDelimiter           = Maybe Bucket
delimiter
                                              , gbovrKeyMarker :: Maybe Bucket
gbovrKeyMarker           = Maybe Bucket
keyMarker
                                              , gbovrMaxKeys :: Maybe Int
gbovrMaxKeys             = Maybe Int
maxKeys
                                              , gbovrPrefix :: Maybe Bucket
gbovrPrefix              = Maybe Bucket
prefix
                                              , gbovrVersionIdMarker :: Maybe Bucket
gbovrVersionIdMarker     = Maybe Bucket
versionMarker
                                              , gbovrContents :: [ObjectVersionInfo]
gbovrContents            = [ObjectVersionInfo]
contents
                                              , gbovrCommonPrefixes :: [Bucket]
gbovrCommonPrefixes      = [Bucket]
commonPrefixes
                                              , gbovrIsTruncated :: Bool
gbovrIsTruncated         = Bool
truncated
                                              , gbovrNextKeyMarker :: Maybe Bucket
gbovrNextKeyMarker       = Maybe Bucket
nextKeyMarker
                                              , gbovrNextVersionIdMarker :: Maybe Bucket
gbovrNextVersionIdMarker = Maybe Bucket
nextVersionMarker
                                              }
              objectNodeName :: Name -> Bool
objectNodeName Name
n = let fn :: Bucket
fn = Bucket -> Bucket
T.toCaseFold forall a b. (a -> b) -> a -> b
$ Name -> Bucket
XML.nameLocalName Name
n
                                  in Bucket
fn forall a. Eq a => a -> a -> Bool
== Bucket -> Bucket
T.toCaseFold Bucket
"Version" Bool -> Bool -> Bool
|| Bucket
fn 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]
_             ) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ GetBucketObjectVersions
request { gbovKeyMarker :: Maybe Bucket
gbovKeyMarker = forall a. a -> Maybe a
Just Bucket
keyMarker, gbovVersionIdMarker :: Maybe Bucket
gbovVersionIdMarker = forall a. a -> Maybe a
Just Bucket
versionMarker }
            (Bool
True, Maybe Bucket
Nothing,        Maybe Bucket
Nothing,            contents :: [ObjectVersionInfo]
contents@(ObjectVersionInfo
_:[ObjectVersionInfo]
_)) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ GetBucketObjectVersions
request { gbovKeyMarker :: Maybe Bucket
gbovKeyMarker = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ObjectVersionInfo -> Bucket
oviKey forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last [ObjectVersionInfo]
contents, gbovVersionIdMarker :: Maybe Bucket
gbovVersionIdMarker = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ObjectVersionInfo -> Bucket
oviVersionId forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last [ObjectVersionInfo]
contents }
            (Bool
_,    Maybe Bucket
_,              Maybe Bucket
_,                  [ObjectVersionInfo]
_             ) -> 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 = forall (m :: * -> *) a. Monad m => a -> m a
return