{-# 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
(Int -> GetService -> ShowS)
-> (GetService -> String)
-> ([GetService] -> ShowS)
-> Show GetService
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GetService -> ShowS
showsPrec :: Int -> GetService -> ShowS
$cshow :: GetService -> String
show :: GetService -> String
$cshowList :: [GetService] -> ShowS
showList :: [GetService] -> ShowS
Show)

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

instance ResponseConsumer r GetServiceResponse where
    type ResponseMetadata GetServiceResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetServiceResponse)
-> HTTPResponseConsumer GetServiceResponse
responseConsumer Request
_ r
_ = (Cursor -> Response S3Metadata GetServiceResponse)
-> IORef S3Metadata -> HTTPResponseConsumer GetServiceResponse
forall a.
(Cursor -> Response S3Metadata a)
-> IORef S3Metadata -> HTTPResponseConsumer a
s3XmlResponseConsumer Cursor -> Response S3Metadata GetServiceResponse
forall {m :: * -> *}.
MonadThrow m =>
Cursor -> m GetServiceResponse
parse
        where
          parse :: Cursor -> m GetServiceResponse
parse Cursor
el = do
            UserInfo
owner <- String -> [m UserInfo] -> m UserInfo
forall (m :: * -> *) a. MonadThrow m => String -> [m a] -> m a
forceM String
"Missing Owner" ([m UserInfo] -> m UserInfo) -> [m UserInfo] -> m UserInfo
forall a b. (a -> b) -> a -> b
$ Cursor
el Cursor -> (Cursor -> [m UserInfo]) -> [m UserInfo]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Axis
Cu.laxElement Text
"Owner" Axis -> (Cursor -> m UserInfo) -> Cursor -> [m UserInfo]
forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Cursor -> m UserInfo
forall (m :: * -> *). MonadThrow m => Cursor -> m UserInfo
parseUserInfo
            [BucketInfo]
buckets <- [m BucketInfo] -> m [BucketInfo]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence ([m BucketInfo] -> m [BucketInfo])
-> [m BucketInfo] -> m [BucketInfo]
forall a b. (a -> b) -> a -> b
$ Cursor
el Cursor -> (Cursor -> [m BucketInfo]) -> [m BucketInfo]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$// Text -> Axis
Cu.laxElement Text
"Bucket" Axis -> (Cursor -> m BucketInfo) -> Cursor -> [m BucketInfo]
forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Cursor -> m BucketInfo
forall {m :: * -> *}. MonadThrow m => Cursor -> m BucketInfo
parseBucket
            GetServiceResponse -> m GetServiceResponse
forall a. a -> m a
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 <- String -> [Text] -> m Text
forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing owner Name" ([Text] -> m Text) -> [Text] -> m Text
forall a b. (a -> b) -> a -> b
$ Cursor
el Cursor -> (Cursor -> [Text]) -> [Text]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Cursor -> [Text]
elContent Text
"Name"
            String
creationDateString <- String -> [String] -> m String
forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Missing owner CreationDate" ([String] -> m String) -> [String] -> m String
forall a b. (a -> b) -> a -> b
$ Cursor
el Cursor -> (Cursor -> [String]) -> [String]
forall node a. Cursor node -> (Cursor node -> [a]) -> [a]
$/ Text -> Cursor -> [Text]
elContent Text
"CreationDate" (Cursor -> [Text]) -> (Text -> String) -> Cursor -> [String]
forall node a b.
(Cursor node -> [a]) -> (a -> b) -> Cursor node -> [b]
&| Text -> String
T.unpack
            UTCTime
creationDate <- String -> [UTCTime] -> m UTCTime
forall (m :: * -> *) a. MonadThrow m => String -> [a] -> m a
force String
"Invalid CreationDate" ([UTCTime] -> m UTCTime)
-> (Maybe UTCTime -> [UTCTime]) -> Maybe UTCTime -> m UTCTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe UTCTime -> [UTCTime]
forall a. Maybe a -> [a]
maybeToList (Maybe UTCTime -> m UTCTime) -> Maybe UTCTime -> m UTCTime
forall a b. (a -> b) -> a -> b
$ Bool -> TimeLocale -> String -> String -> Maybe UTCTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
iso8601UtcDate String
creationDateString
            BucketInfo -> m BucketInfo
forall a. a -> m a
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 = S3Query
-> S3Configuration queryType -> SignatureData -> SignedQuery
forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery S3Query {
                                s3QMethod :: Method
s3QMethod = Method
Get
                              , s3QBucket :: Maybe ByteString
s3QBucket = Maybe ByteString
forall a. Maybe a
Nothing
                              , s3QObject :: Maybe ByteString
s3QObject = Maybe ByteString
forall a. Maybe a
Nothing
                              , s3QSubresources :: Query
s3QSubresources = []
                              , 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
                              , s3QAmzHeaders :: RequestHeaders
s3QAmzHeaders = []
                              , s3QOtherHeaders :: RequestHeaders
s3QOtherHeaders = []
                              , s3QRequestBody :: Maybe RequestBody
s3QRequestBody = Maybe RequestBody
forall a. Maybe a
Nothing
                              }

instance Transaction GetService GetServiceResponse

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