{-# 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.IoT.GetCardinality
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Returns the approximate count of unique values that match the query.
--
-- Requires permission to access the
-- <https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsiot.html#awsiot-actions-as-permissions GetCardinality>
-- action.
module Amazonka.IoT.GetCardinality
  ( -- * Creating a Request
    GetCardinality (..),
    newGetCardinality,

    -- * Request Lenses
    getCardinality_queryVersion,
    getCardinality_aggregationField,
    getCardinality_indexName,
    getCardinality_queryString,

    -- * Destructuring the Response
    GetCardinalityResponse (..),
    newGetCardinalityResponse,

    -- * Response Lenses
    getCardinalityResponse_cardinality,
    getCardinalityResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.IoT.Types
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newGetCardinality' smart constructor.
data GetCardinality = GetCardinality'
  { -- | The query version.
    GetCardinality -> Maybe Text
queryVersion :: Prelude.Maybe Prelude.Text,
    -- | The field to aggregate.
    GetCardinality -> Maybe Text
aggregationField :: Prelude.Maybe Prelude.Text,
    -- | The name of the index to search.
    GetCardinality -> Maybe Text
indexName :: Prelude.Maybe Prelude.Text,
    -- | The search query string.
    GetCardinality -> Text
queryString :: Prelude.Text
  }
  deriving (GetCardinality -> GetCardinality -> Bool
(GetCardinality -> GetCardinality -> Bool)
-> (GetCardinality -> GetCardinality -> Bool) -> Eq GetCardinality
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetCardinality -> GetCardinality -> Bool
$c/= :: GetCardinality -> GetCardinality -> Bool
== :: GetCardinality -> GetCardinality -> Bool
$c== :: GetCardinality -> GetCardinality -> Bool
Prelude.Eq, ReadPrec [GetCardinality]
ReadPrec GetCardinality
Int -> ReadS GetCardinality
ReadS [GetCardinality]
(Int -> ReadS GetCardinality)
-> ReadS [GetCardinality]
-> ReadPrec GetCardinality
-> ReadPrec [GetCardinality]
-> Read GetCardinality
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetCardinality]
$creadListPrec :: ReadPrec [GetCardinality]
readPrec :: ReadPrec GetCardinality
$creadPrec :: ReadPrec GetCardinality
readList :: ReadS [GetCardinality]
$creadList :: ReadS [GetCardinality]
readsPrec :: Int -> ReadS GetCardinality
$creadsPrec :: Int -> ReadS GetCardinality
Prelude.Read, Int -> GetCardinality -> ShowS
[GetCardinality] -> ShowS
GetCardinality -> String
(Int -> GetCardinality -> ShowS)
-> (GetCardinality -> String)
-> ([GetCardinality] -> ShowS)
-> Show GetCardinality
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetCardinality] -> ShowS
$cshowList :: [GetCardinality] -> ShowS
show :: GetCardinality -> String
$cshow :: GetCardinality -> String
showsPrec :: Int -> GetCardinality -> ShowS
$cshowsPrec :: Int -> GetCardinality -> ShowS
Prelude.Show, (forall x. GetCardinality -> Rep GetCardinality x)
-> (forall x. Rep GetCardinality x -> GetCardinality)
-> Generic GetCardinality
forall x. Rep GetCardinality x -> GetCardinality
forall x. GetCardinality -> Rep GetCardinality x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetCardinality x -> GetCardinality
$cfrom :: forall x. GetCardinality -> Rep GetCardinality x
Prelude.Generic)

-- |
-- Create a value of 'GetCardinality' 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:
--
-- 'queryVersion', 'getCardinality_queryVersion' - The query version.
--
-- 'aggregationField', 'getCardinality_aggregationField' - The field to aggregate.
--
-- 'indexName', 'getCardinality_indexName' - The name of the index to search.
--
-- 'queryString', 'getCardinality_queryString' - The search query string.
newGetCardinality ::
  -- | 'queryString'
  Prelude.Text ->
  GetCardinality
newGetCardinality :: Text -> GetCardinality
newGetCardinality Text
pQueryString_ =
  GetCardinality' :: Maybe Text -> Maybe Text -> Maybe Text -> Text -> GetCardinality
GetCardinality'
    { $sel:queryVersion:GetCardinality' :: Maybe Text
queryVersion = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:aggregationField:GetCardinality' :: Maybe Text
aggregationField = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:indexName:GetCardinality' :: Maybe Text
indexName = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:queryString:GetCardinality' :: Text
queryString = Text
pQueryString_
    }

-- | The query version.
getCardinality_queryVersion :: Lens.Lens' GetCardinality (Prelude.Maybe Prelude.Text)
getCardinality_queryVersion :: (Maybe Text -> f (Maybe Text))
-> GetCardinality -> f GetCardinality
getCardinality_queryVersion = (GetCardinality -> Maybe Text)
-> (GetCardinality -> Maybe Text -> GetCardinality)
-> Lens GetCardinality GetCardinality (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetCardinality' {Maybe Text
queryVersion :: Maybe Text
$sel:queryVersion:GetCardinality' :: GetCardinality -> Maybe Text
queryVersion} -> Maybe Text
queryVersion) (\s :: GetCardinality
s@GetCardinality' {} Maybe Text
a -> GetCardinality
s {$sel:queryVersion:GetCardinality' :: Maybe Text
queryVersion = Maybe Text
a} :: GetCardinality)

-- | The field to aggregate.
getCardinality_aggregationField :: Lens.Lens' GetCardinality (Prelude.Maybe Prelude.Text)
getCardinality_aggregationField :: (Maybe Text -> f (Maybe Text))
-> GetCardinality -> f GetCardinality
getCardinality_aggregationField = (GetCardinality -> Maybe Text)
-> (GetCardinality -> Maybe Text -> GetCardinality)
-> Lens GetCardinality GetCardinality (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetCardinality' {Maybe Text
aggregationField :: Maybe Text
$sel:aggregationField:GetCardinality' :: GetCardinality -> Maybe Text
aggregationField} -> Maybe Text
aggregationField) (\s :: GetCardinality
s@GetCardinality' {} Maybe Text
a -> GetCardinality
s {$sel:aggregationField:GetCardinality' :: Maybe Text
aggregationField = Maybe Text
a} :: GetCardinality)

-- | The name of the index to search.
getCardinality_indexName :: Lens.Lens' GetCardinality (Prelude.Maybe Prelude.Text)
getCardinality_indexName :: (Maybe Text -> f (Maybe Text))
-> GetCardinality -> f GetCardinality
getCardinality_indexName = (GetCardinality -> Maybe Text)
-> (GetCardinality -> Maybe Text -> GetCardinality)
-> Lens GetCardinality GetCardinality (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetCardinality' {Maybe Text
indexName :: Maybe Text
$sel:indexName:GetCardinality' :: GetCardinality -> Maybe Text
indexName} -> Maybe Text
indexName) (\s :: GetCardinality
s@GetCardinality' {} Maybe Text
a -> GetCardinality
s {$sel:indexName:GetCardinality' :: Maybe Text
indexName = Maybe Text
a} :: GetCardinality)

-- | The search query string.
getCardinality_queryString :: Lens.Lens' GetCardinality Prelude.Text
getCardinality_queryString :: (Text -> f Text) -> GetCardinality -> f GetCardinality
getCardinality_queryString = (GetCardinality -> Text)
-> (GetCardinality -> Text -> GetCardinality)
-> Lens GetCardinality GetCardinality Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetCardinality' {Text
queryString :: Text
$sel:queryString:GetCardinality' :: GetCardinality -> Text
queryString} -> Text
queryString) (\s :: GetCardinality
s@GetCardinality' {} Text
a -> GetCardinality
s {$sel:queryString:GetCardinality' :: Text
queryString = Text
a} :: GetCardinality)

instance Core.AWSRequest GetCardinality where
  type
    AWSResponse GetCardinality =
      GetCardinalityResponse
  request :: GetCardinality -> Request GetCardinality
request = Service -> GetCardinality -> Request GetCardinality
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy GetCardinality
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GetCardinality)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse GetCardinality))
-> Logger
-> Service
-> Proxy GetCardinality
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GetCardinality)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe Int -> Int -> GetCardinalityResponse
GetCardinalityResponse'
            (Maybe Int -> Int -> GetCardinalityResponse)
-> Either String (Maybe Int)
-> Either String (Int -> GetCardinalityResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe Int)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"cardinality")
            Either String (Int -> GetCardinalityResponse)
-> Either String Int -> Either String GetCardinalityResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable GetCardinality

instance Prelude.NFData GetCardinality

instance Core.ToHeaders GetCardinality where
  toHeaders :: GetCardinality -> ResponseHeaders
toHeaders = ResponseHeaders -> GetCardinality -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const ResponseHeaders
forall a. Monoid a => a
Prelude.mempty

instance Core.ToJSON GetCardinality where
  toJSON :: GetCardinality -> Value
toJSON GetCardinality' {Maybe Text
Text
queryString :: Text
indexName :: Maybe Text
aggregationField :: Maybe Text
queryVersion :: Maybe Text
$sel:queryString:GetCardinality' :: GetCardinality -> Text
$sel:indexName:GetCardinality' :: GetCardinality -> Maybe Text
$sel:aggregationField:GetCardinality' :: GetCardinality -> Maybe Text
$sel:queryVersion:GetCardinality' :: GetCardinality -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"queryVersion" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
queryVersion,
            (Text
"aggregationField" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
aggregationField,
            (Text
"indexName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
indexName,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"queryString" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
queryString)
          ]
      )

instance Core.ToPath GetCardinality where
  toPath :: GetCardinality -> ByteString
toPath = ByteString -> GetCardinality -> ByteString
forall a b. a -> b -> a
Prelude.const ByteString
"/indices/cardinality"

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

-- | /See:/ 'newGetCardinalityResponse' smart constructor.
data GetCardinalityResponse = GetCardinalityResponse'
  { -- | The approximate count of unique values that match the query.
    GetCardinalityResponse -> Maybe Int
cardinality :: Prelude.Maybe Prelude.Int,
    -- | The response's http status code.
    GetCardinalityResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (GetCardinalityResponse -> GetCardinalityResponse -> Bool
(GetCardinalityResponse -> GetCardinalityResponse -> Bool)
-> (GetCardinalityResponse -> GetCardinalityResponse -> Bool)
-> Eq GetCardinalityResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetCardinalityResponse -> GetCardinalityResponse -> Bool
$c/= :: GetCardinalityResponse -> GetCardinalityResponse -> Bool
== :: GetCardinalityResponse -> GetCardinalityResponse -> Bool
$c== :: GetCardinalityResponse -> GetCardinalityResponse -> Bool
Prelude.Eq, ReadPrec [GetCardinalityResponse]
ReadPrec GetCardinalityResponse
Int -> ReadS GetCardinalityResponse
ReadS [GetCardinalityResponse]
(Int -> ReadS GetCardinalityResponse)
-> ReadS [GetCardinalityResponse]
-> ReadPrec GetCardinalityResponse
-> ReadPrec [GetCardinalityResponse]
-> Read GetCardinalityResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetCardinalityResponse]
$creadListPrec :: ReadPrec [GetCardinalityResponse]
readPrec :: ReadPrec GetCardinalityResponse
$creadPrec :: ReadPrec GetCardinalityResponse
readList :: ReadS [GetCardinalityResponse]
$creadList :: ReadS [GetCardinalityResponse]
readsPrec :: Int -> ReadS GetCardinalityResponse
$creadsPrec :: Int -> ReadS GetCardinalityResponse
Prelude.Read, Int -> GetCardinalityResponse -> ShowS
[GetCardinalityResponse] -> ShowS
GetCardinalityResponse -> String
(Int -> GetCardinalityResponse -> ShowS)
-> (GetCardinalityResponse -> String)
-> ([GetCardinalityResponse] -> ShowS)
-> Show GetCardinalityResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetCardinalityResponse] -> ShowS
$cshowList :: [GetCardinalityResponse] -> ShowS
show :: GetCardinalityResponse -> String
$cshow :: GetCardinalityResponse -> String
showsPrec :: Int -> GetCardinalityResponse -> ShowS
$cshowsPrec :: Int -> GetCardinalityResponse -> ShowS
Prelude.Show, (forall x. GetCardinalityResponse -> Rep GetCardinalityResponse x)
-> (forall x.
    Rep GetCardinalityResponse x -> GetCardinalityResponse)
-> Generic GetCardinalityResponse
forall x. Rep GetCardinalityResponse x -> GetCardinalityResponse
forall x. GetCardinalityResponse -> Rep GetCardinalityResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetCardinalityResponse x -> GetCardinalityResponse
$cfrom :: forall x. GetCardinalityResponse -> Rep GetCardinalityResponse x
Prelude.Generic)

-- |
-- Create a value of 'GetCardinalityResponse' 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:
--
-- 'cardinality', 'getCardinalityResponse_cardinality' - The approximate count of unique values that match the query.
--
-- 'httpStatus', 'getCardinalityResponse_httpStatus' - The response's http status code.
newGetCardinalityResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  GetCardinalityResponse
newGetCardinalityResponse :: Int -> GetCardinalityResponse
newGetCardinalityResponse Int
pHttpStatus_ =
  GetCardinalityResponse' :: Maybe Int -> Int -> GetCardinalityResponse
GetCardinalityResponse'
    { $sel:cardinality:GetCardinalityResponse' :: Maybe Int
cardinality =
        Maybe Int
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:GetCardinalityResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The approximate count of unique values that match the query.
getCardinalityResponse_cardinality :: Lens.Lens' GetCardinalityResponse (Prelude.Maybe Prelude.Int)
getCardinalityResponse_cardinality :: (Maybe Int -> f (Maybe Int))
-> GetCardinalityResponse -> f GetCardinalityResponse
getCardinalityResponse_cardinality = (GetCardinalityResponse -> Maybe Int)
-> (GetCardinalityResponse -> Maybe Int -> GetCardinalityResponse)
-> Lens
     GetCardinalityResponse
     GetCardinalityResponse
     (Maybe Int)
     (Maybe Int)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetCardinalityResponse' {Maybe Int
cardinality :: Maybe Int
$sel:cardinality:GetCardinalityResponse' :: GetCardinalityResponse -> Maybe Int
cardinality} -> Maybe Int
cardinality) (\s :: GetCardinalityResponse
s@GetCardinalityResponse' {} Maybe Int
a -> GetCardinalityResponse
s {$sel:cardinality:GetCardinalityResponse' :: Maybe Int
cardinality = Maybe Int
a} :: GetCardinalityResponse)

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

instance Prelude.NFData GetCardinalityResponse