Just a blog to share my tricks, code snippets

Thursday, November 9, 2017

An example of Github Rest API using Python


Suppose I want a python script to get some information of Customer IO's Github:
  • How many total open issues are there across all repositories?
  • Sort the repositories by date updated in descending order.
  • Which repository has the most watchers?

Github has an official REST API v3 document at HERE
Here are some notes before jumping into the script. They are all stated in Github API's document but I just want to make sure you are aware of:
  • For GET request, parameters are passed in the HTTP query string
curl "https://api.github.com/repos/customerio/esdb/subscribers?page=2"
In above example, the parameter page is passed in the query string. For PUT, POST, PATCH, DELETE requests, parameters are passed in the data of the request instead of query string. See the official document for more details
  • Requests that return multiple items will be paginated to 30 items by default. Information of pagination is in the response's header.
curl -i "https://api.github.com/repos/customerio/esdb/subscribers"
You will see the header will has a Link. next indicates the next page and last indicates the number of the last page
Link: <https: api.github.com="" page="2" repositories="" subscribers="">; rel="next", <https: api.github.com="" page="2" repositories="" subscribers="">; rel="last"       
  • You do not need to authenticate your requests. However, the rate limit for unauthenticated requests is only 60 requests per hour. Therefore, it is better to authenticate requests. For requests using Basic Authentication or OAuth, you can make up to 5000 requests per hour. First, you need to create an access token. Github has a guide at HERE
curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/repos/customerio/esdb/subscribers       

Now it is the full script

0 comments:

Post a Comment