module Aws.S3.Commands.PutBucket where

import           Aws.Core
import           Aws.S3.Core
import           Control.Monad
import           Data.Maybe
import qualified Data.Map             as M
import qualified Data.Text            as T
import qualified Data.Text.Encoding   as T
import qualified Network.HTTP.Conduit as HTTP
import qualified Text.XML             as XML

data PutBucket
    = PutBucket {
        PutBucket -> Bucket
pbBucket :: Bucket
      , PutBucket -> Maybe CannedAcl
pbCannedAcl :: Maybe CannedAcl
      , PutBucket -> Bucket
pbLocationConstraint :: LocationConstraint
      , PutBucket -> Maybe StorageClass
pbXStorageClass :: Maybe StorageClass -- ^ Google Cloud Storage S3 nonstandard extension
      }
    deriving (Int -> PutBucket -> ShowS
[PutBucket] -> ShowS
PutBucket -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutBucket] -> ShowS
$cshowList :: [PutBucket] -> ShowS
show :: PutBucket -> String
$cshow :: PutBucket -> String
showsPrec :: Int -> PutBucket -> ShowS
$cshowsPrec :: Int -> PutBucket -> ShowS
Show)

putBucket :: Bucket -> PutBucket
putBucket :: Bucket -> PutBucket
putBucket Bucket
bucket = Bucket
-> Maybe CannedAcl -> Bucket -> Maybe StorageClass -> PutBucket
PutBucket Bucket
bucket forall a. Maybe a
Nothing Bucket
locationUsClassic forall a. Maybe a
Nothing

data PutBucketResponse
    = PutBucketResponse
    deriving (Int -> PutBucketResponse -> ShowS
[PutBucketResponse] -> ShowS
PutBucketResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutBucketResponse] -> ShowS
$cshowList :: [PutBucketResponse] -> ShowS
show :: PutBucketResponse -> String
$cshow :: PutBucketResponse -> String
showsPrec :: Int -> PutBucketResponse -> ShowS
$cshowsPrec :: Int -> PutBucketResponse -> ShowS
Show)

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

    signQuery :: forall queryType.
PutBucket
-> ServiceConfiguration PutBucket queryType
-> SignatureData
-> SignedQuery
signQuery PutBucket{Maybe StorageClass
Maybe CannedAcl
Bucket
pbXStorageClass :: Maybe StorageClass
pbLocationConstraint :: Bucket
pbCannedAcl :: Maybe CannedAcl
pbBucket :: Bucket
pbXStorageClass :: PutBucket -> Maybe StorageClass
pbLocationConstraint :: PutBucket -> Bucket
pbCannedAcl :: PutBucket -> Maybe CannedAcl
pbBucket :: PutBucket -> Bucket
..} = forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery (S3Query {
                                             s3QMethod :: Method
s3QMethod       = Method
Put
                                           , s3QBucket :: Maybe ByteString
s3QBucket       = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Bucket -> ByteString
T.encodeUtf8 Bucket
pbBucket
                                           , s3QSubresources :: Query
s3QSubresources = []
                                           , s3QQuery :: Query
s3QQuery        = []
                                           , s3QContentType :: Maybe ByteString
s3QContentType  = forall a. Maybe a
Nothing
                                           , s3QContentMd5 :: Maybe (Digest MD5)
s3QContentMd5   = forall a. Maybe a
Nothing
                                           , s3QObject :: Maybe ByteString
s3QObject       = forall a. Maybe a
Nothing
                                           , s3QAmzHeaders :: RequestHeaders
s3QAmzHeaders   = case Maybe CannedAcl
pbCannedAcl of
                                                                 Maybe CannedAcl
Nothing -> []
                                                                 Just CannedAcl
acl -> [(HeaderName
"x-amz-acl", Bucket -> ByteString
T.encodeUtf8 forall a b. (a -> b) -> a -> b
$ CannedAcl -> Bucket
writeCannedAcl CannedAcl
acl)]
                                           , s3QOtherHeaders :: RequestHeaders
s3QOtherHeaders = []
                                           , s3QRequestBody :: Maybe RequestBody
s3QRequestBody
                                               = forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Node]
elts)) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                 (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyLBS forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenderSettings -> Document -> ByteString
XML.renderLBS forall a. Default a => a
XML.def)
                                                 XML.Document {
                                                          documentPrologue :: Prologue
XML.documentPrologue = [Miscellaneous] -> Maybe Doctype -> [Miscellaneous] -> Prologue
XML.Prologue [] forall a. Maybe a
Nothing []
                                                        , documentRoot :: Element
XML.documentRoot = Element
root
                                                        , documentEpilogue :: [Miscellaneous]
XML.documentEpilogue = []
                                                        }
                                           })
        where root :: Element
root = XML.Element {
                               elementName :: Name
XML.elementName = Name
"{http://s3.amazonaws.com/doc/2006-03-01/}CreateBucketConfiguration"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Node]
elts
                             }
              elts :: [Node]
elts = forall a. [Maybe a] -> [a]
catMaybes
                             [ if Bucket -> Bool
T.null Bucket
pbLocationConstraint then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just (Bucket -> Node
locationconstraint Bucket
pbLocationConstraint)
                             , forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StorageClass -> Node
storageclass Maybe StorageClass
pbXStorageClass
                             ]
              locationconstraint :: Bucket -> Node
locationconstraint Bucket
c = Element -> Node
XML.NodeElement (XML.Element {
                               elementName :: Name
XML.elementName = Name
"{http://s3.amazonaws.com/doc/2006-03-01/}LocationConstraint"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Bucket -> Node
XML.NodeContent Bucket
c]
                             })
              storageclass :: StorageClass -> Node
storageclass StorageClass
c = Element -> Node
XML.NodeElement (XML.Element {
                               elementName :: Name
XML.elementName = Name
"StorageClass"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Bucket -> Node
XML.NodeContent (StorageClass -> Bucket
writeStorageClass StorageClass
c)]
                             })

instance ResponseConsumer r PutBucketResponse where
    type ResponseMetadata PutBucketResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata PutBucketResponse)
-> HTTPResponseConsumer PutBucketResponse
responseConsumer Request
_ r
_ = forall a.
HTTPResponseConsumer a
-> IORef S3Metadata -> HTTPResponseConsumer a
s3ResponseConsumer forall a b. (a -> b) -> a -> b
$ \Response (ConduitM () ByteString (ResourceT IO) ())
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return PutBucketResponse
PutBucketResponse

instance Transaction PutBucket PutBucketResponse

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