对于 GitHub,有一个使用 Github GraphQL API 和 Python 3 的示例
https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad
(检查链接,因为它有很多评论,包括更好的身份验证代码)
# An example to get the remaining rate limit using the Github GraphQL API.
import requests
headers = {"Authorization": "Bearer YOUR API KEY"}
def run_query(query): # A simple function to use requests.post to make the API call. Note the json= section.
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers)
if request.status_code == 200:
return request.json()
else:
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query))
# The GraphQL query (with a few aditional bits included) itself defined as a multi-line string.
query = """
{
viewer {
login
}
rateLimit {
limit
cost
remaining
resetAt
}
}
"""
result = run_query(query) # Execute the query
remaining_rate_limit = result["data"]["rateLimit"]["remaining"] # Drill down the dictionary
print("Remaining rate limit - {}".format(remaining_rate_limit))
并且有很多 Python GraphQL 客户端库:
官方列表在https://graphql.org/code/#python
(向下滚动,客户端库在服务器库之后)