{-# LANGUAGE NumDecimals #-}
module Network.HTTP.Client.BrReadWithTimeout (
fromResponseTimeout
,brReadWithTimeout
,httpLbsBrReadWithTimeout
) where
import Network.HTTP.Client hiding (HttpExceptionContent (..))
import qualified Network.HTTP.Client as E (HttpExceptionContent (..))
import qualified Network.HTTP.Client.Internal as I (ResponseTimeout (..)
,mResponseTimeout
)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as L
import Control.Exception
import System.Timeout
fromResponseTimeout :: Request -> Manager -> Int
fromResponseTimeout :: Request -> Manager -> Int
fromResponseTimeout Request
req Manager
man =
case Request -> ResponseTimeout
responseTimeout Request
req of
ResponseTimeout
I.ResponseTimeoutDefault ->
case Manager -> ResponseTimeout
I.mResponseTimeout Manager
man of
ResponseTimeout
I.ResponseTimeoutDefault -> Int
30e6
ResponseTimeout
I.ResponseTimeoutNone -> -Int
1
I.ResponseTimeoutMicro Int
u -> Int
u
ResponseTimeout
I.ResponseTimeoutNone -> -Int
1
I.ResponseTimeoutMicro Int
u -> Int
u
brReadWithTimeout :: Int -> Request -> BodyReader -> IO ByteString
brReadWithTimeout :: Int -> Request -> BodyReader -> BodyReader
brReadWithTimeout Int
tmo Request
req BodyReader
br = do
Maybe ByteString
x <- Int -> BodyReader -> IO (Maybe ByteString)
forall a. Int -> IO a -> IO (Maybe a)
timeout Int
tmo BodyReader
br
case Maybe ByteString
x of
Maybe ByteString
Nothing -> HttpException -> BodyReader
forall e a. Exception e => e -> IO a
throwIO (HttpException -> BodyReader) -> HttpException -> BodyReader
forall a b. (a -> b) -> a -> b
$ Request -> HttpExceptionContent -> HttpException
HttpExceptionRequest
Request
req { responseTimeout :: ResponseTimeout
responseTimeout = Int -> ResponseTimeout
I.ResponseTimeoutMicro Int
tmo }
HttpExceptionContent
E.ResponseTimeout
Just ByteString
bs -> ByteString -> BodyReader
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs
httpLbsBrReadWithTimeout :: Request -> Manager -> IO (Response L.ByteString)
httpLbsBrReadWithTimeout :: Request -> Manager -> IO (Response ByteString)
httpLbsBrReadWithTimeout Request
req Manager
man = Request
-> Manager
-> (Response BodyReader -> IO (Response ByteString))
-> IO (Response ByteString)
forall a.
Request -> Manager -> (Response BodyReader -> IO a) -> IO a
withResponse Request
req Manager
man ((Response BodyReader -> IO (Response ByteString))
-> IO (Response ByteString))
-> (Response BodyReader -> IO (Response ByteString))
-> IO (Response ByteString)
forall a b. (a -> b) -> a -> b
$ \Response BodyReader
res -> do
let tmo :: Int
tmo = Request -> Manager -> Int
fromResponseTimeout Request
req Manager
man
[ByteString]
bss <- BodyReader -> IO [ByteString]
brConsume (BodyReader -> IO [ByteString]) -> BodyReader -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ Int -> Request -> BodyReader -> BodyReader
brReadWithTimeout Int
tmo Request
req (BodyReader -> BodyReader) -> BodyReader -> BodyReader
forall a b. (a -> b) -> a -> b
$ Response BodyReader -> BodyReader
forall body. Response body -> body
responseBody Response BodyReader
res
Response ByteString -> IO (Response ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return Response BodyReader
res { responseBody :: ByteString
responseBody = [ByteString] -> ByteString
L.fromChunks [ByteString]
bss }