{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.QLDB.GetBlock
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Returns a block object at a specified address in a journal. Also returns
-- a proof of the specified block for verification if @DigestTipAddress@ is
-- provided.
--
-- For information about the data contents in a block, see
-- <https://docs.aws.amazon.com/qldb/latest/developerguide/journal-contents.html Journal contents>
-- in the /Amazon QLDB Developer Guide/.
--
-- If the specified ledger doesn\'t exist or is in @DELETING@ status, then
-- throws @ResourceNotFoundException@.
--
-- If the specified ledger is in @CREATING@ status, then throws
-- @ResourcePreconditionNotMetException@.
--
-- If no block exists with the specified address, then throws
-- @InvalidParameterException@.
module Amazonka.QLDB.GetBlock
  ( -- * Creating a Request
    GetBlock (..),
    newGetBlock,

    -- * Request Lenses
    getBlock_digestTipAddress,
    getBlock_name,
    getBlock_blockAddress,

    -- * Destructuring the Response
    GetBlockResponse (..),
    newGetBlockResponse,

    -- * Response Lenses
    getBlockResponse_proof,
    getBlockResponse_httpStatus,
    getBlockResponse_block,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude
import Amazonka.QLDB.Types
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newGetBlock' smart constructor.
data GetBlock = GetBlock'
  { -- | The latest block location covered by the digest for which to request a
    -- proof. An address is an Amazon Ion structure that has two fields:
    -- @strandId@ and @sequenceNo@.
    --
    -- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:49}@.
    GetBlock -> Maybe (Sensitive ValueHolder)
digestTipAddress :: Prelude.Maybe (Data.Sensitive ValueHolder),
    -- | The name of the ledger.
    GetBlock -> Text
name :: Prelude.Text,
    -- | The location of the block that you want to request. An address is an
    -- Amazon Ion structure that has two fields: @strandId@ and @sequenceNo@.
    --
    -- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:14}@.
    GetBlock -> Sensitive ValueHolder
blockAddress :: Data.Sensitive ValueHolder
  }
  deriving (GetBlock -> GetBlock -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetBlock -> GetBlock -> Bool
$c/= :: GetBlock -> GetBlock -> Bool
== :: GetBlock -> GetBlock -> Bool
$c== :: GetBlock -> GetBlock -> Bool
Prelude.Eq, Int -> GetBlock -> ShowS
[GetBlock] -> ShowS
GetBlock -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetBlock] -> ShowS
$cshowList :: [GetBlock] -> ShowS
show :: GetBlock -> String
$cshow :: GetBlock -> String
showsPrec :: Int -> GetBlock -> ShowS
$cshowsPrec :: Int -> GetBlock -> ShowS
Prelude.Show, forall x. Rep GetBlock x -> GetBlock
forall x. GetBlock -> Rep GetBlock x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetBlock x -> GetBlock
$cfrom :: forall x. GetBlock -> Rep GetBlock x
Prelude.Generic)

-- |
-- Create a value of 'GetBlock' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'digestTipAddress', 'getBlock_digestTipAddress' - The latest block location covered by the digest for which to request a
-- proof. An address is an Amazon Ion structure that has two fields:
-- @strandId@ and @sequenceNo@.
--
-- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:49}@.
--
-- 'name', 'getBlock_name' - The name of the ledger.
--
-- 'blockAddress', 'getBlock_blockAddress' - The location of the block that you want to request. An address is an
-- Amazon Ion structure that has two fields: @strandId@ and @sequenceNo@.
--
-- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:14}@.
newGetBlock ::
  -- | 'name'
  Prelude.Text ->
  -- | 'blockAddress'
  ValueHolder ->
  GetBlock
newGetBlock :: Text -> ValueHolder -> GetBlock
newGetBlock Text
pName_ ValueHolder
pBlockAddress_ =
  GetBlock'
    { $sel:digestTipAddress:GetBlock' :: Maybe (Sensitive ValueHolder)
digestTipAddress = forall a. Maybe a
Prelude.Nothing,
      $sel:name:GetBlock' :: Text
name = Text
pName_,
      $sel:blockAddress:GetBlock' :: Sensitive ValueHolder
blockAddress = forall a. Iso' (Sensitive a) a
Data._Sensitive forall t b. AReview t b -> b -> t
Lens.# ValueHolder
pBlockAddress_
    }

-- | The latest block location covered by the digest for which to request a
-- proof. An address is an Amazon Ion structure that has two fields:
-- @strandId@ and @sequenceNo@.
--
-- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:49}@.
getBlock_digestTipAddress :: Lens.Lens' GetBlock (Prelude.Maybe ValueHolder)
getBlock_digestTipAddress :: Lens' GetBlock (Maybe ValueHolder)
getBlock_digestTipAddress = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlock' {Maybe (Sensitive ValueHolder)
digestTipAddress :: Maybe (Sensitive ValueHolder)
$sel:digestTipAddress:GetBlock' :: GetBlock -> Maybe (Sensitive ValueHolder)
digestTipAddress} -> Maybe (Sensitive ValueHolder)
digestTipAddress) (\s :: GetBlock
s@GetBlock' {} Maybe (Sensitive ValueHolder)
a -> GetBlock
s {$sel:digestTipAddress:GetBlock' :: Maybe (Sensitive ValueHolder)
digestTipAddress = Maybe (Sensitive ValueHolder)
a} :: GetBlock) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall a. Iso' (Sensitive a) a
Data._Sensitive

-- | The name of the ledger.
getBlock_name :: Lens.Lens' GetBlock Prelude.Text
getBlock_name :: Lens' GetBlock Text
getBlock_name = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlock' {Text
name :: Text
$sel:name:GetBlock' :: GetBlock -> Text
name} -> Text
name) (\s :: GetBlock
s@GetBlock' {} Text
a -> GetBlock
s {$sel:name:GetBlock' :: Text
name = Text
a} :: GetBlock)

-- | The location of the block that you want to request. An address is an
-- Amazon Ion structure that has two fields: @strandId@ and @sequenceNo@.
--
-- For example: @{strandId:\"BlFTjlSXze9BIh1KOszcE3\",sequenceNo:14}@.
getBlock_blockAddress :: Lens.Lens' GetBlock ValueHolder
getBlock_blockAddress :: Lens' GetBlock ValueHolder
getBlock_blockAddress = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlock' {Sensitive ValueHolder
blockAddress :: Sensitive ValueHolder
$sel:blockAddress:GetBlock' :: GetBlock -> Sensitive ValueHolder
blockAddress} -> Sensitive ValueHolder
blockAddress) (\s :: GetBlock
s@GetBlock' {} Sensitive ValueHolder
a -> GetBlock
s {$sel:blockAddress:GetBlock' :: Sensitive ValueHolder
blockAddress = Sensitive ValueHolder
a} :: GetBlock) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall a. Iso' (Sensitive a) a
Data._Sensitive

instance Core.AWSRequest GetBlock where
  type AWSResponse GetBlock = GetBlockResponse
  request :: (Service -> Service) -> GetBlock -> Request GetBlock
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy GetBlock
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GetBlock)))
response =
    forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe (Sensitive ValueHolder)
-> Int -> Sensitive ValueHolder -> GetBlockResponse
GetBlockResponse'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"Proof")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Either String a
Data..:> Key
"Block")
      )

instance Prelude.Hashable GetBlock where
  hashWithSalt :: Int -> GetBlock -> Int
hashWithSalt Int
_salt GetBlock' {Maybe (Sensitive ValueHolder)
Text
Sensitive ValueHolder
blockAddress :: Sensitive ValueHolder
name :: Text
digestTipAddress :: Maybe (Sensitive ValueHolder)
$sel:blockAddress:GetBlock' :: GetBlock -> Sensitive ValueHolder
$sel:name:GetBlock' :: GetBlock -> Text
$sel:digestTipAddress:GetBlock' :: GetBlock -> Maybe (Sensitive ValueHolder)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (Sensitive ValueHolder)
digestTipAddress
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
name
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Sensitive ValueHolder
blockAddress

instance Prelude.NFData GetBlock where
  rnf :: GetBlock -> ()
rnf GetBlock' {Maybe (Sensitive ValueHolder)
Text
Sensitive ValueHolder
blockAddress :: Sensitive ValueHolder
name :: Text
digestTipAddress :: Maybe (Sensitive ValueHolder)
$sel:blockAddress:GetBlock' :: GetBlock -> Sensitive ValueHolder
$sel:name:GetBlock' :: GetBlock -> Text
$sel:digestTipAddress:GetBlock' :: GetBlock -> Maybe (Sensitive ValueHolder)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (Sensitive ValueHolder)
digestTipAddress
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
name
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Sensitive ValueHolder
blockAddress

instance Data.ToHeaders GetBlock where
  toHeaders :: GetBlock -> ResponseHeaders
toHeaders =
    forall a b. a -> b -> a
Prelude.const
      ( forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"application/x-amz-json-1.0" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON GetBlock where
  toJSON :: GetBlock -> Value
toJSON GetBlock' {Maybe (Sensitive ValueHolder)
Text
Sensitive ValueHolder
blockAddress :: Sensitive ValueHolder
name :: Text
digestTipAddress :: Maybe (Sensitive ValueHolder)
$sel:blockAddress:GetBlock' :: GetBlock -> Sensitive ValueHolder
$sel:name:GetBlock' :: GetBlock -> Text
$sel:digestTipAddress:GetBlock' :: GetBlock -> Maybe (Sensitive ValueHolder)
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"DigestTipAddress" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (Sensitive ValueHolder)
digestTipAddress,
            forall a. a -> Maybe a
Prelude.Just (Key
"BlockAddress" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Sensitive ValueHolder
blockAddress)
          ]
      )

instance Data.ToPath GetBlock where
  toPath :: GetBlock -> ByteString
toPath GetBlock' {Maybe (Sensitive ValueHolder)
Text
Sensitive ValueHolder
blockAddress :: Sensitive ValueHolder
name :: Text
digestTipAddress :: Maybe (Sensitive ValueHolder)
$sel:blockAddress:GetBlock' :: GetBlock -> Sensitive ValueHolder
$sel:name:GetBlock' :: GetBlock -> Text
$sel:digestTipAddress:GetBlock' :: GetBlock -> Maybe (Sensitive ValueHolder)
..} =
    forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ByteString
"/ledgers/", forall a. ToByteString a => a -> ByteString
Data.toBS Text
name, ByteString
"/block"]

instance Data.ToQuery GetBlock where
  toQuery :: GetBlock -> QueryString
toQuery = forall a b. a -> b -> a
Prelude.const forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newGetBlockResponse' smart constructor.
data GetBlockResponse = GetBlockResponse'
  { -- | The proof object in Amazon Ion format returned by a @GetBlock@ request.
    -- A proof contains the list of hash values required to recalculate the
    -- specified digest using a Merkle tree, starting with the specified block.
    GetBlockResponse -> Maybe (Sensitive ValueHolder)
proof :: Prelude.Maybe (Data.Sensitive ValueHolder),
    -- | The response's http status code.
    GetBlockResponse -> Int
httpStatus :: Prelude.Int,
    -- | The block data object in Amazon Ion format.
    GetBlockResponse -> Sensitive ValueHolder
block :: Data.Sensitive ValueHolder
  }
  deriving (GetBlockResponse -> GetBlockResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetBlockResponse -> GetBlockResponse -> Bool
$c/= :: GetBlockResponse -> GetBlockResponse -> Bool
== :: GetBlockResponse -> GetBlockResponse -> Bool
$c== :: GetBlockResponse -> GetBlockResponse -> Bool
Prelude.Eq, Int -> GetBlockResponse -> ShowS
[GetBlockResponse] -> ShowS
GetBlockResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetBlockResponse] -> ShowS
$cshowList :: [GetBlockResponse] -> ShowS
show :: GetBlockResponse -> String
$cshow :: GetBlockResponse -> String
showsPrec :: Int -> GetBlockResponse -> ShowS
$cshowsPrec :: Int -> GetBlockResponse -> ShowS
Prelude.Show, forall x. Rep GetBlockResponse x -> GetBlockResponse
forall x. GetBlockResponse -> Rep GetBlockResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetBlockResponse x -> GetBlockResponse
$cfrom :: forall x. GetBlockResponse -> Rep GetBlockResponse x
Prelude.Generic)

-- |
-- Create a value of 'GetBlockResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'proof', 'getBlockResponse_proof' - The proof object in Amazon Ion format returned by a @GetBlock@ request.
-- A proof contains the list of hash values required to recalculate the
-- specified digest using a Merkle tree, starting with the specified block.
--
-- 'httpStatus', 'getBlockResponse_httpStatus' - The response's http status code.
--
-- 'block', 'getBlockResponse_block' - The block data object in Amazon Ion format.
newGetBlockResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  -- | 'block'
  ValueHolder ->
  GetBlockResponse
newGetBlockResponse :: Int -> ValueHolder -> GetBlockResponse
newGetBlockResponse Int
pHttpStatus_ ValueHolder
pBlock_ =
  GetBlockResponse'
    { $sel:proof:GetBlockResponse' :: Maybe (Sensitive ValueHolder)
proof = forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:GetBlockResponse' :: Int
httpStatus = Int
pHttpStatus_,
      $sel:block:GetBlockResponse' :: Sensitive ValueHolder
block = forall a. Iso' (Sensitive a) a
Data._Sensitive forall t b. AReview t b -> b -> t
Lens.# ValueHolder
pBlock_
    }

-- | The proof object in Amazon Ion format returned by a @GetBlock@ request.
-- A proof contains the list of hash values required to recalculate the
-- specified digest using a Merkle tree, starting with the specified block.
getBlockResponse_proof :: Lens.Lens' GetBlockResponse (Prelude.Maybe ValueHolder)
getBlockResponse_proof :: Lens' GetBlockResponse (Maybe ValueHolder)
getBlockResponse_proof = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlockResponse' {Maybe (Sensitive ValueHolder)
proof :: Maybe (Sensitive ValueHolder)
$sel:proof:GetBlockResponse' :: GetBlockResponse -> Maybe (Sensitive ValueHolder)
proof} -> Maybe (Sensitive ValueHolder)
proof) (\s :: GetBlockResponse
s@GetBlockResponse' {} Maybe (Sensitive ValueHolder)
a -> GetBlockResponse
s {$sel:proof:GetBlockResponse' :: Maybe (Sensitive ValueHolder)
proof = Maybe (Sensitive ValueHolder)
a} :: GetBlockResponse) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall a. Iso' (Sensitive a) a
Data._Sensitive

-- | The response's http status code.
getBlockResponse_httpStatus :: Lens.Lens' GetBlockResponse Prelude.Int
getBlockResponse_httpStatus :: Lens' GetBlockResponse Int
getBlockResponse_httpStatus = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlockResponse' {Int
httpStatus :: Int
$sel:httpStatus:GetBlockResponse' :: GetBlockResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: GetBlockResponse
s@GetBlockResponse' {} Int
a -> GetBlockResponse
s {$sel:httpStatus:GetBlockResponse' :: Int
httpStatus = Int
a} :: GetBlockResponse)

-- | The block data object in Amazon Ion format.
getBlockResponse_block :: Lens.Lens' GetBlockResponse ValueHolder
getBlockResponse_block :: Lens' GetBlockResponse ValueHolder
getBlockResponse_block = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetBlockResponse' {Sensitive ValueHolder
block :: Sensitive ValueHolder
$sel:block:GetBlockResponse' :: GetBlockResponse -> Sensitive ValueHolder
block} -> Sensitive ValueHolder
block) (\s :: GetBlockResponse
s@GetBlockResponse' {} Sensitive ValueHolder
a -> GetBlockResponse
s {$sel:block:GetBlockResponse' :: Sensitive ValueHolder
block = Sensitive ValueHolder
a} :: GetBlockResponse) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall a. Iso' (Sensitive a) a
Data._Sensitive

instance Prelude.NFData GetBlockResponse where
  rnf :: GetBlockResponse -> ()
rnf GetBlockResponse' {Int
Maybe (Sensitive ValueHolder)
Sensitive ValueHolder
block :: Sensitive ValueHolder
httpStatus :: Int
proof :: Maybe (Sensitive ValueHolder)
$sel:block:GetBlockResponse' :: GetBlockResponse -> Sensitive ValueHolder
$sel:httpStatus:GetBlockResponse' :: GetBlockResponse -> Int
$sel:proof:GetBlockResponse' :: GetBlockResponse -> Maybe (Sensitive ValueHolder)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (Sensitive ValueHolder)
proof
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Sensitive ValueHolder
block