{-# 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.IoTData.Publish
-- 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)
--
-- Publishes an MQTT message.
--
-- Requires permission to access the
-- <https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsiot.html#awsiot-actions-as-permissions Publish>
-- action.
--
-- For more information about MQTT messages, see
-- <http://docs.aws.amazon.com/iot/latest/developerguide/mqtt.html MQTT Protocol>
-- in the IoT Developer Guide.
--
-- For more information about messaging costs, see
-- <http://aws.amazon.com/iot-core/pricing/#Messaging IoT Core pricing - Messaging>.
module Amazonka.IoTData.Publish
  ( -- * Creating a Request
    Publish (..),
    newPublish,

    -- * Request Lenses
    publish_retain,
    publish_payload,
    publish_qos,
    publish_topic,

    -- * Destructuring the Response
    PublishResponse (..),
    newPublishResponse,
  )
where

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

-- | The input for the Publish operation.
--
-- /See:/ 'newPublish' smart constructor.
data Publish = Publish'
  { -- | A Boolean value that determines whether to set the RETAIN flag when the
    -- message is published.
    --
    -- Setting the RETAIN flag causes the message to be retained and sent to
    -- new subscribers to the topic.
    --
    -- Valid values: @true@ | @false@
    --
    -- Default value: @false@
    Publish -> Maybe Bool
retain :: Prelude.Maybe Prelude.Bool,
    -- | The message body. MQTT accepts text, binary, and empty (null) message
    -- payloads.
    --
    -- Publishing an empty (null) payload with __retain__ = @true@ deletes the
    -- retained message identified by __topic__ from IoT Core.
    Publish -> Maybe ByteString
payload :: Prelude.Maybe Prelude.ByteString,
    -- | The Quality of Service (QoS) level.
    Publish -> Maybe Natural
qos :: Prelude.Maybe Prelude.Natural,
    -- | The name of the MQTT topic.
    Publish -> Text
topic :: Prelude.Text
  }
  deriving (Publish -> Publish -> Bool
(Publish -> Publish -> Bool)
-> (Publish -> Publish -> Bool) -> Eq Publish
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Publish -> Publish -> Bool
$c/= :: Publish -> Publish -> Bool
== :: Publish -> Publish -> Bool
$c== :: Publish -> Publish -> Bool
Prelude.Eq, ReadPrec [Publish]
ReadPrec Publish
Int -> ReadS Publish
ReadS [Publish]
(Int -> ReadS Publish)
-> ReadS [Publish]
-> ReadPrec Publish
-> ReadPrec [Publish]
-> Read Publish
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Publish]
$creadListPrec :: ReadPrec [Publish]
readPrec :: ReadPrec Publish
$creadPrec :: ReadPrec Publish
readList :: ReadS [Publish]
$creadList :: ReadS [Publish]
readsPrec :: Int -> ReadS Publish
$creadsPrec :: Int -> ReadS Publish
Prelude.Read, Int -> Publish -> ShowS
[Publish] -> ShowS
Publish -> String
(Int -> Publish -> ShowS)
-> (Publish -> String) -> ([Publish] -> ShowS) -> Show Publish
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Publish] -> ShowS
$cshowList :: [Publish] -> ShowS
show :: Publish -> String
$cshow :: Publish -> String
showsPrec :: Int -> Publish -> ShowS
$cshowsPrec :: Int -> Publish -> ShowS
Prelude.Show, (forall x. Publish -> Rep Publish x)
-> (forall x. Rep Publish x -> Publish) -> Generic Publish
forall x. Rep Publish x -> Publish
forall x. Publish -> Rep Publish x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Publish x -> Publish
$cfrom :: forall x. Publish -> Rep Publish x
Prelude.Generic)

-- |
-- Create a value of 'Publish' 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:
--
-- 'retain', 'publish_retain' - A Boolean value that determines whether to set the RETAIN flag when the
-- message is published.
--
-- Setting the RETAIN flag causes the message to be retained and sent to
-- new subscribers to the topic.
--
-- Valid values: @true@ | @false@
--
-- Default value: @false@
--
-- 'payload', 'publish_payload' - The message body. MQTT accepts text, binary, and empty (null) message
-- payloads.
--
-- Publishing an empty (null) payload with __retain__ = @true@ deletes the
-- retained message identified by __topic__ from IoT Core.
--
-- 'qos', 'publish_qos' - The Quality of Service (QoS) level.
--
-- 'topic', 'publish_topic' - The name of the MQTT topic.
newPublish ::
  -- | 'topic'
  Prelude.Text ->
  Publish
newPublish :: Text -> Publish
newPublish Text
pTopic_ =
  Publish' :: Maybe Bool -> Maybe ByteString -> Maybe Natural -> Text -> Publish
Publish'
    { $sel:retain:Publish' :: Maybe Bool
retain = Maybe Bool
forall a. Maybe a
Prelude.Nothing,
      $sel:payload:Publish' :: Maybe ByteString
payload = Maybe ByteString
forall a. Maybe a
Prelude.Nothing,
      $sel:qos:Publish' :: Maybe Natural
qos = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:topic:Publish' :: Text
topic = Text
pTopic_
    }

-- | A Boolean value that determines whether to set the RETAIN flag when the
-- message is published.
--
-- Setting the RETAIN flag causes the message to be retained and sent to
-- new subscribers to the topic.
--
-- Valid values: @true@ | @false@
--
-- Default value: @false@
publish_retain :: Lens.Lens' Publish (Prelude.Maybe Prelude.Bool)
publish_retain :: (Maybe Bool -> f (Maybe Bool)) -> Publish -> f Publish
publish_retain = (Publish -> Maybe Bool)
-> (Publish -> Maybe Bool -> Publish)
-> Lens Publish Publish (Maybe Bool) (Maybe Bool)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Publish' {Maybe Bool
retain :: Maybe Bool
$sel:retain:Publish' :: Publish -> Maybe Bool
retain} -> Maybe Bool
retain) (\s :: Publish
s@Publish' {} Maybe Bool
a -> Publish
s {$sel:retain:Publish' :: Maybe Bool
retain = Maybe Bool
a} :: Publish)

-- | The message body. MQTT accepts text, binary, and empty (null) message
-- payloads.
--
-- Publishing an empty (null) payload with __retain__ = @true@ deletes the
-- retained message identified by __topic__ from IoT Core.
publish_payload :: Lens.Lens' Publish (Prelude.Maybe Prelude.ByteString)
publish_payload :: (Maybe ByteString -> f (Maybe ByteString)) -> Publish -> f Publish
publish_payload = (Publish -> Maybe ByteString)
-> (Publish -> Maybe ByteString -> Publish)
-> Lens Publish Publish (Maybe ByteString) (Maybe ByteString)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Publish' {Maybe ByteString
payload :: Maybe ByteString
$sel:payload:Publish' :: Publish -> Maybe ByteString
payload} -> Maybe ByteString
payload) (\s :: Publish
s@Publish' {} Maybe ByteString
a -> Publish
s {$sel:payload:Publish' :: Maybe ByteString
payload = Maybe ByteString
a} :: Publish)

-- | The Quality of Service (QoS) level.
publish_qos :: Lens.Lens' Publish (Prelude.Maybe Prelude.Natural)
publish_qos :: (Maybe Natural -> f (Maybe Natural)) -> Publish -> f Publish
publish_qos = (Publish -> Maybe Natural)
-> (Publish -> Maybe Natural -> Publish)
-> Lens Publish Publish (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Publish' {Maybe Natural
qos :: Maybe Natural
$sel:qos:Publish' :: Publish -> Maybe Natural
qos} -> Maybe Natural
qos) (\s :: Publish
s@Publish' {} Maybe Natural
a -> Publish
s {$sel:qos:Publish' :: Maybe Natural
qos = Maybe Natural
a} :: Publish)

-- | The name of the MQTT topic.
publish_topic :: Lens.Lens' Publish Prelude.Text
publish_topic :: (Text -> f Text) -> Publish -> f Publish
publish_topic = (Publish -> Text)
-> (Publish -> Text -> Publish) -> Lens Publish Publish Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Publish' {Text
topic :: Text
$sel:topic:Publish' :: Publish -> Text
topic} -> Text
topic) (\s :: Publish
s@Publish' {} Text
a -> Publish
s {$sel:topic:Publish' :: Text
topic = Text
a} :: Publish)

instance Core.AWSRequest Publish where
  type AWSResponse Publish = PublishResponse
  request :: Publish -> Request Publish
request = Service -> Publish -> Request Publish
forall a. (ToRequest a, ToBody a) => Service -> a -> Request a
Request.postBody Service
defaultService
  response :: Logger
-> Service
-> Proxy Publish
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse Publish)))
response = AWSResponse Publish
-> Logger
-> Service
-> Proxy Publish
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse Publish)))
forall (m :: * -> *) a.
MonadResource m =>
AWSResponse a
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveNull AWSResponse Publish
PublishResponse
PublishResponse'

instance Prelude.Hashable Publish

instance Prelude.NFData Publish

instance Core.ToBody Publish where
  toBody :: Publish -> RequestBody
toBody Publish' {Maybe Bool
Maybe Natural
Maybe ByteString
Text
topic :: Text
qos :: Maybe Natural
payload :: Maybe ByteString
retain :: Maybe Bool
$sel:topic:Publish' :: Publish -> Text
$sel:qos:Publish' :: Publish -> Maybe Natural
$sel:payload:Publish' :: Publish -> Maybe ByteString
$sel:retain:Publish' :: Publish -> Maybe Bool
..} = Maybe ByteString -> RequestBody
forall a. ToBody a => a -> RequestBody
Core.toBody Maybe ByteString
payload

instance Core.ToHeaders Publish where
  toHeaders :: Publish -> [Header]
toHeaders = [Header] -> Publish -> [Header]
forall a b. a -> b -> a
Prelude.const [Header]
forall a. Monoid a => a
Prelude.mempty

instance Core.ToPath Publish where
  toPath :: Publish -> ByteString
toPath Publish' {Maybe Bool
Maybe Natural
Maybe ByteString
Text
topic :: Text
qos :: Maybe Natural
payload :: Maybe ByteString
retain :: Maybe Bool
$sel:topic:Publish' :: Publish -> Text
$sel:qos:Publish' :: Publish -> Maybe Natural
$sel:payload:Publish' :: Publish -> Maybe ByteString
$sel:retain:Publish' :: Publish -> Maybe Bool
..} =
    [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
Prelude.mconcat [ByteString
"/topics/", Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
topic]

instance Core.ToQuery Publish where
  toQuery :: Publish -> QueryString
toQuery Publish' {Maybe Bool
Maybe Natural
Maybe ByteString
Text
topic :: Text
qos :: Maybe Natural
payload :: Maybe ByteString
retain :: Maybe Bool
$sel:topic:Publish' :: Publish -> Text
$sel:qos:Publish' :: Publish -> Maybe Natural
$sel:payload:Publish' :: Publish -> Maybe ByteString
$sel:retain:Publish' :: Publish -> Maybe Bool
..} =
    [QueryString] -> QueryString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ByteString
"retain" ByteString -> Maybe Bool -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Maybe Bool
retain, ByteString
"qos" ByteString -> Maybe Natural -> QueryString
forall a. ToQuery a => ByteString -> a -> QueryString
Core.=: Maybe Natural
qos]

-- | /See:/ 'newPublishResponse' smart constructor.
data PublishResponse = PublishResponse'
  {
  }
  deriving (PublishResponse -> PublishResponse -> Bool
(PublishResponse -> PublishResponse -> Bool)
-> (PublishResponse -> PublishResponse -> Bool)
-> Eq PublishResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PublishResponse -> PublishResponse -> Bool
$c/= :: PublishResponse -> PublishResponse -> Bool
== :: PublishResponse -> PublishResponse -> Bool
$c== :: PublishResponse -> PublishResponse -> Bool
Prelude.Eq, ReadPrec [PublishResponse]
ReadPrec PublishResponse
Int -> ReadS PublishResponse
ReadS [PublishResponse]
(Int -> ReadS PublishResponse)
-> ReadS [PublishResponse]
-> ReadPrec PublishResponse
-> ReadPrec [PublishResponse]
-> Read PublishResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PublishResponse]
$creadListPrec :: ReadPrec [PublishResponse]
readPrec :: ReadPrec PublishResponse
$creadPrec :: ReadPrec PublishResponse
readList :: ReadS [PublishResponse]
$creadList :: ReadS [PublishResponse]
readsPrec :: Int -> ReadS PublishResponse
$creadsPrec :: Int -> ReadS PublishResponse
Prelude.Read, Int -> PublishResponse -> ShowS
[PublishResponse] -> ShowS
PublishResponse -> String
(Int -> PublishResponse -> ShowS)
-> (PublishResponse -> String)
-> ([PublishResponse] -> ShowS)
-> Show PublishResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PublishResponse] -> ShowS
$cshowList :: [PublishResponse] -> ShowS
show :: PublishResponse -> String
$cshow :: PublishResponse -> String
showsPrec :: Int -> PublishResponse -> ShowS
$cshowsPrec :: Int -> PublishResponse -> ShowS
Prelude.Show, (forall x. PublishResponse -> Rep PublishResponse x)
-> (forall x. Rep PublishResponse x -> PublishResponse)
-> Generic PublishResponse
forall x. Rep PublishResponse x -> PublishResponse
forall x. PublishResponse -> Rep PublishResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PublishResponse x -> PublishResponse
$cfrom :: forall x. PublishResponse -> Rep PublishResponse x
Prelude.Generic)

-- |
-- Create a value of 'PublishResponse' 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.
newPublishResponse ::
  PublishResponse
newPublishResponse :: PublishResponse
newPublishResponse = PublishResponse
PublishResponse'

instance Prelude.NFData PublishResponse