module Imm.HTTP.Simple (defaultManager, mkCoHttpClient, module Reexport) where
import Imm.HTTP
import Imm.Prelude
import Imm.Pretty
import Data.CaseInsensitive
import Network.Connection as Reexport
import Network.HTTP.Client as Reexport
import Network.HTTP.Client.TLS as Reexport
import URI.ByteString
mkCoHttpClient :: (MonadIO m, MonadCatch m) => Manager -> CoHttpClientF m Manager
mkCoHttpClient manager = CoHttpClientF coGet where
coGet uri = handleAny (\e -> return (Left e, manager)) $ do
result <- httpGet manager uri
return (Right result, manager)
defaultManager :: IO Manager
defaultManager = newManager $ mkManagerSettings (TLSSettingsSimple False False False) Nothing
httpGet :: (MonadIO m, MonadThrow m)
=> Manager -> URI -> m LByteString
httpGet manager uri = do
request <- makeRequest uri
responseBody <$> io (httpLbs request manager)
parseRequest' :: (MonadThrow m) => URI -> m Request
parseRequest' = parseRequest . show . prettyURI
makeRequest :: (MonadIO m, MonadThrow m) => URI -> m Request
makeRequest uri = do
req <- parseRequest' uri
return $ req { requestHeaders = [
(mk "User-Agent", "Mozilla/4.0"),
(mk "Accept", "*/*")]}