尝试使用wreq解码时出错

时间:2015-05-27 15:10:16

标签: http haskell authentication

我正在努力了解如何使用镜头和wreq,结果真的让我慢下来。

这个错误似乎声称这里有一些不匹配的类型。我不确定如何处理它。我对haskell仍然相当新,这些镜头非常令人困惑。然而,wreq似乎更清洁,这就是我选择使用它的原因。任何人都可以帮我理解错误是什么,以及如何解决它?我似乎遇到了很多这些类型的错误。我知道我的代码目前不会返回Maybe TestInfo。没关系。那个错误知道如何处理。然而,这个错误,我没有。

这是我的代码:

模块测试信息:

{-# LANGUAGE OverloadedStrings #-}

module TestInformation where

import Auth
import Network.Wreq
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens (_String)


type TestNumber = String

data TestInfo = TestInfo {
                TestId       :: Int,
                TestName     :: String,
               }

instance FromJSON TestInfo


getTestInfo :: Key -> TestNumber -> Maybe TestInfo
getTestInfo key test =
  decode (res ^. responseBody . _String)
  where opts = defaults & auth ?~ oauth2Bearer key
        res  = getWith opts ("http://testsite.com/v1/tests/" ++ test)

模块验证:

module Auth where

import qualified Data.ByteString as B

type Key = B.ByteString

错误:

GHCi, version 7.10.1: http://www.haskell.org/ghc/  :? for help
[1 of 2] Compiling Auth   ( Auth.hs, interpreted )
[2 of 2] Compiling TestInformation ( TestInformation.hs, interpreted )

TestInformation.hs:36:18:
  Couldn't match type ‘Response body10’
               with ‘IO (Response Data.ByteString.Lazy.Internal.ByteString)’
Expected type: (body10
                -> Const Data.ByteString.Lazy.Internal.ByteString body10)
               -> IO (Response Data.ByteString.Lazy.Internal.ByteString)
               -> Const
                    Data.ByteString.Lazy.Internal.ByteString
                    (IO (Response Data.ByteString.Lazy.Internal.ByteString))
  Actual type: (body10
                -> Const Data.ByteString.Lazy.Internal.ByteString body10)
               -> Response body10
               -> Const Data.ByteString.Lazy.Internal.ByteString (Response body10)
In the first argument of ‘(.)’, namely ‘responseBody’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’

TestInformation.hs:36:33:
Couldn't match type ‘Data.ByteString.Lazy.Internal.ByteString’
               with ‘Data.Text.Internal.Text’
Expected type: (Data.ByteString.Lazy.Internal.ByteString
                -> Const
                     Data.ByteString.Lazy.Internal.ByteString
                     Data.ByteString.Lazy.Internal.ByteString)
               -> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
  Actual type: (Data.Text.Internal.Text
                -> Const
                     Data.ByteString.Lazy.Internal.ByteString Data.Text.Internal.Text)
               -> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
In the second argument of ‘(.)’, namely ‘_String’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’
Failed, modules loaded: Auth.
Leaving GHCi.

1 个答案:

答案 0 :(得分:1)

此类型会检查我:

getTestInfo :: Key -> TestNumber -> IO (Maybe TestInfo)
getTestInfo key test = do
  res <- getWith opts ("http://testsite.com/v1/tests/" ++ test)
  return $ decode (res ^. responseBody)
  where opts = defaults & auth ?~ oauth2Bearer key

getWith是一个IO动作,因此要获得其返回值,您需要使用monadic绑定运算符<-

完整计划: http://lpaste.net/133443 http://lpaste.net/133498