{-# LANGUAGE CPP #-}
module Aws.S3.Commands.GetService
where

import           Aws.Core
import           Aws.S3.Core
import           Data.Maybe
import           Data.Time.Format
#if !MIN_VERSION_time(1,5,0)
import           System.Locale
#endif
import           Text.XML.Cursor  (($/), ($//), (&|))
import qualified Data.Text        as T
import qualified Text.XML.Cursor  as Cu

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

data GetServiceResponse
    = GetServiceResponse {
        GetServiceResponse -> UserInfo
gsrOwner :: UserInfo
      , GetServiceResponse -> [BucketInfo]
gsrBuckets :: [BucketInfo]
      }
    deriving (Int -> GetServiceResponse -> ShowS
[GetServiceResponse] -> ShowS
GetServiceResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetServiceResponse] -> ShowS
$cshowList :: [GetServiceResponse] -> ShowS
show :: GetServiceResponse -> String
$cshow :: GetServiceResponse -> String
showsPrec :: Int -> GetServiceResponse -> ShowS
$cshowsPrec :: Int -> GetServiceResponse -> ShowS
Show)

instance ResponseConsumer r GetServiceResponse where
    type ResponseMetadata GetServiceResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetServiceResponse)
-> HTTPResponseConsumer GetServiceResponse
responseConsumer Request
_ r
_ = forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer forall {m :: * -> *}.
MonadThrow m =>
Cursor -> m GetServiceResponse
parse
        where
          parse :: Cursor -> m GetServiceResponse
parse Cursor
el = do
            UserInfo
owner <- forall (m :: * -> *) a. MonadThrow m => String -> [m a] -> m a
forceM String
"Missing Owner" forall a b. (a -> b) -> a -> b
$ Cursor
el forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Axis
Cu.laxElement Text
"Owner" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall (m :: * -> *). MonadThrow m => Cursor -> m UserInfo
parseUserInfo
            [BucketInfo]
buckets <- forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall a b. (a -> b) -> a -> b
$ Cursor
el forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$// Text -> Axis
Cu.laxElement Text
"Bucket" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| forall {m :: * -> *}. MonadThrow m => Cursor -> m BucketInfo
parseBucket
            forall (m :: * -> *) a. Monad m => a -> m a
return GetServiceResponse { gsrOwner :: UserInfo
gsrOwner = UserInfo
owner, gsrBuckets :: [BucketInfo]
gsrBuckets = [BucketInfo]
buckets }

          parseBucket :: Cursor -> m BucketInfo
parseBucket Cursor
el = do
            Text
name <- forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing owner Name" forall a b. (a -> b) -> a -> b
$ Cursor
el forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Cursor -> [Text]
elContent Text
"Name"
            String
creationDateString <- forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing owner CreationDate" forall a b. (a -> b) -> a -> b
$ Cursor
el forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Cursor -> [Text]
elContent Text
"CreationDate" forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Text -> String
T.unpack
            UTCTime
creationDate <- forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Invalid CreationDate" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Maybe a -> [a]
maybeToList forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
iso8601UtcDate String
creationDateString
            forall (m :: * -> *) a. Monad m => a -> m a
return BucketInfo { bucketName :: Text
bucketName = Text
name, bucketCreationDate :: UTCTime
bucketCreationDate = UTCTime
creationDate }

-- | ServiceConfiguration: 'S3Configuration'
instance SignQuery GetService where
    type ServiceConfiguration GetService = S3Configuration
    signQuery :: forall queryType.
GetService
-> ServiceConfiguration GetService queryType
-> SignatureData
-> SignedQuery
signQuery GetService
GetService = forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery S3Query {
                                s3QMethod :: Method
s3QMethod = Method
Get
                              , s3QBucket :: Maybe ByteString
s3QBucket = forall a. Maybe a
Nothing
                              , s3QObject :: Maybe ByteString
s3QObject = forall a. Maybe a
Nothing
                              , 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
                              , s3QAmzHeaders :: RequestHeaders
s3QAmzHeaders = []
                              , s3QOtherHeaders :: RequestHeaders
s3QOtherHeaders = []
                              , s3QRequestBody :: Maybe RequestBody
s3QRequestBody = forall a. Maybe a
Nothing
                              }

instance Transaction GetService GetServiceResponse

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