module Aws.S3.Commands.GetBucketVersioning 
( 
  module Aws.S3.Commands.GetBucketVersioning
, VersioningState(..)
) where

import           Aws.Core
import           Aws.S3.Commands.PutBucketVersioning (VersioningState(..))
import           Aws.S3.Core
import           Control.Monad.Trans.Resource (throwM)
import           Network.HTTP.Types (toQuery)
import qualified Data.Text.Encoding   as T
import           Text.XML.Cursor (($.//))
import qualified Data.ByteString.Lazy.Char8 as B8

-- | Gets the versioning state of an existing bucket.
data GetBucketVersioning
    = GetBucketVersioning
      { GetBucketVersioning -> Bucket
gbvBucket :: Bucket
      }
    deriving (Int -> GetBucketVersioning -> ShowS
[GetBucketVersioning] -> ShowS
GetBucketVersioning -> String
(Int -> GetBucketVersioning -> ShowS)
-> (GetBucketVersioning -> String)
-> ([GetBucketVersioning] -> ShowS)
-> Show GetBucketVersioning
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GetBucketVersioning -> ShowS
showsPrec :: Int -> GetBucketVersioning -> ShowS
$cshow :: GetBucketVersioning -> String
show :: GetBucketVersioning -> String
$cshowList :: [GetBucketVersioning] -> ShowS
showList :: [GetBucketVersioning] -> ShowS
Show)

getBucketVersioning :: Bucket -> GetBucketVersioning
getBucketVersioning :: Bucket -> GetBucketVersioning
getBucketVersioning = Bucket -> GetBucketVersioning
GetBucketVersioning

data GetBucketVersioningResponse
    = GetBucketVersioningResponse
        { GetBucketVersioningResponse -> Maybe VersioningState
gbvVersioning :: Maybe VersioningState }
        -- ^ Nothing when the bucket is not versioned
    deriving (Int -> GetBucketVersioningResponse -> ShowS
[GetBucketVersioningResponse] -> ShowS
GetBucketVersioningResponse -> String
(Int -> GetBucketVersioningResponse -> ShowS)
-> (GetBucketVersioningResponse -> String)
-> ([GetBucketVersioningResponse] -> ShowS)
-> Show GetBucketVersioningResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GetBucketVersioningResponse -> ShowS
showsPrec :: Int -> GetBucketVersioningResponse -> ShowS
$cshow :: GetBucketVersioningResponse -> String
show :: GetBucketVersioningResponse -> String
$cshowList :: [GetBucketVersioningResponse] -> ShowS
showList :: [GetBucketVersioningResponse] -> ShowS
Show)

-- | ServiceConfiguration: 'S3Configuration'
instance SignQuery GetBucketVersioning where
    type ServiceConfiguration GetBucketVersioning = S3Configuration

    signQuery :: forall queryType.
GetBucketVersioning
-> ServiceConfiguration GetBucketVersioning queryType
-> SignatureData
-> SignedQuery
signQuery GetBucketVersioning{Bucket
gbvBucket :: GetBucketVersioning -> Bucket
gbvBucket :: Bucket
..} = S3Query
-> S3Configuration queryType -> SignatureData -> SignedQuery
forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery (S3Query
 -> S3Configuration queryType -> SignatureData -> SignedQuery)
-> S3Query
-> S3Configuration queryType
-> SignatureData
-> SignedQuery
forall a b. (a -> b) -> a -> b
$ 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
gbvBucket
      , s3QSubresources :: Query
s3QSubresources = [(ByteString, Maybe ByteString)] -> Query
forall a. QueryLike a => a -> Query
toQuery [(ByteString
"versioning" :: B8.ByteString, Maybe ByteString
forall a. Maybe a
Nothing :: Maybe B8.ByteString)]
      , s3QQuery :: Query
s3QQuery        = []
      , s3QContentType :: Maybe ByteString
s3QContentType  = Maybe ByteString
forall a. Maybe a
Nothing
      , s3QContentMd5 :: Maybe (Digest MD5)
s3QContentMd5   = Maybe (Digest MD5)
forall a. Maybe a
Nothing
      , s3QObject :: Maybe ByteString
s3QObject       = Maybe ByteString
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 GetBucketVersioningResponse where
    type ResponseMetadata GetBucketVersioningResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetBucketVersioningResponse)
-> HTTPResponseConsumer GetBucketVersioningResponse
responseConsumer Request
_ r
_ = (Cursor -> Response S3Metadata GetBucketVersioningResponse)
-> IORef S3Metadata
-> HTTPResponseConsumer GetBucketVersioningResponse
forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer Cursor -> Response S3Metadata GetBucketVersioningResponse
forall {m :: * -> *}.
MonadThrow m =>
Cursor -> m GetBucketVersioningResponse
parse
      where parse :: Cursor -> m GetBucketVersioningResponse
parse Cursor
cursor = do
              Maybe VersioningState
v <- case Cursor
cursor Cursor -> (Cursor -> [Bucket]) -> [Bucket]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$.// Bucket -> Cursor -> [Bucket]
elContent Bucket
"Status" of
                   [] -> Maybe VersioningState -> m (Maybe VersioningState)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe VersioningState
forall a. Maybe a
Nothing
                   (Bucket
"Enabled":[]) -> Maybe VersioningState -> m (Maybe VersioningState)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (VersioningState -> Maybe VersioningState
forall a. a -> Maybe a
Just VersioningState
VersioningEnabled)
                   (Bucket
"Suspended":[]) -> Maybe VersioningState -> m (Maybe VersioningState)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (VersioningState -> Maybe VersioningState
forall a. a -> Maybe a
Just VersioningState
VersioningSuspended)
                   [Bucket]
_ -> XmlException -> m (Maybe VersioningState)
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM (XmlException -> m (Maybe VersioningState))
-> XmlException -> m (Maybe VersioningState)
forall a b. (a -> b) -> a -> b
$ String -> XmlException
XmlException String
"Invalid Status"
              GetBucketVersioningResponse -> m GetBucketVersioningResponse
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return GetBucketVersioningResponse { gbvVersioning :: Maybe VersioningState
gbvVersioning = Maybe VersioningState
v }

instance Transaction GetBucketVersioning GetBucketVersioningResponse

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