{-# 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.DeviceFarm.PurchaseOffering
-- 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)
--
-- Immediately purchases offerings for an AWS account. Offerings renew with
-- the latest total purchased quantity for an offering, unless the renewal
-- was overridden. The API returns a @NotEligible@ error if the user is not
-- permitted to invoke the operation. If you must be able to invoke this
-- operation, contact
-- <mailto:aws-devicefarm-support@amazon.com aws-devicefarm-support\@amazon.com>.
module Amazonka.DeviceFarm.PurchaseOffering
  ( -- * Creating a Request
    PurchaseOffering (..),
    newPurchaseOffering,

    -- * Request Lenses
    purchaseOffering_offeringPromotionId,
    purchaseOffering_offeringId,
    purchaseOffering_quantity,

    -- * Destructuring the Response
    PurchaseOfferingResponse (..),
    newPurchaseOfferingResponse,

    -- * Response Lenses
    purchaseOfferingResponse_offeringTransaction,
    purchaseOfferingResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.DeviceFarm.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

-- | Represents a request for a purchase offering.
--
-- /See:/ 'newPurchaseOffering' smart constructor.
data PurchaseOffering = PurchaseOffering'
  { -- | The ID of the offering promotion to be applied to the purchase.
    PurchaseOffering -> Maybe Text
offeringPromotionId :: Prelude.Maybe Prelude.Text,
    -- | The ID of the offering.
    PurchaseOffering -> Text
offeringId :: Prelude.Text,
    -- | The number of device slots to purchase in an offering request.
    PurchaseOffering -> Int
quantity :: Prelude.Int
  }
  deriving (PurchaseOffering -> PurchaseOffering -> Bool
(PurchaseOffering -> PurchaseOffering -> Bool)
-> (PurchaseOffering -> PurchaseOffering -> Bool)
-> Eq PurchaseOffering
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PurchaseOffering -> PurchaseOffering -> Bool
$c/= :: PurchaseOffering -> PurchaseOffering -> Bool
== :: PurchaseOffering -> PurchaseOffering -> Bool
$c== :: PurchaseOffering -> PurchaseOffering -> Bool
Prelude.Eq, ReadPrec [PurchaseOffering]
ReadPrec PurchaseOffering
Int -> ReadS PurchaseOffering
ReadS [PurchaseOffering]
(Int -> ReadS PurchaseOffering)
-> ReadS [PurchaseOffering]
-> ReadPrec PurchaseOffering
-> ReadPrec [PurchaseOffering]
-> Read PurchaseOffering
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PurchaseOffering]
$creadListPrec :: ReadPrec [PurchaseOffering]
readPrec :: ReadPrec PurchaseOffering
$creadPrec :: ReadPrec PurchaseOffering
readList :: ReadS [PurchaseOffering]
$creadList :: ReadS [PurchaseOffering]
readsPrec :: Int -> ReadS PurchaseOffering
$creadsPrec :: Int -> ReadS PurchaseOffering
Prelude.Read, Int -> PurchaseOffering -> ShowS
[PurchaseOffering] -> ShowS
PurchaseOffering -> String
(Int -> PurchaseOffering -> ShowS)
-> (PurchaseOffering -> String)
-> ([PurchaseOffering] -> ShowS)
-> Show PurchaseOffering
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PurchaseOffering] -> ShowS
$cshowList :: [PurchaseOffering] -> ShowS
show :: PurchaseOffering -> String
$cshow :: PurchaseOffering -> String
showsPrec :: Int -> PurchaseOffering -> ShowS
$cshowsPrec :: Int -> PurchaseOffering -> ShowS
Prelude.Show, (forall x. PurchaseOffering -> Rep PurchaseOffering x)
-> (forall x. Rep PurchaseOffering x -> PurchaseOffering)
-> Generic PurchaseOffering
forall x. Rep PurchaseOffering x -> PurchaseOffering
forall x. PurchaseOffering -> Rep PurchaseOffering x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PurchaseOffering x -> PurchaseOffering
$cfrom :: forall x. PurchaseOffering -> Rep PurchaseOffering x
Prelude.Generic)

-- |
-- Create a value of 'PurchaseOffering' 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:
--
-- 'offeringPromotionId', 'purchaseOffering_offeringPromotionId' - The ID of the offering promotion to be applied to the purchase.
--
-- 'offeringId', 'purchaseOffering_offeringId' - The ID of the offering.
--
-- 'quantity', 'purchaseOffering_quantity' - The number of device slots to purchase in an offering request.
newPurchaseOffering ::
  -- | 'offeringId'
  Prelude.Text ->
  -- | 'quantity'
  Prelude.Int ->
  PurchaseOffering
newPurchaseOffering :: Text -> Int -> PurchaseOffering
newPurchaseOffering Text
pOfferingId_ Int
pQuantity_ =
  PurchaseOffering' :: Maybe Text -> Text -> Int -> PurchaseOffering
PurchaseOffering'
    { $sel:offeringPromotionId:PurchaseOffering' :: Maybe Text
offeringPromotionId =
        Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:offeringId:PurchaseOffering' :: Text
offeringId = Text
pOfferingId_,
      $sel:quantity:PurchaseOffering' :: Int
quantity = Int
pQuantity_
    }

-- | The ID of the offering promotion to be applied to the purchase.
purchaseOffering_offeringPromotionId :: Lens.Lens' PurchaseOffering (Prelude.Maybe Prelude.Text)
purchaseOffering_offeringPromotionId :: (Maybe Text -> f (Maybe Text))
-> PurchaseOffering -> f PurchaseOffering
purchaseOffering_offeringPromotionId = (PurchaseOffering -> Maybe Text)
-> (PurchaseOffering -> Maybe Text -> PurchaseOffering)
-> Lens PurchaseOffering PurchaseOffering (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PurchaseOffering' {Maybe Text
offeringPromotionId :: Maybe Text
$sel:offeringPromotionId:PurchaseOffering' :: PurchaseOffering -> Maybe Text
offeringPromotionId} -> Maybe Text
offeringPromotionId) (\s :: PurchaseOffering
s@PurchaseOffering' {} Maybe Text
a -> PurchaseOffering
s {$sel:offeringPromotionId:PurchaseOffering' :: Maybe Text
offeringPromotionId = Maybe Text
a} :: PurchaseOffering)

-- | The ID of the offering.
purchaseOffering_offeringId :: Lens.Lens' PurchaseOffering Prelude.Text
purchaseOffering_offeringId :: (Text -> f Text) -> PurchaseOffering -> f PurchaseOffering
purchaseOffering_offeringId = (PurchaseOffering -> Text)
-> (PurchaseOffering -> Text -> PurchaseOffering)
-> Lens PurchaseOffering PurchaseOffering Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PurchaseOffering' {Text
offeringId :: Text
$sel:offeringId:PurchaseOffering' :: PurchaseOffering -> Text
offeringId} -> Text
offeringId) (\s :: PurchaseOffering
s@PurchaseOffering' {} Text
a -> PurchaseOffering
s {$sel:offeringId:PurchaseOffering' :: Text
offeringId = Text
a} :: PurchaseOffering)

-- | The number of device slots to purchase in an offering request.
purchaseOffering_quantity :: Lens.Lens' PurchaseOffering Prelude.Int
purchaseOffering_quantity :: (Int -> f Int) -> PurchaseOffering -> f PurchaseOffering
purchaseOffering_quantity = (PurchaseOffering -> Int)
-> (PurchaseOffering -> Int -> PurchaseOffering)
-> Lens PurchaseOffering PurchaseOffering Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PurchaseOffering' {Int
quantity :: Int
$sel:quantity:PurchaseOffering' :: PurchaseOffering -> Int
quantity} -> Int
quantity) (\s :: PurchaseOffering
s@PurchaseOffering' {} Int
a -> PurchaseOffering
s {$sel:quantity:PurchaseOffering' :: Int
quantity = Int
a} :: PurchaseOffering)

instance Core.AWSRequest PurchaseOffering where
  type
    AWSResponse PurchaseOffering =
      PurchaseOfferingResponse
  request :: PurchaseOffering -> Request PurchaseOffering
request = Service -> PurchaseOffering -> Request PurchaseOffering
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy PurchaseOffering
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PurchaseOffering)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse PurchaseOffering))
-> Logger
-> Service
-> Proxy PurchaseOffering
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PurchaseOffering)))
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 OfferingTransaction -> Int -> PurchaseOfferingResponse
PurchaseOfferingResponse'
            (Maybe OfferingTransaction -> Int -> PurchaseOfferingResponse)
-> Either String (Maybe OfferingTransaction)
-> Either String (Int -> PurchaseOfferingResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe OfferingTransaction)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"offeringTransaction")
            Either String (Int -> PurchaseOfferingResponse)
-> Either String Int -> Either String PurchaseOfferingResponse
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 PurchaseOffering

instance Prelude.NFData PurchaseOffering

instance Core.ToHeaders PurchaseOffering where
  toHeaders :: PurchaseOffering -> ResponseHeaders
toHeaders =
    ResponseHeaders -> PurchaseOffering -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"DeviceFarm_20150623.PurchaseOffering" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON PurchaseOffering where
  toJSON :: PurchaseOffering -> Value
toJSON PurchaseOffering' {Int
Maybe Text
Text
quantity :: Int
offeringId :: Text
offeringPromotionId :: Maybe Text
$sel:quantity:PurchaseOffering' :: PurchaseOffering -> Int
$sel:offeringId:PurchaseOffering' :: PurchaseOffering -> Text
$sel:offeringPromotionId:PurchaseOffering' :: PurchaseOffering -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"offeringPromotionId" 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
offeringPromotionId,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"offeringId" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
offeringId),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"quantity" Text -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Int
quantity)
          ]
      )

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

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

-- | The result of the purchase offering (for example, success or failure).
--
-- /See:/ 'newPurchaseOfferingResponse' smart constructor.
data PurchaseOfferingResponse = PurchaseOfferingResponse'
  { -- | Represents the offering transaction for the purchase result.
    PurchaseOfferingResponse -> Maybe OfferingTransaction
offeringTransaction :: Prelude.Maybe OfferingTransaction,
    -- | The response's http status code.
    PurchaseOfferingResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool
(PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool)
-> (PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool)
-> Eq PurchaseOfferingResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool
$c/= :: PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool
== :: PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool
$c== :: PurchaseOfferingResponse -> PurchaseOfferingResponse -> Bool
Prelude.Eq, ReadPrec [PurchaseOfferingResponse]
ReadPrec PurchaseOfferingResponse
Int -> ReadS PurchaseOfferingResponse
ReadS [PurchaseOfferingResponse]
(Int -> ReadS PurchaseOfferingResponse)
-> ReadS [PurchaseOfferingResponse]
-> ReadPrec PurchaseOfferingResponse
-> ReadPrec [PurchaseOfferingResponse]
-> Read PurchaseOfferingResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PurchaseOfferingResponse]
$creadListPrec :: ReadPrec [PurchaseOfferingResponse]
readPrec :: ReadPrec PurchaseOfferingResponse
$creadPrec :: ReadPrec PurchaseOfferingResponse
readList :: ReadS [PurchaseOfferingResponse]
$creadList :: ReadS [PurchaseOfferingResponse]
readsPrec :: Int -> ReadS PurchaseOfferingResponse
$creadsPrec :: Int -> ReadS PurchaseOfferingResponse
Prelude.Read, Int -> PurchaseOfferingResponse -> ShowS
[PurchaseOfferingResponse] -> ShowS
PurchaseOfferingResponse -> String
(Int -> PurchaseOfferingResponse -> ShowS)
-> (PurchaseOfferingResponse -> String)
-> ([PurchaseOfferingResponse] -> ShowS)
-> Show PurchaseOfferingResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PurchaseOfferingResponse] -> ShowS
$cshowList :: [PurchaseOfferingResponse] -> ShowS
show :: PurchaseOfferingResponse -> String
$cshow :: PurchaseOfferingResponse -> String
showsPrec :: Int -> PurchaseOfferingResponse -> ShowS
$cshowsPrec :: Int -> PurchaseOfferingResponse -> ShowS
Prelude.Show, (forall x.
 PurchaseOfferingResponse -> Rep PurchaseOfferingResponse x)
-> (forall x.
    Rep PurchaseOfferingResponse x -> PurchaseOfferingResponse)
-> Generic PurchaseOfferingResponse
forall x.
Rep PurchaseOfferingResponse x -> PurchaseOfferingResponse
forall x.
PurchaseOfferingResponse -> Rep PurchaseOfferingResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep PurchaseOfferingResponse x -> PurchaseOfferingResponse
$cfrom :: forall x.
PurchaseOfferingResponse -> Rep PurchaseOfferingResponse x
Prelude.Generic)

-- |
-- Create a value of 'PurchaseOfferingResponse' 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:
--
-- 'offeringTransaction', 'purchaseOfferingResponse_offeringTransaction' - Represents the offering transaction for the purchase result.
--
-- 'httpStatus', 'purchaseOfferingResponse_httpStatus' - The response's http status code.
newPurchaseOfferingResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  PurchaseOfferingResponse
newPurchaseOfferingResponse :: Int -> PurchaseOfferingResponse
newPurchaseOfferingResponse Int
pHttpStatus_ =
  PurchaseOfferingResponse' :: Maybe OfferingTransaction -> Int -> PurchaseOfferingResponse
PurchaseOfferingResponse'
    { $sel:offeringTransaction:PurchaseOfferingResponse' :: Maybe OfferingTransaction
offeringTransaction =
        Maybe OfferingTransaction
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:PurchaseOfferingResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | Represents the offering transaction for the purchase result.
purchaseOfferingResponse_offeringTransaction :: Lens.Lens' PurchaseOfferingResponse (Prelude.Maybe OfferingTransaction)
purchaseOfferingResponse_offeringTransaction :: (Maybe OfferingTransaction -> f (Maybe OfferingTransaction))
-> PurchaseOfferingResponse -> f PurchaseOfferingResponse
purchaseOfferingResponse_offeringTransaction = (PurchaseOfferingResponse -> Maybe OfferingTransaction)
-> (PurchaseOfferingResponse
    -> Maybe OfferingTransaction -> PurchaseOfferingResponse)
-> Lens
     PurchaseOfferingResponse
     PurchaseOfferingResponse
     (Maybe OfferingTransaction)
     (Maybe OfferingTransaction)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PurchaseOfferingResponse' {Maybe OfferingTransaction
offeringTransaction :: Maybe OfferingTransaction
$sel:offeringTransaction:PurchaseOfferingResponse' :: PurchaseOfferingResponse -> Maybe OfferingTransaction
offeringTransaction} -> Maybe OfferingTransaction
offeringTransaction) (\s :: PurchaseOfferingResponse
s@PurchaseOfferingResponse' {} Maybe OfferingTransaction
a -> PurchaseOfferingResponse
s {$sel:offeringTransaction:PurchaseOfferingResponse' :: Maybe OfferingTransaction
offeringTransaction = Maybe OfferingTransaction
a} :: PurchaseOfferingResponse)

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

instance Prelude.NFData PurchaseOfferingResponse