{-# 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
$c== :: GetItem -> GetItem -> Bool
== :: GetItem -> GetItem -> Bool
$c/= :: GetItem -> GetItem -> Bool
/= :: 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
$cshowsPrec :: Int -> GetItem -> ShowS
showsPrec :: Int -> GetItem -> ShowS
$cshow :: GetItem -> String
show :: GetItem -> String
$cshowList :: [GetItem] -> ShowS
showList :: [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
$creadsPrec :: Int -> ReadS GetItem
readsPrec :: Int -> ReadS GetItem
$creadList :: ReadS [GetItem]
readList :: ReadS [GetItem]
$creadPrec :: ReadPrec GetItem
readPrec :: ReadPrec GetItem
$creadListPrec :: ReadPrec [GetItem]
readListPrec :: ReadPrec [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
$ccompare :: GetItem -> GetItem -> Ordering
compare :: GetItem -> GetItem -> Ordering
$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
>= :: GetItem -> GetItem -> Bool
$cmax :: GetItem -> GetItem -> GetItem
max :: GetItem -> GetItem -> GetItem
$cmin :: GetItem -> GetItem -> GetItem
min :: GetItem -> GetItem -> 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
$c== :: GetItemResponse -> GetItemResponse -> Bool
== :: GetItemResponse -> GetItemResponse -> Bool
$c/= :: GetItemResponse -> GetItemResponse -> Bool
/= :: 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
$cshowsPrec :: Int -> GetItemResponse -> ShowS
showsPrec :: Int -> GetItemResponse -> ShowS
$cshow :: GetItemResponse -> String
show :: GetItemResponse -> String
$cshowList :: [GetItemResponse] -> ShowS
showList :: [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
$creadsPrec :: Int -> ReadS GetItemResponse
readsPrec :: Int -> ReadS GetItemResponse
$creadList :: ReadS [GetItemResponse]
readList :: ReadS [GetItemResponse]
$creadPrec :: ReadPrec GetItemResponse
readPrec :: ReadPrec GetItemResponse
$creadListPrec :: ReadPrec [GetItemResponse]
readListPrec :: ReadPrec [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
$ccompare :: GetItemResponse -> GetItemResponse -> Ordering
compare :: GetItemResponse -> GetItemResponse -> Ordering
$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
>= :: GetItemResponse -> GetItemResponse -> Bool
$cmax :: GetItemResponse -> GetItemResponse -> GetItemResponse
max :: GetItemResponse -> GetItemResponse -> GetItemResponse
$cmin :: GetItemResponse -> GetItemResponse -> GetItemResponse
min :: GetItemResponse -> GetItemResponse -> GetItemResponse
Ord)


instance Transaction GetItem GetItemResponse


instance ToJSON GetItem where
    toJSON :: GetItem -> Value
toJSON GetItem{Bool
Maybe [Text]
Text
ReturnConsumption
PrimaryKey
giTableName :: GetItem -> Text
giKey :: GetItem -> PrimaryKey
giAttrs :: GetItem -> Maybe [Text]
giConsistent :: GetItem -> Bool
giRetCons :: GetItem -> ReturnConsumption
giTableName :: Text
giKey :: PrimaryKey
giAttrs :: Maybe [Text]
giConsistent :: Bool
giRetCons :: ReturnConsumption
..} = [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 a. a -> [a]
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 v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.=)) Maybe [Text]
giAttrs [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++
        [ Key
"TableName" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
giTableName
        , Key
"Key" Key -> PrimaryKey -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PrimaryKey
giKey
        , Key
"ConsistentRead" Key -> Bool -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Bool
giConsistent
        , Key
"ReturnConsumedCapacity" Key -> ReturnConsumption -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ReturnConsumption
giRetCons
        ]


instance SignQuery GetItem where
    type ServiceConfiguration GetItem = DdbConfiguration
    signQuery :: forall queryType.
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 a b. Parser (a -> b) -> Parser a -> Parser b
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 a. String -> Parser a
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)
GetItemResponse -> ResourceT IO GetItemResponse
forall a. a -> ResourceT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return