{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE DeriveFoldable        #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DeriveTraversable     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}

module Servant.Client.Core.Response (
    Response,
    StreamingResponse,
    ResponseF (..),
    ) where

import           Prelude ()
import           Prelude.Compat

import           Control.DeepSeq
                 (NFData (..))
import qualified Data.ByteString      as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Sequence        as Seq
import           Data.Typeable
                 (Typeable)
import           GHC.Generics
                 (Generic)
import           Network.HTTP.Types
                 (Header, HttpVersion (..), Status (..))

import           Servant.API.Stream
                 (SourceIO)

data ResponseF a = Response
  { forall a. ResponseF a -> Status
responseStatusCode  :: Status
  , forall a. ResponseF a -> Seq Header
responseHeaders     :: Seq.Seq Header
  , forall a. ResponseF a -> HttpVersion
responseHttpVersion :: HttpVersion
  , forall a. ResponseF a -> a
responseBody        :: a
  } deriving (ResponseF a -> ResponseF a -> Bool
(ResponseF a -> ResponseF a -> Bool)
-> (ResponseF a -> ResponseF a -> Bool) -> Eq (ResponseF a)
forall a. Eq a => ResponseF a -> ResponseF a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ResponseF a -> ResponseF a -> Bool
== :: ResponseF a -> ResponseF a -> Bool
$c/= :: forall a. Eq a => ResponseF a -> ResponseF a -> Bool
/= :: ResponseF a -> ResponseF a -> Bool
Eq, Int -> ResponseF a -> ShowS
[ResponseF a] -> ShowS
ResponseF a -> String
(Int -> ResponseF a -> ShowS)
-> (ResponseF a -> String)
-> ([ResponseF a] -> ShowS)
-> Show (ResponseF a)
forall a. Show a => Int -> ResponseF a -> ShowS
forall a. Show a => [ResponseF a] -> ShowS
forall a. Show a => ResponseF a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ResponseF a -> ShowS
showsPrec :: Int -> ResponseF a -> ShowS
$cshow :: forall a. Show a => ResponseF a -> String
show :: ResponseF a -> String
$cshowList :: forall a. Show a => [ResponseF a] -> ShowS
showList :: [ResponseF a] -> ShowS
Show, (forall x. ResponseF a -> Rep (ResponseF a) x)
-> (forall x. Rep (ResponseF a) x -> ResponseF a)
-> Generic (ResponseF a)
forall x. Rep (ResponseF a) x -> ResponseF a
forall x. ResponseF a -> Rep (ResponseF a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ResponseF a) x -> ResponseF a
forall a x. ResponseF a -> Rep (ResponseF a) x
$cfrom :: forall a x. ResponseF a -> Rep (ResponseF a) x
from :: forall x. ResponseF a -> Rep (ResponseF a) x
$cto :: forall a x. Rep (ResponseF a) x -> ResponseF a
to :: forall x. Rep (ResponseF a) x -> ResponseF a
Generic, Typeable, (forall a b. (a -> b) -> ResponseF a -> ResponseF b)
-> (forall a b. a -> ResponseF b -> ResponseF a)
-> Functor ResponseF
forall a b. a -> ResponseF b -> ResponseF a
forall a b. (a -> b) -> ResponseF a -> ResponseF b
forall (f :: Type -> Type).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ResponseF a -> ResponseF b
fmap :: forall a b. (a -> b) -> ResponseF a -> ResponseF b
$c<$ :: forall a b. a -> ResponseF b -> ResponseF a
<$ :: forall a b. a -> ResponseF b -> ResponseF a
Functor, (forall m. Monoid m => ResponseF m -> m)
-> (forall m a. Monoid m => (a -> m) -> ResponseF a -> m)
-> (forall m a. Monoid m => (a -> m) -> ResponseF a -> m)
-> (forall a b. (a -> b -> b) -> b -> ResponseF a -> b)
-> (forall a b. (a -> b -> b) -> b -> ResponseF a -> b)
-> (forall b a. (b -> a -> b) -> b -> ResponseF a -> b)
-> (forall b a. (b -> a -> b) -> b -> ResponseF a -> b)
-> (forall a. (a -> a -> a) -> ResponseF a -> a)
-> (forall a. (a -> a -> a) -> ResponseF a -> a)
-> (forall a. ResponseF a -> [a])
-> (forall a. ResponseF a -> Bool)
-> (forall a. ResponseF a -> Int)
-> (forall a. Eq a => a -> ResponseF a -> Bool)
-> (forall a. Ord a => ResponseF a -> a)
-> (forall a. Ord a => ResponseF a -> a)
-> (forall a. Num a => ResponseF a -> a)
-> (forall a. Num a => ResponseF a -> a)
-> Foldable ResponseF
forall a. Eq a => a -> ResponseF a -> Bool
forall a. Num a => ResponseF a -> a
forall a. Ord a => ResponseF a -> a
forall m. Monoid m => ResponseF m -> m
forall a. ResponseF a -> Bool
forall a. ResponseF a -> Int
forall a. ResponseF a -> [a]
forall a. (a -> a -> a) -> ResponseF a -> a
forall m a. Monoid m => (a -> m) -> ResponseF a -> m
forall b a. (b -> a -> b) -> b -> ResponseF a -> b
forall a b. (a -> b -> b) -> b -> ResponseF a -> b
forall (t :: Type -> Type).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => ResponseF m -> m
fold :: forall m. Monoid m => ResponseF m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> ResponseF a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ResponseF a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> ResponseF a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ResponseF a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> ResponseF a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ResponseF a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> ResponseF a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ResponseF a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> ResponseF a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ResponseF a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> ResponseF a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ResponseF a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> ResponseF a -> a
foldr1 :: forall a. (a -> a -> a) -> ResponseF a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ResponseF a -> a
foldl1 :: forall a. (a -> a -> a) -> ResponseF a -> a
$ctoList :: forall a. ResponseF a -> [a]
toList :: forall a. ResponseF a -> [a]
$cnull :: forall a. ResponseF a -> Bool
null :: forall a. ResponseF a -> Bool
$clength :: forall a. ResponseF a -> Int
length :: forall a. ResponseF a -> Int
$celem :: forall a. Eq a => a -> ResponseF a -> Bool
elem :: forall a. Eq a => a -> ResponseF a -> Bool
$cmaximum :: forall a. Ord a => ResponseF a -> a
maximum :: forall a. Ord a => ResponseF a -> a
$cminimum :: forall a. Ord a => ResponseF a -> a
minimum :: forall a. Ord a => ResponseF a -> a
$csum :: forall a. Num a => ResponseF a -> a
sum :: forall a. Num a => ResponseF a -> a
$cproduct :: forall a. Num a => ResponseF a -> a
product :: forall a. Num a => ResponseF a -> a
Foldable, Functor ResponseF
Foldable ResponseF
(Functor ResponseF, Foldable ResponseF) =>
(forall (f :: Type -> Type) a b.
 Applicative f =>
 (a -> f b) -> ResponseF a -> f (ResponseF b))
-> (forall (f :: Type -> Type) a.
    Applicative f =>
    ResponseF (f a) -> f (ResponseF a))
-> (forall (m :: Type -> Type) a b.
    Monad m =>
    (a -> m b) -> ResponseF a -> m (ResponseF b))
-> (forall (m :: Type -> Type) a.
    Monad m =>
    ResponseF (m a) -> m (ResponseF a))
-> Traversable ResponseF
forall (t :: Type -> Type).
(Functor t, Foldable t) =>
(forall (f :: Type -> Type) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: Type -> Type) a.
    Applicative f =>
    t (f a) -> f (t a))
-> (forall (m :: Type -> Type) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: Type -> Type) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: Type -> Type) a.
Monad m =>
ResponseF (m a) -> m (ResponseF a)
forall (f :: Type -> Type) a.
Applicative f =>
ResponseF (f a) -> f (ResponseF a)
forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> ResponseF a -> m (ResponseF b)
forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> ResponseF a -> f (ResponseF b)
$ctraverse :: forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> ResponseF a -> f (ResponseF b)
traverse :: forall (f :: Type -> Type) a b.
Applicative f =>
(a -> f b) -> ResponseF a -> f (ResponseF b)
$csequenceA :: forall (f :: Type -> Type) a.
Applicative f =>
ResponseF (f a) -> f (ResponseF a)
sequenceA :: forall (f :: Type -> Type) a.
Applicative f =>
ResponseF (f a) -> f (ResponseF a)
$cmapM :: forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> ResponseF a -> m (ResponseF b)
mapM :: forall (m :: Type -> Type) a b.
Monad m =>
(a -> m b) -> ResponseF a -> m (ResponseF b)
$csequence :: forall (m :: Type -> Type) a.
Monad m =>
ResponseF (m a) -> m (ResponseF a)
sequence :: forall (m :: Type -> Type) a.
Monad m =>
ResponseF (m a) -> m (ResponseF a)
Traversable)

instance NFData a => NFData (ResponseF a) where
    rnf :: ResponseF a -> ()
rnf (Response Status
sc Seq Header
hs HttpVersion
hv a
body) =
        Status -> ()
rnfStatus Status
sc () -> () -> ()
forall a b. a -> b -> b
`seq`
        Seq Header -> ()
forall a. NFData a => a -> ()
rnf Seq Header
hs () -> () -> ()
forall a b. a -> b -> b
`seq`
        HttpVersion -> ()
rnfHttpVersion HttpVersion
hv () -> () -> ()
forall a b. a -> b -> b
`seq`
        a -> ()
forall a. NFData a => a -> ()
rnf a
body
      where
        rnfStatus :: Status -> ()
rnfStatus (Status Int
code ByteString
msg) = Int -> ()
forall a. NFData a => a -> ()
rnf Int
code () -> () -> ()
forall a b. a -> b -> b
`seq` ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
msg
        rnfHttpVersion :: HttpVersion -> ()
rnfHttpVersion (HttpVersion Int
_ Int
_) = () -- HttpVersion fields are strict

type Response = ResponseF LBS.ByteString
type StreamingResponse = ResponseF (SourceIO BS.ByteString)