{-# LANGUAGE DeriveGeneric #-}

{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module      : Network.DO.Spaces.Actions.CreateBucket
-- Copyright   : (c) 2021 Rory Tyler Hayford
-- License     : BSD-3-Clause
-- Maintainer  : rory.hayford@protonmail.com
-- Stability   : experimental
-- Portability : GHC
--
module Network.DO.Spaces.Actions.CreateBucket
    ( CreateBucket(..)
    , CreateBucketResponse
    ) where

import           Control.Monad.Reader    ( MonadReader(ask) )

import qualified Data.CaseInsensitive    as CI
import           Data.Maybe              ( catMaybes )

import           GHC.Generics            ( Generic )

import           Network.DO.Spaces.Types
import           Network.DO.Spaces.Utils

-- | Create a new, empty 'Bucket'
data CreateBucket = CreateBucket
    { CreateBucket -> Bucket
bucket :: Bucket -- ^ The name of the new 'Bucket' to create
    , CreateBucket -> Maybe Region
region :: Maybe Region
    , CreateBucket -> Maybe CannedACL
acl    :: Maybe CannedACL
      -- ^ The 'CannedACL' to use; defaults to
      -- 'Network.DO.Spaces.Types.CannedACL.Private'
    }
    deriving stock ( Int -> CreateBucket -> ShowS
[CreateBucket] -> ShowS
CreateBucket -> String
(Int -> CreateBucket -> ShowS)
-> (CreateBucket -> String)
-> ([CreateBucket] -> ShowS)
-> Show CreateBucket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateBucket] -> ShowS
$cshowList :: [CreateBucket] -> ShowS
show :: CreateBucket -> String
$cshow :: CreateBucket -> String
showsPrec :: Int -> CreateBucket -> ShowS
$cshowsPrec :: Int -> CreateBucket -> ShowS
Show, CreateBucket -> CreateBucket -> Bool
(CreateBucket -> CreateBucket -> Bool)
-> (CreateBucket -> CreateBucket -> Bool) -> Eq CreateBucket
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateBucket -> CreateBucket -> Bool
$c/= :: CreateBucket -> CreateBucket -> Bool
== :: CreateBucket -> CreateBucket -> Bool
$c== :: CreateBucket -> CreateBucket -> Bool
Eq, (forall x. CreateBucket -> Rep CreateBucket x)
-> (forall x. Rep CreateBucket x -> CreateBucket)
-> Generic CreateBucket
forall x. Rep CreateBucket x -> CreateBucket
forall x. CreateBucket -> Rep CreateBucket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateBucket x -> CreateBucket
$cfrom :: forall x. CreateBucket -> Rep CreateBucket x
Generic )

type CreateBucketResponse = ()

instance MonadSpaces m => Action m CreateBucket where
    type ConsumedResponse CreateBucket = CreateBucketResponse

    buildRequest :: CreateBucket -> m SpacesRequestBuilder
buildRequest CreateBucket { Maybe CannedACL
Maybe Region
Bucket
acl :: Maybe CannedACL
region :: Maybe Region
bucket :: Bucket
acl :: CreateBucket -> Maybe CannedACL
region :: CreateBucket -> Maybe Region
bucket :: CreateBucket -> Bucket
.. } = do
        Spaces
spaces <- m Spaces
forall r (m :: * -> *). MonadReader r m => m r
ask
        SpacesRequestBuilder -> m SpacesRequestBuilder
forall (f :: * -> *) a. Applicative f => a -> f a
pure SpacesRequestBuilder :: Spaces
-> Maybe RequestBody
-> Maybe Method
-> [Header]
-> Maybe Bucket
-> Maybe Object
-> Maybe Query
-> Maybe Query
-> Maybe Region
-> SpacesRequestBuilder
SpacesRequestBuilder
             { $sel:bucket:SpacesRequestBuilder :: Maybe Bucket
bucket         = Bucket -> Maybe Bucket
forall a. a -> Maybe a
Just Bucket
bucket
             , $sel:method:SpacesRequestBuilder :: Maybe Method
method         = Method -> Maybe Method
forall a. a -> Maybe a
Just Method
PUT
             , $sel:overrideRegion:SpacesRequestBuilder :: Maybe Region
overrideRegion = Maybe Region
region
             , $sel:body:SpacesRequestBuilder :: Maybe RequestBody
body           = Maybe RequestBody
forall a. Maybe a
Nothing
             , $sel:object:SpacesRequestBuilder :: Maybe Object
object         = Maybe Object
forall a. Maybe a
Nothing
             , $sel:queryString:SpacesRequestBuilder :: Maybe Query
queryString    = Maybe Query
forall a. Maybe a
Nothing
             , $sel:subresources:SpacesRequestBuilder :: Maybe Query
subresources   = Maybe Query
forall a. Maybe a
Nothing
             , [Header]
Spaces
$sel:headers:SpacesRequestBuilder :: [Header]
$sel:spaces:SpacesRequestBuilder :: Spaces
headers :: [Header]
spaces :: Spaces
..
             }
      where
        headers :: [Header]
headers = [Maybe Header] -> [Header]
forall a. [Maybe a] -> [a]
catMaybes [ (ByteString -> CI ByteString
forall s. FoldCase s => s -> CI s
CI.mk ByteString
"x-amz-acl", ) (ByteString -> Header)
-> (CannedACL -> ByteString) -> CannedACL -> Header
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CannedACL -> ByteString
forall a. IsString a => CannedACL -> a
showCannedACL (CannedACL -> Header) -> Maybe CannedACL -> Maybe Header
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe CannedACL
acl ]

    consumeResponse :: RawResponse m -> m (ConsumedResponse CreateBucket)
consumeResponse RawResponse m
_ = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()