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

getBucket :: Bucket -> GetBucket
getBucket :: Bucket -> GetBucket
getBucket Bucket
bucket
    = GetBucket {
        gbBucket :: Bucket
gbBucket    = Bucket
bucket
      , gbDelimiter :: Maybe Bucket
gbDelimiter = forall a. Maybe a
Nothing
      , gbMarker :: Maybe Bucket
gbMarker    = forall a. Maybe a
Nothing
      , gbMaxKeys :: Maybe Int
gbMaxKeys   = forall a. Maybe a
Nothing
      , gbPrefix :: Maybe Bucket
gbPrefix    = 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
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetBucketResponse] -> ShowS
$cshowList :: [GetBucketResponse] -> ShowS
show :: GetBucketResponse -> String
$cshow :: GetBucketResponse -> String
showsPrec :: Int -> GetBucketResponse -> ShowS
$cshowsPrec :: Int -> 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
gbPrefix :: Maybe Bucket
gbMaxKeys :: Maybe Int
gbMarker :: Maybe Bucket
gbDelimiter :: Maybe Bucket
gbBucket :: Bucket
gbPrefix :: GetBucket -> Maybe Bucket
gbMaxKeys :: GetBucket -> Maybe Int
gbMarker :: GetBucket -> Maybe Bucket
gbDelimiter :: GetBucket -> Maybe Bucket
gbBucket :: GetBucket -> 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
gbBucket
                               , s3QObject :: Maybe ByteString
s3QObject = forall a. Maybe a
Nothing
                               , s3QSubresources :: Query
s3QSubresources = []
                               , 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
gbDelimiter
                                            , (ByteString
"marker",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbMarker
                                            , (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
gbMaxKeys
                                            , (ByteString
"prefix",) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Bucket
gbPrefix
                                            ]
                               , 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 GetBucketResponse where
    type ResponseMetadata GetBucketResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetBucketResponse)
-> HTTPResponseConsumer GetBucketResponse
responseConsumer Request
_ r
_ = forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer forall {m :: * -> *}. MonadThrow m => Cursor -> m GetBucketResponse
parse
        where parse :: Cursor -> m GetBucketResponse
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 marker :: Maybe Bucket
marker = 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
"Marker"
                       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 nextMarker :: Maybe Bucket
nextMarker = 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
"NextMarker"
                       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"
                       [ObjectInfo]
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]
$/ Bucket -> Axis
Cu.laxElement Bucket
"Contents" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall (m :: * -> *). MonadThrow m => Cursor -> m ObjectInfo
parseObjectInfo
                       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 GetBucketResponse{
                                                gbrName :: Bucket
gbrName           = Bucket
name
                                              , gbrDelimiter :: Maybe Bucket
gbrDelimiter      = Maybe Bucket
delimiter
                                              , gbrMarker :: Maybe Bucket
gbrMarker         = Maybe Bucket
marker
                                              , gbrMaxKeys :: Maybe Int
gbrMaxKeys        = Maybe Int
maxKeys
                                              , gbrPrefix :: Maybe Bucket
gbrPrefix         = Maybe Bucket
prefix
                                              , gbrContents :: [ObjectInfo]
gbrContents       = [ObjectInfo]
contents
                                              , gbrCommonPrefixes :: [Bucket]
gbrCommonPrefixes = [Bucket]
commonPrefixes
                                              , gbrIsTruncated :: Bool
gbrIsTruncated    = Bool
truncated
                                              , gbrNextMarker :: Maybe Bucket
gbrNextMarker     = Maybe Bucket
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]
_             ) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ GetBucket
request { gbMarker :: Maybe Bucket
gbMarker = forall a. a -> Maybe a
Just Bucket
marker }
            (Bool
True, Maybe Bucket
Nothing,     contents :: [ObjectInfo]
contents@(ObjectInfo
_:[ObjectInfo]
_)) -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ GetBucket
request { gbMarker :: Maybe Bucket
gbMarker = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ ObjectInfo -> Bucket
objectKey forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
last [ObjectInfo]
contents }
            (Bool
_,    Maybe Bucket
_,           [ObjectInfo]
_             ) -> 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 = forall (m :: * -> *) a. Monad m => a -> m a
return