    module Aws.S3.Commands.GetBucket
    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

    data GetBucket
        = GetBucket {
            GetBucket -> Bucket
    gbBucket    :: Bucket
          , GetBucket -> Maybe Bucket
    gbDelimiter :: Maybe T.Text
          , GetBucket -> Maybe Bucket
    gbMarker    :: Maybe T.Text
          , GetBucket -> Maybe Int
    gbMaxKeys   :: Maybe Int
          , GetBucket -> Maybe Bucket
    gbPrefix    :: Maybe T.Text
          }
        deriving (Int -> GetBucket -> ShowS
    [GetBucket] -> ShowS
    GetBucket -> String
    (Int -> GetBucket -> ShowS)
    -> (GetBucket -> String)
    -> ([GetBucket] -> ShowS)
    -> Show GetBucket
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> GetBucket -> ShowS
    showsPrec :: Int -> GetBucket -> ShowS
    $cshow :: GetBucket -> String
    show :: GetBucket -> String
    $cshowList :: [GetBucket] -> ShowS
    showList :: [GetBucket] -> ShowS
    Show)

    getBucket :: Bucket -> GetBucket
    getBucket :: Bucket -> GetBucket
    getBucket Bucket
    bucket
        = GetBucket {
            gbBucket :: Bucket
    gbBucket    = Bucket
    bucket
          , gbDelimiter :: Maybe Bucket
    gbDelimiter = Maybe Bucket
    forall a. Maybe a
    Nothing
          , gbMarker :: Maybe Bucket
    gbMarker    = Maybe Bucket
    forall a. Maybe a
    Nothing
          , gbMaxKeys :: Maybe Int
    gbMaxKeys   = Maybe Int
    forall a. Maybe a
    Nothing
          , gbPrefix :: Maybe Bucket
    gbPrefix    = Maybe Bucket
    forall a. Maybe a
    Nothing
          }

    data GetBucketResponse
        = GetBucketResponse {
            GetBucketResponse -> Bucket
    gbrName           :: Bucket
          , GetBucketResponse -> Maybe Bucket
    gbrDelimiter      :: Maybe T.Text
          , GetBucketResponse -> Maybe Bucket
    gbrMarker         :: Maybe T.Text
          , GetBucketResponse -> Maybe Int
    gbrMaxKeys        :: Maybe Int
          , GetBucketResponse -> Maybe Bucket
    gbrPrefix         :: Maybe T.Text
          , GetBucketResponse -> [ObjectInfo]
    gbrContents       :: [ObjectInfo]
          , GetBucketResponse -> [Bucket]
    gbrCommonPrefixes :: [T.Text]
          , GetBucketResponse -> Bool
    gbrIsTruncated    :: Bool
          , GetBucketResponse -> Maybe Bucket
    gbrNextMarker     :: Maybe T.Text
          }
        deriving (Int -> GetBucketResponse -> ShowS
    [GetBucketResponse] -> ShowS
    GetBucketResponse -> String
    (Int -> GetBucketResponse -> ShowS)
    -> (GetBucketResponse -> String)
    -> ([GetBucketResponse] -> ShowS)
    -> Show GetBucketResponse
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> GetBucketResponse -> ShowS
    showsPrec :: Int -> GetBucketResponse -> ShowS
    $cshow :: GetBucketResponse -> String
    show :: GetBucketResponse -> String
    $cshowList :: [GetBucketResponse] -> ShowS
    showList :: [GetBucketResponse] -> ShowS
    Show)

    -- | ServiceConfiguration: 'S3Configuration'
    instance SignQuery GetBucket where
        type ServiceConfiguration GetBucket = S3Configuration
        signQuery :: forall queryType.
    GetBucket
    -> ServiceConfiguration GetBucket queryType
    -> SignatureData
    -> SignedQuery
    signQuery GetBucket {Maybe Int
    Maybe Bucket
    Bucket
    gbBucket :: GetBucket -> Bucket
    gbDelimiter :: GetBucket -> Maybe Bucket
    gbMarker :: GetBucket -> Maybe Bucket
    gbMaxKeys :: GetBucket -> Maybe Int
    gbPrefix :: GetBucket -> Maybe Bucket
    gbBucket :: Bucket
    gbDelimiter :: Maybe Bucket
    gbMarker :: Maybe Bucket
    gbMaxKeys :: Maybe Int
    gbPrefix :: 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
    gbBucket
                                   , s3QObject :: Maybe ByteString
    s3QObject = Maybe ByteString
    forall a. Maybe a
    Nothing
                                   , s3QSubresources :: Query
    s3QSubresources = []
                                   , 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
    gbDelimiter
                                                , (ByteString
    "marker",) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbMarker
                                                , (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
    gbMaxKeys
                                                , (ByteString
    "prefix",) (Bucket -> (ByteString, Bucket))
    -> Maybe Bucket -> Maybe (ByteString, Bucket)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe Bucket
    gbPrefix
                                                ]
                                   , 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 GetBucketResponse where
        type ResponseMetadata GetBucketResponse = S3Metadata

        responseConsumer :: Request
    -> r
    -> IORef (ResponseMetadata GetBucketResponse)
    -> HTTPResponseConsumer GetBucketResponse
    responseConsumer Request
    _ r
    _ = (Cursor -> Response S3Metadata GetBucketResponse)
    -> IORef S3Metadata -> HTTPResponseConsumer GetBucketResponse
    forall a.
    (Cursor -> Response S3Metadata a)
    -> IORef S3Metadata -> HTTPResponseConsumer a
    s3XmlResponseConsumer Cursor -> Response S3Metadata GetBucketResponse
    forall {m :: * -> *}. MonadThrow m => Cursor -> m GetBucketResponse
    parse
            where parse :: Cursor -> m GetBucketResponse
    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 marker = [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
    "Marker"
                           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 nextMarker = [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
    "NextMarker"
                           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.laxElement "Contents" &| parseObjectInfo
                           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 GetBucketResponse{
                                                    gbrName           = name
                                                  , gbrDelimiter      = delimiter
                                                  , gbrMarker         = marker
                                                  , gbrMaxKeys        = maxKeys
                                                  , gbrPrefix         = prefix
                                                  , gbrContents       = contents
                                                  , gbrCommonPrefixes = commonPrefixes
                                                  , gbrIsTruncated    = truncated
                                                  , gbrNextMarker     = nextMarker
                                                  }

    instance Transaction GetBucket GetBucketResponse

    instance IteratedTransaction GetBucket GetBucketResponse where
        nextIteratedRequest :: GetBucket -> GetBucketResponse -> Maybe GetBucket
    nextIteratedRequest GetBucket
    request GetBucketResponse
    response
            = case (GetBucketResponse -> Bool
    gbrIsTruncated GetBucketResponse
    response, GetBucketResponse -> Maybe Bucket
    gbrNextMarker GetBucketResponse
    response, GetBucketResponse -> [ObjectInfo]
    gbrContents GetBucketResponse
    response) of
                (Bool
    True, Just Bucket
    marker, [ObjectInfo]
    _             ) -> GetBucket -> Maybe GetBucket
    forall a. a -> Maybe a
    Just (GetBucket -> Maybe GetBucket) -> GetBucket -> Maybe GetBucket
    forall a b. (a -> b) -> a -> b
    $ GetBucket
    request { gbMarker = Just marker }
                (Bool
    True, Maybe Bucket
    Nothing,     contents :: [ObjectInfo]
    contents@(ObjectInfo
    _:[ObjectInfo]
    _)) -> GetBucket -> Maybe GetBucket
    forall a. a -> Maybe a
    Just (GetBucket -> Maybe GetBucket) -> GetBucket -> Maybe GetBucket
    forall a b. (a -> b) -> a -> b
    $ GetBucket
    request { gbMarker = Just $ objectKey $ last contents }
                (Bool
    _,    Maybe Bucket
    _,           [ObjectInfo]
    _             ) -> Maybe GetBucket
    forall a. Maybe a
    Nothing

    instance ListResponse GetBucketResponse ObjectInfo where
        listResponse :: GetBucketResponse -> [ObjectInfo]
    listResponse = GetBucketResponse -> [ObjectInfo]
    gbrContents

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