{-# 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.CloudWatchEvents.PutEvents
-- 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)
--
-- Sends custom events to Amazon EventBridge so that they can be matched to
-- rules.
module Amazonka.CloudWatchEvents.PutEvents
  ( -- * Creating a Request
    PutEvents (..),
    newPutEvents,

    -- * Request Lenses
    putEvents_entries,

    -- * Destructuring the Response
    PutEventsResponse (..),
    newPutEventsResponse,

    -- * Response Lenses
    putEventsResponse_failedEntryCount,
    putEventsResponse_entries,
    putEventsResponse_httpStatus,
  )
where

import Amazonka.CloudWatchEvents.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

-- | /See:/ 'newPutEvents' smart constructor.
data PutEvents = PutEvents'
  { -- | The entry that defines an event in your system. You can specify several
    -- parameters for the entry such as the source and type of the event,
    -- resources associated with the event, and so on.
    PutEvents -> NonEmpty PutEventsRequestEntry
entries :: Prelude.NonEmpty PutEventsRequestEntry
  }
  deriving (PutEvents -> PutEvents -> Bool
(PutEvents -> PutEvents -> Bool)
-> (PutEvents -> PutEvents -> Bool) -> Eq PutEvents
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutEvents -> PutEvents -> Bool
$c/= :: PutEvents -> PutEvents -> Bool
== :: PutEvents -> PutEvents -> Bool
$c== :: PutEvents -> PutEvents -> Bool
Prelude.Eq, ReadPrec [PutEvents]
ReadPrec PutEvents
Int -> ReadS PutEvents
ReadS [PutEvents]
(Int -> ReadS PutEvents)
-> ReadS [PutEvents]
-> ReadPrec PutEvents
-> ReadPrec [PutEvents]
-> Read PutEvents
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutEvents]
$creadListPrec :: ReadPrec [PutEvents]
readPrec :: ReadPrec PutEvents
$creadPrec :: ReadPrec PutEvents
readList :: ReadS [PutEvents]
$creadList :: ReadS [PutEvents]
readsPrec :: Int -> ReadS PutEvents
$creadsPrec :: Int -> ReadS PutEvents
Prelude.Read, Int -> PutEvents -> ShowS
[PutEvents] -> ShowS
PutEvents -> String
(Int -> PutEvents -> ShowS)
-> (PutEvents -> String)
-> ([PutEvents] -> ShowS)
-> Show PutEvents
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutEvents] -> ShowS
$cshowList :: [PutEvents] -> ShowS
show :: PutEvents -> String
$cshow :: PutEvents -> String
showsPrec :: Int -> PutEvents -> ShowS
$cshowsPrec :: Int -> PutEvents -> ShowS
Prelude.Show, (forall x. PutEvents -> Rep PutEvents x)
-> (forall x. Rep PutEvents x -> PutEvents) -> Generic PutEvents
forall x. Rep PutEvents x -> PutEvents
forall x. PutEvents -> Rep PutEvents x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutEvents x -> PutEvents
$cfrom :: forall x. PutEvents -> Rep PutEvents x
Prelude.Generic)

-- |
-- Create a value of 'PutEvents' 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:
--
-- 'entries', 'putEvents_entries' - The entry that defines an event in your system. You can specify several
-- parameters for the entry such as the source and type of the event,
-- resources associated with the event, and so on.
newPutEvents ::
  -- | 'entries'
  Prelude.NonEmpty PutEventsRequestEntry ->
  PutEvents
newPutEvents :: NonEmpty PutEventsRequestEntry -> PutEvents
newPutEvents NonEmpty PutEventsRequestEntry
pEntries_ =
  PutEvents' :: NonEmpty PutEventsRequestEntry -> PutEvents
PutEvents' {$sel:entries:PutEvents' :: NonEmpty PutEventsRequestEntry
entries = Tagged
  (NonEmpty PutEventsRequestEntry)
  (Identity (NonEmpty PutEventsRequestEntry))
-> Tagged
     (NonEmpty PutEventsRequestEntry)
     (Identity (NonEmpty PutEventsRequestEntry))
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced (Tagged
   (NonEmpty PutEventsRequestEntry)
   (Identity (NonEmpty PutEventsRequestEntry))
 -> Tagged
      (NonEmpty PutEventsRequestEntry)
      (Identity (NonEmpty PutEventsRequestEntry)))
-> NonEmpty PutEventsRequestEntry -> NonEmpty PutEventsRequestEntry
forall t b. AReview t b -> b -> t
Lens.# NonEmpty PutEventsRequestEntry
pEntries_}

-- | The entry that defines an event in your system. You can specify several
-- parameters for the entry such as the source and type of the event,
-- resources associated with the event, and so on.
putEvents_entries :: Lens.Lens' PutEvents (Prelude.NonEmpty PutEventsRequestEntry)
putEvents_entries :: (NonEmpty PutEventsRequestEntry
 -> f (NonEmpty PutEventsRequestEntry))
-> PutEvents -> f PutEvents
putEvents_entries = (PutEvents -> NonEmpty PutEventsRequestEntry)
-> (PutEvents -> NonEmpty PutEventsRequestEntry -> PutEvents)
-> Lens
     PutEvents
     PutEvents
     (NonEmpty PutEventsRequestEntry)
     (NonEmpty PutEventsRequestEntry)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutEvents' {NonEmpty PutEventsRequestEntry
entries :: NonEmpty PutEventsRequestEntry
$sel:entries:PutEvents' :: PutEvents -> NonEmpty PutEventsRequestEntry
entries} -> NonEmpty PutEventsRequestEntry
entries) (\s :: PutEvents
s@PutEvents' {} NonEmpty PutEventsRequestEntry
a -> PutEvents
s {$sel:entries:PutEvents' :: NonEmpty PutEventsRequestEntry
entries = NonEmpty PutEventsRequestEntry
a} :: PutEvents) ((NonEmpty PutEventsRequestEntry
  -> f (NonEmpty PutEventsRequestEntry))
 -> PutEvents -> f PutEvents)
-> ((NonEmpty PutEventsRequestEntry
     -> f (NonEmpty PutEventsRequestEntry))
    -> NonEmpty PutEventsRequestEntry
    -> f (NonEmpty PutEventsRequestEntry))
-> (NonEmpty PutEventsRequestEntry
    -> f (NonEmpty PutEventsRequestEntry))
-> PutEvents
-> f PutEvents
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (NonEmpty PutEventsRequestEntry
 -> f (NonEmpty PutEventsRequestEntry))
-> NonEmpty PutEventsRequestEntry
-> f (NonEmpty PutEventsRequestEntry)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.AWSRequest PutEvents where
  type AWSResponse PutEvents = PutEventsResponse
  request :: PutEvents -> Request PutEvents
request = Service -> PutEvents -> Request PutEvents
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy PutEvents
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutEvents)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse PutEvents))
-> Logger
-> Service
-> Proxy PutEvents
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutEvents)))
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 Int
-> Maybe [PutEventsResultEntry] -> Int -> PutEventsResponse
PutEventsResponse'
            (Maybe Int
 -> Maybe [PutEventsResultEntry] -> Int -> PutEventsResponse)
-> Either String (Maybe Int)
-> Either
     String (Maybe [PutEventsResultEntry] -> Int -> PutEventsResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Key -> Either String (Maybe Int)
forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Core..?> Key
"FailedEntryCount")
            Either
  String (Maybe [PutEventsResultEntry] -> Int -> PutEventsResponse)
-> Either String (Maybe [PutEventsResultEntry])
-> Either String (Int -> PutEventsResponse)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object
-> Key -> Either String (Maybe (Maybe [PutEventsResultEntry]))
forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Core..?> Key
"Entries" Either String (Maybe (Maybe [PutEventsResultEntry]))
-> Maybe [PutEventsResultEntry]
-> Either String (Maybe [PutEventsResultEntry])
forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ Maybe [PutEventsResultEntry]
forall a. Monoid a => a
Prelude.mempty)
            Either String (Int -> PutEventsResponse)
-> Either String Int -> Either String PutEventsResponse
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 PutEvents

instance Prelude.NFData PutEvents

instance Core.ToHeaders PutEvents where
  toHeaders :: PutEvents -> ResponseHeaders
toHeaders =
    ResponseHeaders -> PutEvents -> 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
"AWSEvents.PutEvents" :: 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 PutEvents where
  toJSON :: PutEvents -> Value
toJSON PutEvents' {NonEmpty PutEventsRequestEntry
entries :: NonEmpty PutEventsRequestEntry
$sel:entries:PutEvents' :: PutEvents -> NonEmpty PutEventsRequestEntry
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Key
"Entries" Key -> NonEmpty PutEventsRequestEntry -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Core..= NonEmpty PutEventsRequestEntry
entries)]
      )

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

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

-- | /See:/ 'newPutEventsResponse' smart constructor.
data PutEventsResponse = PutEventsResponse'
  { -- | The number of failed entries.
    PutEventsResponse -> Maybe Int
failedEntryCount :: Prelude.Maybe Prelude.Int,
    -- | The successfully and unsuccessfully ingested events results. If the
    -- ingestion was successful, the entry has the event ID in it. Otherwise,
    -- you can use the error code and error message to identify the problem
    -- with the entry.
    PutEventsResponse -> Maybe [PutEventsResultEntry]
entries :: Prelude.Maybe [PutEventsResultEntry],
    -- | The response's http status code.
    PutEventsResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (PutEventsResponse -> PutEventsResponse -> Bool
(PutEventsResponse -> PutEventsResponse -> Bool)
-> (PutEventsResponse -> PutEventsResponse -> Bool)
-> Eq PutEventsResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutEventsResponse -> PutEventsResponse -> Bool
$c/= :: PutEventsResponse -> PutEventsResponse -> Bool
== :: PutEventsResponse -> PutEventsResponse -> Bool
$c== :: PutEventsResponse -> PutEventsResponse -> Bool
Prelude.Eq, ReadPrec [PutEventsResponse]
ReadPrec PutEventsResponse
Int -> ReadS PutEventsResponse
ReadS [PutEventsResponse]
(Int -> ReadS PutEventsResponse)
-> ReadS [PutEventsResponse]
-> ReadPrec PutEventsResponse
-> ReadPrec [PutEventsResponse]
-> Read PutEventsResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutEventsResponse]
$creadListPrec :: ReadPrec [PutEventsResponse]
readPrec :: ReadPrec PutEventsResponse
$creadPrec :: ReadPrec PutEventsResponse
readList :: ReadS [PutEventsResponse]
$creadList :: ReadS [PutEventsResponse]
readsPrec :: Int -> ReadS PutEventsResponse
$creadsPrec :: Int -> ReadS PutEventsResponse
Prelude.Read, Int -> PutEventsResponse -> ShowS
[PutEventsResponse] -> ShowS
PutEventsResponse -> String
(Int -> PutEventsResponse -> ShowS)
-> (PutEventsResponse -> String)
-> ([PutEventsResponse] -> ShowS)
-> Show PutEventsResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutEventsResponse] -> ShowS
$cshowList :: [PutEventsResponse] -> ShowS
show :: PutEventsResponse -> String
$cshow :: PutEventsResponse -> String
showsPrec :: Int -> PutEventsResponse -> ShowS
$cshowsPrec :: Int -> PutEventsResponse -> ShowS
Prelude.Show, (forall x. PutEventsResponse -> Rep PutEventsResponse x)
-> (forall x. Rep PutEventsResponse x -> PutEventsResponse)
-> Generic PutEventsResponse
forall x. Rep PutEventsResponse x -> PutEventsResponse
forall x. PutEventsResponse -> Rep PutEventsResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutEventsResponse x -> PutEventsResponse
$cfrom :: forall x. PutEventsResponse -> Rep PutEventsResponse x
Prelude.Generic)

-- |
-- Create a value of 'PutEventsResponse' 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:
--
-- 'failedEntryCount', 'putEventsResponse_failedEntryCount' - The number of failed entries.
--
-- 'entries', 'putEventsResponse_entries' - The successfully and unsuccessfully ingested events results. If the
-- ingestion was successful, the entry has the event ID in it. Otherwise,
-- you can use the error code and error message to identify the problem
-- with the entry.
--
-- 'httpStatus', 'putEventsResponse_httpStatus' - The response's http status code.
newPutEventsResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  PutEventsResponse
newPutEventsResponse :: Int -> PutEventsResponse
newPutEventsResponse Int
pHttpStatus_ =
  PutEventsResponse' :: Maybe Int
-> Maybe [PutEventsResultEntry] -> Int -> PutEventsResponse
PutEventsResponse'
    { $sel:failedEntryCount:PutEventsResponse' :: Maybe Int
failedEntryCount =
        Maybe Int
forall a. Maybe a
Prelude.Nothing,
      $sel:entries:PutEventsResponse' :: Maybe [PutEventsResultEntry]
entries = Maybe [PutEventsResultEntry]
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:PutEventsResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The number of failed entries.
putEventsResponse_failedEntryCount :: Lens.Lens' PutEventsResponse (Prelude.Maybe Prelude.Int)
putEventsResponse_failedEntryCount :: (Maybe Int -> f (Maybe Int))
-> PutEventsResponse -> f PutEventsResponse
putEventsResponse_failedEntryCount = (PutEventsResponse -> Maybe Int)
-> (PutEventsResponse -> Maybe Int -> PutEventsResponse)
-> Lens PutEventsResponse PutEventsResponse (Maybe Int) (Maybe Int)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutEventsResponse' {Maybe Int
failedEntryCount :: Maybe Int
$sel:failedEntryCount:PutEventsResponse' :: PutEventsResponse -> Maybe Int
failedEntryCount} -> Maybe Int
failedEntryCount) (\s :: PutEventsResponse
s@PutEventsResponse' {} Maybe Int
a -> PutEventsResponse
s {$sel:failedEntryCount:PutEventsResponse' :: Maybe Int
failedEntryCount = Maybe Int
a} :: PutEventsResponse)

-- | The successfully and unsuccessfully ingested events results. If the
-- ingestion was successful, the entry has the event ID in it. Otherwise,
-- you can use the error code and error message to identify the problem
-- with the entry.
putEventsResponse_entries :: Lens.Lens' PutEventsResponse (Prelude.Maybe [PutEventsResultEntry])
putEventsResponse_entries :: (Maybe [PutEventsResultEntry] -> f (Maybe [PutEventsResultEntry]))
-> PutEventsResponse -> f PutEventsResponse
putEventsResponse_entries = (PutEventsResponse -> Maybe [PutEventsResultEntry])
-> (PutEventsResponse
    -> Maybe [PutEventsResultEntry] -> PutEventsResponse)
-> Lens
     PutEventsResponse
     PutEventsResponse
     (Maybe [PutEventsResultEntry])
     (Maybe [PutEventsResultEntry])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutEventsResponse' {Maybe [PutEventsResultEntry]
entries :: Maybe [PutEventsResultEntry]
$sel:entries:PutEventsResponse' :: PutEventsResponse -> Maybe [PutEventsResultEntry]
entries} -> Maybe [PutEventsResultEntry]
entries) (\s :: PutEventsResponse
s@PutEventsResponse' {} Maybe [PutEventsResultEntry]
a -> PutEventsResponse
s {$sel:entries:PutEventsResponse' :: Maybe [PutEventsResultEntry]
entries = Maybe [PutEventsResultEntry]
a} :: PutEventsResponse) ((Maybe [PutEventsResultEntry] -> f (Maybe [PutEventsResultEntry]))
 -> PutEventsResponse -> f PutEventsResponse)
-> ((Maybe [PutEventsResultEntry]
     -> f (Maybe [PutEventsResultEntry]))
    -> Maybe [PutEventsResultEntry]
    -> f (Maybe [PutEventsResultEntry]))
-> (Maybe [PutEventsResultEntry]
    -> f (Maybe [PutEventsResultEntry]))
-> PutEventsResponse
-> f PutEventsResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  [PutEventsResultEntry]
  [PutEventsResultEntry]
  [PutEventsResultEntry]
  [PutEventsResultEntry]
-> Iso
     (Maybe [PutEventsResultEntry])
     (Maybe [PutEventsResultEntry])
     (Maybe [PutEventsResultEntry])
     (Maybe [PutEventsResultEntry])
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
  [PutEventsResultEntry]
  [PutEventsResultEntry]
  [PutEventsResultEntry]
  [PutEventsResultEntry]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

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

instance Prelude.NFData PutEventsResponse