gpt4 book ai didi

http - 如何使用 wreq 只下载部分响应?

转载 作者:可可西里 更新时间:2023-11-01 17:34:52 24 4
gpt4 key购买 nike

如何使用 wreq 仅下载响应的前 1 KiB?图书馆?

理论上我可以使用 Range HTTP header 。但是,这需要服务器的支持,我不能依赖它。

一旦客户端上有足够的数据,我该如何中止请求?

最佳答案

一种选择是使用 foldGet函数,在数据到达时累积数据 block ,并在下载足够的数据后抛出异常。累积的数据将在异常中传递出去。

import qualified Control.Exception           as E
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.ByteString.Builder

data EnoughException = Enough Builder

instance Show EnoughException where
show _ = "EnoughException"

instance E.Exception EnoughException

getFile :: Int -> String -> IO BL.ByteString
getFile maxLen url = ((extract . fst) <$> foldGet process (mempty, 0) url) `E.catch` enough
where
extract :: Builder -> String
extract = BL.take (fromIntegral maxLen) . toLazyByteString

process :: (Builder, Int) -- ^(Accumulated data, current length)
-> BS.ByteString -- ^Downloded chunk
-> IO (Builder, Int)
process (acc, cur) now = do
let acc' = acc <> byteString now
let cur' = cur + BS.length now
if cur' > maxLen
then E.throw (Enough acc')
else return (acc', cur')

enough (Enough d) = return (extract d)

(注意:此函数不处理可能由 wreq 抛出的异常。对于实际使用,这是必要的。)

关于http - 如何使用 wreq 只下载部分响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34706535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com