{-# LANGUAGE DataKinds                   #-}
{-# LANGUAGE DeriveGeneric               #-}
{-# LANGUAGE FlexibleInstances           #-}
{-# LANGUAGE GeneralizedNewtypeDeriving  #-}
{-# LANGUAGE LambdaCase                  #-}
{-# LANGUAGE NoImplicitPrelude           #-}
{-# LANGUAGE OverloadedStrings           #-}
{-# LANGUAGE RecordWildCards             #-}
{-# LANGUAGE TypeFamilies                #-}

{-# OPTIONS_GHC -fno-warn-unused-imports #-}

-- Module      : Network.AWS.CloudFront.UpdateStreamingDistribution
-- Copyright   : (c) 2013-2014 Brendan Hay <brendan.g.hay@gmail.com>
-- License     : This Source Code Form is subject to the terms of
--               the Mozilla Public License, v. 2.0.
--               A copy of the MPL can be found in the LICENSE file or
--               you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer  : Brendan Hay <brendan.g.hay@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--
-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- | Update a streaming distribution.
--
-- <http://docs.aws.amazon.com/AmazonCloudFront/latest/APIReference/UpdateStreamingDistribution.html>
module Network.AWS.CloudFront.UpdateStreamingDistribution
    (
    -- * Request
      UpdateStreamingDistribution
    -- ** Request constructor
    , updateStreamingDistribution
    -- ** Request lenses
    , usdId
    , usdIfMatch
    , usdStreamingDistributionConfig

    -- * Response
    , UpdateStreamingDistributionResponse
    -- ** Response constructor
    , updateStreamingDistributionResponse
    -- ** Response lenses
    , usdrETag
    , usdrStreamingDistribution
    ) where

import Network.AWS.Prelude
import Network.AWS.Request.RestXML
import Network.AWS.CloudFront.Types
import qualified GHC.Exts

data UpdateStreamingDistribution = UpdateStreamingDistribution
    { _usdId                          :: Text
    , _usdIfMatch                     :: Maybe Text
    , _usdStreamingDistributionConfig :: StreamingDistributionConfig
    } deriving (Eq, Read, Show)

-- | 'UpdateStreamingDistribution' constructor.
--
-- The fields accessible through corresponding lenses are:
--
-- * 'usdId' @::@ 'Text'
--
-- * 'usdIfMatch' @::@ 'Maybe' 'Text'
--
-- * 'usdStreamingDistributionConfig' @::@ 'StreamingDistributionConfig'
--
updateStreamingDistribution :: StreamingDistributionConfig -- ^ 'usdStreamingDistributionConfig'
                            -> Text -- ^ 'usdId'
                            -> UpdateStreamingDistribution
updateStreamingDistribution p1 p2 = UpdateStreamingDistribution
    { _usdStreamingDistributionConfig = p1
    , _usdId                          = p2
    , _usdIfMatch                     = Nothing
    }

-- | The streaming distribution's id.
usdId :: Lens' UpdateStreamingDistribution Text
usdId = lens _usdId (\s a -> s { _usdId = a })

-- | The value of the ETag header you received when retrieving the streaming
-- distribution's configuration. For example: E2QWRUHAPOMQZL.
usdIfMatch :: Lens' UpdateStreamingDistribution (Maybe Text)
usdIfMatch = lens _usdIfMatch (\s a -> s { _usdIfMatch = a })

-- | The streaming distribution's configuration information.
usdStreamingDistributionConfig :: Lens' UpdateStreamingDistribution StreamingDistributionConfig
usdStreamingDistributionConfig =
    lens _usdStreamingDistributionConfig
        (\s a -> s { _usdStreamingDistributionConfig = a })

data UpdateStreamingDistributionResponse = UpdateStreamingDistributionResponse
    { _usdrETag                  :: Maybe Text
    , _usdrStreamingDistribution :: Maybe StreamingDistribution
    } deriving (Eq, Read, Show)

-- | 'UpdateStreamingDistributionResponse' constructor.
--
-- The fields accessible through corresponding lenses are:
--
-- * 'usdrETag' @::@ 'Maybe' 'Text'
--
-- * 'usdrStreamingDistribution' @::@ 'Maybe' 'StreamingDistribution'
--
updateStreamingDistributionResponse :: UpdateStreamingDistributionResponse
updateStreamingDistributionResponse = UpdateStreamingDistributionResponse
    { _usdrStreamingDistribution = Nothing
    , _usdrETag                  = Nothing
    }

-- | The current version of the configuration. For example: E2QWRUHAPOMQZL.
usdrETag :: Lens' UpdateStreamingDistributionResponse (Maybe Text)
usdrETag = lens _usdrETag (\s a -> s { _usdrETag = a })

-- | The streaming distribution's information.
usdrStreamingDistribution :: Lens' UpdateStreamingDistributionResponse (Maybe StreamingDistribution)
usdrStreamingDistribution =
    lens _usdrStreamingDistribution
        (\s a -> s { _usdrStreamingDistribution = a })

instance ToPath UpdateStreamingDistribution where
    toPath UpdateStreamingDistribution{..} = mconcat
        [ "/2014-11-06/streaming-distribution/"
        , toText _usdId
        , "/config"
        ]

instance ToQuery UpdateStreamingDistribution where
    toQuery = const mempty

instance ToHeaders UpdateStreamingDistribution where
    toHeaders UpdateStreamingDistribution{..} = mconcat
        [ "If-Match" =: _usdIfMatch
        ]

instance ToXMLRoot UpdateStreamingDistribution where
    toXMLRoot = extractRoot ns . toXML . _usdStreamingDistributionConfig

instance ToXML UpdateStreamingDistribution

instance AWSRequest UpdateStreamingDistribution where
    type Sv UpdateStreamingDistribution = CloudFront
    type Rs UpdateStreamingDistribution = UpdateStreamingDistributionResponse

    request  = put
    response = xmlHeaderResponse $ \h x -> UpdateStreamingDistributionResponse
        <$> h ~:? "ETag"
        <*> x .@? "StreamingDistribution"
