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
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 :: Bucket
-> Maybe Bucket
-> Maybe Bucket
-> Maybe Int
-> Maybe Bucket
-> GetBucket
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
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 :: 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
..} = S3Query
-> S3Configuration queryType -> SignatureData -> SignedQuery
forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery S3Query :: Method
-> Maybe ByteString
-> Maybe ByteString
-> Query
-> Query
-> Maybe ByteString
-> Maybe (Digest MD5)
-> RequestHeaders
-> RequestHeaders
-> Maybe RequestBody
-> S3Query
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 Bucket
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 :: Maybe Bucket
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 :: Maybe Bucket
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"
                       Maybe Int
maxKeys <- Maybe (m Int) -> m (Maybe Int)
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
Data.Traversable.sequence (Maybe (m Int) -> m (Maybe Int))
-> ([m Int] -> Maybe (m Int)) -> [m Int] -> m (Maybe Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [m Int] -> Maybe (m Int)
forall a. [a] -> Maybe a
listToMaybe ([m Int] -> m (Maybe Int)) -> [m Int] -> m (Maybe Int)
forall a b. (a -> b) -> a -> b
$ Cursor
cursor Cursor -> (Cursor -> [m Int]) -> [m Int]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Cursor -> [Bucket]
elContent Bucket
"MaxKeys" (Cursor -> [Bucket]) -> (Bucket -> m Int) -> Cursor -> [m Int]
forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Bucket -> m Int
forall (m :: * -> *) a. (MonadThrow m, Num a) => Bucket -> m a
textReadInt
                       let truncated :: Bool
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 :: Maybe Bucket
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 :: Maybe Bucket
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"
                       [ObjectInfo]
contents <- [m ObjectInfo] -> m [ObjectInfo]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence ([m ObjectInfo] -> m [ObjectInfo])
-> [m ObjectInfo] -> m [ObjectInfo]
forall a b. (a -> b) -> a -> b
$ Cursor
cursor Cursor -> (Cursor -> [m ObjectInfo]) -> [m ObjectInfo]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Bucket -> Axis
Cu.laxElement Bucket
"Contents" Axis -> (Cursor -> m ObjectInfo) -> Cursor -> [m ObjectInfo]
forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Cursor -> m ObjectInfo
forall (m :: * -> *). MonadThrow m => Cursor -> m ObjectInfo
parseObjectInfo
                       let commonPrefixes :: [Bucket]
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
                       GetBucketResponse -> m GetBucketResponse
forall (m :: * -> *) a. Monad m => a -> m a
return GetBucketResponse :: Bucket
-> Maybe Bucket
-> Maybe Bucket
-> Maybe Int
-> Maybe Bucket
-> [ObjectInfo]
-> [Bucket]
-> Bool
-> Maybe Bucket
-> GetBucketResponse
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]
_             ) -> 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 :: Maybe Bucket
gbMarker = Bucket -> Maybe Bucket
forall a. a -> Maybe a
Just Bucket
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 :: Maybe Bucket
gbMarker = Bucket -> Maybe Bucket
forall a. a -> Maybe a
Just (Bucket -> Maybe Bucket) -> Bucket -> Maybe Bucket
forall a b. (a -> b) -> a -> b
$ ObjectInfo -> Bucket
objectKey (ObjectInfo -> Bucket) -> ObjectInfo -> Bucket
forall a b. (a -> b) -> a -> b
$ [ObjectInfo] -> ObjectInfo
forall a. [a] -> a
last [ObjectInfo]
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)
forall (m :: * -> *) a. Monad m => a -> m a
return