gpt4 book ai didi

http - 尝试使用 wreq 解码时出错

转载 作者:可可西里 更新时间:2023-11-01 17:36:20 26 4
gpt4 key购买 nike

我非常努力地了解如何使用 lenses 和 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.

最佳答案

这种类型为我检查:

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 操作,因此要获取其返回值,您需要使用单子(monad)绑定(bind)运算符 <- .

完整程序: http://lpaste.net/133443 http://lpaste.net/133498

关于http - 尝试使用 wreq 解码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com