From what I understand, you want to pipe the response body to some conduit if the response was successful and an alternate conduit in case the response wasn't successful.
I believe that the easiest solution would involve "choosing" a conduit using the if ... then ... else you already have in your code - something like
module Main where
import Conduit ( printC
)
import Data.Conduit ( runConduitRes
, (.|)
, yield
)
import Data.Conduit.Binary ( sinkFile
)
import Network.HTTP.Simple ( parseRequest
, httpSource
, getResponseStatus
, getResponseBody
)
import Network.HTTP.Types.Status ( statusIsSuccessful
)
main :: IO ()
main = do
requestText <- init <$> readFile "notes/request.txt"
downloadURL requestText "notes/sink.txt"
downloadURL :: String -> FilePath -> IO ()
downloadURL url location = do
request <- parseRequest url
runConduitRes (httpSource request processResponse)
where
processResponse response =
if statusIsSuccessful (getResponseStatus response)
then (getResponseBody response) .| sinkFile location
else yield "an alternate operation" .| printC
You can replace the yield "an alternate operation" .| printC with another conduit that does what you actually want.
Note that now sinkFile location is only executed in the success case so the failure case doesn't create any files.