Python: 3 Ways to Fetch Data from GitHub API

Updated: January 2, 2024 By: Guest Contributor Post a comment

Method 1: Using requests library

The Python requests library is a popular HTTP library used for making all kinds of requests to web servers. It’s simple to use, and perfect for quickly fetching data from the GitHub API with minimal setup.

The steps:

  • Install the requests library using pip.
  • Import the library into your Python script.
  • Use the requests.get() method to make a GET request to the GitHub API.
  • Handle the response, which typically includes parsing the JSON data.

Example:

import requests

url = 'https://api.github.com/users/username/repos'
response = requests.get(url)
repos = response.json()

for repo in repos:
    print(repo['name'])

Pros: Easy to implement; does not require Oauth tokens for public data; well-documented library.

Cons: Not as robust for handling GitHub API’s pagination for large data sets; might require additional error handling.

Method 2: Using PyGitHub

Description: PyGitHub is a Python library to access the GitHub API v3. It’s more specialized than requests since it’s designed to work with GitHub’s endpoints, making it a powerful tool if your interactions with GitHub are complex or frequent.

The process is as follow:

  • Install PyGitHub.
  • Import the library.
  • Create a GitHub instance.
  • Fetch repositories or any other data available through the GitHub API.

Code example:

from github import Github

g = Github()
user = g.get_user('username')
for repo in user.get_repos():
    print(repo.name)

Pros: Easily interacts with GitHub’s API; abstracts away the HTTP requests; handles pagination automatically.

Cons: Heavier than using requests for simple tasks; can be slower due to its comprehensive nature.

Method 3: Using aiohttp for Asynchronous Requests

Description: aiohttp is an asynchronous HTTP client/server framework. Using it allows for non-blocking HTTP requests. This is especially useful when you need to make many API calls and don’t want to wait for each call to complete before making the next one.

You can follow these steps:

  • Install aiohttp.
  • Write an async function to make GET requests.
  • Use aiohttp to create a session and fetch data from GitHub asynchronously.
  • Parse the JSON response.

Code example (using async/await):

import aiohttp
import asyncio

async def get_repos(username):
    async with aiohttp.ClientSession() as session:
        url = f'https://api.github.com/users/{username}/repos'
        async with session.get(url) as response:
            repos = await response.json()
            return repos

username = 'username'
loop = asyncio.get_event_loop()
repos = loop.run_until_complete(get_repos(username))

for repo in repos:
    print(repo['name'])

Pros: Non-blocking; can handle multiple requests efficiently; good for high-performance applications.

Cons: More complex due to asynchronous programming; newer developers might find it challenging to understand and debug asynchronous code.

Conclusion

Accessing GitHub’s data through its API can be done in several ways using Python. The requests library is sufficient for straightforward requests, while PyGitHub provides a higher-level interface that simplifies interactions with various endpoints. For high-performance applications that require handling many requests simultaneously, aiohttp with asyncio is the better choice despite it being more challenging. Your choice on how to access the GitHub API with Python depends on your specific needs and the complexity of the API interactions you plan to perform.