{-# 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-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)
--
-- 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_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 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'
  { -- | A list of update operations to be applied to the specified resource and
    -- in the order specified in this list.
    UpdateUsage -> Maybe [PatchOperation]
patchOperations :: Prelude.Maybe [PatchOperation],
    -- | [Required] The Id of the usage plan associated with the usage data.
    UpdateUsage -> Text
usagePlanId :: Prelude.Text,
    -- | [Required] 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
(UpdateUsage -> UpdateUsage -> Bool)
-> (UpdateUsage -> UpdateUsage -> Bool) -> Eq UpdateUsage
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]
(Int -> ReadS UpdateUsage)
-> ReadS [UpdateUsage]
-> ReadPrec UpdateUsage
-> ReadPrec [UpdateUsage]
-> Read 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
(Int -> UpdateUsage -> ShowS)
-> (UpdateUsage -> String)
-> ([UpdateUsage] -> ShowS)
-> Show UpdateUsage
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. UpdateUsage -> Rep UpdateUsage x)
-> (forall x. Rep UpdateUsage x -> UpdateUsage)
-> Generic UpdateUsage
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' - A list of update operations to be applied to the specified resource and
-- in the order specified in this list.
--
-- 'usagePlanId', 'updateUsage_usagePlanId' - [Required] The Id of the usage plan associated with the usage data.
--
-- 'keyId', 'updateUsage_keyId' - [Required] 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' :: Maybe [PatchOperation] -> Text -> Text -> UpdateUsage
UpdateUsage'
    { $sel:patchOperations:UpdateUsage' :: Maybe [PatchOperation]
patchOperations = Maybe [PatchOperation]
forall a. Maybe a
Prelude.Nothing,
      $sel:usagePlanId:UpdateUsage' :: Text
usagePlanId = Text
pUsagePlanId_,
      $sel:keyId:UpdateUsage' :: Text
keyId = Text
pKeyId_
    }

-- | A list of update operations to be applied to the specified resource and
-- in the order specified in this list.
updateUsage_patchOperations :: Lens.Lens' UpdateUsage (Prelude.Maybe [PatchOperation])
updateUsage_patchOperations :: (Maybe [PatchOperation] -> f (Maybe [PatchOperation]))
-> UpdateUsage -> f UpdateUsage
updateUsage_patchOperations = (UpdateUsage -> Maybe [PatchOperation])
-> (UpdateUsage -> Maybe [PatchOperation] -> UpdateUsage)
-> Lens
     UpdateUsage
     UpdateUsage
     (Maybe [PatchOperation])
     (Maybe [PatchOperation])
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) ((Maybe [PatchOperation] -> f (Maybe [PatchOperation]))
 -> UpdateUsage -> f UpdateUsage)
-> ((Maybe [PatchOperation] -> f (Maybe [PatchOperation]))
    -> Maybe [PatchOperation] -> f (Maybe [PatchOperation]))
-> (Maybe [PatchOperation] -> f (Maybe [PatchOperation]))
-> UpdateUsage
-> f UpdateUsage
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  [PatchOperation] [PatchOperation] [PatchOperation] [PatchOperation]
-> Iso
     (Maybe [PatchOperation])
     (Maybe [PatchOperation])
     (Maybe [PatchOperation])
     (Maybe [PatchOperation])
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 AnIso
  [PatchOperation] [PatchOperation] [PatchOperation] [PatchOperation]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | [Required] The Id of the usage plan associated with the usage data.
updateUsage_usagePlanId :: Lens.Lens' UpdateUsage Prelude.Text
updateUsage_usagePlanId :: (Text -> f Text) -> UpdateUsage -> f UpdateUsage
updateUsage_usagePlanId = (UpdateUsage -> Text)
-> (UpdateUsage -> Text -> UpdateUsage)
-> Lens UpdateUsage UpdateUsage Text Text
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)

-- | [Required] 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 :: (Text -> f Text) -> UpdateUsage -> f UpdateUsage
updateUsage_keyId = (UpdateUsage -> Text)
-> (UpdateUsage -> Text -> UpdateUsage)
-> Lens UpdateUsage UpdateUsage Text Text
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 :: UpdateUsage -> Request UpdateUsage
request = Service -> UpdateUsage -> Request UpdateUsage
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.patchJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy UpdateUsage
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse UpdateUsage)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse UpdateUsage))
-> Logger
-> Service
-> Proxy UpdateUsage
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse UpdateUsage)))
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 UpdateUsage

instance Prelude.NFData UpdateUsage

instance Core.ToHeaders UpdateUsage where
  toHeaders :: UpdateUsage -> ResponseHeaders
toHeaders =
    ResponseHeaders -> UpdateUsage -> 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.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
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"patchOperations" Text -> [PatchOperation] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              ([PatchOperation] -> Pair) -> Maybe [PatchOperation] -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [PatchOperation]
patchOperations
          ]
      )

instance Core.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]
..} =
    [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
"/keys/",
        Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
keyId,
        ByteString
"/usage"
      ]

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