{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Provider.Blockfrost.Internal.Http
( Method (..),
Request (..),
Response (..),
HttpFailure (..),
Transport (..),
withTransport,
withEndpoint,
withEndpointSettings,
maximumResponseBytes,
)
where
import qualified Cometa.Provider.Blockfrost.Internal.Config as Config
import qualified Cometa.Provider.Blockfrost.Internal.Connection as Connection
import Control.Concurrent.MVar (MVar, modifyMVar, newEmptyMVar, newMVar, putMVar, takeMVar, withMVar)
import Control.Exception (bracket, finally, mask, try, uninterruptibleMask_)
import Control.Monad (when)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Maybe (fromMaybe, isNothing)
import Data.Text (Text)
import qualified Data.Text as TextValue
import qualified Data.Text.Encoding as Text
import qualified Network.HTTP.Client as Http
import qualified Network.HTTP.Types as HTTP
import qualified Network.HTTP.Types.URI as URI
import System.Timeout (timeout)
data Method = Get | Post deriving (Method -> Method -> Bool
(Method -> Method -> Bool)
-> (Method -> Method -> Bool) -> Eq Method
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Method -> Method -> Bool
== :: Method -> Method -> Bool
$c/= :: Method -> Method -> Bool
/= :: Method -> Method -> Bool
Eq, Int -> Method -> ShowS
[Method] -> ShowS
Method -> String
(Int -> Method -> ShowS)
-> (Method -> String) -> ([Method] -> ShowS) -> Show Method
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Method -> ShowS
showsPrec :: Int -> Method -> ShowS
$cshow :: Method -> String
show :: Method -> String
$cshowList :: [Method] -> ShowS
showList :: [Method] -> ShowS
Show)
data Request = Request
{ Request -> Method
method :: !Method,
Request -> [Text]
path :: ![Text],
Request -> [(Text, Text)]
query :: ![(Text, Text)],
Request -> ByteString
body :: !ByteString,
Request -> ByteString
contentType :: !ByteString,
Request -> Maybe Int
requestTimeoutMillis :: !(Maybe Int)
}
data Response = Response
{ Response -> Int
status :: !Int,
Response -> ByteString
bodyBytes :: !ByteString,
Response -> Maybe ByteString
retryAfter :: !(Maybe ByteString)
}
deriving (Response -> Response -> Bool
(Response -> Response -> Bool)
-> (Response -> Response -> Bool) -> Eq Response
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Response -> Response -> Bool
== :: Response -> Response -> Bool
$c/= :: Response -> Response -> Bool
/= :: Response -> Response -> Bool
Eq, Int -> Response -> ShowS
[Response] -> ShowS
Response -> String
(Int -> Response -> ShowS)
-> (Response -> String) -> ([Response] -> ShowS) -> Show Response
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Response -> ShowS
showsPrec :: Int -> Response -> ShowS
$cshow :: Response -> String
show :: Response -> String
$cshowList :: [Response] -> ShowS
showList :: [Response] -> ShowS
Show)
data HttpFailure = Timeout | ConnectionFailure | ResponseTooLarge | Closed
deriving (HttpFailure -> HttpFailure -> Bool
(HttpFailure -> HttpFailure -> Bool)
-> (HttpFailure -> HttpFailure -> Bool) -> Eq HttpFailure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HttpFailure -> HttpFailure -> Bool
== :: HttpFailure -> HttpFailure -> Bool
$c/= :: HttpFailure -> HttpFailure -> Bool
/= :: HttpFailure -> HttpFailure -> Bool
Eq, Int -> HttpFailure -> ShowS
[HttpFailure] -> ShowS
HttpFailure -> String
(Int -> HttpFailure -> ShowS)
-> (HttpFailure -> String)
-> ([HttpFailure] -> ShowS)
-> Show HttpFailure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HttpFailure -> ShowS
showsPrec :: Int -> HttpFailure -> ShowS
$cshow :: HttpFailure -> String
show :: HttpFailure -> String
$cshowList :: [HttpFailure] -> ShowS
showList :: [HttpFailure] -> ShowS
Show)
data Transport = Transport
{ Transport -> Request -> IO (Either HttpFailure Response)
send :: Request -> IO (Either HttpFailure Response),
Transport -> IO (Either HttpFailure ())
checkOpen :: IO (Either HttpFailure ())
}
type Scope = MVar (Maybe Http.Manager, Int)
maximumResponseBytes :: Int
maximumResponseBytes :: Int
maximumResponseBytes = Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1024
withTransport :: Config.Config -> (Transport -> IO a) -> IO a
withTransport :: forall a. Config -> (Transport -> IO a) -> IO a
withTransport Config
config = Config -> String -> (Transport -> IO a) -> IO a
forall a. Config -> String -> (Transport -> IO a) -> IO a
withEndpoint Config
config (Text -> String
TextValue.unpack (Config -> Text
Config.endpoint Config
config))
withEndpoint :: Config.Config -> String -> (Transport -> IO a) -> IO a
withEndpoint :: forall a. Config -> String -> (Transport -> IO a) -> IO a
withEndpoint = IO ManagerSettings
-> Config -> String -> (Transport -> IO a) -> IO a
forall a.
IO ManagerSettings
-> Config -> String -> (Transport -> IO a) -> IO a
withEndpointSettings IO ManagerSettings
Connection.managerSettings
withEndpointSettings :: IO Http.ManagerSettings -> Config.Config -> String -> (Transport -> IO a) -> IO a
withEndpointSettings :: forall a.
IO ManagerSettings
-> Config -> String -> (Transport -> IO a) -> IO a
withEndpointSettings IO ManagerSettings
acquireSettings Config
config String
endpoint Transport -> IO a
action =
(Error -> IO a)
-> (Request -> IO a) -> Either Error Request -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
(IO a -> Error -> IO a
forall a b. a -> b -> a
const (IOError -> IO a
forall a. IOError -> IO a
ioError (String -> IOError
userError String
"Invalid Blockfrost transport configuration")))
Request -> IO a
acquire
(Config -> Either Error ()
Config.validateConfig Config
config Either Error () -> Either Error Request -> Either Error Request
forall a b. Either Error a -> Either Error b -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Either Error Request
Config.parseEndpoint (String -> Text
TextValue.pack String
endpoint))
where
acquire :: Request -> IO a
acquire Request
template = do
ManagerSettings
settings <- IO ManagerSettings
acquireSettings
Manager
manager <- ManagerSettings -> IO Manager
Http.newManager ManagerSettings
settings
MVar ()
drained <- IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
IO (MVar (Maybe Manager, Int))
-> (MVar (Maybe Manager, Int) -> IO ())
-> (MVar (Maybe Manager, Int) -> IO a)
-> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket ((Maybe Manager, Int) -> IO (MVar (Maybe Manager, Int))
forall a. a -> IO (MVar a)
newMVar (Manager -> Maybe Manager
forall a. a -> Maybe a
Just Manager
manager, Int
0)) (MVar () -> MVar (Maybe Manager, Int) -> IO ()
closeScope MVar ()
drained) ((MVar (Maybe Manager, Int) -> IO a) -> IO a)
-> (MVar (Maybe Manager, Int) -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \MVar (Maybe Manager, Int)
scope ->
Transport -> IO a
action ((Request -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure ()) -> Transport
Transport (MVar (Maybe Manager, Int)
-> MVar ()
-> Config
-> Request
-> Request
-> IO (Either HttpFailure Response)
sendScoped MVar (Maybe Manager, Int)
scope MVar ()
drained Config
config Request
template) (MVar (Maybe Manager, Int) -> IO (Either HttpFailure ())
scopeOpen MVar (Maybe Manager, Int)
scope))
scopeOpen :: Scope -> IO (Either HttpFailure ())
scopeOpen :: MVar (Maybe Manager, Int) -> IO (Either HttpFailure ())
scopeOpen MVar (Maybe Manager, Int)
scope = MVar (Maybe Manager, Int)
-> ((Maybe Manager, Int) -> IO (Either HttpFailure ()))
-> IO (Either HttpFailure ())
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar MVar (Maybe Manager, Int)
scope (((Maybe Manager, Int) -> IO (Either HttpFailure ()))
-> IO (Either HttpFailure ()))
-> ((Maybe Manager, Int) -> IO (Either HttpFailure ()))
-> IO (Either HttpFailure ())
forall a b. (a -> b) -> a -> b
$ \(Maybe Manager
manager, Int
_) -> Either HttpFailure () -> IO (Either HttpFailure ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either HttpFailure ()
-> (Manager -> Either HttpFailure ())
-> Maybe Manager
-> Either HttpFailure ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (HttpFailure -> Either HttpFailure ()
forall a b. a -> Either a b
Left HttpFailure
Closed) (Either HttpFailure () -> Manager -> Either HttpFailure ()
forall a b. a -> b -> a
const (() -> Either HttpFailure ()
forall a b. b -> Either a b
Right ())) Maybe Manager
manager)
closeScope :: MVar () -> Scope -> IO ()
closeScope :: MVar () -> MVar (Maybe Manager, Int) -> IO ()
closeScope MVar ()
drained MVar (Maybe Manager, Int)
scope = IO () -> IO ()
forall a. IO a -> IO a
uninterruptibleMask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
Int
active <- MVar (Maybe Manager, Int)
-> ((Maybe Manager, Int) -> IO ((Maybe Manager, Int), Int))
-> IO Int
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar (Maybe Manager, Int)
scope (((Maybe Manager, Int) -> IO ((Maybe Manager, Int), Int))
-> IO Int)
-> ((Maybe Manager, Int) -> IO ((Maybe Manager, Int), Int))
-> IO Int
forall a b. (a -> b) -> a -> b
$ \(Maybe Manager
_, Int
count) -> ((Maybe Manager, Int), Int) -> IO ((Maybe Manager, Int), Int)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Maybe Manager
forall a. Maybe a
Nothing, Int
count), Int
count)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
active Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) (MVar () -> IO ()
forall a. MVar a -> IO a
takeMVar MVar ()
drained)
sendScoped :: Scope -> MVar () -> Config.Config -> Http.Request -> Request -> IO (Either HttpFailure Response)
sendScoped :: MVar (Maybe Manager, Int)
-> MVar ()
-> Config
-> Request
-> Request
-> IO (Either HttpFailure Response)
sendScoped MVar (Maybe Manager, Int)
scope MVar ()
drained Config
config Request
template Request
request = ((forall a. IO a -> IO a) -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response)
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask (((forall a. IO a -> IO a) -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response))
-> ((forall a. IO a -> IO a) -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
Maybe Manager
acquired <- MVar (Maybe Manager, Int)
-> ((Maybe Manager, Int)
-> IO ((Maybe Manager, Int), Maybe Manager))
-> IO (Maybe Manager)
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar (Maybe Manager, Int)
scope (((Maybe Manager, Int) -> IO ((Maybe Manager, Int), Maybe Manager))
-> IO (Maybe Manager))
-> ((Maybe Manager, Int)
-> IO ((Maybe Manager, Int), Maybe Manager))
-> IO (Maybe Manager)
forall a b. (a -> b) -> a -> b
$ \(Maybe Manager
manager, Int
count) ->
((Maybe Manager, Int), Maybe Manager)
-> IO ((Maybe Manager, Int), Maybe Manager)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Maybe Manager
manager, Int -> (Manager -> Int) -> Maybe Manager -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
count (Int -> Manager -> Int
forall a b. a -> b -> a
const (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Maybe Manager
manager), Maybe Manager
manager)
case Maybe Manager
acquired of
Maybe Manager
Nothing -> Either HttpFailure Response -> IO (Either HttpFailure Response)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HttpFailure -> Either HttpFailure Response
forall a b. a -> Either a b
Left HttpFailure
Closed)
Just Manager
manager ->
IO (Either HttpFailure Response)
-> IO (Either HttpFailure Response)
forall a. IO a -> IO a
restore (Config
-> Request
-> Manager
-> Request
-> IO (Either HttpFailure Response)
sendOnce Config
config Request
template Manager
manager Request
request) IO (Either HttpFailure Response)
-> IO () -> IO (Either HttpFailure Response)
forall a b. IO a -> IO b -> IO a
`finally` IO ()
release
where
release :: IO ()
release = MVar (Maybe Manager, Int)
-> ((Maybe Manager, Int) -> IO ((Maybe Manager, Int), ())) -> IO ()
forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar (Maybe Manager, Int)
scope (((Maybe Manager, Int) -> IO ((Maybe Manager, Int), ())) -> IO ())
-> ((Maybe Manager, Int) -> IO ((Maybe Manager, Int), ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Maybe Manager
manager, Int
count) -> do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
&& Maybe Manager -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Manager
manager) (MVar () -> () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ()
drained ())
((Maybe Manager, Int), ()) -> IO ((Maybe Manager, Int), ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Maybe Manager
manager, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1), ())
sendOnce :: Config.Config -> Http.Request -> Http.Manager -> Request -> IO (Either HttpFailure Response)
sendOnce :: Config
-> Request
-> Manager
-> Request
-> IO (Either HttpFailure Response)
sendOnce Config
config Request
template Manager
manager Request
request = do
Maybe (Either HttpException (Either HttpFailure Response))
result <- Int
-> IO (Either HttpException (Either HttpFailure Response))
-> IO (Maybe (Either HttpException (Either HttpFailure Response)))
forall a. Int -> IO a -> IO (Maybe a)
timeout (Int
milliseconds Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1000) (IO (Either HttpFailure Response)
-> IO (Either HttpException (Either HttpFailure Response))
forall a. IO a -> IO (Either HttpException a)
tryHttp IO (Either HttpFailure Response)
perform)
Either HttpFailure Response -> IO (Either HttpFailure Response)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either HttpFailure Response -> IO (Either HttpFailure Response))
-> Either HttpFailure Response -> IO (Either HttpFailure Response)
forall a b. (a -> b) -> a -> b
$ case Maybe (Either HttpException (Either HttpFailure Response))
result of
Maybe (Either HttpException (Either HttpFailure Response))
Nothing -> HttpFailure -> Either HttpFailure Response
forall a b. a -> Either a b
Left HttpFailure
Timeout
Just (Left HttpException
failure) -> HttpFailure -> Either HttpFailure Response
forall a b. a -> Either a b
Left (HttpException -> HttpFailure
classify HttpException
failure)
Just (Right Either HttpFailure Response
response) -> Either HttpFailure Response
response
where
milliseconds :: Int
milliseconds = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Config -> Int
Config.responseTimeoutMillis Config
config) (Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe (Config -> Int
Config.responseTimeoutMillis Config
config) (Request -> Maybe Int
requestTimeoutMillis Request
request)))
prefix :: ByteString
prefix = Request -> ByteString
Http.path Request
template
separator :: ByteString
separator = if ByteString -> ByteString -> Bool
B.isSuffixOf ByteString
"/" ByteString
prefix then ByteString
"" else ByteString
"/"
requestPath :: ByteString
requestPath = case Request -> [Text]
path Request
request of
[] -> ByteString
prefix
[Text]
segments -> ByteString
prefix ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
separator ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString -> [ByteString] -> ByteString
B.intercalate ByteString
"/" ((Text -> ByteString) -> [Text] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> ByteString -> ByteString
URI.urlEncode Bool
False (ByteString -> ByteString)
-> (Text -> ByteString) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
Text.encodeUtf8) [Text]
segments)
operationQuery :: [(ByteString, Maybe ByteString)]
operationQuery = [(Text -> ByteString
Text.encodeUtf8 Text
key, ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (Text -> ByteString
Text.encodeUtf8 Text
value)) | (Text
key, Text
value) <- Request -> [(Text, Text)]
query Request
request]
baseQuery :: [(ByteString, Maybe ByteString)]
baseQuery = ((ByteString, Maybe ByteString) -> Bool)
-> [(ByteString, Maybe ByteString)]
-> [(ByteString, Maybe ByteString)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(ByteString
key, Maybe ByteString
_) -> ByteString
key ByteString -> [ByteString] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` ((ByteString, Maybe ByteString) -> ByteString)
-> [(ByteString, Maybe ByteString)] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (ByteString, Maybe ByteString) -> ByteString
forall a b. (a, b) -> a
fst [(ByteString, Maybe ByteString)]
operationQuery) (ByteString -> [(ByteString, Maybe ByteString)]
HTTP.parseQuery (Request -> ByteString
Http.queryString Request
template))
headers :: [(HeaderName, ByteString)]
headers =
[(HeaderName
"Accept", ByteString
"application/json")]
[(HeaderName, ByteString)]
-> [(HeaderName, ByteString)] -> [(HeaderName, ByteString)]
forall a. Semigroup a => a -> a -> a
<> [(HeaderName
"project_id", ProjectId -> ByteString
Config.projectIdBytes ProjectId
credential) | Just ProjectId
credential <- [Config -> Maybe ProjectId
Config.projectId Config
config]]
[(HeaderName, ByteString)]
-> [(HeaderName, ByteString)] -> [(HeaderName, ByteString)]
forall a. Semigroup a => a -> a -> a
<> [(HeaderName
"Content-Type", Request -> ByteString
contentType Request
request) | Bool -> Bool
not (ByteString -> Bool
B.null (Request -> ByteString
contentType Request
request))]
prepared :: Request
prepared =
[(ByteString, Maybe ByteString)] -> Request -> Request
Http.setQueryString ([(ByteString, Maybe ByteString)]
baseQuery [(ByteString, Maybe ByteString)]
-> [(ByteString, Maybe ByteString)]
-> [(ByteString, Maybe ByteString)]
forall a. Semigroup a => a -> a -> a
<> [(ByteString, Maybe ByteString)]
operationQuery) (Request -> Request) -> Request -> Request
forall a b. (a -> b) -> a -> b
$
Request
template
{ Http.method = case method request of Method
Get -> ByteString
"GET"; Method
Post -> ByteString
"POST",
Http.path = requestPath,
Http.requestHeaders = headers,
Http.requestBody = Http.RequestBodyBS (body request),
Http.redirectCount = 0,
Http.checkResponse = \Request
_ Response BodyReader
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (),
Http.responseTimeout = Http.responseTimeoutMicro (milliseconds * 1000),
Http.cookieJar = Nothing
}
perform :: IO (Either HttpFailure Response)
perform = Request
-> Manager
-> (Response BodyReader -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response)
forall a.
Request -> Manager -> (Response BodyReader -> IO a) -> IO a
Http.withResponse Request
prepared Manager
manager ((Response BodyReader -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response))
-> (Response BodyReader -> IO (Either HttpFailure Response))
-> IO (Either HttpFailure Response)
forall a b. (a -> b) -> a -> b
$ \Response BodyReader
response -> do
Either HttpFailure ByteString
bytes <- Int
-> [ByteString] -> BodyReader -> IO (Either HttpFailure ByteString)
readBounded Int
maximumResponseBytes [] (Response BodyReader -> BodyReader
forall body. Response body -> body
Http.responseBody Response BodyReader
response)
Either HttpFailure Response -> IO (Either HttpFailure Response)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either HttpFailure Response -> IO (Either HttpFailure Response))
-> Either HttpFailure Response -> IO (Either HttpFailure Response)
forall a b. (a -> b) -> a -> b
$ (ByteString -> Response)
-> Either HttpFailure ByteString -> Either HttpFailure Response
forall a b.
(a -> b) -> Either HttpFailure a -> Either HttpFailure b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ByteString
payload -> Int -> ByteString -> Maybe ByteString -> Response
Response (Status -> Int
HTTP.statusCode (Response BodyReader -> Status
forall body. Response body -> Status
Http.responseStatus Response BodyReader
response)) ByteString
payload (Int -> ByteString -> ByteString
B.take Int
128 (ByteString -> ByteString) -> Maybe ByteString -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HeaderName -> [(HeaderName, ByteString)] -> Maybe ByteString
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup HeaderName
"Retry-After" (Response BodyReader -> [(HeaderName, ByteString)]
forall body. Response body -> [(HeaderName, ByteString)]
Http.responseHeaders Response BodyReader
response))) Either HttpFailure ByteString
bytes
tryHttp :: IO a -> IO (Either Http.HttpException a)
tryHttp :: forall a. IO a -> IO (Either HttpException a)
tryHttp = IO a -> IO (Either HttpException a)
forall e a. Exception e => IO a -> IO (Either e a)
try
classify :: Http.HttpException -> HttpFailure
classify :: HttpException -> HttpFailure
classify HttpException
failure = case HttpException
failure of
Http.HttpExceptionRequest Request
_ HttpExceptionContent
Http.ResponseTimeout -> HttpFailure
Timeout
Http.HttpExceptionRequest Request
_ HttpExceptionContent
Http.ConnectionTimeout -> HttpFailure
Timeout
HttpException
_ -> HttpFailure
ConnectionFailure
readBounded :: Int -> [ByteString] -> Http.BodyReader -> IO (Either HttpFailure ByteString)
readBounded :: Int
-> [ByteString] -> BodyReader -> IO (Either HttpFailure ByteString)
readBounded Int
remaining [ByteString]
chunks BodyReader
reader = do
ByteString
chunk <- BodyReader -> BodyReader
Http.brRead BodyReader
reader
if ByteString -> Bool
B.null ByteString
chunk
then Either HttpFailure ByteString -> IO (Either HttpFailure ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either HttpFailure ByteString
forall a b. b -> Either a b
Right ([ByteString] -> ByteString
B.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
chunks)))
else
if ByteString -> Int
B.length ByteString
chunk Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
remaining
then Either HttpFailure ByteString -> IO (Either HttpFailure ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HttpFailure -> Either HttpFailure ByteString
forall a b. a -> Either a b
Left HttpFailure
ResponseTooLarge)
else Int
-> [ByteString] -> BodyReader -> IO (Either HttpFailure ByteString)
readBounded (Int
remaining Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
chunk) (ByteString
chunk ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
chunks) BodyReader
reader