Gidgethub

5.4.0 · active · verified Thu Apr 16

Gidgethub is an asynchronous Python library for interacting with the GitHub API. It employs a sans-I/O approach, allowing users to integrate it with their preferred HTTP client library. The current version is 5.4.0 and it maintains an active release cadence, providing ongoing support and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to make a simple API call to fetch user information using `gidgethub` with `aiohttp`. It requires a GitHub Personal Access Token set in the `GH_AUTH` environment variable for authentication.

import asyncio
import os
import aiohttp
from gidgethub.aiohttp import GitHubAPI

async def get_user_info():
    # Replace 'YOUR_GH_USERNAME' with your GitHub username
    # Ensure GH_AUTH environment variable is set with a GitHub Personal Access Token
    # (e.g., export GH_AUTH='ghp_YOURTOKENHERE')
    username = 'YOUR_GH_USERNAME'
    oauth_token = os.environ.get('GH_AUTH', '')

    if not oauth_token:
        print("Error: GH_AUTH environment variable not set. Please set your GitHub Personal Access Token.")
        return

    async with aiohttp.ClientSession() as session:
        gh = GitHubAPI(session, username, oauth_token=oauth_token)
        try:
            # Fetch details for the authenticated user
            user_data = await gh.getitem(f'/users/{username}')
            print(f"User Login: {user_data['login']}")
            print(f"User Name: {user_data.get('name', 'N/A')}")
            print(f"Public Repos: {user_data['public_repos']}")
            print(f"Followers: {user_data['followers']}")
        except Exception as e:
            print(f"An error occurred: {e}")

if __name__ == '__main__':
    # For Python 3.7+
    asyncio.run(get_user_info())

view raw JSON →