Skip to main content

A simple Github client that only provides auth and access to the REST and GraphQL APIs.

Project description

Task Status pre-commit.ci status Code Coverage PyPI version License

simple-github

A simple Python Github client that handles auth and provides easy access to the REST and GraphQL APIs.

Why use simple-github?

You might consider simple-github if..

  1. You don't want to write your own auth (especially app auth) but also don't want to be stuck with an object oriented wrapper.
  2. You want to use both the REST and GraphQL endpoints.

Features

  • Authenticate with a personal access token, as a Github App or a Github App installation.
  • Automatic refreshing of app tokens on expiry.
  • Query both the REST and GraphQL endpoints.
  • Shared aiohttp session across both endpoints.

Installation

Install with pip:

pip install simple-github

Example Usage

Authenticate with an access token

In the simplest case, you can provide an access token to use:

from simple_github import TokenClient
token = "<access token>"
async with TokenClient(token) as session:
    await session.get("/octocat")

If calling synchronously, simply remove the async / await from the examples:

from simple_github import TokenClient
token = "<access token>"
with TokenClient(token) as session:
    session.get("/octocat")

Authenticate as a Github App installation

To authenticate as an app installation you'll need:

  1. The Github app id for your app.
  2. A private key associated with your app. This can be generated from your app's settings page.
  3. The organization or user where the app is installed.
  4. Optionally a list of repositories to limit access to.
from simple_github import AppClient
app_id = 123
privkey = "<private key>"
owner = "mozilla-releng"

async with AppClient(app_id, privkey, owner=owner) as session:
    await session.get("/octocat")

You can also specify repositories if you want to restrict access.

async with AppClient(app_id, privkey, owner=owner, repositories=["simple-github"]) as session:
    await session.get("/octocat")

Authenticate as a Github App

You can also authenticate as the app itself. This is mainly only useful for administering the app. To do this, simply omit the owner argument.

async with AppClient(app_id, privkey) as session:
    await session.get("/octocat")

Query the REST API

simple-github provides only a very basic wrapper around Github's REST API. You can query it by passing in the path fragment to session.get or session.post.

For example, you can list pull requests with a GET request:

pulls = await session.get("/repos/mozilla-releng/simple-github/pulls")
open_pulls = [p for p in pulls if p["state"] == "open"]

Or you can create a pull request with a POST request:

data = {
    "title": "Add feature X",
    "body": "This adds new feature X",
    "head": "simple-github:featureX",
    "base": "main",
}
await session.post("/repos/mozilla-releng/simple-github/pulls", data=data)

Query the GraphQL API

simple-github also supports the GraphQL API. In this example we get the contents of a file:

query = """
  query getFileContents {
    repository(owner: "mozilla-releng", name: "simple-github") {
      object(expression: "HEAD:README.md") {
        ... on Blob {
          text
        }
      }
    }
  }
"""
contents = await session.execute(query)

You can use GraphQL variables via the variables argument to session.execute.

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

simple_github-1.0.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

simple_github-1.0.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file simple_github-1.0.0.tar.gz.

File metadata

  • Download URL: simple_github-1.0.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.6.3-203.fsync.fc38.x86_64

File hashes

Hashes for simple_github-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fbf92ef541e9768a834d267921c9ceb6da18d4740148be41e3c3d31e9d73683d
MD5 74a43b56877da4d8bb5209f9d3644f70
BLAKE2b-256 cab60a38c3525bd851c2562b3629eaacafafe18a93fa8fd8aa0a55fdebef66d0

See more details on using hashes here.

File details

Details for the file simple_github-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: simple_github-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.6.3-203.fsync.fc38.x86_64

File hashes

Hashes for simple_github-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7aa270f33a62c87af9bbbfee595cbf0c7c9f7b24eccc9fef93a05d969695d6e3
MD5 cd65a7665f6b6b396ff9133e38a4a469
BLAKE2b-256 2a4bbf7e970492fb74ff50a428a7d4002af5ed3e266c1b02f5b87168e5ad0da5

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