{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Http.Internal where
import qualified Control.Exception.Safe as Exception
import qualified Data.Aeson as Aeson
import qualified Data.ByteString
import qualified Data.ByteString.Lazy
import qualified Data.Dynamic as Dynamic
import Dict (Dict)
import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types.Header as Header
import qualified Network.Mime as Mime
import qualified Platform
import Prelude (IO)
data Handler = Handler
{ Handler
-> forall e expect.
(Typeable expect, Typeable e) =>
Request' e expect -> Task e expect
handlerRequest :: forall e expect. (Dynamic.Typeable expect, Dynamic.Typeable e) => Request' e expect -> Task e expect,
Handler -> forall e a. (Manager -> Task e a) -> Task e a
handlerWithThirdParty :: forall e a. (HTTP.Manager -> Task e a) -> Task e a,
Handler -> forall a. LogHandler -> (Manager -> IO a) -> IO a
handlerWithThirdPartyIO :: forall a. Platform.LogHandler -> (HTTP.Manager -> IO a) -> IO a
}
type Request a = Request' Error a
data Request' x a = Request
{
Request' x a -> Text
method :: Text,
:: [Header],
Request' x a -> Text
url :: Text,
Request' x a -> Body
body :: Body,
Request' x a -> Maybe Int
timeout :: Maybe Int,
Request' x a -> Expect' x a
expect :: Expect' x a
}
newtype = { :: Header.Header}
deriving (Header -> Header -> Bool
(Header -> Header -> Bool)
-> (Header -> Header -> Bool) -> Eq Header
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Header -> Header -> Bool
$c/= :: Header -> Header -> Bool
== :: Header -> Header -> Bool
$c== :: Header -> Header -> Bool
Eq, Int -> Header -> ShowS
[Header] -> ShowS
Header -> String
(Int -> Header -> ShowS)
-> (Header -> String) -> ([Header] -> ShowS) -> Show Header
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Header] -> ShowS
$cshowList :: [Header] -> ShowS
show :: Header -> String
$cshow :: Header -> String
showsPrec :: Int -> Header -> ShowS
$cshowsPrec :: Int -> Header -> ShowS
Show)
data Body = Body
{ Body -> ByteString
bodyContents :: Data.ByteString.Lazy.ByteString,
Body -> Maybe MimeType
bodyContentType :: Maybe Mime.MimeType
}
type Expect a = Expect' Error a
data Expect' x a where
ExpectJson :: Aeson.FromJSON a => Expect a
ExpectText :: Expect Text
ExpectWhatever :: Expect ()
ExpectTextResponse :: (Response Text -> Result x a) -> Expect' x a
ExpectBytesResponse :: (Response Data.ByteString.ByteString -> Result x a) -> Expect' x a
data Error
= BadUrl Text
| Timeout
| NetworkError Text
| BadStatus Int
| BadBody Text
deriving ((forall x. Error -> Rep Error x)
-> (forall x. Rep Error x -> Error) -> Generic Error
forall x. Rep Error x -> Error
forall x. Error -> Rep Error x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Error x -> Error
$cfrom :: forall x. Error -> Rep Error x
Generic, Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c== :: Error -> Error -> Bool
Eq, Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Error] -> ShowS
$cshowList :: [Error] -> ShowS
show :: Error -> String
$cshow :: Error -> String
showsPrec :: Int -> Error -> ShowS
$cshowsPrec :: Int -> Error -> ShowS
Show)
instance Exception.Exception Error
instance Aeson.ToJSON Error
data Response body
= BadUrl_ Text
| Timeout_
| NetworkError_ Text
| BadStatus_ Metadata body
| GoodStatus_ Metadata body
deriving ((forall x. Response body -> Rep (Response body) x)
-> (forall x. Rep (Response body) x -> Response body)
-> Generic (Response body)
forall x. Rep (Response body) x -> Response body
forall x. Response body -> Rep (Response body) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall body x. Rep (Response body) x -> Response body
forall body x. Response body -> Rep (Response body) x
$cto :: forall body x. Rep (Response body) x -> Response body
$cfrom :: forall body x. Response body -> Rep (Response body) x
Generic, Response body -> Response body -> Bool
(Response body -> Response body -> Bool)
-> (Response body -> Response body -> Bool) -> Eq (Response body)
forall body. Eq body => Response body -> Response body -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Response body -> Response body -> Bool
$c/= :: forall body. Eq body => Response body -> Response body -> Bool
== :: Response body -> Response body -> Bool
$c== :: forall body. Eq body => Response body -> Response body -> Bool
Eq, Int -> Response body -> ShowS
[Response body] -> ShowS
Response body -> String
(Int -> Response body -> ShowS)
-> (Response body -> String)
-> ([Response body] -> ShowS)
-> Show (Response body)
forall body. Show body => Int -> Response body -> ShowS
forall body. Show body => [Response body] -> ShowS
forall body. Show body => Response body -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Response body] -> ShowS
$cshowList :: forall body. Show body => [Response body] -> ShowS
show :: Response body -> String
$cshow :: forall body. Show body => Response body -> String
showsPrec :: Int -> Response body -> ShowS
$cshowsPrec :: forall body. Show body => Int -> Response body -> ShowS
Show)
instance (Dynamic.Typeable body, Show body) => Exception.Exception (Response body)
instance (Aeson.ToJSON body) => Aeson.ToJSON (Response body)
data Metadata = Metadata
{
Metadata -> Int
metadataStatusCode :: Int,
Metadata -> Text
metadataStatusText :: Text,
:: Dict Text Text
}
deriving ((forall x. Metadata -> Rep Metadata x)
-> (forall x. Rep Metadata x -> Metadata) -> Generic Metadata
forall x. Rep Metadata x -> Metadata
forall x. Metadata -> Rep Metadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Metadata x -> Metadata
$cfrom :: forall x. Metadata -> Rep Metadata x
Generic, Metadata -> Metadata -> Bool
(Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool) -> Eq Metadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Metadata -> Metadata -> Bool
$c/= :: Metadata -> Metadata -> Bool
== :: Metadata -> Metadata -> Bool
$c== :: Metadata -> Metadata -> Bool
Eq, Int -> Metadata -> ShowS
[Metadata] -> ShowS
Metadata -> String
(Int -> Metadata -> ShowS)
-> (Metadata -> String) -> ([Metadata] -> ShowS) -> Show Metadata
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Metadata] -> ShowS
$cshowList :: [Metadata] -> ShowS
show :: Metadata -> String
$cshow :: Metadata -> String
showsPrec :: Int -> Metadata -> ShowS
$cshowsPrec :: Int -> Metadata -> ShowS
Show)
instance Aeson.ToJSON Metadata