{-# 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.APIGateway.GetUsage
-- 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)
--
-- Gets the usage data of a usage plan in a specified time interval.
--
-- This operation returns paginated results.
module Amazonka.APIGateway.GetUsage
  ( -- * Creating a Request
    GetUsage (..),
    newGetUsage,

    -- * Request Lenses
    getUsage_keyId,
    getUsage_limit,
    getUsage_position,
    getUsage_usagePlanId,
    getUsage_startDate,
    getUsage_endDate,

    -- * Destructuring the Response
    Usage (..),
    newUsage,

    -- * Response Lenses
    usage_usagePlanId,
    usage_endDate,
    usage_items,
    usage_startDate,
    usage_position,
  )
where

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

-- | The GET request to get the usage data of a usage plan in a specified
-- time interval.
--
-- /See:/ 'newGetUsage' smart constructor.
data GetUsage = GetUsage'
  { -- | The Id of the API key associated with the resultant usage data.
    GetUsage -> Maybe Text
keyId :: Prelude.Maybe Prelude.Text,
    -- | The maximum number of returned results per page. The default value is 25
    -- and the maximum value is 500.
    GetUsage -> Maybe Int
limit :: Prelude.Maybe Prelude.Int,
    -- | The current pagination position in the paged result set.
    GetUsage -> Maybe Text
position :: Prelude.Maybe Prelude.Text,
    -- | [Required] The Id of the usage plan associated with the usage data.
    GetUsage -> Text
usagePlanId :: Prelude.Text,
    -- | [Required] The starting date (e.g., 2016-01-01) of the usage data.
    GetUsage -> Text
startDate :: Prelude.Text,
    -- | [Required] The ending date (e.g., 2016-12-31) of the usage data.
    GetUsage -> Text
endDate :: Prelude.Text
  }
  deriving (GetUsage -> GetUsage -> Bool
(GetUsage -> GetUsage -> Bool)
-> (GetUsage -> GetUsage -> Bool) -> Eq GetUsage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GetUsage -> GetUsage -> Bool
$c/= :: GetUsage -> GetUsage -> Bool
== :: GetUsage -> GetUsage -> Bool
$c== :: GetUsage -> GetUsage -> Bool
Prelude.Eq, ReadPrec [GetUsage]
ReadPrec GetUsage
Int -> ReadS GetUsage
ReadS [GetUsage]
(Int -> ReadS GetUsage)
-> ReadS [GetUsage]
-> ReadPrec GetUsage
-> ReadPrec [GetUsage]
-> Read GetUsage
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GetUsage]
$creadListPrec :: ReadPrec [GetUsage]
readPrec :: ReadPrec GetUsage
$creadPrec :: ReadPrec GetUsage
readList :: ReadS [GetUsage]
$creadList :: ReadS [GetUsage]
readsPrec :: Int -> ReadS GetUsage
$creadsPrec :: Int -> ReadS GetUsage
Prelude.Read, Int -> GetUsage -> ShowS
[GetUsage] -> ShowS
GetUsage -> String
(Int -> GetUsage -> ShowS)
-> (GetUsage -> String) -> ([GetUsage] -> ShowS) -> Show GetUsage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GetUsage] -> ShowS
$cshowList :: [GetUsage] -> ShowS
show :: GetUsage -> String
$cshow :: GetUsage -> String
showsPrec :: Int -> GetUsage -> ShowS
$cshowsPrec :: Int -> GetUsage -> ShowS
Prelude.Show, (forall x. GetUsage -> Rep GetUsage x)
-> (forall x. Rep GetUsage x -> GetUsage) -> Generic GetUsage
forall x. Rep GetUsage x -> GetUsage
forall x. GetUsage -> Rep GetUsage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GetUsage x -> GetUsage
$cfrom :: forall x. GetUsage -> Rep GetUsage x
Prelude.Generic)

-- |
-- Create a value of 'GetUsage' 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:
--
-- 'keyId', 'getUsage_keyId' - The Id of the API key associated with the resultant usage data.
--
-- 'limit', 'getUsage_limit' - The maximum number of returned results per page. The default value is 25
-- and the maximum value is 500.
--
-- 'position', 'getUsage_position' - The current pagination position in the paged result set.
--
-- 'usagePlanId', 'getUsage_usagePlanId' - [Required] The Id of the usage plan associated with the usage data.
--
-- 'startDate', 'getUsage_startDate' - [Required] The starting date (e.g., 2016-01-01) of the usage data.
--
-- 'endDate', 'getUsage_endDate' - [Required] The ending date (e.g., 2016-12-31) of the usage data.
newGetUsage ::
  -- | 'usagePlanId'
  Prelude.Text ->
  -- | 'startDate'
  Prelude.Text ->
  -- | 'endDate'
  Prelude.Text ->
  GetUsage
newGetUsage :: Text -> Text -> Text -> GetUsage
newGetUsage Text
pUsagePlanId_ Text
pStartDate_ Text
pEndDate_ =
  GetUsage' :: Maybe Text
-> Maybe Int -> Maybe Text -> Text -> Text -> Text -> GetUsage
GetUsage'
    { $sel:keyId:GetUsage' :: Maybe Text
keyId = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:limit:GetUsage' :: Maybe Int
limit = Maybe Int
forall a. Maybe a
Prelude.Nothing,
      $sel:position:GetUsage' :: Maybe Text
position = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:usagePlanId:GetUsage' :: Text
usagePlanId = Text
pUsagePlanId_,
      $sel:startDate:GetUsage' :: Text
startDate = Text
pStartDate_,
      $sel:endDate:GetUsage' :: Text
endDate = Text
pEndDate_
    }

-- | The Id of the API key associated with the resultant usage data.
getUsage_keyId :: Lens.Lens' GetUsage (Prelude.Maybe Prelude.Text)
getUsage_keyId :: (Maybe Text -> f (Maybe Text)) -> GetUsage -> f GetUsage
getUsage_keyId = (GetUsage -> Maybe Text)
-> (GetUsage -> Maybe Text -> GetUsage)
-> Lens GetUsage GetUsage (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Maybe Text
keyId :: Maybe Text
$sel:keyId:GetUsage' :: GetUsage -> Maybe Text
keyId} -> Maybe Text
keyId) (\s :: GetUsage
s@GetUsage' {} Maybe Text
a -> GetUsage
s {$sel:keyId:GetUsage' :: Maybe Text
keyId = Maybe Text
a} :: GetUsage)

-- | The maximum number of returned results per page. The default value is 25
-- and the maximum value is 500.
getUsage_limit :: Lens.Lens' GetUsage (Prelude.Maybe Prelude.Int)
getUsage_limit :: (Maybe Int -> f (Maybe Int)) -> GetUsage -> f GetUsage
getUsage_limit = (GetUsage -> Maybe Int)
-> (GetUsage -> Maybe Int -> GetUsage)
-> Lens GetUsage GetUsage (Maybe Int) (Maybe Int)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Maybe Int
limit :: Maybe Int
$sel:limit:GetUsage' :: GetUsage -> Maybe Int
limit} -> Maybe Int
limit) (\s :: GetUsage
s@GetUsage' {} Maybe Int
a -> GetUsage
s {$sel:limit:GetUsage' :: Maybe Int
limit = Maybe Int
a} :: GetUsage)

-- | The current pagination position in the paged result set.
getUsage_position :: Lens.Lens' GetUsage (Prelude.Maybe Prelude.Text)
getUsage_position :: (Maybe Text -> f (Maybe Text)) -> GetUsage -> f GetUsage
getUsage_position = (GetUsage -> Maybe Text)
-> (GetUsage -> Maybe Text -> GetUsage)
-> Lens GetUsage GetUsage (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Maybe Text
position :: Maybe Text
$sel:position:GetUsage' :: GetUsage -> Maybe Text
position} -> Maybe Text
position) (\s :: GetUsage
s@GetUsage' {} Maybe Text
a -> GetUsage
s {$sel:position:GetUsage' :: Maybe Text
position = Maybe Text
a} :: GetUsage)

-- | [Required] The Id of the usage plan associated with the usage data.
getUsage_usagePlanId :: Lens.Lens' GetUsage Prelude.Text
getUsage_usagePlanId :: (Text -> f Text) -> GetUsage -> f GetUsage
getUsage_usagePlanId = (GetUsage -> Text)
-> (GetUsage -> Text -> GetUsage)
-> Lens GetUsage GetUsage Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Text
usagePlanId :: Text
$sel:usagePlanId:GetUsage' :: GetUsage -> Text
usagePlanId} -> Text
usagePlanId) (\s :: GetUsage
s@GetUsage' {} Text
a -> GetUsage
s {$sel:usagePlanId:GetUsage' :: Text
usagePlanId = Text
a} :: GetUsage)

-- | [Required] The starting date (e.g., 2016-01-01) of the usage data.
getUsage_startDate :: Lens.Lens' GetUsage Prelude.Text
getUsage_startDate :: (Text -> f Text) -> GetUsage -> f GetUsage
getUsage_startDate = (GetUsage -> Text)
-> (GetUsage -> Text -> GetUsage)
-> Lens GetUsage GetUsage Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Text
startDate :: Text
$sel:startDate:GetUsage' :: GetUsage -> Text
startDate} -> Text
startDate) (\s :: GetUsage
s@GetUsage' {} Text
a -> GetUsage
s {$sel:startDate:GetUsage' :: Text
startDate = Text
a} :: GetUsage)

-- | [Required] The ending date (e.g., 2016-12-31) of the usage data.
getUsage_endDate :: Lens.Lens' GetUsage Prelude.Text
getUsage_endDate :: (Text -> f Text) -> GetUsage -> f GetUsage
getUsage_endDate = (GetUsage -> Text)
-> (GetUsage -> Text -> GetUsage)
-> Lens GetUsage GetUsage Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GetUsage' {Text
endDate :: Text
$sel:endDate:GetUsage' :: GetUsage -> Text
endDate} -> Text
endDate) (\s :: GetUsage
s@GetUsage' {} Text
a -> GetUsage
s {$sel:endDate:GetUsage' :: Text
endDate = Text
a} :: GetUsage)

instance Core.AWSPager GetUsage where
  page :: GetUsage -> AWSResponse GetUsage -> Maybe GetUsage
page GetUsage
rq AWSResponse GetUsage
rs
    | Maybe Text -> Bool
forall a. AWSTruncated a => a -> Bool
Core.stop
        (AWSResponse GetUsage
Usage
rs Usage -> Getting (First Text) Usage Text -> Maybe Text
forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? (Maybe Text -> Const (First Text) (Maybe Text))
-> Usage -> Const (First Text) Usage
Lens' Usage (Maybe Text)
usage_position ((Maybe Text -> Const (First Text) (Maybe Text))
 -> Usage -> Const (First Text) Usage)
-> ((Text -> Const (First Text) Text)
    -> Maybe Text -> Const (First Text) (Maybe Text))
-> Getting (First Text) Usage Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (Text -> Const (First Text) Text)
-> Maybe Text -> Const (First Text) (Maybe Text)
forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just) =
      Maybe GetUsage
forall a. Maybe a
Prelude.Nothing
    | Maybe (HashMap Text [[Integer]]) -> Bool
forall a. AWSTruncated a => a -> Bool
Core.stop
        (AWSResponse GetUsage
Usage
rs Usage
-> Getting
     (First (HashMap Text [[Integer]])) Usage (HashMap Text [[Integer]])
-> Maybe (HashMap Text [[Integer]])
forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? (Maybe (HashMap Text [[Integer]])
 -> Const
      (First (HashMap Text [[Integer]]))
      (Maybe (HashMap Text [[Integer]])))
-> Usage -> Const (First (HashMap Text [[Integer]])) Usage
Lens' Usage (Maybe (HashMap Text [[Integer]]))
usage_items ((Maybe (HashMap Text [[Integer]])
  -> Const
       (First (HashMap Text [[Integer]]))
       (Maybe (HashMap Text [[Integer]])))
 -> Usage -> Const (First (HashMap Text [[Integer]])) Usage)
-> ((HashMap Text [[Integer]]
     -> Const
          (First (HashMap Text [[Integer]])) (HashMap Text [[Integer]]))
    -> Maybe (HashMap Text [[Integer]])
    -> Const
         (First (HashMap Text [[Integer]]))
         (Maybe (HashMap Text [[Integer]])))
-> Getting
     (First (HashMap Text [[Integer]])) Usage (HashMap Text [[Integer]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (HashMap Text [[Integer]]
 -> Const
      (First (HashMap Text [[Integer]])) (HashMap Text [[Integer]]))
-> Maybe (HashMap Text [[Integer]])
-> Const
     (First (HashMap Text [[Integer]]))
     (Maybe (HashMap Text [[Integer]]))
forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just) =
      Maybe GetUsage
forall a. Maybe a
Prelude.Nothing
    | Bool
Prelude.otherwise =
      GetUsage -> Maybe GetUsage
forall a. a -> Maybe a
Prelude.Just (GetUsage -> Maybe GetUsage) -> GetUsage -> Maybe GetUsage
forall a b. (a -> b) -> a -> b
Prelude.$
        GetUsage
rq
          GetUsage -> (GetUsage -> GetUsage) -> GetUsage
forall a b. a -> (a -> b) -> b
Prelude.& (Maybe Text -> Identity (Maybe Text))
-> GetUsage -> Identity GetUsage
Lens GetUsage GetUsage (Maybe Text) (Maybe Text)
getUsage_position
          ((Maybe Text -> Identity (Maybe Text))
 -> GetUsage -> Identity GetUsage)
-> Maybe Text -> GetUsage -> GetUsage
forall s t a b. ASetter s t a b -> b -> s -> t
Lens..~ AWSResponse GetUsage
Usage
rs Usage -> Getting (First Text) Usage Text -> Maybe Text
forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? (Maybe Text -> Const (First Text) (Maybe Text))
-> Usage -> Const (First Text) Usage
Lens' Usage (Maybe Text)
usage_position ((Maybe Text -> Const (First Text) (Maybe Text))
 -> Usage -> Const (First Text) Usage)
-> ((Text -> Const (First Text) Text)
    -> Maybe Text -> Const (First Text) (Maybe Text))
-> Getting (First Text) Usage Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (Text -> Const (First Text) Text)
-> Maybe Text -> Const (First Text) (Maybe Text)
forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just

instance Core.AWSRequest GetUsage where
  type AWSResponse GetUsage = Usage
  request :: GetUsage -> Request GetUsage
request = Service -> GetUsage -> Request GetUsage
forall a. ToRequest a => Service -> a -> Request a
Request.get Service
defaultService
  response :: Logger
-> Service
-> Proxy GetUsage
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GetUsage)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse GetUsage))
-> Logger
-> Service
-> Proxy GetUsage
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse GetUsage)))
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 -> Object -> Either String Usage
forall a. FromJSON a => Object -> Either String a
Core.eitherParseJSON Object
x)

instance Prelude.Hashable GetUsage

instance Prelude.NFData GetUsage

instance Core.ToHeaders GetUsage where
  toHeaders :: GetUsage -> ResponseHeaders
toHeaders =
    ResponseHeaders -> GetUsage -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"Accept"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# (ByteString
"application/json" :: Prelude.ByteString)
          ]
      )

instance Core.ToPath GetUsage where
  toPath :: GetUsage -> ByteString
toPath GetUsage' {Maybe Int
Maybe Text
Text
endDate :: Text
startDate :: Text
usagePlanId :: Text
position :: Maybe Text
limit :: Maybe Int
keyId :: Maybe Text
$sel:endDate:GetUsage' :: GetUsage -> Text
$sel:startDate:GetUsage' :: GetUsage -> Text
$sel:usagePlanId:GetUsage' :: GetUsage -> Text
$sel:position:GetUsage' :: GetUsage -> Maybe Text
$sel:limit:GetUsage' :: GetUsage -> Maybe Int
$sel:keyId:GetUsage' :: GetUsage -> Maybe Text
..} =
    [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ByteString
"/usageplans/", Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
usagePlanId, ByteString
"/usage"]

instance Core.ToQuery GetUsage where
  toQuery :: GetUsage -> QueryString
toQuery GetUsage' {Maybe Int
Maybe Text
Text
endDate :: Text
startDate :: Text
usagePlanId :: Text
position :: Maybe Text
limit :: Maybe Int
keyId :: Maybe Text
$sel:endDate:GetUsage' :: GetUsage -> Text
$sel:startDate:GetUsage' :: GetUsage -> Text
$sel:usagePlanId:GetUsage' :: GetUsage -> Text
$sel:position:GetUsage' :: GetUsage -> Maybe Text
$sel:limit:GetUsage' :: GetUsage -> Maybe Int
$sel:keyId:GetUsage' :: GetUsage -> Maybe Text
..} =
    [QueryString] -> QueryString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ ByteString
"keyId" ByteString -> Maybe Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Maybe Text
keyId,
        ByteString
"limit" ByteString -> Maybe Int -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Maybe Int
limit,
        ByteString
"position" ByteString -> Maybe Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Maybe Text
position,
        ByteString
"startDate" ByteString -> Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Text
startDate,
        ByteString
"endDate" ByteString -> Text -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Text
endDate
      ]