Skip to main content

GitHub SDK for Python

Project description

githubkit

license pypi python pre-commit

✨ The modern, all-batteries-included GitHub SDK for Python ✨

✨ Support both sync and async calls, fully typed

✨ Always up to date, like octokit ✨

Installation

pip install githubkit
# or, use poetry
poetry add githubkit
# or, use pdm
pdm add githubkit

Usage

Initialization

Initialize a github client using PAT (Token):

from githubkit import GitHub, TokenAuthStrategy

github = GitHub("<your_token_here>")
# or, use TokenAuthStrategy
github = GitHub(TokenAuthStrategy("<your_token_here>"))

or using basic authentication:

from githubkit import GitHub, BasicAuthStrategy

github = GitHub(BasicAuthStrategy("<client_id_here>", "<client_secret_here>"))

Calling Rest API

APIs are fully typed. Typing in the following examples is just for reference only.

Simple sync call:

from githubkit import Response
from githubkit.rest import FullRepository

resp: Response[FullRepository] = github.rest.repos.get(owner="owner", repo="repo")
repo: FullRepository = resp.parsed_data

Simple async call:

from githubkit import Response
from githubkit.rest import FullRepository

resp: Response[FullRepository] = await github.rest.repos.async_get(owner="owner", repo="repo")
repo: FullRepository = resp.parsed_data

Call API with context (reusing client):

from githubkit import Response
from githubkit.rest import FullRepository

# with GitHub("<your_token_here>") as github:
with github:
    resp: Response[FullRepository] = github.rest.repos.get(owner="owner", repo="repo")
    repo: FullRepository = resp.parsed_data
from githubkit import Response
from githubkit.rest import FullRepository

# async with GitHub("<your_token_here>") as github:
async with github:
    resp: Response[FullRepository] = await github.rest.repos.async_get(owner="owner", repo="repo")
    repo: FullRepository = resp.parsed_data

Pagination

from githubkit.rest import Issue

for issue in github.paginate(
    github.rest.issues.list_for_repo, owner="owner", repo="repo", state="open"
):
    issue: Issue
    print(issue.number)
from githubkit.rest import Issue

async for issue in github.paginate(
    github.rest.issues.async_list_for_repo, owner="owner", repo="repo", state="open"
):
    issue: Issue
    print(issue.number)

Calling GraphQL API

Simple sync call:

data: Dict[str, Any] = github.graphql(query, variables={"foo": "bar"})

Simple async call:

data: Dict[str, Any] = github.async_graphql(query, variables={"foo": "bar"})

Webhook Verification

Simple webhook payload verification:

from githubkit.webhooks import verify

valid: bool = verify(secret, request.body, request.headers["X-Hub-Signature-256"])

Sign the webhook payload manually:

from githubkit.webhooks import sign

signature: str = sign(secret, payload, method="sha256")

Webhook Parsing

Parse the payload with event name:

from githubkit.webhooks import parse, WebhookEvent

event: WebhookEvent = parse(request.headers["X-GitHub-Event"], request.body)

Parse the payload without event name (may cost longer time):

from githubkit.webhooks import parse_without_name, WebhookEvent

event: WebhookEvent = parse_without_name(request.body)

Parse dict like payload:

from githubkit.webhooks import parse_obj, parse_obj_without_name, WebhookEvent

event: WebhookEvent = parse_obj(request.headers["X-GitHub-Event"], request.json())
event: WebhookEvent = parse_obj_without_name(request.json())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

GitHubKit-0.4.1.tar.gz (265.9 kB view details)

Uploaded Source

Built Distribution

GitHubKit-0.4.1-py3-none-any.whl (292.3 kB view details)

Uploaded Python 3

File details

Details for the file GitHubKit-0.4.1.tar.gz.

File metadata

  • Download URL: GitHubKit-0.4.1.tar.gz
  • Upload date:
  • Size: 265.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.4.0-1086-azure

File hashes

Hashes for GitHubKit-0.4.1.tar.gz
Algorithm Hash digest
SHA256 3e85e5db982800e637cb9401f9baa9d2fbbe008e4f6b708014c432bb2d4d5718
MD5 437acc7dfe4e424f364075d102a86560
BLAKE2b-256 a61c88be6f24d67912c931f54836313ae4486327ec9092e5ca43eb7c28c56004

See more details on using hashes here.

File details

Details for the file GitHubKit-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: GitHubKit-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 292.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.4.0-1086-azure

File hashes

Hashes for GitHubKit-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f7593274f1e03f1d673703c83ab611ca54bf5fff15c45524ca06b0c1cfc5339
MD5 f7ecb1e95ece9dfb4abdbede5591d6b9
BLAKE2b-256 26ff8a70a8a079466fdd3fd2d431abd9c41d5c5e56b1210a6e78944e1fbe2d9d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page