module Aws.S3.Commands.GetBucketLocation
       where

import           Aws.Core
import           Aws.S3.Core

import qualified Data.ByteString.Char8 as B8

import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types as HTTP
import           Text.XML.Cursor (($.//))

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

getBucketLocation :: Bucket -> GetBucketLocation
getBucketLocation :: Bucket -> GetBucketLocation
getBucketLocation Bucket
bucket
  = GetBucketLocation {
      gblBucket :: Bucket
gblBucket = Bucket
bucket
    }

data GetBucketLocationResponse
  = GetBucketLocationResponse { GetBucketLocationResponse -> Bucket
gblrLocationConstraint :: LocationConstraint }
    deriving Int -> GetBucketLocationResponse -> ShowS
[GetBucketLocationResponse] -> ShowS
GetBucketLocationResponse -> String
(Int -> GetBucketLocationResponse -> ShowS)
-> (GetBucketLocationResponse -> String)
-> ([GetBucketLocationResponse] -> ShowS)
-> Show GetBucketLocationResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GetBucketLocationResponse -> ShowS
showsPrec :: Int -> GetBucketLocationResponse -> ShowS
$cshow :: GetBucketLocationResponse -> String
show :: GetBucketLocationResponse -> String
$cshowList :: [GetBucketLocationResponse] -> ShowS
showList :: [GetBucketLocationResponse] -> ShowS
Show

instance SignQuery GetBucketLocation where
  type ServiceConfiguration GetBucketLocation = S3Configuration
  signQuery :: forall queryType.
GetBucketLocation
-> ServiceConfiguration GetBucketLocation queryType
-> SignatureData
-> SignedQuery
signQuery GetBucketLocation {Bucket
gblBucket :: GetBucketLocation -> Bucket
gblBucket :: Bucket
..} = S3Query
-> S3Configuration queryType -> SignatureData -> SignedQuery
forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery 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
gblBucket
                                     , s3QObject :: Maybe ByteString
s3QObject = Maybe ByteString
forall a. Maybe a
Nothing
                                     , s3QSubresources :: Query
s3QSubresources = [(ByteString
"location" :: B8.ByteString, Maybe ByteString
forall a. Maybe a
Nothing :: Maybe B8.ByteString)]
                                     , s3QQuery :: Query
s3QQuery = [(ByteString, Bucket)] -> Query
forall a. QueryLike a => a -> Query
HTTP.toQuery ([] :: [(B8.ByteString, T.Text)]) 
                                     , 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 GetBucketLocationResponse where
  type ResponseMetadata GetBucketLocationResponse = S3Metadata

  responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetBucketLocationResponse)
-> HTTPResponseConsumer GetBucketLocationResponse
responseConsumer Request
_ r
_ = (Cursor -> Response S3Metadata GetBucketLocationResponse)
-> IORef S3Metadata
-> HTTPResponseConsumer GetBucketLocationResponse
forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer Cursor -> Response S3Metadata GetBucketLocationResponse
forall {m :: * -> *}.
MonadThrow m =>
Cursor -> m GetBucketLocationResponse
parse
    where parse :: Cursor -> m GetBucketLocationResponse
parse Cursor
cursor = do
            Bucket
locationConstraint <- String -> [Bucket] -> m Bucket
forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing Location" ([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
"LocationConstraint"
            GetBucketLocationResponse -> m GetBucketLocationResponse
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return GetBucketLocationResponse { gblrLocationConstraint :: Bucket
gblrLocationConstraint = Bucket -> Bucket
normaliseLocation Bucket
locationConstraint }

instance Transaction GetBucketLocation GetBucketLocationResponse

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