module LaunchDarkly.Server.Config.HttpConfiguration
    ( HttpConfiguration (..)
    , prepareRequest
    )
where

import Control.Monad.Catch (MonadThrow)
import Network.HTTP.Client (Manager, Request, ResponseTimeout, parseRequest, requestHeaders, responseTimeout, setRequestIgnoreStatus)
import Network.HTTP.Types (Header)

data HttpConfiguration = HttpConfiguration
    { HttpConfiguration -> [Header]
defaultRequestHeaders :: ![Header]
    , HttpConfiguration -> ResponseTimeout
defaultRequestTimeout :: !ResponseTimeout
    , HttpConfiguration -> Manager
tlsManager :: !Manager
    }

prepareRequest :: (MonadThrow m) => HttpConfiguration -> String -> m Request
prepareRequest :: HttpConfiguration -> String -> m Request
prepareRequest config :: HttpConfiguration
config uri :: String
uri = do
    Request
baseReq <- String -> m Request
forall (m :: * -> *). MonadThrow m => String -> m Request
parseRequest String
uri
    Request -> m Request
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Request -> m Request) -> Request -> m Request
forall a b. (a -> b) -> a -> b
$
        Request -> Request
setRequestIgnoreStatus (Request -> Request) -> Request -> Request
forall a b. (a -> b) -> a -> b
$
            Request
baseReq
                { requestHeaders :: [Header]
requestHeaders = HttpConfiguration -> [Header]
defaultRequestHeaders HttpConfiguration
config [Header] -> [Header] -> [Header]
forall a. Semigroup a => a -> a -> a
<> Request -> [Header]
requestHeaders Request
baseReq
                , responseTimeout :: ResponseTimeout
responseTimeout = HttpConfiguration -> ResponseTimeout
defaultRequestTimeout HttpConfiguration
config
                }