{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

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

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

import           GHC.Generics            ( Generic )

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

-- | Get information about an 'Object'; the response does not contain the
-- object itself
data GetObjectInfo = GetObjectInfo { GetObjectInfo -> Bucket
bucket :: Bucket, GetObjectInfo -> Object
object :: Object }
    deriving stock ( Int -> GetObjectInfo -> ShowS
[GetObjectInfo] -> ShowS
GetObjectInfo -> String
(Int -> GetObjectInfo -> ShowS)
-> (GetObjectInfo -> String)
-> ([GetObjectInfo] -> ShowS)
-> Show GetObjectInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetObjectInfo] -> ShowS
$cshowList :: [GetObjectInfo] -> ShowS
show :: GetObjectInfo -> String
$cshow :: GetObjectInfo -> String
showsPrec :: Int -> GetObjectInfo -> ShowS
$cshowsPrec :: Int -> GetObjectInfo -> ShowS
Show, GetObjectInfo -> GetObjectInfo -> Bool
(GetObjectInfo -> GetObjectInfo -> Bool)
-> (GetObjectInfo -> GetObjectInfo -> Bool) -> Eq GetObjectInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetObjectInfo -> GetObjectInfo -> Bool
$c/= :: GetObjectInfo -> GetObjectInfo -> Bool
== :: GetObjectInfo -> GetObjectInfo -> Bool
$c== :: GetObjectInfo -> GetObjectInfo -> Bool
Eq, (forall x. GetObjectInfo -> Rep GetObjectInfo x)
-> (forall x. Rep GetObjectInfo x -> GetObjectInfo)
-> Generic GetObjectInfo
forall x. Rep GetObjectInfo x -> GetObjectInfo
forall x. GetObjectInfo -> Rep GetObjectInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetObjectInfo x -> GetObjectInfo
$cfrom :: forall x. GetObjectInfo -> Rep GetObjectInfo x
Generic )

type GetObjectInfoResponse = ObjectMetadata

instance MonadSpaces m => Action m GetObjectInfo where
    type ConsumedResponse GetObjectInfo = GetObjectInfoResponse

    buildRequest :: GetObjectInfo -> m SpacesRequestBuilder
buildRequest GetObjectInfo { Object
Bucket
object :: Object
bucket :: Bucket
$sel:object:GetObjectInfo :: GetObjectInfo -> Object
$sel:bucket:GetObjectInfo :: GetObjectInfo -> 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:object:SpacesRequestBuilder :: Maybe Object
object         = Object -> Maybe Object
forall a. a -> Maybe a
Just Object
object
             , $sel:method:SpacesRequestBuilder :: Maybe Method
method         = Method -> Maybe Method
forall a. a -> Maybe a
Just Method
HEAD
             , $sel:body:SpacesRequestBuilder :: Maybe RequestBody
body           = Maybe RequestBody
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
             , $sel:headers:SpacesRequestBuilder :: [Header]
headers        = [Header]
forall a. Monoid a => a
mempty
             , $sel:overrideRegion:SpacesRequestBuilder :: Maybe Region
overrideRegion = Maybe Region
forall a. Maybe a
Nothing
             , Spaces
$sel:spaces:SpacesRequestBuilder :: Spaces
spaces :: Spaces
..
             }

    consumeResponse :: RawResponse m -> m (ConsumedResponse GetObjectInfo)
consumeResponse = RawResponse m -> m (ConsumedResponse GetObjectInfo)
forall (m :: * -> *).
MonadThrow m =>
RawResponse m -> m ObjectMetadata
lookupObjectMetadata