Skip to content

Instantly share code, notes, and snippets.

@chrissimpkins
Forked from gbaman/graphql_example.py
Created February 14, 2023 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrissimpkins/0b86e34fc77e5454a01d69b8e9d13439 to your computer and use it in GitHub Desktop.
Save chrissimpkins/0b86e34fc77e5454a01d69b8e9d13439 to your computer and use it in GitHub Desktop.
An example on using the Github GraphQL API with Python 3
# 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment