{-# 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.CloudDirectory.BatchWrite
-- 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)
--
-- Performs all the write operations in a batch. Either all the operations
-- succeed or none.
module Amazonka.CloudDirectory.BatchWrite
  ( -- * Creating a Request
    BatchWrite (..),
    newBatchWrite,

    -- * Request Lenses
    batchWrite_directoryArn,
    batchWrite_operations,

    -- * Destructuring the Response
    BatchWriteResponse (..),
    newBatchWriteResponse,

    -- * Response Lenses
    batchWriteResponse_responses,
    batchWriteResponse_httpStatus,
  )
where

import Amazonka.CloudDirectory.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:/ 'newBatchWrite' smart constructor.
data BatchWrite = BatchWrite'
  { -- | The Amazon Resource Name (ARN) that is associated with the Directory.
    -- For more information, see arns.
    BatchWrite -> Text
directoryArn :: Prelude.Text,
    -- | A list of operations that are part of the batch.
    BatchWrite -> [BatchWriteOperation]
operations :: [BatchWriteOperation]
  }
  deriving (BatchWrite -> BatchWrite -> Bool
(BatchWrite -> BatchWrite -> Bool)
-> (BatchWrite -> BatchWrite -> Bool) -> Eq BatchWrite
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchWrite -> BatchWrite -> Bool
$c/= :: BatchWrite -> BatchWrite -> Bool
== :: BatchWrite -> BatchWrite -> Bool
$c== :: BatchWrite -> BatchWrite -> Bool
Prelude.Eq, ReadPrec [BatchWrite]
ReadPrec BatchWrite
Int -> ReadS BatchWrite
ReadS [BatchWrite]
(Int -> ReadS BatchWrite)
-> ReadS [BatchWrite]
-> ReadPrec BatchWrite
-> ReadPrec [BatchWrite]
-> Read BatchWrite
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchWrite]
$creadListPrec :: ReadPrec [BatchWrite]
readPrec :: ReadPrec BatchWrite
$creadPrec :: ReadPrec BatchWrite
readList :: ReadS [BatchWrite]
$creadList :: ReadS [BatchWrite]
readsPrec :: Int -> ReadS BatchWrite
$creadsPrec :: Int -> ReadS BatchWrite
Prelude.Read, Int -> BatchWrite -> ShowS
[BatchWrite] -> ShowS
BatchWrite -> String
(Int -> BatchWrite -> ShowS)
-> (BatchWrite -> String)
-> ([BatchWrite] -> ShowS)
-> Show BatchWrite
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchWrite] -> ShowS
$cshowList :: [BatchWrite] -> ShowS
show :: BatchWrite -> String
$cshow :: BatchWrite -> String
showsPrec :: Int -> BatchWrite -> ShowS
$cshowsPrec :: Int -> BatchWrite -> ShowS
Prelude.Show, (forall x. BatchWrite -> Rep BatchWrite x)
-> (forall x. Rep BatchWrite x -> BatchWrite) -> Generic BatchWrite
forall x. Rep BatchWrite x -> BatchWrite
forall x. BatchWrite -> Rep BatchWrite x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BatchWrite x -> BatchWrite
$cfrom :: forall x. BatchWrite -> Rep BatchWrite x
Prelude.Generic)

-- |
-- Create a value of 'BatchWrite' 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:
--
-- 'directoryArn', 'batchWrite_directoryArn' - The Amazon Resource Name (ARN) that is associated with the Directory.
-- For more information, see arns.
--
-- 'operations', 'batchWrite_operations' - A list of operations that are part of the batch.
newBatchWrite ::
  -- | 'directoryArn'
  Prelude.Text ->
  BatchWrite
newBatchWrite :: Text -> BatchWrite
newBatchWrite Text
pDirectoryArn_ =
  BatchWrite' :: Text -> [BatchWriteOperation] -> BatchWrite
BatchWrite'
    { $sel:directoryArn:BatchWrite' :: Text
directoryArn = Text
pDirectoryArn_,
      $sel:operations:BatchWrite' :: [BatchWriteOperation]
operations = [BatchWriteOperation]
forall a. Monoid a => a
Prelude.mempty
    }

-- | The Amazon Resource Name (ARN) that is associated with the Directory.
-- For more information, see arns.
batchWrite_directoryArn :: Lens.Lens' BatchWrite Prelude.Text
batchWrite_directoryArn :: (Text -> f Text) -> BatchWrite -> f BatchWrite
batchWrite_directoryArn = (BatchWrite -> Text)
-> (BatchWrite -> Text -> BatchWrite)
-> Lens BatchWrite BatchWrite Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\BatchWrite' {Text
directoryArn :: Text
$sel:directoryArn:BatchWrite' :: BatchWrite -> Text
directoryArn} -> Text
directoryArn) (\s :: BatchWrite
s@BatchWrite' {} Text
a -> BatchWrite
s {$sel:directoryArn:BatchWrite' :: Text
directoryArn = Text
a} :: BatchWrite)

-- | A list of operations that are part of the batch.
batchWrite_operations :: Lens.Lens' BatchWrite [BatchWriteOperation]
batchWrite_operations :: ([BatchWriteOperation] -> f [BatchWriteOperation])
-> BatchWrite -> f BatchWrite
batchWrite_operations = (BatchWrite -> [BatchWriteOperation])
-> (BatchWrite -> [BatchWriteOperation] -> BatchWrite)
-> Lens
     BatchWrite BatchWrite [BatchWriteOperation] [BatchWriteOperation]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\BatchWrite' {[BatchWriteOperation]
operations :: [BatchWriteOperation]
$sel:operations:BatchWrite' :: BatchWrite -> [BatchWriteOperation]
operations} -> [BatchWriteOperation]
operations) (\s :: BatchWrite
s@BatchWrite' {} [BatchWriteOperation]
a -> BatchWrite
s {$sel:operations:BatchWrite' :: [BatchWriteOperation]
operations = [BatchWriteOperation]
a} :: BatchWrite) (([BatchWriteOperation] -> f [BatchWriteOperation])
 -> BatchWrite -> f BatchWrite)
-> (([BatchWriteOperation] -> f [BatchWriteOperation])
    -> [BatchWriteOperation] -> f [BatchWriteOperation])
-> ([BatchWriteOperation] -> f [BatchWriteOperation])
-> BatchWrite
-> f BatchWrite
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([BatchWriteOperation] -> f [BatchWriteOperation])
-> [BatchWriteOperation] -> f [BatchWriteOperation]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

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

instance Prelude.NFData BatchWrite

instance Core.ToHeaders BatchWrite where
  toHeaders :: BatchWrite -> ResponseHeaders
toHeaders BatchWrite' {[BatchWriteOperation]
Text
operations :: [BatchWriteOperation]
directoryArn :: Text
$sel:operations:BatchWrite' :: BatchWrite -> [BatchWriteOperation]
$sel:directoryArn:BatchWrite' :: BatchWrite -> Text
..} =
    [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [HeaderName
"x-amz-data-partition" HeaderName -> Text -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# Text
directoryArn]

instance Core.ToJSON BatchWrite where
  toJSON :: BatchWrite -> Value
toJSON BatchWrite' {[BatchWriteOperation]
Text
operations :: [BatchWriteOperation]
directoryArn :: Text
$sel:operations:BatchWrite' :: BatchWrite -> [BatchWriteOperation]
$sel:directoryArn:BatchWrite' :: BatchWrite -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Operations" Text -> [BatchWriteOperation] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= [BatchWriteOperation]
operations)]
      )

instance Core.ToPath BatchWrite where
  toPath :: BatchWrite -> ByteString
toPath =
    ByteString -> BatchWrite -> ByteString
forall a b. a -> b -> a
Prelude.const
      ByteString
"/amazonclouddirectory/2017-01-11/batchwrite"

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

-- | /See:/ 'newBatchWriteResponse' smart constructor.
data BatchWriteResponse = BatchWriteResponse'
  { -- | A list of all the responses for each batch write.
    BatchWriteResponse -> Maybe [BatchWriteOperationResponse]
responses :: Prelude.Maybe [BatchWriteOperationResponse],
    -- | The response's http status code.
    BatchWriteResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (BatchWriteResponse -> BatchWriteResponse -> Bool
(BatchWriteResponse -> BatchWriteResponse -> Bool)
-> (BatchWriteResponse -> BatchWriteResponse -> Bool)
-> Eq BatchWriteResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BatchWriteResponse -> BatchWriteResponse -> Bool
$c/= :: BatchWriteResponse -> BatchWriteResponse -> Bool
== :: BatchWriteResponse -> BatchWriteResponse -> Bool
$c== :: BatchWriteResponse -> BatchWriteResponse -> Bool
Prelude.Eq, ReadPrec [BatchWriteResponse]
ReadPrec BatchWriteResponse
Int -> ReadS BatchWriteResponse
ReadS [BatchWriteResponse]
(Int -> ReadS BatchWriteResponse)
-> ReadS [BatchWriteResponse]
-> ReadPrec BatchWriteResponse
-> ReadPrec [BatchWriteResponse]
-> Read BatchWriteResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BatchWriteResponse]
$creadListPrec :: ReadPrec [BatchWriteResponse]
readPrec :: ReadPrec BatchWriteResponse
$creadPrec :: ReadPrec BatchWriteResponse
readList :: ReadS [BatchWriteResponse]
$creadList :: ReadS [BatchWriteResponse]
readsPrec :: Int -> ReadS BatchWriteResponse
$creadsPrec :: Int -> ReadS BatchWriteResponse
Prelude.Read, Int -> BatchWriteResponse -> ShowS
[BatchWriteResponse] -> ShowS
BatchWriteResponse -> String
(Int -> BatchWriteResponse -> ShowS)
-> (BatchWriteResponse -> String)
-> ([BatchWriteResponse] -> ShowS)
-> Show BatchWriteResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BatchWriteResponse] -> ShowS
$cshowList :: [BatchWriteResponse] -> ShowS
show :: BatchWriteResponse -> String
$cshow :: BatchWriteResponse -> String
showsPrec :: Int -> BatchWriteResponse -> ShowS
$cshowsPrec :: Int -> BatchWriteResponse -> ShowS
Prelude.Show, (forall x. BatchWriteResponse -> Rep BatchWriteResponse x)
-> (forall x. Rep BatchWriteResponse x -> BatchWriteResponse)
-> Generic BatchWriteResponse
forall x. Rep BatchWriteResponse x -> BatchWriteResponse
forall x. BatchWriteResponse -> Rep BatchWriteResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BatchWriteResponse x -> BatchWriteResponse
$cfrom :: forall x. BatchWriteResponse -> Rep BatchWriteResponse x
Prelude.Generic)

-- |
-- Create a value of 'BatchWriteResponse' 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:
--
-- 'responses', 'batchWriteResponse_responses' - A list of all the responses for each batch write.
--
-- 'httpStatus', 'batchWriteResponse_httpStatus' - The response's http status code.
newBatchWriteResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  BatchWriteResponse
newBatchWriteResponse :: Int -> BatchWriteResponse
newBatchWriteResponse Int
pHttpStatus_ =
  BatchWriteResponse' :: Maybe [BatchWriteOperationResponse] -> Int -> BatchWriteResponse
BatchWriteResponse'
    { $sel:responses:BatchWriteResponse' :: Maybe [BatchWriteOperationResponse]
responses = Maybe [BatchWriteOperationResponse]
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:BatchWriteResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | A list of all the responses for each batch write.
batchWriteResponse_responses :: Lens.Lens' BatchWriteResponse (Prelude.Maybe [BatchWriteOperationResponse])
batchWriteResponse_responses :: (Maybe [BatchWriteOperationResponse]
 -> f (Maybe [BatchWriteOperationResponse]))
-> BatchWriteResponse -> f BatchWriteResponse
batchWriteResponse_responses = (BatchWriteResponse -> Maybe [BatchWriteOperationResponse])
-> (BatchWriteResponse
    -> Maybe [BatchWriteOperationResponse] -> BatchWriteResponse)
-> Lens
     BatchWriteResponse
     BatchWriteResponse
     (Maybe [BatchWriteOperationResponse])
     (Maybe [BatchWriteOperationResponse])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\BatchWriteResponse' {Maybe [BatchWriteOperationResponse]
responses :: Maybe [BatchWriteOperationResponse]
$sel:responses:BatchWriteResponse' :: BatchWriteResponse -> Maybe [BatchWriteOperationResponse]
responses} -> Maybe [BatchWriteOperationResponse]
responses) (\s :: BatchWriteResponse
s@BatchWriteResponse' {} Maybe [BatchWriteOperationResponse]
a -> BatchWriteResponse
s {$sel:responses:BatchWriteResponse' :: Maybe [BatchWriteOperationResponse]
responses = Maybe [BatchWriteOperationResponse]
a} :: BatchWriteResponse) ((Maybe [BatchWriteOperationResponse]
  -> f (Maybe [BatchWriteOperationResponse]))
 -> BatchWriteResponse -> f BatchWriteResponse)
-> ((Maybe [BatchWriteOperationResponse]
     -> f (Maybe [BatchWriteOperationResponse]))
    -> Maybe [BatchWriteOperationResponse]
    -> f (Maybe [BatchWriteOperationResponse]))
-> (Maybe [BatchWriteOperationResponse]
    -> f (Maybe [BatchWriteOperationResponse]))
-> BatchWriteResponse
-> f BatchWriteResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
-> Iso
     (Maybe [BatchWriteOperationResponse])
     (Maybe [BatchWriteOperationResponse])
     (Maybe [BatchWriteOperationResponse])
     (Maybe [BatchWriteOperationResponse])
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
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
  [BatchWriteOperationResponse]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

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

instance Prelude.NFData BatchWriteResponse