Haskell Servant客户指南

问题描述 投票:1回答:1

我是Haskell和Servant的新手,正在尝试启动并运行官方教程。我一直在努力使本教程正常运行,无法一整天地工作。我讨厌在这里发布这样的问题,但老实说,我不知道为什么代码无法正常工作我没有做任何改变。我想知道还有没有其他人尝试实施本教程,并且有类似的问题。

[我已经看到一些教程的帖子不再适用于当前的仆人版本,但该教程似乎是Servant Client的最新教程。

这是教程http://haskell-servant.readthedocs.io/en/stable/tutorial/Client.html

这里是代码

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}

module Client where

import Data.Aeson
import Data.Proxy
import GHC.Generics
import Network.HTTP.Client (newManager, defaultManagerSettings)
import Servant.API
import Servant.Client

data Position = Position
  { x :: Int
  , y :: Int
  } deriving (Show, Generic)

instance FromJSON Position

newtype HelloMessage = HelloMessage { msg :: String }
  deriving (Show, Generic)

instance FromJSON HelloMessage

data ClientInfo = ClientInfo
  { clientName :: String
  , clientEmail :: String
  , clientAge :: Int
  , clientInterestedIn :: [String]
  } deriving Generic

instance ToJSON ClientInfo

data Email = Email
  { from :: String
  , to :: String
  , subject :: String
  , body :: String
  } deriving (Show, Generic)

instance FromJSON Email

type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
      :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
      :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email

position :: Int -> Int -> ClientM Position

hello :: Maybe String -> ClientM HelloMessage

marketing :: ClientInfo -> ClientM Email

api :: Proxy API
api = Proxy

position :<|> hello :<|> marketing = client api

queries :: ClientM (Position, HelloMessage, Email)
queries = do
  pos <- position 10 10 
  message <- hello (Just "servant") 
  em  <- marketing (ClientInfo "Alp" "[email protected]" 26 ["haskell", "mathematics"])
  return (pos, message, em)

run :: IO ()
run = do
  manager <- newManager defaultManagerSettings
  res <- runClientM queries (ClientEnv manager (BaseUrl Http "localhost" 8081 ""))
  case res of
    Left err -> putStrLn $ "Error: " ++ show err
    Right (pos, message, em) -> do
      print pos
      print message
      print em

这是我遇到的错误

Couldn't match type ‘http-client-0.4.31.2:Network.HTTP.Client.Types.Manager
                           -> BaseUrl -> ClientM Position’
                     with ‘Control.Monad.Trans.Except.ExceptT ServantError IO Position’
      Expected type: Int -> Int -> ClientM Position
        Actual type: Int
                     -> Int
                     -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager
                     -> BaseUrl
                     -> ClientM Position
    • When checking that the inferred type
        position :: Int
                    -> Int
                    -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager
                    -> BaseUrl
                    -> ClientM Position
      is as general as its signature
        position :: Int -> Int -> ClientM Position


Variable not in scope:
      runClientM
        :: ClientM (Position, HelloMessage, Email)
           -> t0 -> IO (Either a0 (a1, a2, a3))


Data constructor not in scope:
      ClientEnv
        :: http-client-0.4.31.2:Network.HTTP.Client.Types.Manager
           -> BaseUrl -> t0
haskell cabal servant
1个回答
0
投票

第一个“不在范围内的错误”在这里提到ClientM类型,根据the servant-client changelog,它是servant-client的0.9版本引入的。在.cabal文件依赖项中指定servant-client >= 0.9将确保cabal-install不会提取较旧的版本(尽管请注意,截至2020年3月,0.9本身已经很旧了)。对于cabal-install版本3,不需要进一步的配置步骤,因为cabal build将自动安装任何指定的依赖项。

© www.soinside.com 2019 - 2024. All rights reserved.