douji9518
douji9518
2016-06-21 13:36

无法通过github API获取所有存储库

已采纳

I want to allow the users to signup using github account, and display all his/her private and public repositories. I am able to get the token from github and get the repositories (both public and private), but the only problem is that it is not returning all repositories (i.e. some repositories are not fetched).

I am using golang for server side implementation. Using this method to get repositories.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanpi7578 duanpi7578 5年前

    By default all the commands that accept a ListOptions argument have a PerPage attribute. In order to get all the data, you'll have to iterate through the pages using the Page attribute until the number of results you get is less than PerPage.

    In Go-ish pseudo-code, it'd look like this:

    totalResults := []Result{}
    for page := 0; ; page++ {
      results := fetch current page
      totalResults = append(totalResults, results)
      if len(results) < per page {
        break
      }
    } 
    

    You can see the ListOptions struct defined here.

    点赞 评论 复制链接分享
  • dpgvdfg321041670 dpgvdfg321041670 5年前

    As pointed out by robbrit to get all repos we have to use PerPage option, because by default only 30 repos are returned. That solved my problem.

    点赞 评论 复制链接分享

相关推荐