{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies    #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Aws.DynamoDb.Commands.GetItem
-- Copyright   :  Soostone Inc
-- License     :  BSD3
--
-- Maintainer  :  Ozgun Ataman <ozgun.ataman@soostone.com>
-- Stability   :  experimental
--
--
----------------------------------------------------------------------------

module Aws.DynamoDb.Commands.GetItem where

-------------------------------------------------------------------------------
import           Control.Applicative
import           Data.Aeson
import           Data.Default
import qualified Data.Text           as T
import           Prelude
-------------------------------------------------------------------------------
import           Aws.Core
import           Aws.DynamoDb.Core
-------------------------------------------------------------------------------


-- | A GetItem query that fetches a specific object from DDB.
--
-- See: @http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_GetItem.html@
data GetItem = GetItem {
      GetItem -> Text
giTableName  :: T.Text
    , GetItem -> PrimaryKey
giKey        :: PrimaryKey
    , GetItem -> Maybe [Text]
giAttrs      :: Maybe [T.Text]
    -- ^ Attributes to get. 'Nothing' grabs everything.
    , GetItem -> Bool
giConsistent :: Bool
    -- ^ Whether to issue a consistent read.
    , GetItem -> ReturnConsumption
giRetCons    :: ReturnConsumption
    -- ^ Whether to return consumption stats.
    } deriving (GetItem -> GetItem -> Bool
(GetItem -> GetItem -> Bool)
-> (GetItem -> GetItem -> Bool) -> Eq GetItem
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetItem -> GetItem -> Bool
$c/= :: GetItem -> GetItem -> Bool
== :: GetItem -> GetItem -> Bool
$c== :: GetItem -> GetItem -> Bool
Eq,Int -> GetItem -> ShowS
[GetItem] -> ShowS
GetItem -> String
(Int -> GetItem -> ShowS)
-> (GetItem -> String) -> ([GetItem] -> ShowS) -> Show GetItem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetItem] -> ShowS
$cshowList :: [GetItem] -> ShowS
show :: GetItem -> String
$cshow :: GetItem -> String
showsPrec :: Int -> GetItem -> ShowS
$cshowsPrec :: Int -> GetItem -> ShowS
Show,ReadPrec [GetItem]
ReadPrec GetItem
Int -> ReadS GetItem
ReadS [GetItem]
(Int -> ReadS GetItem)
-> ReadS [GetItem]
-> ReadPrec GetItem
-> ReadPrec [GetItem]
-> Read GetItem
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetItem]
$creadListPrec :: ReadPrec [GetItem]
readPrec :: ReadPrec GetItem
$creadPrec :: ReadPrec GetItem
readList :: ReadS [GetItem]
$creadList :: ReadS [GetItem]
readsPrec :: Int -> ReadS GetItem
$creadsPrec :: Int -> ReadS GetItem
Read,Eq GetItem
Eq GetItem
-> (GetItem -> GetItem -> Ordering)
-> (GetItem -> GetItem -> Bool)
-> (GetItem -> GetItem -> Bool)
-> (GetItem -> GetItem -> Bool)
-> (GetItem -> GetItem -> Bool)
-> (GetItem -> GetItem -> GetItem)
-> (GetItem -> GetItem -> GetItem)
-> Ord GetItem
GetItem -> GetItem -> Bool
GetItem -> GetItem -> Ordering
GetItem -> GetItem -> GetItem
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: GetItem -> GetItem -> GetItem
$cmin :: GetItem -> GetItem -> GetItem
max :: GetItem -> GetItem -> GetItem
$cmax :: GetItem -> GetItem -> GetItem
>= :: GetItem -> GetItem -> Bool
$c>= :: GetItem -> GetItem -> Bool
> :: GetItem -> GetItem -> Bool
$c> :: GetItem -> GetItem -> Bool
<= :: GetItem -> GetItem -> Bool
$c<= :: GetItem -> GetItem -> Bool
< :: GetItem -> GetItem -> Bool
$c< :: GetItem -> GetItem -> Bool
compare :: GetItem -> GetItem -> Ordering
$ccompare :: GetItem -> GetItem -> Ordering
$cp1Ord :: Eq GetItem
Ord)


-------------------------------------------------------------------------------
-- | Construct a minimal 'GetItem' request.
getItem
    :: T.Text                   -- ^ Table name
    -> PrimaryKey               -- ^ Primary key
    -> GetItem
getItem :: Text -> PrimaryKey -> GetItem
getItem Text
tn PrimaryKey
k = Text
-> PrimaryKey
-> Maybe [Text]
-> Bool
-> ReturnConsumption
-> GetItem
GetItem Text
tn PrimaryKey
k Maybe [Text]
forall a. Maybe a
Nothing Bool
False ReturnConsumption
forall a. Default a => a
def


-- | Response to a 'GetItem' query.
data GetItemResponse = GetItemResponse {
      GetItemResponse -> Maybe Item
girItem     :: Maybe Item
    , GetItemResponse -> Maybe ConsumedCapacity
girConsumed :: Maybe ConsumedCapacity
    } deriving (GetItemResponse -> GetItemResponse -> Bool
(GetItemResponse -> GetItemResponse -> Bool)
-> (GetItemResponse -> GetItemResponse -> Bool)
-> Eq GetItemResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetItemResponse -> GetItemResponse -> Bool
$c/= :: GetItemResponse -> GetItemResponse -> Bool
== :: GetItemResponse -> GetItemResponse -> Bool
$c== :: GetItemResponse -> GetItemResponse -> Bool
Eq,Int -> GetItemResponse -> ShowS
[GetItemResponse] -> ShowS
GetItemResponse -> String
(Int -> GetItemResponse -> ShowS)
-> (GetItemResponse -> String)
-> ([GetItemResponse] -> ShowS)
-> Show GetItemResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetItemResponse] -> ShowS
$cshowList :: [GetItemResponse] -> ShowS
show :: GetItemResponse -> String
$cshow :: GetItemResponse -> String
showsPrec :: Int -> GetItemResponse -> ShowS
$cshowsPrec :: Int -> GetItemResponse -> ShowS
Show,ReadPrec [GetItemResponse]
ReadPrec GetItemResponse
Int -> ReadS GetItemResponse
ReadS [GetItemResponse]
(Int -> ReadS GetItemResponse)
-> ReadS [GetItemResponse]
-> ReadPrec GetItemResponse
-> ReadPrec [GetItemResponse]
-> Read GetItemResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetItemResponse]
$creadListPrec :: ReadPrec [GetItemResponse]
readPrec :: ReadPrec GetItemResponse
$creadPrec :: ReadPrec GetItemResponse
readList :: ReadS [GetItemResponse]
$creadList :: ReadS [GetItemResponse]
readsPrec :: Int -> ReadS GetItemResponse
$creadsPrec :: Int -> ReadS GetItemResponse
Read,Eq GetItemResponse
Eq GetItemResponse
-> (GetItemResponse -> GetItemResponse -> Ordering)
-> (GetItemResponse -> GetItemResponse -> Bool)
-> (GetItemResponse -> GetItemResponse -> Bool)
-> (GetItemResponse -> GetItemResponse -> Bool)
-> (GetItemResponse -> GetItemResponse -> Bool)
-> (GetItemResponse -> GetItemResponse -> GetItemResponse)
-> (GetItemResponse -> GetItemResponse -> GetItemResponse)
-> Ord GetItemResponse
GetItemResponse -> GetItemResponse -> Bool
GetItemResponse -> GetItemResponse -> Ordering
GetItemResponse -> GetItemResponse -> GetItemResponse
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: GetItemResponse -> GetItemResponse -> GetItemResponse
$cmin :: GetItemResponse -> GetItemResponse -> GetItemResponse
max :: GetItemResponse -> GetItemResponse -> GetItemResponse
$cmax :: GetItemResponse -> GetItemResponse -> GetItemResponse
>= :: GetItemResponse -> GetItemResponse -> Bool
$c>= :: GetItemResponse -> GetItemResponse -> Bool
> :: GetItemResponse -> GetItemResponse -> Bool
$c> :: GetItemResponse -> GetItemResponse -> Bool
<= :: GetItemResponse -> GetItemResponse -> Bool
$c<= :: GetItemResponse -> GetItemResponse -> Bool
< :: GetItemResponse -> GetItemResponse -> Bool
$c< :: GetItemResponse -> GetItemResponse -> Bool
compare :: GetItemResponse -> GetItemResponse -> Ordering
$ccompare :: GetItemResponse -> GetItemResponse -> Ordering
$cp1Ord :: Eq GetItemResponse
Ord)


instance Transaction GetItem GetItemResponse


instance ToJSON GetItem where
    toJSON :: GetItem -> Value
toJSON GetItem{Bool
Maybe [Text]
Text
ReturnConsumption
PrimaryKey
giRetCons :: ReturnConsumption
giConsistent :: Bool
giAttrs :: Maybe [Text]
giKey :: PrimaryKey
giTableName :: Text
giRetCons :: GetItem -> ReturnConsumption
giConsistent :: GetItem -> Bool
giAttrs :: GetItem -> Maybe [Text]
giKey :: GetItem -> PrimaryKey
giTableName :: GetItem -> Text
..} = [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$
        [Pair] -> ([Text] -> [Pair]) -> Maybe [Text] -> [Pair]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Pair -> [Pair]
forall (m :: * -> *) a. Monad m => a -> m a
return (Pair -> [Pair]) -> ([Text] -> Pair) -> [Text] -> [Pair]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key
"AttributesToGet" Key -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=)) Maybe [Text]
giAttrs [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"TableName" Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text
giTableName
        , Key
"Key" Key -> PrimaryKey -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= PrimaryKey
giKey
        , Key
"ConsistentRead" Key -> Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
giConsistent
        , Key
"ReturnConsumedCapacity" Key -> ReturnConsumption -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ReturnConsumption
giRetCons
        ]


instance SignQuery GetItem where
    type ServiceConfiguration GetItem = DdbConfiguration
    signQuery :: GetItem
-> ServiceConfiguration GetItem queryType
-> SignatureData
-> SignedQuery
signQuery GetItem
gi = ByteString
-> GetItem
-> DdbConfiguration queryType
-> SignatureData
-> SignedQuery
forall a qt.
ToJSON a =>
ByteString
-> a -> DdbConfiguration qt -> SignatureData -> SignedQuery
ddbSignQuery ByteString
"GetItem" GetItem
gi



instance FromJSON GetItemResponse where
    parseJSON :: Value -> Parser GetItemResponse
parseJSON (Object Object
v) = Maybe Item -> Maybe ConsumedCapacity -> GetItemResponse
GetItemResponse
        (Maybe Item -> Maybe ConsumedCapacity -> GetItemResponse)
-> Parser (Maybe Item)
-> Parser (Maybe ConsumedCapacity -> GetItemResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Maybe Item)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"Item"
        Parser (Maybe ConsumedCapacity -> GetItemResponse)
-> Parser (Maybe ConsumedCapacity) -> Parser GetItemResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe ConsumedCapacity)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ConsumedCapacity"
    parseJSON Value
_ = String -> Parser GetItemResponse
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"GetItemResponse must be an object."


instance ResponseConsumer r GetItemResponse where
    type ResponseMetadata GetItemResponse = DdbResponse
    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata GetItemResponse)
-> HTTPResponseConsumer GetItemResponse
responseConsumer Request
_ r
_ IORef (ResponseMetadata GetItemResponse)
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp = IORef DdbResponse -> HTTPResponseConsumer GetItemResponse
forall a. FromJSON a => IORef DdbResponse -> HTTPResponseConsumer a
ddbResponseConsumer IORef (ResponseMetadata GetItemResponse)
IORef DdbResponse
ref Response (ConduitM () ByteString (ResourceT IO) ())
resp


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