Skip to main content

GitHub SDK for Python

Reason this release was yanked:

See https://github.com/yanyongyu/githubkit/issues/16

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

if you want to auth as github app, extra dependencies are required:

pip install githubkit[auth-app]
# or, use poetry
poetry add githubkit[auth-app]
# or, use pdm
pdm add githubkit[auth-app]

if you want to mix sync and async calls in oauth device callback, extra dependencies are required:

pip install githubkit[auth-oauth-device]
# or, use poetry
poetry add githubkit[auth-oauth-device]
# or, use pdm
pdm add githubkit[auth-oauth-device]

Usage

Authentication

Initialize a github client with no authentication:

from githubkit import GitHub

github = GitHub()
# or, use UnauthAuthStrategy
github = GitHub(UnauthAuthStrategy())

or using PAT (Token):

from githubkit import GitHub, TokenAuthStrategy

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

or using GitHub APP authentication:

from githubkit import GitHub, AppAuthStrategy

github = GitHub(
    AppAuthStrategy(
        "<app_id>", "<private_key>", "<optional_client_id>", "<optional_client_secret>"
    )
)

or using GitHub APP Installation authentication:

from githubkit import GitHub, AppInstallationAuthStrategy

github = GitHub(
    AppInstallationAuthStrategy(
        "<app_id>", "<private_key>", installation_id, "<optional_client_id>", "<optional_client_secret>",
    )
)

or using OAuth APP authentication:

from githubkit import GitHub, OAuthAppAuthStrategy

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

or using GitHub APP / OAuth APP web flow authentication:

from githubkit import GitHub, OAuthWebAuthStrategy

github = GitHub(
    OAuthWebAuthStrategy(
        "<client_id_here>", "<client_secret_here>", "<web_flow_exchange_code_here>"
    )
)

or using GitHub Action authentication:

from githubkit import GitHub, ActionAuthStrategy

github = GitHub(ActionAuthStrategy())

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:
    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:
    resp: Response[FullRepository] = await github.rest.repos.async_get(owner="owner", repo="repo")
    repo: FullRepository = resp.parsed_data

Pagination

Pagination type checking is also supported:

Typing is tested with Pylance (Pyright).

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)

complex pagination with custom map function (some api returns data in a nested field):

async for accessible_repo in github.paginate(
    github.rest.apps.async_list_installation_repos_for_authenticated_user,
    map_func=lambda r: r.parsed_data.repositories,
    installation_id=1,
):
    accessible_repo: Repository
    print(accessible_repo.full_name)

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())

Switch between AuthStrategy

You can change the auth strategy and get a new client simplely using with_auth.

Change from AppAuthStrategy to AppInstallationAuthStrategy:

from githubkit import GitHub, AppAuthStrategy

github = GitHub(AppAuthStrategy("<app_id>", "<private_key>"))
installation_github = github.with_auth(
    github.auth.as_installation(installation_id)
)

Change from OAuthAppAuthStrategy to OAuthWebAuthStrategy:

from githubkit import GitHub, OAuthAppAuthStrategy

github = GitHub(OAuthAppAuthStrategy("<client_id>", "<client_secret>"))
user_github = github.with_auth(github.auth.as_web_user("<code>"))

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.9.5.tar.gz (294.1 kB view details)

Uploaded Source

Built Distribution

githubkit-0.9.5-py3-none-any.whl (321.6 kB view details)

Uploaded Python 3

File details

Details for the file githubkit-0.9.5.tar.gz.

File metadata

  • Download URL: githubkit-0.9.5.tar.gz
  • Upload date:
  • Size: 294.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-1031-azure

File hashes

Hashes for githubkit-0.9.5.tar.gz
Algorithm Hash digest
SHA256 4ca8782a29c99c028773b05e9af883fc59d0597ce131838b0c9a74fa0ebf90de
MD5 7649d99da0a69e64ab13c46602072f65
BLAKE2b-256 c765924a8aa5302df0252b64166de6328f4a7dc0941affa3aed139d4d55dd874

See more details on using hashes here.

File details

Details for the file githubkit-0.9.5-py3-none-any.whl.

File metadata

  • Download URL: githubkit-0.9.5-py3-none-any.whl
  • Upload date:
  • Size: 321.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.6 Linux/5.15.0-1031-azure

File hashes

Hashes for githubkit-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3c7c14d7b912f739b17136191971811e8db0cb98b3ef90734c011308e77dc1e2
MD5 e39df5b449e7177dd99a2eccd03efd69
BLAKE2b-256 012853518575d938b077d76f8b6361de282aed9d36919cf45001ae07acfc51dc

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