Yesod:为 Haskell 使用 Github API v3 库

Yesod: Using Github API v3 Library for Haskell

我正在开发一个基于简单 yesod 模板的项目。我是函数式编程、haskell 和 Yesod 的新手,所以对于任何有 Yesod 经验的人来说这可能是显而易见的。目前我正在尝试使用此 library 进行 github API 调用。我遇到了一些类型问题,我什至不确定如何着手解决这些问题。

你可以找到我的经纪人here

   Handler/Home.hs:43:19:
    Couldn't match expected type ‘HandlerT
                                    App IO (Either a0 GitHub.User)’
                with actual type ‘GitHub.Request k0 GitHub.User’
    In a stmt of a 'do' block:
      possibleUser <- GitHub.userInfoForR "mike-burns"
    In the expression:
      do { maid <- maybeAuthId;
           possibleUser <- GitHub.userInfoForR "mike-burns";
           result <- either (("Error: " <>) . tshow) formatUser possibleUser;
           defaultLayout
             (do { (asWidgetT GHC.Base.. toWidget)
                     ((blaze-markup-0.7.1.1:Text.Blaze.Internal.preEscapedText
                       GHC.Base.. Data.Text.pack)
                        "<p>Your current auth ID: ");
                   (asWidgetT GHC.Base.. toWidget) (toHtml (show maid));
                   (asWidgetT GHC.Base.. toWidget)
                     ((blaze-markup-0.7.1.1:Text.Blaze.Internal.preEscapedText
                       GHC.Base.. Data.Text.pack)
                        "</p>\n");
                   .... }) }


   Handler/Home.hs:44:38:
    Couldn't match type ‘Text’ with ‘HandlerT App IO a1’
    Expected type: a0 -> HandlerT App IO a1
      Actual type: a0 -> Text
    In the second argument of ‘(.)’, namely ‘tshow’
    In the first argument of ‘either’, namely
      ‘(("Error: " <>) . tshow)’


   Handler/Home.hs:44:45:
    Couldn't match type ‘Text’ with ‘HandlerT App IO a1’
    Expected type: GitHub.User -> HandlerT App IO a1
      Actual type: GitHub.User -> Text
    In the second argument of ‘either’, namely ‘formatUser’
    In a stmt of a 'do' block:
      result <- either (("Error: " <>) . tshow) formatUser possibleUser

GitHub 库似乎是关于构建请求并 运行 处理它们的。 userInfoForR 做了这样的事 :

userInfoForR :: Name User -> Request k User

收到请求后,您可以 运行 使用以下功能之一进行请求,具体取决于您是否需要进行身份验证:

executeRequest :: Auth -> Request k a -> IO (Either Error a)
executeRequest' :: Request RO a -> IO (Either Error a)

我不知道这个具体情况,但假设您不需要身份验证。所以,下面的表达式可以解决问题:

executeRequest' (userInfoForR "mike-burns") :: IO (Either Error User)

现在,为了在 Handler 中使用它,您需要了解 HandlerMonadIO 的实例这一事实,您可以这样做:

euser <- liftIO (executeRequest' (userInfoForR "mike-burns"))
case euser of
    Left rr -> ...
    Right user -> ...