{-# 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.LookoutVision.DetectAnomalies
-- 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 anomalies in an image that you supply.
--
-- The response from @DetectAnomalies@ includes a boolean prediction that
-- the image contains one or more anomalies and a confidence value for the
-- prediction. If the model is an image segmentation model, the response
-- also includes segmentation information for each type of anomaly found in
-- the image.
--
-- Before calling @DetectAnomalies@, you must first start your model with
-- the StartModel operation. You are charged for the amount of time, in
-- minutes, that a model runs and for the number of anomaly detection units
-- that your model uses. If you are not using a model, use the StopModel
-- operation to stop your model.
--
-- For more information, see /Detecting anomalies in an image/ in the
-- Amazon Lookout for Vision developer guide.
--
-- This operation requires permissions to perform the
-- @lookoutvision:DetectAnomalies@ operation.
module Amazonka.LookoutVision.DetectAnomalies
  ( -- * Creating a Request
    DetectAnomalies (..),
    newDetectAnomalies,

    -- * Request Lenses
    detectAnomalies_projectName,
    detectAnomalies_modelVersion,
    detectAnomalies_contentType,
    detectAnomalies_body,

    -- * Destructuring the Response
    DetectAnomaliesResponse (..),
    newDetectAnomaliesResponse,

    -- * Response Lenses
    detectAnomaliesResponse_detectAnomalyResult,
    detectAnomaliesResponse_httpStatus,
  )
where

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

-- | /See:/ 'newDetectAnomalies' smart constructor.
data DetectAnomalies = DetectAnomalies'
  { -- | The name of the project that contains the model version that you want to
    -- use.
    DetectAnomalies -> Text
projectName :: Prelude.Text,
    -- | The version of the model that you want to use.
    DetectAnomalies -> Text
modelVersion :: Prelude.Text,
    -- | The type of the image passed in @Body@. Valid values are @image\/png@
    -- (PNG format images) and @image\/jpeg@ (JPG format images).
    DetectAnomalies -> Text
contentType :: Prelude.Text,
    -- | The unencrypted image bytes that you want to analyze.
    DetectAnomalies -> HashedBody
body :: Data.HashedBody
  }
  deriving (Int -> DetectAnomalies -> ShowS
[DetectAnomalies] -> ShowS
DetectAnomalies -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectAnomalies] -> ShowS
$cshowList :: [DetectAnomalies] -> ShowS
show :: DetectAnomalies -> String
$cshow :: DetectAnomalies -> String
showsPrec :: Int -> DetectAnomalies -> ShowS
$cshowsPrec :: Int -> DetectAnomalies -> ShowS
Prelude.Show, forall x. Rep DetectAnomalies x -> DetectAnomalies
forall x. DetectAnomalies -> Rep DetectAnomalies x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DetectAnomalies x -> DetectAnomalies
$cfrom :: forall x. DetectAnomalies -> Rep DetectAnomalies x
Prelude.Generic)

-- |
-- Create a value of 'DetectAnomalies' 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:
--
-- 'projectName', 'detectAnomalies_projectName' - The name of the project that contains the model version that you want to
-- use.
--
-- 'modelVersion', 'detectAnomalies_modelVersion' - The version of the model that you want to use.
--
-- 'contentType', 'detectAnomalies_contentType' - The type of the image passed in @Body@. Valid values are @image\/png@
-- (PNG format images) and @image\/jpeg@ (JPG format images).
--
-- 'body', 'detectAnomalies_body' - The unencrypted image bytes that you want to analyze.
newDetectAnomalies ::
  -- | 'projectName'
  Prelude.Text ->
  -- | 'modelVersion'
  Prelude.Text ->
  -- | 'contentType'
  Prelude.Text ->
  -- | 'body'
  Data.HashedBody ->
  DetectAnomalies
newDetectAnomalies :: Text -> Text -> Text -> HashedBody -> DetectAnomalies
newDetectAnomalies
  Text
pProjectName_
  Text
pModelVersion_
  Text
pContentType_
  HashedBody
pBody_ =
    DetectAnomalies'
      { $sel:projectName:DetectAnomalies' :: Text
projectName = Text
pProjectName_,
        $sel:modelVersion:DetectAnomalies' :: Text
modelVersion = Text
pModelVersion_,
        $sel:contentType:DetectAnomalies' :: Text
contentType = Text
pContentType_,
        $sel:body:DetectAnomalies' :: HashedBody
body = HashedBody
pBody_
      }

-- | The name of the project that contains the model version that you want to
-- use.
detectAnomalies_projectName :: Lens.Lens' DetectAnomalies Prelude.Text
detectAnomalies_projectName :: Lens' DetectAnomalies Text
detectAnomalies_projectName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectAnomalies' {Text
projectName :: Text
$sel:projectName:DetectAnomalies' :: DetectAnomalies -> Text
projectName} -> Text
projectName) (\s :: DetectAnomalies
s@DetectAnomalies' {} Text
a -> DetectAnomalies
s {$sel:projectName:DetectAnomalies' :: Text
projectName = Text
a} :: DetectAnomalies)

-- | The version of the model that you want to use.
detectAnomalies_modelVersion :: Lens.Lens' DetectAnomalies Prelude.Text
detectAnomalies_modelVersion :: Lens' DetectAnomalies Text
detectAnomalies_modelVersion = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectAnomalies' {Text
modelVersion :: Text
$sel:modelVersion:DetectAnomalies' :: DetectAnomalies -> Text
modelVersion} -> Text
modelVersion) (\s :: DetectAnomalies
s@DetectAnomalies' {} Text
a -> DetectAnomalies
s {$sel:modelVersion:DetectAnomalies' :: Text
modelVersion = Text
a} :: DetectAnomalies)

-- | The type of the image passed in @Body@. Valid values are @image\/png@
-- (PNG format images) and @image\/jpeg@ (JPG format images).
detectAnomalies_contentType :: Lens.Lens' DetectAnomalies Prelude.Text
detectAnomalies_contentType :: Lens' DetectAnomalies Text
detectAnomalies_contentType = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectAnomalies' {Text
contentType :: Text
$sel:contentType:DetectAnomalies' :: DetectAnomalies -> Text
contentType} -> Text
contentType) (\s :: DetectAnomalies
s@DetectAnomalies' {} Text
a -> DetectAnomalies
s {$sel:contentType:DetectAnomalies' :: Text
contentType = Text
a} :: DetectAnomalies)

-- | The unencrypted image bytes that you want to analyze.
detectAnomalies_body :: Lens.Lens' DetectAnomalies Data.HashedBody
detectAnomalies_body :: Lens' DetectAnomalies HashedBody
detectAnomalies_body = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectAnomalies' {HashedBody
body :: HashedBody
$sel:body:DetectAnomalies' :: DetectAnomalies -> HashedBody
body} -> HashedBody
body) (\s :: DetectAnomalies
s@DetectAnomalies' {} HashedBody
a -> DetectAnomalies
s {$sel:body:DetectAnomalies' :: HashedBody
body = HashedBody
a} :: DetectAnomalies)

instance Core.AWSRequest DetectAnomalies where
  type
    AWSResponse DetectAnomalies =
      DetectAnomaliesResponse
  request :: (Service -> Service) -> DetectAnomalies -> Request DetectAnomalies
request Service -> Service
overrides =
    forall a. (ToRequest a, ToBody a) => Service -> a -> Request a
Request.postBody (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy DetectAnomalies
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse DetectAnomalies)))
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 DetectAnomalyResult -> Int -> DetectAnomaliesResponse
DetectAnomaliesResponse'
            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
"DetectAnomalyResult")
            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 Data.ToBody DetectAnomalies where
  toBody :: DetectAnomalies -> RequestBody
toBody DetectAnomalies' {Text
HashedBody
body :: HashedBody
contentType :: Text
modelVersion :: Text
projectName :: Text
$sel:body:DetectAnomalies' :: DetectAnomalies -> HashedBody
$sel:contentType:DetectAnomalies' :: DetectAnomalies -> Text
$sel:modelVersion:DetectAnomalies' :: DetectAnomalies -> Text
$sel:projectName:DetectAnomalies' :: DetectAnomalies -> Text
..} = forall a. ToBody a => a -> RequestBody
Data.toBody HashedBody
body

instance Data.ToHeaders DetectAnomalies where
  toHeaders :: DetectAnomalies -> ResponseHeaders
toHeaders DetectAnomalies' {Text
HashedBody
body :: HashedBody
contentType :: Text
modelVersion :: Text
projectName :: Text
$sel:body:DetectAnomalies' :: DetectAnomalies -> HashedBody
$sel:contentType:DetectAnomalies' :: DetectAnomalies -> Text
$sel:modelVersion:DetectAnomalies' :: DetectAnomalies -> Text
$sel:projectName:DetectAnomalies' :: DetectAnomalies -> Text
..} =
    forall a. Monoid a => [a] -> a
Prelude.mconcat
      [HeaderName
"Content-Type" forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# Text
contentType]

instance Data.ToPath DetectAnomalies where
  toPath :: DetectAnomalies -> ByteString
toPath DetectAnomalies' {Text
HashedBody
body :: HashedBody
contentType :: Text
modelVersion :: Text
projectName :: Text
$sel:body:DetectAnomalies' :: DetectAnomalies -> HashedBody
$sel:contentType:DetectAnomalies' :: DetectAnomalies -> Text
$sel:modelVersion:DetectAnomalies' :: DetectAnomalies -> Text
$sel:projectName:DetectAnomalies' :: DetectAnomalies -> Text
..} =
    forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ ByteString
"/2020-11-20/projects/",
        forall a. ToByteString a => a -> ByteString
Data.toBS Text
projectName,
        ByteString
"/models/",
        forall a. ToByteString a => a -> ByteString
Data.toBS Text
modelVersion,
        ByteString
"/detect"
      ]

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

-- | /See:/ 'newDetectAnomaliesResponse' smart constructor.
data DetectAnomaliesResponse = DetectAnomaliesResponse'
  { -- | The results of the @DetectAnomalies@ operation.
    DetectAnomaliesResponse -> Maybe DetectAnomalyResult
detectAnomalyResult :: Prelude.Maybe DetectAnomalyResult,
    -- | The response's http status code.
    DetectAnomaliesResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (DetectAnomaliesResponse -> DetectAnomaliesResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DetectAnomaliesResponse -> DetectAnomaliesResponse -> Bool
$c/= :: DetectAnomaliesResponse -> DetectAnomaliesResponse -> Bool
== :: DetectAnomaliesResponse -> DetectAnomaliesResponse -> Bool
$c== :: DetectAnomaliesResponse -> DetectAnomaliesResponse -> Bool
Prelude.Eq, ReadPrec [DetectAnomaliesResponse]
ReadPrec DetectAnomaliesResponse
Int -> ReadS DetectAnomaliesResponse
ReadS [DetectAnomaliesResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [DetectAnomaliesResponse]
$creadListPrec :: ReadPrec [DetectAnomaliesResponse]
readPrec :: ReadPrec DetectAnomaliesResponse
$creadPrec :: ReadPrec DetectAnomaliesResponse
readList :: ReadS [DetectAnomaliesResponse]
$creadList :: ReadS [DetectAnomaliesResponse]
readsPrec :: Int -> ReadS DetectAnomaliesResponse
$creadsPrec :: Int -> ReadS DetectAnomaliesResponse
Prelude.Read, Int -> DetectAnomaliesResponse -> ShowS
[DetectAnomaliesResponse] -> ShowS
DetectAnomaliesResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectAnomaliesResponse] -> ShowS
$cshowList :: [DetectAnomaliesResponse] -> ShowS
show :: DetectAnomaliesResponse -> String
$cshow :: DetectAnomaliesResponse -> String
showsPrec :: Int -> DetectAnomaliesResponse -> ShowS
$cshowsPrec :: Int -> DetectAnomaliesResponse -> ShowS
Prelude.Show, forall x. Rep DetectAnomaliesResponse x -> DetectAnomaliesResponse
forall x. DetectAnomaliesResponse -> Rep DetectAnomaliesResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DetectAnomaliesResponse x -> DetectAnomaliesResponse
$cfrom :: forall x. DetectAnomaliesResponse -> Rep DetectAnomaliesResponse x
Prelude.Generic)

-- |
-- Create a value of 'DetectAnomaliesResponse' 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:
--
-- 'detectAnomalyResult', 'detectAnomaliesResponse_detectAnomalyResult' - The results of the @DetectAnomalies@ operation.
--
-- 'httpStatus', 'detectAnomaliesResponse_httpStatus' - The response's http status code.
newDetectAnomaliesResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  DetectAnomaliesResponse
newDetectAnomaliesResponse :: Int -> DetectAnomaliesResponse
newDetectAnomaliesResponse Int
pHttpStatus_ =
  DetectAnomaliesResponse'
    { $sel:detectAnomalyResult:DetectAnomaliesResponse' :: Maybe DetectAnomalyResult
detectAnomalyResult =
        forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:DetectAnomaliesResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The results of the @DetectAnomalies@ operation.
detectAnomaliesResponse_detectAnomalyResult :: Lens.Lens' DetectAnomaliesResponse (Prelude.Maybe DetectAnomalyResult)
detectAnomaliesResponse_detectAnomalyResult :: Lens' DetectAnomaliesResponse (Maybe DetectAnomalyResult)
detectAnomaliesResponse_detectAnomalyResult = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectAnomaliesResponse' {Maybe DetectAnomalyResult
detectAnomalyResult :: Maybe DetectAnomalyResult
$sel:detectAnomalyResult:DetectAnomaliesResponse' :: DetectAnomaliesResponse -> Maybe DetectAnomalyResult
detectAnomalyResult} -> Maybe DetectAnomalyResult
detectAnomalyResult) (\s :: DetectAnomaliesResponse
s@DetectAnomaliesResponse' {} Maybe DetectAnomalyResult
a -> DetectAnomaliesResponse
s {$sel:detectAnomalyResult:DetectAnomaliesResponse' :: Maybe DetectAnomalyResult
detectAnomalyResult = Maybe DetectAnomalyResult
a} :: DetectAnomaliesResponse)

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

instance Prelude.NFData DetectAnomaliesResponse where
  rnf :: DetectAnomaliesResponse -> ()
rnf DetectAnomaliesResponse' {Int
Maybe DetectAnomalyResult
httpStatus :: Int
detectAnomalyResult :: Maybe DetectAnomalyResult
$sel:httpStatus:DetectAnomaliesResponse' :: DetectAnomaliesResponse -> Int
$sel:detectAnomalyResult:DetectAnomaliesResponse' :: DetectAnomaliesResponse -> Maybe DetectAnomalyResult
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe DetectAnomalyResult
detectAnomalyResult
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus