{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.MarketplaceMetering.Types.UsageRecord
-- 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)
module Amazonka.MarketplaceMetering.Types.UsageRecord where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import Amazonka.MarketplaceMetering.Types.UsageAllocation
import qualified Amazonka.Prelude as Prelude

-- | A UsageRecord indicates a quantity of usage for a given product,
-- customer, dimension and time.
--
-- Multiple requests with the same UsageRecords as input will be
-- deduplicated to prevent double charges.
--
-- /See:/ 'newUsageRecord' smart constructor.
data UsageRecord = UsageRecord'
  { -- | The quantity of usage consumed by the customer for the given dimension
    -- and time. Defaults to @0@ if not specified.
    UsageRecord -> Maybe Natural
quantity :: Prelude.Maybe Prelude.Natural,
    -- | The set of UsageAllocations to submit. The sum of all UsageAllocation
    -- quantities must equal the Quantity of the UsageRecord.
    UsageRecord -> Maybe (NonEmpty UsageAllocation)
usageAllocations :: Prelude.Maybe (Prelude.NonEmpty UsageAllocation),
    -- | Timestamp, in UTC, for which the usage is being reported.
    --
    -- Your application can meter usage for up to one hour in the past. Make
    -- sure the timestamp value is not before the start of the software usage.
    UsageRecord -> POSIX
timestamp :: Core.POSIX,
    -- | The CustomerIdentifier is obtained through the ResolveCustomer operation
    -- and represents an individual buyer in your application.
    UsageRecord -> Text
customerIdentifier :: Prelude.Text,
    -- | During the process of registering a product on AWS Marketplace, up to
    -- eight dimensions are specified. These represent different units of value
    -- in your application.
    UsageRecord -> Text
dimension :: Prelude.Text
  }
  deriving (UsageRecord -> UsageRecord -> Bool
(UsageRecord -> UsageRecord -> Bool)
-> (UsageRecord -> UsageRecord -> Bool) -> Eq UsageRecord
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UsageRecord -> UsageRecord -> Bool
$c/= :: UsageRecord -> UsageRecord -> Bool
== :: UsageRecord -> UsageRecord -> Bool
$c== :: UsageRecord -> UsageRecord -> Bool
Prelude.Eq, ReadPrec [UsageRecord]
ReadPrec UsageRecord
Int -> ReadS UsageRecord
ReadS [UsageRecord]
(Int -> ReadS UsageRecord)
-> ReadS [UsageRecord]
-> ReadPrec UsageRecord
-> ReadPrec [UsageRecord]
-> Read UsageRecord
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [UsageRecord]
$creadListPrec :: ReadPrec [UsageRecord]
readPrec :: ReadPrec UsageRecord
$creadPrec :: ReadPrec UsageRecord
readList :: ReadS [UsageRecord]
$creadList :: ReadS [UsageRecord]
readsPrec :: Int -> ReadS UsageRecord
$creadsPrec :: Int -> ReadS UsageRecord
Prelude.Read, Int -> UsageRecord -> ShowS
[UsageRecord] -> ShowS
UsageRecord -> String
(Int -> UsageRecord -> ShowS)
-> (UsageRecord -> String)
-> ([UsageRecord] -> ShowS)
-> Show UsageRecord
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UsageRecord] -> ShowS
$cshowList :: [UsageRecord] -> ShowS
show :: UsageRecord -> String
$cshow :: UsageRecord -> String
showsPrec :: Int -> UsageRecord -> ShowS
$cshowsPrec :: Int -> UsageRecord -> ShowS
Prelude.Show, (forall x. UsageRecord -> Rep UsageRecord x)
-> (forall x. Rep UsageRecord x -> UsageRecord)
-> Generic UsageRecord
forall x. Rep UsageRecord x -> UsageRecord
forall x. UsageRecord -> Rep UsageRecord x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep UsageRecord x -> UsageRecord
$cfrom :: forall x. UsageRecord -> Rep UsageRecord x
Prelude.Generic)

-- |
-- Create a value of 'UsageRecord' 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:
--
-- 'quantity', 'usageRecord_quantity' - The quantity of usage consumed by the customer for the given dimension
-- and time. Defaults to @0@ if not specified.
--
-- 'usageAllocations', 'usageRecord_usageAllocations' - The set of UsageAllocations to submit. The sum of all UsageAllocation
-- quantities must equal the Quantity of the UsageRecord.
--
-- 'timestamp', 'usageRecord_timestamp' - Timestamp, in UTC, for which the usage is being reported.
--
-- Your application can meter usage for up to one hour in the past. Make
-- sure the timestamp value is not before the start of the software usage.
--
-- 'customerIdentifier', 'usageRecord_customerIdentifier' - The CustomerIdentifier is obtained through the ResolveCustomer operation
-- and represents an individual buyer in your application.
--
-- 'dimension', 'usageRecord_dimension' - During the process of registering a product on AWS Marketplace, up to
-- eight dimensions are specified. These represent different units of value
-- in your application.
newUsageRecord ::
  -- | 'timestamp'
  Prelude.UTCTime ->
  -- | 'customerIdentifier'
  Prelude.Text ->
  -- | 'dimension'
  Prelude.Text ->
  UsageRecord
newUsageRecord :: UTCTime -> Text -> Text -> UsageRecord
newUsageRecord
  UTCTime
pTimestamp_
  Text
pCustomerIdentifier_
  Text
pDimension_ =
    UsageRecord' :: Maybe Natural
-> Maybe (NonEmpty UsageAllocation)
-> POSIX
-> Text
-> Text
-> UsageRecord
UsageRecord'
      { $sel:quantity:UsageRecord' :: Maybe Natural
quantity = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
        $sel:usageAllocations:UsageRecord' :: Maybe (NonEmpty UsageAllocation)
usageAllocations = Maybe (NonEmpty UsageAllocation)
forall a. Maybe a
Prelude.Nothing,
        $sel:timestamp:UsageRecord' :: POSIX
timestamp = Tagged UTCTime (Identity UTCTime) -> Tagged POSIX (Identity POSIX)
forall (a :: Format). Iso' (Time a) UTCTime
Core._Time (Tagged UTCTime (Identity UTCTime)
 -> Tagged POSIX (Identity POSIX))
-> UTCTime -> POSIX
forall t b. AReview t b -> b -> t
Lens.# UTCTime
pTimestamp_,
        $sel:customerIdentifier:UsageRecord' :: Text
customerIdentifier = Text
pCustomerIdentifier_,
        $sel:dimension:UsageRecord' :: Text
dimension = Text
pDimension_
      }

-- | The quantity of usage consumed by the customer for the given dimension
-- and time. Defaults to @0@ if not specified.
usageRecord_quantity :: Lens.Lens' UsageRecord (Prelude.Maybe Prelude.Natural)
usageRecord_quantity :: (Maybe Natural -> f (Maybe Natural))
-> UsageRecord -> f UsageRecord
usageRecord_quantity = (UsageRecord -> Maybe Natural)
-> (UsageRecord -> Maybe Natural -> UsageRecord)
-> Lens UsageRecord UsageRecord (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Maybe Natural
quantity :: Maybe Natural
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
quantity} -> Maybe Natural
quantity) (\s :: UsageRecord
s@UsageRecord' {} Maybe Natural
a -> UsageRecord
s {$sel:quantity:UsageRecord' :: Maybe Natural
quantity = Maybe Natural
a} :: UsageRecord)

-- | The set of UsageAllocations to submit. The sum of all UsageAllocation
-- quantities must equal the Quantity of the UsageRecord.
usageRecord_usageAllocations :: Lens.Lens' UsageRecord (Prelude.Maybe (Prelude.NonEmpty UsageAllocation))
usageRecord_usageAllocations :: (Maybe (NonEmpty UsageAllocation)
 -> f (Maybe (NonEmpty UsageAllocation)))
-> UsageRecord -> f UsageRecord
usageRecord_usageAllocations = (UsageRecord -> Maybe (NonEmpty UsageAllocation))
-> (UsageRecord -> Maybe (NonEmpty UsageAllocation) -> UsageRecord)
-> Lens
     UsageRecord
     UsageRecord
     (Maybe (NonEmpty UsageAllocation))
     (Maybe (NonEmpty UsageAllocation))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Maybe (NonEmpty UsageAllocation)
usageAllocations :: Maybe (NonEmpty UsageAllocation)
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
usageAllocations} -> Maybe (NonEmpty UsageAllocation)
usageAllocations) (\s :: UsageRecord
s@UsageRecord' {} Maybe (NonEmpty UsageAllocation)
a -> UsageRecord
s {$sel:usageAllocations:UsageRecord' :: Maybe (NonEmpty UsageAllocation)
usageAllocations = Maybe (NonEmpty UsageAllocation)
a} :: UsageRecord) ((Maybe (NonEmpty UsageAllocation)
  -> f (Maybe (NonEmpty UsageAllocation)))
 -> UsageRecord -> f UsageRecord)
-> ((Maybe (NonEmpty UsageAllocation)
     -> f (Maybe (NonEmpty UsageAllocation)))
    -> Maybe (NonEmpty UsageAllocation)
    -> f (Maybe (NonEmpty UsageAllocation)))
-> (Maybe (NonEmpty UsageAllocation)
    -> f (Maybe (NonEmpty UsageAllocation)))
-> UsageRecord
-> f UsageRecord
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
-> Iso
     (Maybe (NonEmpty UsageAllocation))
     (Maybe (NonEmpty UsageAllocation))
     (Maybe (NonEmpty UsageAllocation))
     (Maybe (NonEmpty UsageAllocation))
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
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
  (NonEmpty UsageAllocation)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Timestamp, in UTC, for which the usage is being reported.
--
-- Your application can meter usage for up to one hour in the past. Make
-- sure the timestamp value is not before the start of the software usage.
usageRecord_timestamp :: Lens.Lens' UsageRecord Prelude.UTCTime
usageRecord_timestamp :: (UTCTime -> f UTCTime) -> UsageRecord -> f UsageRecord
usageRecord_timestamp = (UsageRecord -> POSIX)
-> (UsageRecord -> POSIX -> UsageRecord)
-> Lens UsageRecord UsageRecord POSIX POSIX
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {POSIX
timestamp :: POSIX
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
timestamp} -> POSIX
timestamp) (\s :: UsageRecord
s@UsageRecord' {} POSIX
a -> UsageRecord
s {$sel:timestamp:UsageRecord' :: POSIX
timestamp = POSIX
a} :: UsageRecord) ((POSIX -> f POSIX) -> UsageRecord -> f UsageRecord)
-> ((UTCTime -> f UTCTime) -> POSIX -> f POSIX)
-> (UTCTime -> f UTCTime)
-> UsageRecord
-> f UsageRecord
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (UTCTime -> f UTCTime) -> POSIX -> f POSIX
forall (a :: Format). Iso' (Time a) UTCTime
Core._Time

-- | The CustomerIdentifier is obtained through the ResolveCustomer operation
-- and represents an individual buyer in your application.
usageRecord_customerIdentifier :: Lens.Lens' UsageRecord Prelude.Text
usageRecord_customerIdentifier :: (Text -> f Text) -> UsageRecord -> f UsageRecord
usageRecord_customerIdentifier = (UsageRecord -> Text)
-> (UsageRecord -> Text -> UsageRecord)
-> Lens UsageRecord UsageRecord Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Text
customerIdentifier :: Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
customerIdentifier} -> Text
customerIdentifier) (\s :: UsageRecord
s@UsageRecord' {} Text
a -> UsageRecord
s {$sel:customerIdentifier:UsageRecord' :: Text
customerIdentifier = Text
a} :: UsageRecord)

-- | During the process of registering a product on AWS Marketplace, up to
-- eight dimensions are specified. These represent different units of value
-- in your application.
usageRecord_dimension :: Lens.Lens' UsageRecord Prelude.Text
usageRecord_dimension :: (Text -> f Text) -> UsageRecord -> f UsageRecord
usageRecord_dimension = (UsageRecord -> Text)
-> (UsageRecord -> Text -> UsageRecord)
-> Lens UsageRecord UsageRecord Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\UsageRecord' {Text
dimension :: Text
$sel:dimension:UsageRecord' :: UsageRecord -> Text
dimension} -> Text
dimension) (\s :: UsageRecord
s@UsageRecord' {} Text
a -> UsageRecord
s {$sel:dimension:UsageRecord' :: Text
dimension = Text
a} :: UsageRecord)

instance Core.FromJSON UsageRecord where
  parseJSON :: Value -> Parser UsageRecord
parseJSON =
    String
-> (Object -> Parser UsageRecord) -> Value -> Parser UsageRecord
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"UsageRecord"
      ( \Object
x ->
          Maybe Natural
-> Maybe (NonEmpty UsageAllocation)
-> POSIX
-> Text
-> Text
-> UsageRecord
UsageRecord'
            (Maybe Natural
 -> Maybe (NonEmpty UsageAllocation)
 -> POSIX
 -> Text
 -> Text
 -> UsageRecord)
-> Parser (Maybe Natural)
-> Parser
     (Maybe (NonEmpty UsageAllocation)
      -> POSIX -> Text -> Text -> UsageRecord)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Natural)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Quantity")
            Parser
  (Maybe (NonEmpty UsageAllocation)
   -> POSIX -> Text -> Text -> UsageRecord)
-> Parser (Maybe (NonEmpty UsageAllocation))
-> Parser (POSIX -> Text -> Text -> UsageRecord)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe (NonEmpty UsageAllocation))
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"UsageAllocations")
            Parser (POSIX -> Text -> Text -> UsageRecord)
-> Parser POSIX -> Parser (Text -> Text -> UsageRecord)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser POSIX
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"Timestamp")
            Parser (Text -> Text -> UsageRecord)
-> Parser Text -> Parser (Text -> UsageRecord)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"CustomerIdentifier")
            Parser (Text -> UsageRecord) -> Parser Text -> Parser UsageRecord
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"Dimension")
      )

instance Prelude.Hashable UsageRecord

instance Prelude.NFData UsageRecord

instance Core.ToJSON UsageRecord where
  toJSON :: UsageRecord -> Value
toJSON UsageRecord' {Maybe Natural
Maybe (NonEmpty UsageAllocation)
Text
POSIX
dimension :: Text
customerIdentifier :: Text
timestamp :: POSIX
usageAllocations :: Maybe (NonEmpty UsageAllocation)
quantity :: Maybe Natural
$sel:dimension:UsageRecord' :: UsageRecord -> Text
$sel:customerIdentifier:UsageRecord' :: UsageRecord -> Text
$sel:timestamp:UsageRecord' :: UsageRecord -> POSIX
$sel:usageAllocations:UsageRecord' :: UsageRecord -> Maybe (NonEmpty UsageAllocation)
$sel:quantity:UsageRecord' :: UsageRecord -> Maybe Natural
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"Quantity" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
quantity,
            (Text
"UsageAllocations" Text -> NonEmpty UsageAllocation -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (NonEmpty UsageAllocation -> Pair)
-> Maybe (NonEmpty UsageAllocation) -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty UsageAllocation)
usageAllocations,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Timestamp" Text -> POSIX -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= POSIX
timestamp),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"CustomerIdentifier" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
customerIdentifier),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Dimension" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
dimension)
          ]
      )