The two resources I found that suggested recipes for streaming downloads using popular Haskell libraries were:
- https://haskell-lang.org/library/http-client#Streaming
- http://www.alfredodinapoli.com/posts/2013-07-20-slick-http-download-in-haskell.html
How would I modify the code in the former to (a) save to file, and (b) print only a (take 5) of the byte response, rather than the whole response to stdout?
My attempt at (b) is:
#!/usr/bin/env stack
{- stack --install-ghc --resolver lts-5.13 runghc
--package http-conduit
-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as S
import qualified Data.Conduit.List as CL
import Network.HTTP.Simple
import System.IO (stdout)
main :: IO ()
main = httpSink "http://httpbin.org/get" $ \response -> do
liftIO $ putStrLn
$ "The status code was: "
++ show (getResponseStatusCode response)
CL.mapM_ (take 5) (S.hPut stdout)
Which fails to map the (take 5), and suggests to me among other things I still don't understand how mapping over monads works, or liftIO.
Also, this resource:
http://haskelliseasy.readthedocs.io/en/latest/#note-on-streaming
...gave me a warning, "I know what I'm doing and I'd like more fine-grained control over resources, such as streaming" that this not easily or generally supported.
Other places I looked:
- Downloading large files from the Internet in Haskell
- https://hackage.haskell.org/package/wreq
- https://hackage.haskell.org/package/pipes-http
If there's anything in the Haskellverse that makes this easier, more like Python's requests:
response = requests.get(URL, stream=True)
for i,chunk in enumerate(response.iter_content(BLOCK)):
f.write(chunk)
I'd appreciate the tip there, too, or pointers towards the 2016 state of the art.