http管道中的ResumableSource导致“无法构造无限类型”错误

时间:2015-04-30 08:55:45

标签: haskell http-conduit

我正在尝试创建一个函数,该函数创建一个Source,给出一些参数,以便在ResourceT monad中正确连接到URL。我正在尝试以下方法:

{-# LANGUAGE OverloadedStrings #-}

import           Control.Monad.IO.Class       (liftIO)
import           Data.Conduit                 (($$), yield, unwrapResumable)
import qualified Data.Conduit.List            as CL
import           Network.HTTP.Conduit
import           Network.HTTP.Types           (methodPost)
import Control.Monad.Trans.Resource (runResourceT)

runquery manager  = do
  initreq <- parseUrl "http://localhost/test"
  let request = initreq{method=methodPost, requestBody=RequestBodyLBS "test"}
  response <- http request manager
  (httpsource, finalizer) <- unwrapResumable (responseBody response)
  httpsource
  finalizer

main = do
  manager <- newManager conduitManagerSettings
  runResourceT $ (runquery manager $$ CL.mapM_ (liftIO . print))

它不起作用,我从编译器得到'无法构造无限类型'错误。我可以return (httpsource,finalizer)(或者只是整个responseBody)并在以后使用它,但在我看来很奇怪。编写此代码的正确方法是什么?为什么会出现无限类型错误?

没有类型签名,我收到以下错误:

test.hs:17:3:
    Occurs check: cannot construct the infinite type:
      m
      ~
      conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
        () ByteString m
    Expected type: m ()
      Actual type: Source m ByteString
    Relevant bindings include
      finalizer :: m () (bound at test.hs:16:16)
      httpsource :: Source m ByteString (bound at test.hs:16:4)
      response :: Response
                    (conduit-1.2.4:Data.Conduit.Internal.Conduit.ResumableSource
                       m ByteString)

当我添加签名时(我希望应该这样):

runquery :: Manager -> Source (ResourceT IO) ByteString

我收到错误:

Couldn't match type ‘conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
                       () ByteString (ResourceT IO)’
              with ‘ResourceT IO’
Expected type: conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
                 () ByteString (ResourceT IO) ()
  Actual type: Source
                 (conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
                    () ByteString (ResourceT IO))
                 ByteString

我可能正在做一些无法做到的事情,但我无法确定问题出在哪里。

1 个答案:

答案 0 :(得分:0)

unwrapResumable,http和需要在resourceT中运行的终结器似乎只能在MonadResource上下文中工作(例如resourceT),所以你必须将它们提升到它中。

runquery :: MonadResource m => Manager -> Source m ByteString
-- or runquery :: Manager -> Source (ResourceT IO) ByteString
runquery manager  = do
  initreq <- parseUrl "https://localhost/test"
  let request = initreq{method=methodPost, requestBody=RequestBodyLBS "test"}
  response <- lift $ http request manager
  (httpsource, finalizer) <- lift $ unwrapResumable (responseBody response)
  httpsource
  lift finalizer

main :: IO ()
main = do
  manager <- newManager conduitManagerSettings
  runResourceT $ (runquery manager $$ CL.mapM_ (liftIO . print))