{-# 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.Textract.DetectDocumentText
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Detects text in the input document. Amazon Textract can detect lines of
-- text and the words that make up a line of text. The input document must
-- be in one of the following image formats: JPEG, PNG, PDF, or TIFF.
-- @DetectDocumentText@ returns the detected text in an array of Block
-- objects.
--
-- Each document page has as an associated @Block@ of type PAGE. Each PAGE
-- @Block@ object is the parent of LINE @Block@ objects that represent the
-- lines of detected text on a page. A LINE @Block@ object is a parent for
-- each word that makes up the line. Words are represented by @Block@
-- objects of type WORD.
--
-- @DetectDocumentText@ is a synchronous operation. To analyze documents
-- asynchronously, use StartDocumentTextDetection.
--
-- For more information, see
-- <https://docs.aws.amazon.com/textract/latest/dg/how-it-works-detecting.html Document Text Detection>.
module Amazonka.Textract.DetectDocumentText
  ( -- * Creating a Request
    DetectDocumentText (..),
    newDetectDocumentText,

    -- * Request Lenses
    detectDocumentText_document,

    -- * Destructuring the Response
    DetectDocumentTextResponse (..),
    newDetectDocumentTextResponse,

    -- * Response Lenses
    detectDocumentTextResponse_blocks,
    detectDocumentTextResponse_detectDocumentTextModelVersion,
    detectDocumentTextResponse_documentMetadata,
    detectDocumentTextResponse_httpStatus,
  )
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 qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response
import Amazonka.Textract.Types

-- | /See:/ 'newDetectDocumentText' smart constructor.
data DetectDocumentText = DetectDocumentText'
  { -- | The input document as base64-encoded bytes or an Amazon S3 object. If
    -- you use the AWS CLI to call Amazon Textract operations, you can\'t pass
    -- image bytes. The document must be an image in JPEG or PNG format.
    --
    -- If you\'re using an AWS SDK to call Amazon Textract, you might not need
    -- to base64-encode image bytes that are passed using the @Bytes@ field.
    DetectDocumentText -> Document
document :: Document
  }
  deriving (DetectDocumentText -> DetectDocumentText -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DetectDocumentText -> DetectDocumentText -> Bool
$c/= :: DetectDocumentText -> DetectDocumentText -> Bool
== :: DetectDocumentText -> DetectDocumentText -> Bool
$c== :: DetectDocumentText -> DetectDocumentText -> Bool
Prelude.Eq, ReadPrec [DetectDocumentText]
ReadPrec DetectDocumentText
Int -> ReadS DetectDocumentText
ReadS [DetectDocumentText]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DetectDocumentText]
$creadListPrec :: ReadPrec [DetectDocumentText]
readPrec :: ReadPrec DetectDocumentText
$creadPrec :: ReadPrec DetectDocumentText
readList :: ReadS [DetectDocumentText]
$creadList :: ReadS [DetectDocumentText]
readsPrec :: Int -> ReadS DetectDocumentText
$creadsPrec :: Int -> ReadS DetectDocumentText
Prelude.Read, Int -> DetectDocumentText -> ShowS
[DetectDocumentText] -> ShowS
DetectDocumentText -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectDocumentText] -> ShowS
$cshowList :: [DetectDocumentText] -> ShowS
show :: DetectDocumentText -> String
$cshow :: DetectDocumentText -> String
showsPrec :: Int -> DetectDocumentText -> ShowS
$cshowsPrec :: Int -> DetectDocumentText -> ShowS
Prelude.Show, forall x. Rep DetectDocumentText x -> DetectDocumentText
forall x. DetectDocumentText -> Rep DetectDocumentText x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DetectDocumentText x -> DetectDocumentText
$cfrom :: forall x. DetectDocumentText -> Rep DetectDocumentText x
Prelude.Generic)

-- |
-- Create a value of 'DetectDocumentText' 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:
--
-- 'document', 'detectDocumentText_document' - The input document as base64-encoded bytes or an Amazon S3 object. If
-- you use the AWS CLI to call Amazon Textract operations, you can\'t pass
-- image bytes. The document must be an image in JPEG or PNG format.
--
-- If you\'re using an AWS SDK to call Amazon Textract, you might not need
-- to base64-encode image bytes that are passed using the @Bytes@ field.
newDetectDocumentText ::
  -- | 'document'
  Document ->
  DetectDocumentText
newDetectDocumentText :: Document -> DetectDocumentText
newDetectDocumentText Document
pDocument_ =
  DetectDocumentText' {$sel:document:DetectDocumentText' :: Document
document = Document
pDocument_}

-- | The input document as base64-encoded bytes or an Amazon S3 object. If
-- you use the AWS CLI to call Amazon Textract operations, you can\'t pass
-- image bytes. The document must be an image in JPEG or PNG format.
--
-- If you\'re using an AWS SDK to call Amazon Textract, you might not need
-- to base64-encode image bytes that are passed using the @Bytes@ field.
detectDocumentText_document :: Lens.Lens' DetectDocumentText Document
detectDocumentText_document :: Lens' DetectDocumentText Document
detectDocumentText_document = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectDocumentText' {Document
document :: Document
$sel:document:DetectDocumentText' :: DetectDocumentText -> Document
document} -> Document
document) (\s :: DetectDocumentText
s@DetectDocumentText' {} Document
a -> DetectDocumentText
s {$sel:document:DetectDocumentText' :: Document
document = Document
a} :: DetectDocumentText)

instance Core.AWSRequest DetectDocumentText where
  type
    AWSResponse DetectDocumentText =
      DetectDocumentTextResponse
  request :: (Service -> Service)
-> DetectDocumentText -> Request DetectDocumentText
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 DetectDocumentText
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse DetectDocumentText)))
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 [Block]
-> Maybe Text
-> Maybe DocumentMetadata
-> Int
-> DetectDocumentTextResponse
DetectDocumentTextResponse'
            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
"Blocks" forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"DetectDocumentTextModelVersion")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"DocumentMetadata")
            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))
      )

instance Prelude.Hashable DetectDocumentText where
  hashWithSalt :: Int -> DetectDocumentText -> Int
hashWithSalt Int
_salt DetectDocumentText' {Document
document :: Document
$sel:document:DetectDocumentText' :: DetectDocumentText -> Document
..} =
    Int
_salt forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Document
document

instance Prelude.NFData DetectDocumentText where
  rnf :: DetectDocumentText -> ()
rnf DetectDocumentText' {Document
document :: Document
$sel:document:DetectDocumentText' :: DetectDocumentText -> Document
..} = forall a. NFData a => a -> ()
Prelude.rnf Document
document

instance Data.ToHeaders DetectDocumentText where
  toHeaders :: DetectDocumentText -> ResponseHeaders
toHeaders =
    forall a b. a -> b -> a
Prelude.const
      ( forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"Textract.DetectDocumentText" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON DetectDocumentText where
  toJSON :: DetectDocumentText -> Value
toJSON DetectDocumentText' {Document
document :: Document
$sel:document:DetectDocumentText' :: DetectDocumentText -> Document
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [forall a. a -> Maybe a
Prelude.Just (Key
"Document" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Document
document)]
      )

instance Data.ToPath DetectDocumentText where
  toPath :: DetectDocumentText -> ByteString
toPath = forall a b. a -> b -> a
Prelude.const ByteString
"/"

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

-- | /See:/ 'newDetectDocumentTextResponse' smart constructor.
data DetectDocumentTextResponse = DetectDocumentTextResponse'
  { -- | An array of @Block@ objects that contain the text that\'s detected in
    -- the document.
    DetectDocumentTextResponse -> Maybe [Block]
blocks :: Prelude.Maybe [Block],
    DetectDocumentTextResponse -> Maybe Text
detectDocumentTextModelVersion :: Prelude.Maybe Prelude.Text,
    -- | Metadata about the document. It contains the number of pages that are
    -- detected in the document.
    DetectDocumentTextResponse -> Maybe DocumentMetadata
documentMetadata :: Prelude.Maybe DocumentMetadata,
    -- | The response's http status code.
    DetectDocumentTextResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (DetectDocumentTextResponse -> DetectDocumentTextResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DetectDocumentTextResponse -> DetectDocumentTextResponse -> Bool
$c/= :: DetectDocumentTextResponse -> DetectDocumentTextResponse -> Bool
== :: DetectDocumentTextResponse -> DetectDocumentTextResponse -> Bool
$c== :: DetectDocumentTextResponse -> DetectDocumentTextResponse -> Bool
Prelude.Eq, ReadPrec [DetectDocumentTextResponse]
ReadPrec DetectDocumentTextResponse
Int -> ReadS DetectDocumentTextResponse
ReadS [DetectDocumentTextResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DetectDocumentTextResponse]
$creadListPrec :: ReadPrec [DetectDocumentTextResponse]
readPrec :: ReadPrec DetectDocumentTextResponse
$creadPrec :: ReadPrec DetectDocumentTextResponse
readList :: ReadS [DetectDocumentTextResponse]
$creadList :: ReadS [DetectDocumentTextResponse]
readsPrec :: Int -> ReadS DetectDocumentTextResponse
$creadsPrec :: Int -> ReadS DetectDocumentTextResponse
Prelude.Read, Int -> DetectDocumentTextResponse -> ShowS
[DetectDocumentTextResponse] -> ShowS
DetectDocumentTextResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectDocumentTextResponse] -> ShowS
$cshowList :: [DetectDocumentTextResponse] -> ShowS
show :: DetectDocumentTextResponse -> String
$cshow :: DetectDocumentTextResponse -> String
showsPrec :: Int -> DetectDocumentTextResponse -> ShowS
$cshowsPrec :: Int -> DetectDocumentTextResponse -> ShowS
Prelude.Show, forall x.
Rep DetectDocumentTextResponse x -> DetectDocumentTextResponse
forall x.
DetectDocumentTextResponse -> Rep DetectDocumentTextResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep DetectDocumentTextResponse x -> DetectDocumentTextResponse
$cfrom :: forall x.
DetectDocumentTextResponse -> Rep DetectDocumentTextResponse x
Prelude.Generic)

-- |
-- Create a value of 'DetectDocumentTextResponse' 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:
--
-- 'blocks', 'detectDocumentTextResponse_blocks' - An array of @Block@ objects that contain the text that\'s detected in
-- the document.
--
-- 'detectDocumentTextModelVersion', 'detectDocumentTextResponse_detectDocumentTextModelVersion' -
--
-- 'documentMetadata', 'detectDocumentTextResponse_documentMetadata' - Metadata about the document. It contains the number of pages that are
-- detected in the document.
--
-- 'httpStatus', 'detectDocumentTextResponse_httpStatus' - The response's http status code.
newDetectDocumentTextResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  DetectDocumentTextResponse
newDetectDocumentTextResponse :: Int -> DetectDocumentTextResponse
newDetectDocumentTextResponse Int
pHttpStatus_ =
  DetectDocumentTextResponse'
    { $sel:blocks:DetectDocumentTextResponse' :: Maybe [Block]
blocks =
        forall a. Maybe a
Prelude.Nothing,
      $sel:detectDocumentTextModelVersion:DetectDocumentTextResponse' :: Maybe Text
detectDocumentTextModelVersion =
        forall a. Maybe a
Prelude.Nothing,
      $sel:documentMetadata:DetectDocumentTextResponse' :: Maybe DocumentMetadata
documentMetadata = forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:DetectDocumentTextResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | An array of @Block@ objects that contain the text that\'s detected in
-- the document.
detectDocumentTextResponse_blocks :: Lens.Lens' DetectDocumentTextResponse (Prelude.Maybe [Block])
detectDocumentTextResponse_blocks :: Lens' DetectDocumentTextResponse (Maybe [Block])
detectDocumentTextResponse_blocks = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectDocumentTextResponse' {Maybe [Block]
blocks :: Maybe [Block]
$sel:blocks:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe [Block]
blocks} -> Maybe [Block]
blocks) (\s :: DetectDocumentTextResponse
s@DetectDocumentTextResponse' {} Maybe [Block]
a -> DetectDocumentTextResponse
s {$sel:blocks:DetectDocumentTextResponse' :: Maybe [Block]
blocks = Maybe [Block]
a} :: DetectDocumentTextResponse) 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 s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

detectDocumentTextResponse_detectDocumentTextModelVersion :: Lens.Lens' DetectDocumentTextResponse (Prelude.Maybe Prelude.Text)
detectDocumentTextResponse_detectDocumentTextModelVersion :: Lens' DetectDocumentTextResponse (Maybe Text)
detectDocumentTextResponse_detectDocumentTextModelVersion = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectDocumentTextResponse' {Maybe Text
detectDocumentTextModelVersion :: Maybe Text
$sel:detectDocumentTextModelVersion:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe Text
detectDocumentTextModelVersion} -> Maybe Text
detectDocumentTextModelVersion) (\s :: DetectDocumentTextResponse
s@DetectDocumentTextResponse' {} Maybe Text
a -> DetectDocumentTextResponse
s {$sel:detectDocumentTextModelVersion:DetectDocumentTextResponse' :: Maybe Text
detectDocumentTextModelVersion = Maybe Text
a} :: DetectDocumentTextResponse)

-- | Metadata about the document. It contains the number of pages that are
-- detected in the document.
detectDocumentTextResponse_documentMetadata :: Lens.Lens' DetectDocumentTextResponse (Prelude.Maybe DocumentMetadata)
detectDocumentTextResponse_documentMetadata :: Lens' DetectDocumentTextResponse (Maybe DocumentMetadata)
detectDocumentTextResponse_documentMetadata = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectDocumentTextResponse' {Maybe DocumentMetadata
documentMetadata :: Maybe DocumentMetadata
$sel:documentMetadata:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe DocumentMetadata
documentMetadata} -> Maybe DocumentMetadata
documentMetadata) (\s :: DetectDocumentTextResponse
s@DetectDocumentTextResponse' {} Maybe DocumentMetadata
a -> DetectDocumentTextResponse
s {$sel:documentMetadata:DetectDocumentTextResponse' :: Maybe DocumentMetadata
documentMetadata = Maybe DocumentMetadata
a} :: DetectDocumentTextResponse)

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

instance Prelude.NFData DetectDocumentTextResponse where
  rnf :: DetectDocumentTextResponse -> ()
rnf DetectDocumentTextResponse' {Int
Maybe [Block]
Maybe Text
Maybe DocumentMetadata
httpStatus :: Int
documentMetadata :: Maybe DocumentMetadata
detectDocumentTextModelVersion :: Maybe Text
blocks :: Maybe [Block]
$sel:httpStatus:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Int
$sel:documentMetadata:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe DocumentMetadata
$sel:detectDocumentTextModelVersion:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe Text
$sel:blocks:DetectDocumentTextResponse' :: DetectDocumentTextResponse -> Maybe [Block]
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe [Block]
blocks
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
detectDocumentTextModelVersion
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe DocumentMetadata
documentMetadata
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus