or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

authentication.mdcli-tools.mdcore-api.mdevents.mdgithub-actions.mdindex.mdpagination.md
tile.json

tessl/pypi-ghapi

A python client for the GitHub API

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/ghapi@1.0.x

To install, run

npx @tessl/cli install tessl/pypi-ghapi@1.0.0

index.mddocs/

ghapi

A Python client for the GitHub API that provides 100% always-updated coverage of the entire GitHub REST API. The library automatically converts GitHub's OpenAPI specification to a Pythonic API, ensuring it stays current with the latest GitHub API changes while maintaining a compact package size.

Package Information

  • Package Name: ghapi
  • Language: Python
  • Installation: pip install ghapi
  • Requirements: Python 3.7+, fastcore>=1.7.2

Core Imports

from ghapi.all import GhApi

Individual modules:

from ghapi.core import GhApi, date2gh, gh2date, print_summary, GH_HOST, EMPTY_TREE_SHA
from ghapi.auth import GhDeviceAuth, github_auth_device, Scope, scope_str
from ghapi.actions import Event, contexts, env_github, user_repo
from ghapi.page import paged, parse_link_hdr
from ghapi.event import GhEvent, load_sample_events, save_sample_events
from ghapi.cli import ghapi, ghpath, ghraw, completion_ghapi

Basic Usage

from ghapi.all import GhApi

# Create API client with token authentication
api = GhApi(token='your_github_token')

# Access API endpoints by group and operation
repo = api.repos.get(owner='fastai', repo='ghapi')
print(f"Repository: {repo.full_name}")
print(f"Stars: {repo.stargazers_count}")

# List issues
issues = api.issues.list_for_repo(owner='fastai', repo='ghapi', state='open')
for issue in issues:
    print(f"#{issue.number}: {issue.title}")

# Create a new issue
new_issue = api.issues.create(
    owner='fastai', 
    repo='ghapi',
    title='Example Issue',
    body='This is an example issue created via ghapi'
)

# Work with gists
gist = api.create_gist(
    description='Example gist',
    content='print("Hello from ghapi!")',
    filename='example.py',
    public=False
)

Architecture

ghapi is built around several key components:

  • GhApi: The main API client that dynamically creates methods from GitHub's OpenAPI specification
  • Endpoint Groups: GitHub API endpoints organized by functionality (repos, issues, actions, etc.)
  • Authentication: Support for token-based and JWT authentication, plus OAuth device flow
  • Pagination: Automatic handling of paginated API responses
  • CLI Tools: Command-line interfaces for direct API access
  • Event System: Support for GitHub webhook events and GitHub Actions contexts

The library uses a dynamic approach where all GitHub API endpoints are automatically generated from metadata, ensuring 100% API coverage that stays synchronized with GitHub's evolving API.

Capabilities

Core API Client

The main GhApi class providing access to all GitHub API endpoints, authentication management, and core functionality like repository operations, issue management, and release handling.

class GhApi:
    def __init__(self, owner=None, repo=None, token=None, jwt_token=None, 
                 debug=None, limit_cb=None, gh_host=None, authenticate=True, **kwargs): ...
    def __call__(self, path: str, verb: str = None, headers: dict = None, 
                 route: dict = None, query: dict = None, data=None, timeout=None, decode=True): ...
    def __getitem__(self, k): ...
    def full_docs(self): ...

def print_summary(req: Request): ...
def date2gh(dt: datetime) -> str: ...
def gh2date(dtstr: str) -> datetime: ...

Core API Client

Authentication & Authorization

OAuth device flow authentication, token management, and GitHub API scopes for secure API access.

class GhDeviceAuth:
    def __init__(self, client_id=None, *scopes): ...
    def url_docs(self) -> str: ...
    def open_browser(self): ...
    def auth(self) -> str: ...
    def wait(self, cb: callable = None, n_polls=9999) -> str: ...

def github_auth_device(wb='', n_polls=9999): ...
def scope_str(*scopes) -> str: ...

Authentication

GitHub Actions Integration

Support for GitHub Actions workflows, contexts, events, and automation workflows including workflow creation and event handling.

def user_repo(): ...
def create_workflow_files(): ...
def fill_workflow_templates(): ...
def create_workflow(): ...
def gh_create_workflow(): ...
def actions_output(): ...
def actions_debug(): ...
def actions_warn(): ...
def actions_error(): ...
def actions_group(): ...
def actions_mask(): ...
def set_git_user(): ...

GitHub Actions

Pagination & Data Handling

Utilities for handling paginated API responses and parsing GitHub API response headers.

def paged(oper, *args, per_page=30, max_pages=9999, **kwargs): ...
def pages(): ...
def parse_link_hdr(header): ...

Pagination

Event System

GitHub webhook event handling, event parsing, and sample event management for testing and development.

class GhEvent: ...
def load_sample_events(): ...
def save_sample_events(): ...

Events

Command Line Interface

CLI tools for direct GitHub API access from the command line, including endpoint calling and tab completion.

def ghapi(): ...
def ghpath(): ...
def ghraw(): ...
def completion_ghapi(): ...

CLI Tools

Types

# Standard library imports used in API signatures
from datetime import datetime
from urllib.request import Request

# GitHub API endpoint groups (dynamically created)
class _GhVerbGroup:
    def __init__(self, name, verbs): ...

class _GhVerb:
    def __init__(self, path, verb, oper, summary, url, params, data, preview, client, kwargs): ...
    def __call__(self, *args, headers=None, **kwargs): ...

# Authentication scopes
Scope = AttrDict({
    'repo': 'repo',
    'repo_status': 'repo:status',
    'repo_deployment': 'repo_deployment',
    'public_repo': 'public_repo',
    'repo_invite': 'repo:invite',
    'security_events': 'security_events',
    'admin_repo_hook': 'admin:repo_hook',
    'write_repo_hook': 'write:repo_hook',
    'read_repo_hook': 'read:repo_hook',
    'admin_org': 'admin:org',
    'write_org': 'write:org',
    'read_org': 'read:org',
    'admin_public_key': 'admin:public_key',
    'write_public_key': 'write:public_key',
    'read_public_key': 'read:public_key',
    'admin_org_hook': 'admin:org_hook',
    'gist': 'gist',
    'notifications': 'notifications',
    'user': 'user',
    'read_user': 'read:user',
    'user_email': 'user:email',
    'user_follow': 'user:follow',
    'delete_repo': 'delete_repo',
    'write_discussion': 'write:discussion',
    'read_discussion': 'read:discussion',
    'write_packages': 'write:packages',
    'read_packages': 'read:packages',
    'delete_packages': 'delete:packages',
    'admin_gpg_key': 'admin:gpg_key',
    'write_gpg_key': 'write:gpg_key',
    'read_gpg_key': 'read:gpg_key',
    'workflow': 'workflow'
})

# GitHub Actions event types
Event = str_enum('Event', [
    'page_build', 'content_reference', 'repository_import', 'create', 'workflow_run',
    'delete', 'organization', 'sponsorship', 'project_column', 'push', 'context',
    'milestone', 'project_card', 'project', 'package', 'pull_request',
    'repository_dispatch', 'team_add', 'workflow_dispatch', 'member', 'meta',
    'code_scanning_alert', 'public', 'needs', 'check_run', 'security_advisory',
    'pull_request_review_comment', 'org_block', 'commit_comment', 'watch',
    'marketplace_purchase', 'star', 'installation_repositories', 'check_suite',
    'github_app_authorization', 'team', 'status', 'repository_vulnerability_alert',
    'pull_request_review', 'label', 'installation', 'release', 'issues',
    'repository', 'gollum', 'membership', 'deployment', 'deploy_key',
    'issue_comment', 'ping', 'deployment_status', 'fork', 'schedule'
])

# Context objects for GitHub Actions
context_github: dict
context_env: dict  
context_job: dict
context_steps: dict
context_runner: dict
context_secrets: dict
context_strategy: dict
context_matrix: dict
context_needs: dict

# Environment data
env_github: dict

# Constants
GH_HOST: str = "https://api.github.com"
EMPTY_TREE_SHA: str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
contexts: tuple = ('github', 'env', 'job', 'steps', 'runner', 'secrets', 'strategy', 'matrix', 'needs')