{-# LANGUAGE DeriveDataTypeable        #-}
{-# LANGUAGE FlexibleContexts          #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE MultiParamTypeClasses     #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE RecordWildCards           #-}
{-# LANGUAGE ScopedTypeVariables       #-}
{-# LANGUAGE TypeFamilies              #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Aws.DynamoDb.Commands.BatchGetItem
-- Copyright   :  Soostone Inc
-- License     :  BSD3
--
-- Maintainer  :  Justin Dawson <jtdawso@gmail.com>
-- Stability   :  experimental
--
-- @http:\/\/docs.aws.amazon.com\/amazondynamodb\/latest\/APIReference\/API_BatchGetItem.html@
----------------------------------------------------------------------------

module Aws.DynamoDb.Commands.BatchGetItem where

-------------------------------------------------------------------------------
import           Control.Applicative
import           Data.Aeson
import           Data.Default
import qualified Data.HashMap.Strict as HM
import qualified Data.Text           as T
import           Prelude
-------------------------------------------------------------------------------
import           Aws.Core
import           Aws.DynamoDb.Core
import           Aws.DynamoDb.Commands.GetItem
-------------------------------------------------------------------------------


data GetRequestItem = GetRequestItem{
         GetRequestItem -> Maybe Text
griProjExpr :: Maybe T.Text
       , GetRequestItem -> Bool
griConsistent ::Bool
       , GetRequestItem -> [PrimaryKey]
griKeys :: [PrimaryKey]  
     } deriving (GetRequestItem -> GetRequestItem -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetRequestItem -> GetRequestItem -> Bool
$c/= :: GetRequestItem -> GetRequestItem -> Bool
== :: GetRequestItem -> GetRequestItem -> Bool
$c== :: GetRequestItem -> GetRequestItem -> Bool
Eq,Int -> GetRequestItem -> ShowS
[GetRequestItem] -> ShowS
GetRequestItem -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetRequestItem] -> ShowS
$cshowList :: [GetRequestItem] -> ShowS
show :: GetRequestItem -> String
$cshow :: GetRequestItem -> String
showsPrec :: Int -> GetRequestItem -> ShowS
$cshowsPrec :: Int -> GetRequestItem -> ShowS
Show,ReadPrec [GetRequestItem]
ReadPrec GetRequestItem
Int -> ReadS GetRequestItem
ReadS [GetRequestItem]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetRequestItem]
$creadListPrec :: ReadPrec [GetRequestItem]
readPrec :: ReadPrec GetRequestItem
$creadPrec :: ReadPrec GetRequestItem
readList :: ReadS [GetRequestItem]
$creadList :: ReadS [GetRequestItem]
readsPrec :: Int -> ReadS GetRequestItem
$creadsPrec :: Int -> ReadS GetRequestItem
Read,Eq GetRequestItem
GetRequestItem -> GetRequestItem -> Bool
GetRequestItem -> GetRequestItem -> Ordering
GetRequestItem -> GetRequestItem -> GetRequestItem
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 :: GetRequestItem -> GetRequestItem -> GetRequestItem
$cmin :: GetRequestItem -> GetRequestItem -> GetRequestItem
max :: GetRequestItem -> GetRequestItem -> GetRequestItem
$cmax :: GetRequestItem -> GetRequestItem -> GetRequestItem
>= :: GetRequestItem -> GetRequestItem -> Bool
$c>= :: GetRequestItem -> GetRequestItem -> Bool
> :: GetRequestItem -> GetRequestItem -> Bool
$c> :: GetRequestItem -> GetRequestItem -> Bool
<= :: GetRequestItem -> GetRequestItem -> Bool
$c<= :: GetRequestItem -> GetRequestItem -> Bool
< :: GetRequestItem -> GetRequestItem -> Bool
$c< :: GetRequestItem -> GetRequestItem -> Bool
compare :: GetRequestItem -> GetRequestItem -> Ordering
$ccompare :: GetRequestItem -> GetRequestItem -> Ordering
Ord)

data BatchGetItem = BatchGetItem {
      BatchGetItem -> [(Text, GetRequestItem)]
bgRequests :: [(T.Text,GetRequestItem)]
    -- ^ Get Requests for a specified table
    , BatchGetItem -> ReturnConsumption
bgRetCons :: ReturnConsumption
    } deriving (BatchGetItem -> BatchGetItem -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchGetItem -> BatchGetItem -> Bool
$c/= :: BatchGetItem -> BatchGetItem -> Bool
== :: BatchGetItem -> BatchGetItem -> Bool
$c== :: BatchGetItem -> BatchGetItem -> Bool
Eq,Int -> BatchGetItem -> ShowS
[BatchGetItem] -> ShowS
BatchGetItem -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchGetItem] -> ShowS
$cshowList :: [BatchGetItem] -> ShowS
show :: BatchGetItem -> String
$cshow :: BatchGetItem -> String
showsPrec :: Int -> BatchGetItem -> ShowS
$cshowsPrec :: Int -> BatchGetItem -> ShowS
Show,ReadPrec [BatchGetItem]
ReadPrec BatchGetItem
Int -> ReadS BatchGetItem
ReadS [BatchGetItem]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchGetItem]
$creadListPrec :: ReadPrec [BatchGetItem]
readPrec :: ReadPrec BatchGetItem
$creadPrec :: ReadPrec BatchGetItem
readList :: ReadS [BatchGetItem]
$creadList :: ReadS [BatchGetItem]
readsPrec :: Int -> ReadS BatchGetItem
$creadsPrec :: Int -> ReadS BatchGetItem
Read,Eq BatchGetItem
BatchGetItem -> BatchGetItem -> Bool
BatchGetItem -> BatchGetItem -> Ordering
BatchGetItem -> BatchGetItem -> BatchGetItem
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 :: BatchGetItem -> BatchGetItem -> BatchGetItem
$cmin :: BatchGetItem -> BatchGetItem -> BatchGetItem
max :: BatchGetItem -> BatchGetItem -> BatchGetItem
$cmax :: BatchGetItem -> BatchGetItem -> BatchGetItem
>= :: BatchGetItem -> BatchGetItem -> Bool
$c>= :: BatchGetItem -> BatchGetItem -> Bool
> :: BatchGetItem -> BatchGetItem -> Bool
$c> :: BatchGetItem -> BatchGetItem -> Bool
<= :: BatchGetItem -> BatchGetItem -> Bool
$c<= :: BatchGetItem -> BatchGetItem -> Bool
< :: BatchGetItem -> BatchGetItem -> Bool
$c< :: BatchGetItem -> BatchGetItem -> Bool
compare :: BatchGetItem -> BatchGetItem -> Ordering
$ccompare :: BatchGetItem -> BatchGetItem -> Ordering
Ord)

-------------------------------------------------------------------------------

-- | Construct a RequestItem .
batchGetRequestItem :: Maybe T.Text
               -- ^ Projection Expression
               -> Bool
               -- ^ Consistent Read
               -> [PrimaryKey]
               -- ^ Items to be deleted
               -> GetRequestItem
batchGetRequestItem :: Maybe Text -> Bool -> [PrimaryKey] -> GetRequestItem
batchGetRequestItem Maybe Text
expr Bool
consistent [PrimaryKey]
keys = Maybe Text -> Bool -> [PrimaryKey] -> GetRequestItem
GetRequestItem Maybe Text
expr Bool
consistent [PrimaryKey]
keys

toBatchGet :: [GetItem] -> BatchGetItem
toBatchGet :: [GetItem] -> BatchGetItem
toBatchGet [GetItem]
gs = [(Text, GetRequestItem)] -> ReturnConsumption -> BatchGetItem
BatchGetItem ([GetItem] -> [(Text, GetRequestItem)]
convert [GetItem]
gs) forall a. Default a => a
def

  where
    groupItems :: [GetItem]-> HM.HashMap T.Text [GetItem] -> HM.HashMap T.Text [GetItem]
    groupItems :: [GetItem] -> HashMap Text [GetItem] -> HashMap Text [GetItem]
groupItems [] HashMap Text [GetItem]
hm = HashMap Text [GetItem]
hm
    groupItems (GetItem
x:[GetItem]
xs) HashMap Text [GetItem]
hm = let key :: Text
key = GetItem -> Text
giTableName GetItem
x
                             in [GetItem] -> HashMap Text [GetItem] -> HashMap Text [GetItem]
groupItems [GetItem]
xs (forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert Text
key (GetItem
x forall a. a -> [a] -> [a]
: (forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HM.lookupDefault [] Text
key HashMap Text [GetItem]
hm)) HashMap Text [GetItem]
hm)
    
    convert :: [GetItem] -> [(T.Text,GetRequestItem)] 
    convert :: [GetItem] -> [(Text, GetRequestItem)]
convert [GetItem]
gs' = let l :: [(Text, [GetItem])]
l = forall k v. HashMap k v -> [(k, v)]
HM.toList forall a b. (a -> b) -> a -> b
$ [GetItem] -> HashMap Text [GetItem] -> HashMap Text [GetItem]
groupItems [GetItem]
gs' forall k v. HashMap k v
HM.empty
                    -- Uses one GetItem to specify ProjectionExpression
                    -- and ConsistentRead for the entire batch
                    in forall a b. (a -> b) -> [a] -> [b]
map (\(Text
table,items :: [GetItem]
items@(GetItem
i:[GetItem]
_)) ->(Text
table,Maybe Text -> Bool -> [PrimaryKey] -> GetRequestItem
GetRequestItem 
                                                    (Text -> [Text] -> Text
T.intercalate Text
"," forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GetItem -> Maybe [Text]
giAttrs GetItem
i)
                                                    (GetItem -> Bool
giConsistent GetItem
i)
                                                    (forall a b. (a -> b) -> [a] -> [b]
map GetItem -> PrimaryKey
giKey [GetItem]
items)) ) [(Text, [GetItem])]
l

-- | Construct a BatchGetItem
batchGetItem :: [(T.Text, GetRequestItem)]
               -> BatchGetItem
batchGetItem :: [(Text, GetRequestItem)] -> BatchGetItem
batchGetItem [(Text, GetRequestItem)]
reqs = [(Text, GetRequestItem)] -> ReturnConsumption -> BatchGetItem
BatchGetItem [(Text, GetRequestItem)]
reqs forall a. Default a => a
def


instance ToJSON GetRequestItem where
   toJSON :: GetRequestItem -> Value
toJSON GetRequestItem{Bool
[PrimaryKey]
Maybe Text
griKeys :: [PrimaryKey]
griConsistent :: Bool
griProjExpr :: Maybe Text
griKeys :: GetRequestItem -> [PrimaryKey]
griConsistent :: GetRequestItem -> Bool
griProjExpr :: GetRequestItem -> Maybe Text
..} =
       ([Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Key
"ProjectionExpression" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=)) Maybe Text
griProjExpr forall a. [a] -> [a] -> [a]
++
                 [Key
"ConsistentRead" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Bool
griConsistent
                 , Key
"Keys" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= [PrimaryKey]
griKeys])
         

instance ToJSON BatchGetItem where
    toJSON :: BatchGetItem -> Value
toJSON BatchGetItem{[(Text, GetRequestItem)]
ReturnConsumption
bgRetCons :: ReturnConsumption
bgRequests :: [(Text, GetRequestItem)]
bgRetCons :: BatchGetItem -> ReturnConsumption
bgRequests :: BatchGetItem -> [(Text, GetRequestItem)]
..} =
        [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$
          [ Key
"RequestItems" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList [(Text, GetRequestItem)]
bgRequests
          , Key
"ReturnConsumedCapacity" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ReturnConsumption
bgRetCons
          ]

instance FromJSON GetRequestItem where
    parseJSON :: Value -> Parser GetRequestItem
parseJSON (Object Object
p) = do
                 Maybe Text -> Bool -> [PrimaryKey] -> GetRequestItem
GetRequestItem forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
p forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ProjectionExpression"
                                forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
p forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ConsistentRead"
                                forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
p forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"Keys"
    parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unable to parse GetRequestItem"
    
         
data BatchGetItemResponse = BatchGetItemResponse {
      BatchGetItemResponse -> [(Text, [Item])]
bgResponses :: [(T.Text, [Item])]
    , BatchGetItemResponse -> Maybe [(Text, GetRequestItem)]
bgUnprocessed    :: Maybe [(T.Text,GetRequestItem)]
    -- ^ Unprocessed Requests on failure
    , BatchGetItemResponse -> Maybe ConsumedCapacity
bgConsumed :: Maybe ConsumedCapacity
    -- ^ Amount of capacity consumed
    } deriving (BatchGetItemResponse -> BatchGetItemResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c/= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
== :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c== :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
Eq,Int -> BatchGetItemResponse -> ShowS
[BatchGetItemResponse] -> ShowS
BatchGetItemResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchGetItemResponse] -> ShowS
$cshowList :: [BatchGetItemResponse] -> ShowS
show :: BatchGetItemResponse -> String
$cshow :: BatchGetItemResponse -> String
showsPrec :: Int -> BatchGetItemResponse -> ShowS
$cshowsPrec :: Int -> BatchGetItemResponse -> ShowS
Show,ReadPrec [BatchGetItemResponse]
ReadPrec BatchGetItemResponse
Int -> ReadS BatchGetItemResponse
ReadS [BatchGetItemResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchGetItemResponse]
$creadListPrec :: ReadPrec [BatchGetItemResponse]
readPrec :: ReadPrec BatchGetItemResponse
$creadPrec :: ReadPrec BatchGetItemResponse
readList :: ReadS [BatchGetItemResponse]
$creadList :: ReadS [BatchGetItemResponse]
readsPrec :: Int -> ReadS BatchGetItemResponse
$creadsPrec :: Int -> ReadS BatchGetItemResponse
Read,Eq BatchGetItemResponse
BatchGetItemResponse -> BatchGetItemResponse -> Bool
BatchGetItemResponse -> BatchGetItemResponse -> Ordering
BatchGetItemResponse
-> BatchGetItemResponse -> BatchGetItemResponse
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 :: BatchGetItemResponse
-> BatchGetItemResponse -> BatchGetItemResponse
$cmin :: BatchGetItemResponse
-> BatchGetItemResponse -> BatchGetItemResponse
max :: BatchGetItemResponse
-> BatchGetItemResponse -> BatchGetItemResponse
$cmax :: BatchGetItemResponse
-> BatchGetItemResponse -> BatchGetItemResponse
>= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c>= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
> :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c> :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
<= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c<= :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
< :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
$c< :: BatchGetItemResponse -> BatchGetItemResponse -> Bool
compare :: BatchGetItemResponse -> BatchGetItemResponse -> Ordering
$ccompare :: BatchGetItemResponse -> BatchGetItemResponse -> Ordering
Ord)



instance Transaction BatchGetItem BatchGetItemResponse


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


instance FromJSON BatchGetItemResponse where
    parseJSON :: Value -> Parser BatchGetItemResponse
parseJSON (Object Object
v) = [(Text, [Item])]
-> Maybe [(Text, GetRequestItem)]
-> Maybe ConsumedCapacity
-> BatchGetItemResponse
BatchGetItemResponse
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall k v. HashMap k v -> [(k, v)]
HM.toList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
v forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"Responses"))
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"UnprocessedItems"
        forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ConsumedCapacity"

    parseJSON Value
_ = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"BatchGetItemResponse must be an object."

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

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