简体   繁体   English

Python需要两次请求GitHub / API / repo / statistics / contributors url以获得内容

[英]Python need to request GitHub/API/repo/statistics/contributors url twice to get content

I want to get particular repository's contributors and their total number of commits. 我想获取特定存储库的贡献者及其提交的总数。 I'm using Python 2.7 and Requests 2.7.0 library to request GitHub API url like: ' https://api.github.com/repos/marcboeker/mongodb-utils/stats/contributors ' (This is a random link, sorry marcboeker ^_^). 我正在使用Python 2.7和Requests 2.7.0库来请求GitHub API网址,例如:' https : //api.github.com/repos/marcboeker/mongodb-utils/stats/contributors '(这是一个随机链接,抱歉marcboeker ^ _ ^)。

However, the first time I request particular url, I got an empty dictionary response. 但是,第一次请求特定的url时,我得到了一个空的字典响应。 The second time I request the same url, I can get a list that contains information I need. 第二次我请求相同的URL时,可以获得包含所需信息的列表。 Here is my code: 这是我的代码:

import requests

contributors_url = 'https://api.github.com/repos/marcboeker/mongodb-utils/stats/contributors'
contributors = requests.get(contributors_url).json()
print contributors

I also tried to use GitHub authentication and tried to use urllib2 library. 我也尝试使用GitHub身份验证,并尝试使用urllib2库。 I also tried in Python 3.4. 我也在Python 3.4中尝试过。 But I got the same result. 但是我得到了相同的结果。 I need to get right result the first time I request the url, not second or third time. 我需要在第一次请求网址时获得正确的结果,而不是第二或第三次。 Since other GitHub API urls works fine, please explain why it's happening to 'contributors'. 由于其他GitHub API网址可以正常工作,请向“贡献者”解释为什么会发生这种情况。

第一次结果

第二次成绩

I believe this is due to the computation that needs to occur for generating the statistics. 我相信这是由于生成统计信息需要进行计算。 The API documentation outlines this: API文档对此进行了概述:

If the data hasn't been cached when you query a repository's statistics, you'll receive a 202 response; 如果在查询存储库的统计信息时尚未缓存数据,则将收到202响应;否则,将返回202 a background job is also fired to start compiling these statistics. 还触发了后台作业以开始编译这些统计信息。 Give the job a few moments to complete, and then submit the request again. 请稍等片刻以完成工作,然后再次提交请求。 If the job has completed, that request will receive a 200 response with the statistics in the response body. 如果作业已完成,则该请求将收到200响应,且响应正文中包含统计信息。

Can you check the status code in the request to verify that it's an empty response: 您能否检查请求中的状态码以验证其为空响应:

contributors_url = 'https://api.github.com/repos/marcboeker/mongodb-utils/stats/contributors' request = requests.get(contributors_url) print request.status_code

I tested this with Fiddler on a fresh repository and received both the 202 status code and an empty JSON array as the response. 我用Fiddler在一个新的存储库上对此进行了测试,并收到了202状态代码和一个空JSON数组作为响应。 So I think you need to check that status code and then retry the call after a delay... 因此,我认为您需要检查该状态码,然后在延迟后重试通话...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM