Yesod:为Haskell使用Github API v3库

时间:2017-02-07 22:05:15

标签: haskell github monads github-api yesod

我正在开发一个基于简单的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

1 个答案:

答案 0 :(得分:0)

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的实例的事实,您可以这样做:< / p>

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