{-# 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.UpdateUsage
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Grants a temporary extension to the remaining quota of a usage plan
-- associated with a specified API key.
module Amazonka.APIGateway.UpdateUsage
  ( -- * Creating a Request
    UpdateUsage (..),
    newUpdateUsage,

    -- * Request Lenses
    updateUsage_patchOperations,
    updateUsage_usagePlanId,
    updateUsage_keyId,

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

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

import Amazonka.APIGateway.Types
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

-- | The PATCH request to grant a temporary extension to the remaining quota
-- of a usage plan associated with a specified API key.
--
-- /See:/ 'newUpdateUsage' smart constructor.
data UpdateUsage = UpdateUsage'
  { -- | For more information about supported patch operations, see
    -- <https://docs.aws.amazon.com/apigateway/latest/api/patch-operations.html Patch Operations>.
    UpdateUsage -> Maybe [PatchOperation]
patchOperations :: Prelude.Maybe [PatchOperation],
    -- | The Id of the usage plan associated with the usage data.
    UpdateUsage -> Text
usagePlanId :: Prelude.Text,
    -- | The identifier of the API key associated with the usage plan in which a
    -- temporary extension is granted to the remaining quota.
    UpdateUsage -> Text
keyId :: Prelude.Text
  }
  deriving (UpdateUsage -> UpdateUsage -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UpdateUsage -> UpdateUsage -> Bool
$c/= :: UpdateUsage -> UpdateUsage -> Bool
== :: UpdateUsage -> UpdateUsage -> Bool
$c== :: UpdateUsage -> UpdateUsage -> Bool
Prelude.Eq, ReadPrec [UpdateUsage]
ReadPrec UpdateUsage
Int -> ReadS UpdateUsage
ReadS [UpdateUsage]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [UpdateUsage]
$creadListPrec :: ReadPrec [UpdateUsage]
readPrec :: ReadPrec UpdateUsage
$creadPrec :: ReadPrec UpdateUsage
readList :: ReadS [UpdateUsage]
$creadList :: ReadS [UpdateUsage]
readsPrec :: Int -> ReadS UpdateUsage
$creadsPrec :: Int -> ReadS UpdateUsage
Prelude.Read, Int -> UpdateUsage -> ShowS
[UpdateUsage] -> ShowS
UpdateUsage -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UpdateUsage] -> ShowS
$cshowList :: [UpdateUsage] -> ShowS
show :: UpdateUsage -> String
$cshow :: UpdateUsage -> String
showsPrec :: Int -> UpdateUsage -> ShowS
$cshowsPrec :: Int -> UpdateUsage -> ShowS
Prelude.Show, forall x. Rep UpdateUsage x -> UpdateUsage
forall x. UpdateUsage -> Rep UpdateUsage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UpdateUsage x -> UpdateUsage
$cfrom :: forall x. UpdateUsage -> Rep UpdateUsage x
Prelude.Generic)

-- |
-- Create a value of 'UpdateUsage' 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:
--
-- 'patchOperations', 'updateUsage_patchOperations' - For more information about supported patch operations, see
-- <https://docs.aws.amazon.com/apigateway/latest/api/patch-operations.html Patch Operations>.
--
-- 'usagePlanId', 'updateUsage_usagePlanId' - The Id of the usage plan associated with the usage data.
--
-- 'keyId', 'updateUsage_keyId' - The identifier of the API key associated with the usage plan in which a
-- temporary extension is granted to the remaining quota.
newUpdateUsage ::
  -- | 'usagePlanId'
  Prelude.Text ->
  -- | 'keyId'
  Prelude.Text ->
  UpdateUsage
newUpdateUsage :: Text -> Text -> UpdateUsage
newUpdateUsage Text
pUsagePlanId_ Text
pKeyId_ =
  UpdateUsage'
    { $sel:patchOperations:UpdateUsage' :: Maybe [PatchOperation]
patchOperations = forall a. Maybe a
Prelude.Nothing,
      $sel:usagePlanId:UpdateUsage' :: Text
usagePlanId = Text
pUsagePlanId_,
      $sel:keyId:UpdateUsage' :: Text
keyId = Text
pKeyId_
    }

-- | For more information about supported patch operations, see
-- <https://docs.aws.amazon.com/apigateway/latest/api/patch-operations.html Patch Operations>.
updateUsage_patchOperations :: Lens.Lens' UpdateUsage (Prelude.Maybe [PatchOperation])
updateUsage_patchOperations :: Lens' UpdateUsage (Maybe [PatchOperation])
updateUsage_patchOperations = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UpdateUsage' {Maybe [PatchOperation]
patchOperations :: Maybe [PatchOperation]
$sel:patchOperations:UpdateUsage' :: UpdateUsage -> Maybe [PatchOperation]
patchOperations} -> Maybe [PatchOperation]
patchOperations) (\s :: UpdateUsage
s@UpdateUsage' {} Maybe [PatchOperation]
a -> UpdateUsage
s {$sel:patchOperations:UpdateUsage' :: Maybe [PatchOperation]
patchOperations = Maybe [PatchOperation]
a} :: UpdateUsage) 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

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

-- | The identifier of the API key associated with the usage plan in which a
-- temporary extension is granted to the remaining quota.
updateUsage_keyId :: Lens.Lens' UpdateUsage Prelude.Text
updateUsage_keyId :: Lens' UpdateUsage Text
updateUsage_keyId = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UpdateUsage' {Text
keyId :: Text
$sel:keyId:UpdateUsage' :: UpdateUsage -> Text
keyId} -> Text
keyId) (\s :: UpdateUsage
s@UpdateUsage' {} Text
a -> UpdateUsage
s {$sel:keyId:UpdateUsage' :: Text
keyId = Text
a} :: UpdateUsage)

instance Core.AWSRequest UpdateUsage where
  type AWSResponse UpdateUsage = Usage
  request :: (Service -> Service) -> UpdateUsage -> Request UpdateUsage
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.patchJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy UpdateUsage
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse UpdateUsage)))
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 -> forall a. FromJSON a => Object -> Either String a
Data.eitherParseJSON Object
x)

instance Prelude.Hashable UpdateUsage where
  hashWithSalt :: Int -> UpdateUsage -> Int
hashWithSalt Int
_salt UpdateUsage' {Maybe [PatchOperation]
Text
keyId :: Text
usagePlanId :: Text
patchOperations :: Maybe [PatchOperation]
$sel:keyId:UpdateUsage' :: UpdateUsage -> Text
$sel:usagePlanId:UpdateUsage' :: UpdateUsage -> Text
$sel:patchOperations:UpdateUsage' :: UpdateUsage -> Maybe [PatchOperation]
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [PatchOperation]
patchOperations
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
usagePlanId
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
keyId

instance Prelude.NFData UpdateUsage where
  rnf :: UpdateUsage -> ()
rnf UpdateUsage' {Maybe [PatchOperation]
Text
keyId :: Text
usagePlanId :: Text
patchOperations :: Maybe [PatchOperation]
$sel:keyId:UpdateUsage' :: UpdateUsage -> Text
$sel:usagePlanId:UpdateUsage' :: UpdateUsage -> Text
$sel:patchOperations:UpdateUsage' :: UpdateUsage -> Maybe [PatchOperation]
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe [PatchOperation]
patchOperations
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
usagePlanId
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
keyId

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

instance Data.ToJSON UpdateUsage where
  toJSON :: UpdateUsage -> Value
toJSON UpdateUsage' {Maybe [PatchOperation]
Text
keyId :: Text
usagePlanId :: Text
patchOperations :: Maybe [PatchOperation]
$sel:keyId:UpdateUsage' :: UpdateUsage -> Text
$sel:usagePlanId:UpdateUsage' :: UpdateUsage -> Text
$sel:patchOperations:UpdateUsage' :: UpdateUsage -> Maybe [PatchOperation]
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"patchOperations" 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 [PatchOperation]
patchOperations
          ]
      )

instance Data.ToPath UpdateUsage where
  toPath :: UpdateUsage -> ByteString
toPath UpdateUsage' {Maybe [PatchOperation]
Text
keyId :: Text
usagePlanId :: Text
patchOperations :: Maybe [PatchOperation]
$sel:keyId:UpdateUsage' :: UpdateUsage -> Text
$sel:usagePlanId:UpdateUsage' :: UpdateUsage -> Text
$sel:patchOperations:UpdateUsage' :: UpdateUsage -> Maybe [PatchOperation]
..} =
    forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ ByteString
"/usageplans/",
        forall a. ToByteString a => a -> ByteString
Data.toBS Text
usagePlanId,
        ByteString
"/keys/",
        forall a. ToByteString a => a -> ByteString
Data.toBS Text
keyId,
        ByteString
"/usage"
      ]

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