{-# 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.MediaStore.Types.CorsRule
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.MediaStore.Types.CorsRule where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.MediaStore.Types.MethodName
import qualified Amazonka.Prelude as Prelude

-- | A rule for a CORS policy. You can add up to 100 rules to a CORS policy.
-- If more than one rule applies, the service uses the first applicable
-- rule listed.
--
-- /See:/ 'newCorsRule' smart constructor.
data CorsRule = CorsRule'
  { -- | Identifies an HTTP method that the origin that is specified in the rule
    -- is allowed to execute.
    --
    -- Each CORS rule must contain at least one @AllowedMethods@ and one
    -- @AllowedOrigins@ element.
    CorsRule -> Maybe (NonEmpty MethodName)
allowedMethods :: Prelude.Maybe (Prelude.NonEmpty MethodName),
    -- | One or more headers in the response that you want users to be able to
    -- access from their applications (for example, from a JavaScript
    -- @XMLHttpRequest@ object).
    --
    -- This element is optional for each rule.
    CorsRule -> Maybe [Text]
exposeHeaders :: Prelude.Maybe [Prelude.Text],
    -- | The time in seconds that your browser caches the preflight response for
    -- the specified resource.
    --
    -- A CORS rule can have only one @MaxAgeSeconds@ element.
    CorsRule -> Maybe Natural
maxAgeSeconds :: Prelude.Maybe Prelude.Natural,
    -- | One or more response headers that you want users to be able to access
    -- from their applications (for example, from a JavaScript @XMLHttpRequest@
    -- object).
    --
    -- Each CORS rule must have at least one @AllowedOrigins@ element. The
    -- string value can include only one wildcard character (*), for example,
    -- http:\/\/*.example.com. Additionally, you can specify only one wildcard
    -- character to allow cross-origin access for all origins.
    CorsRule -> NonEmpty Text
allowedOrigins :: Prelude.NonEmpty Prelude.Text,
    -- | Specifies which headers are allowed in a preflight @OPTIONS@ request
    -- through the @Access-Control-Request-Headers@ header. Each header name
    -- that is specified in @Access-Control-Request-Headers@ must have a
    -- corresponding entry in the rule. Only the headers that were requested
    -- are sent back.
    --
    -- This element can contain only one wildcard character (*).
    CorsRule -> [Text]
allowedHeaders :: [Prelude.Text]
  }
  deriving (CorsRule -> CorsRule -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CorsRule -> CorsRule -> Bool
$c/= :: CorsRule -> CorsRule -> Bool
== :: CorsRule -> CorsRule -> Bool
$c== :: CorsRule -> CorsRule -> Bool
Prelude.Eq, ReadPrec [CorsRule]
ReadPrec CorsRule
Int -> ReadS CorsRule
ReadS [CorsRule]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CorsRule]
$creadListPrec :: ReadPrec [CorsRule]
readPrec :: ReadPrec CorsRule
$creadPrec :: ReadPrec CorsRule
readList :: ReadS [CorsRule]
$creadList :: ReadS [CorsRule]
readsPrec :: Int -> ReadS CorsRule
$creadsPrec :: Int -> ReadS CorsRule
Prelude.Read, Int -> CorsRule -> ShowS
[CorsRule] -> ShowS
CorsRule -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CorsRule] -> ShowS
$cshowList :: [CorsRule] -> ShowS
show :: CorsRule -> String
$cshow :: CorsRule -> String
showsPrec :: Int -> CorsRule -> ShowS
$cshowsPrec :: Int -> CorsRule -> ShowS
Prelude.Show, forall x. Rep CorsRule x -> CorsRule
forall x. CorsRule -> Rep CorsRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CorsRule x -> CorsRule
$cfrom :: forall x. CorsRule -> Rep CorsRule x
Prelude.Generic)

-- |
-- Create a value of 'CorsRule' 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:
--
-- 'allowedMethods', 'corsRule_allowedMethods' - Identifies an HTTP method that the origin that is specified in the rule
-- is allowed to execute.
--
-- Each CORS rule must contain at least one @AllowedMethods@ and one
-- @AllowedOrigins@ element.
--
-- 'exposeHeaders', 'corsRule_exposeHeaders' - One or more headers in the response that you want users to be able to
-- access from their applications (for example, from a JavaScript
-- @XMLHttpRequest@ object).
--
-- This element is optional for each rule.
--
-- 'maxAgeSeconds', 'corsRule_maxAgeSeconds' - The time in seconds that your browser caches the preflight response for
-- the specified resource.
--
-- A CORS rule can have only one @MaxAgeSeconds@ element.
--
-- 'allowedOrigins', 'corsRule_allowedOrigins' - One or more response headers that you want users to be able to access
-- from their applications (for example, from a JavaScript @XMLHttpRequest@
-- object).
--
-- Each CORS rule must have at least one @AllowedOrigins@ element. The
-- string value can include only one wildcard character (*), for example,
-- http:\/\/*.example.com. Additionally, you can specify only one wildcard
-- character to allow cross-origin access for all origins.
--
-- 'allowedHeaders', 'corsRule_allowedHeaders' - Specifies which headers are allowed in a preflight @OPTIONS@ request
-- through the @Access-Control-Request-Headers@ header. Each header name
-- that is specified in @Access-Control-Request-Headers@ must have a
-- corresponding entry in the rule. Only the headers that were requested
-- are sent back.
--
-- This element can contain only one wildcard character (*).
newCorsRule ::
  -- | 'allowedOrigins'
  Prelude.NonEmpty Prelude.Text ->
  CorsRule
newCorsRule :: NonEmpty Text -> CorsRule
newCorsRule NonEmpty Text
pAllowedOrigins_ =
  CorsRule'
    { $sel:allowedMethods:CorsRule' :: Maybe (NonEmpty MethodName)
allowedMethods = forall a. Maybe a
Prelude.Nothing,
      $sel:exposeHeaders:CorsRule' :: Maybe [Text]
exposeHeaders = forall a. Maybe a
Prelude.Nothing,
      $sel:maxAgeSeconds:CorsRule' :: Maybe Natural
maxAgeSeconds = forall a. Maybe a
Prelude.Nothing,
      $sel:allowedOrigins:CorsRule' :: NonEmpty Text
allowedOrigins =
        forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced forall t b. AReview t b -> b -> t
Lens.# NonEmpty Text
pAllowedOrigins_,
      $sel:allowedHeaders:CorsRule' :: [Text]
allowedHeaders = forall a. Monoid a => a
Prelude.mempty
    }

-- | Identifies an HTTP method that the origin that is specified in the rule
-- is allowed to execute.
--
-- Each CORS rule must contain at least one @AllowedMethods@ and one
-- @AllowedOrigins@ element.
corsRule_allowedMethods :: Lens.Lens' CorsRule (Prelude.Maybe (Prelude.NonEmpty MethodName))
corsRule_allowedMethods :: Lens' CorsRule (Maybe (NonEmpty MethodName))
corsRule_allowedMethods = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CorsRule' {Maybe (NonEmpty MethodName)
allowedMethods :: Maybe (NonEmpty MethodName)
$sel:allowedMethods:CorsRule' :: CorsRule -> Maybe (NonEmpty MethodName)
allowedMethods} -> Maybe (NonEmpty MethodName)
allowedMethods) (\s :: CorsRule
s@CorsRule' {} Maybe (NonEmpty MethodName)
a -> CorsRule
s {$sel:allowedMethods:CorsRule' :: Maybe (NonEmpty MethodName)
allowedMethods = Maybe (NonEmpty MethodName)
a} :: CorsRule) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. 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 forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | One or more headers in the response that you want users to be able to
-- access from their applications (for example, from a JavaScript
-- @XMLHttpRequest@ object).
--
-- This element is optional for each rule.
corsRule_exposeHeaders :: Lens.Lens' CorsRule (Prelude.Maybe [Prelude.Text])
corsRule_exposeHeaders :: Lens' CorsRule (Maybe [Text])
corsRule_exposeHeaders = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CorsRule' {Maybe [Text]
exposeHeaders :: Maybe [Text]
$sel:exposeHeaders:CorsRule' :: CorsRule -> Maybe [Text]
exposeHeaders} -> Maybe [Text]
exposeHeaders) (\s :: CorsRule
s@CorsRule' {} Maybe [Text]
a -> CorsRule
s {$sel:exposeHeaders:CorsRule' :: Maybe [Text]
exposeHeaders = Maybe [Text]
a} :: CorsRule) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. 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 forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The time in seconds that your browser caches the preflight response for
-- the specified resource.
--
-- A CORS rule can have only one @MaxAgeSeconds@ element.
corsRule_maxAgeSeconds :: Lens.Lens' CorsRule (Prelude.Maybe Prelude.Natural)
corsRule_maxAgeSeconds :: Lens' CorsRule (Maybe Natural)
corsRule_maxAgeSeconds = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CorsRule' {Maybe Natural
maxAgeSeconds :: Maybe Natural
$sel:maxAgeSeconds:CorsRule' :: CorsRule -> Maybe Natural
maxAgeSeconds} -> Maybe Natural
maxAgeSeconds) (\s :: CorsRule
s@CorsRule' {} Maybe Natural
a -> CorsRule
s {$sel:maxAgeSeconds:CorsRule' :: Maybe Natural
maxAgeSeconds = Maybe Natural
a} :: CorsRule)

-- | One or more response headers that you want users to be able to access
-- from their applications (for example, from a JavaScript @XMLHttpRequest@
-- object).
--
-- Each CORS rule must have at least one @AllowedOrigins@ element. The
-- string value can include only one wildcard character (*), for example,
-- http:\/\/*.example.com. Additionally, you can specify only one wildcard
-- character to allow cross-origin access for all origins.
corsRule_allowedOrigins :: Lens.Lens' CorsRule (Prelude.NonEmpty Prelude.Text)
corsRule_allowedOrigins :: Lens' CorsRule (NonEmpty Text)
corsRule_allowedOrigins = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CorsRule' {NonEmpty Text
allowedOrigins :: NonEmpty Text
$sel:allowedOrigins:CorsRule' :: CorsRule -> NonEmpty Text
allowedOrigins} -> NonEmpty Text
allowedOrigins) (\s :: CorsRule
s@CorsRule' {} NonEmpty Text
a -> CorsRule
s {$sel:allowedOrigins:CorsRule' :: NonEmpty Text
allowedOrigins = NonEmpty Text
a} :: CorsRule) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Specifies which headers are allowed in a preflight @OPTIONS@ request
-- through the @Access-Control-Request-Headers@ header. Each header name
-- that is specified in @Access-Control-Request-Headers@ must have a
-- corresponding entry in the rule. Only the headers that were requested
-- are sent back.
--
-- This element can contain only one wildcard character (*).
corsRule_allowedHeaders :: Lens.Lens' CorsRule [Prelude.Text]
corsRule_allowedHeaders :: Lens' CorsRule [Text]
corsRule_allowedHeaders = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CorsRule' {[Text]
allowedHeaders :: [Text]
$sel:allowedHeaders:CorsRule' :: CorsRule -> [Text]
allowedHeaders} -> [Text]
allowedHeaders) (\s :: CorsRule
s@CorsRule' {} [Text]
a -> CorsRule
s {$sel:allowedHeaders:CorsRule' :: [Text]
allowedHeaders = [Text]
a} :: CorsRule) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Data.FromJSON CorsRule where
  parseJSON :: Value -> Parser CorsRule
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"CorsRule"
      ( \Object
x ->
          Maybe (NonEmpty MethodName)
-> Maybe [Text]
-> Maybe Natural
-> NonEmpty Text
-> [Text]
-> CorsRule
CorsRule'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"AllowedMethods")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"ExposeHeaders" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"MaxAgeSeconds")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"AllowedOrigins")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ( Object
x
                            forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"AllowedHeaders"
                            forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty
                        )
      )

instance Prelude.Hashable CorsRule where
  hashWithSalt :: Int -> CorsRule -> Int
hashWithSalt Int
_salt CorsRule' {[Text]
Maybe Natural
Maybe [Text]
Maybe (NonEmpty MethodName)
NonEmpty Text
allowedHeaders :: [Text]
allowedOrigins :: NonEmpty Text
maxAgeSeconds :: Maybe Natural
exposeHeaders :: Maybe [Text]
allowedMethods :: Maybe (NonEmpty MethodName)
$sel:allowedHeaders:CorsRule' :: CorsRule -> [Text]
$sel:allowedOrigins:CorsRule' :: CorsRule -> NonEmpty Text
$sel:maxAgeSeconds:CorsRule' :: CorsRule -> Maybe Natural
$sel:exposeHeaders:CorsRule' :: CorsRule -> Maybe [Text]
$sel:allowedMethods:CorsRule' :: CorsRule -> Maybe (NonEmpty MethodName)
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty MethodName)
allowedMethods
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Text]
exposeHeaders
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
maxAgeSeconds
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` NonEmpty Text
allowedOrigins
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` [Text]
allowedHeaders

instance Prelude.NFData CorsRule where
  rnf :: CorsRule -> ()
rnf CorsRule' {[Text]
Maybe Natural
Maybe [Text]
Maybe (NonEmpty MethodName)
NonEmpty Text
allowedHeaders :: [Text]
allowedOrigins :: NonEmpty Text
maxAgeSeconds :: Maybe Natural
exposeHeaders :: Maybe [Text]
allowedMethods :: Maybe (NonEmpty MethodName)
$sel:allowedHeaders:CorsRule' :: CorsRule -> [Text]
$sel:allowedOrigins:CorsRule' :: CorsRule -> NonEmpty Text
$sel:maxAgeSeconds:CorsRule' :: CorsRule -> Maybe Natural
$sel:exposeHeaders:CorsRule' :: CorsRule -> Maybe [Text]
$sel:allowedMethods:CorsRule' :: CorsRule -> Maybe (NonEmpty MethodName)
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty MethodName)
allowedMethods
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Text]
exposeHeaders
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
maxAgeSeconds
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf NonEmpty Text
allowedOrigins
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf [Text]
allowedHeaders

instance Data.ToJSON CorsRule where
  toJSON :: CorsRule -> Value
toJSON CorsRule' {[Text]
Maybe Natural
Maybe [Text]
Maybe (NonEmpty MethodName)
NonEmpty Text
allowedHeaders :: [Text]
allowedOrigins :: NonEmpty Text
maxAgeSeconds :: Maybe Natural
exposeHeaders :: Maybe [Text]
allowedMethods :: Maybe (NonEmpty MethodName)
$sel:allowedHeaders:CorsRule' :: CorsRule -> [Text]
$sel:allowedOrigins:CorsRule' :: CorsRule -> NonEmpty Text
$sel:maxAgeSeconds:CorsRule' :: CorsRule -> Maybe Natural
$sel:exposeHeaders:CorsRule' :: CorsRule -> Maybe [Text]
$sel:allowedMethods:CorsRule' :: CorsRule -> Maybe (NonEmpty MethodName)
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"AllowedMethods" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty MethodName)
allowedMethods,
            (Key
"ExposeHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
exposeHeaders,
            (Key
"MaxAgeSeconds" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
maxAgeSeconds,
            forall a. a -> Maybe a
Prelude.Just
              (Key
"AllowedOrigins" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= NonEmpty Text
allowedOrigins),
            forall a. a -> Maybe a
Prelude.Just
              (Key
"AllowedHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= [Text]
allowedHeaders)
          ]
      )