haskellconduit

conduit http streaming to file (sinkFile) avoid file creation when http returns error


What is a value that I need to return from the getSrc when the http code is unsuccessful and I wish not to create a file (by sinkFile)
If i just return getResponseBody res the http error itself is saved into the file.

downloadURL :: String -> FilePath -> IO ()
downloadURL url location = do
  request <- parseRequest url
  runResourceT
         $ runConduit$  httpSource request getSrc
         .| sinkFile location
   where
     getSrc res = do
         let success = statusIsSuccessful . getResponseStatus $ res
         if success then
            getResponseBody res
         else
            ???

Solution

  • Kartin's solution should work just fine. Another approach you could take would be to use sinkFileCautious instead of sinkFile, and throw a runtime exception on an invalid status code. In fact, you could replace parseRequest with parseUrlThrow to automatically get this behavior.