Skip to main content

GitHub Tokens Loader

A simple utility library to load GitHub tokens from a structured file which helps track who the token is linked to and when it expires.

Usage

GitHub Tokens File

To use this library, create a file with the following structure:

tokens:
  jane:
    token: "github_pat_56..."
    expiration_date: null
  john:
    token: "github_pat_41..."
    expiration_date: "2026-02-13"
  bob:
    token: "ghp_VeaD3..."
    expiration_date: null
  sally:
    token: "ghp_VVv5E..."
    expiration_date: "2023-02-13"

Each token has a short name (generally who or what it is linked to), the token itself, and an optional expiration date. The expiration date should be in the format YYYY-MM-DD.

⚠️⚠️ Important: Do not share this file with anyone else. Keep it secure and private. It is recommended to add the filename to your .gitignore as well. ⚠️⚠️

Loading Tokens

To load the tokens from the file, use the following code:

from gh_tokens_loader import load_github_tokens

tokens = load_github_tokens("path/to/tokens.yaml")

This will return only the tokens that are valid (i.e., not expired).

Why

I am a researcher who spends a lot of time mining the GitHub API for data about scientific software. Due to the rate limits on the GitHub API, my research collaborators and I will commonly pool our tokens. I found myself copy pasting some form of this code around to different projects and decided to make it a library.

In my own usage, I generally use the GitHubTokensCycler that is also builtin to the library:

import time
from concurrent.futures import ThreadPoolExecutor

from gh_tokens_loader import GitHubTokensCycler

# Load tokens
gh_tokens_cycler = GitHubTokensCycler("path/to/tokens.yaml")

# Imagine some function that uses the GitHub API
def get_repo_data(repo: str, gh_token: str) -> dict:
    # Important to sleep to avoid rate limits
    time.sleep(1)

    # Use the token to get data from the GitHub API
    # ...
    # Return the data
    return {"some": "data"}

# Imagine some list of repos
repos = ["repo1", "repo2", "repo3", "..."]

# Thread with cycling tokens
with ThreadPoolExecutor(max_workers=len(gh_tokens_cycler)) as exe:
    results = list(exe.map(
        get_repo_data,
        repos,
        [next(gh_tokens_cycler) for _ in range(len(repos))],
    ))

# Do something with the results
# ...

The GitHubTokensCycler will occasionally also refresh it's internal set of tokens to only include valid tokens.

License

This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.

Metadata

Release files for gh-tokens-loader 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gh-tokens-loader 0.1.2
File Size Uploaded
gh_tokens_loader-0.1.2.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gh-tokens-loader 0.1.2
File Interpreter ABI Platform
gh_tokens_loader-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / gh_tokens_loader-0.1.2.tar.gz

Download URL gh_tokens_loader-0.1.2.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d50fd38cf0117e70fd633e537165fe4a14651d4d0904b7b29af0db94b9a8f9be
BLAKE2b-256 checksum
How to use checksums
cd5db51bb9bbba1463090ab6df5137a1d677c14682e259eb8b818d7565210e19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 14, 2025.

Transparency log

Release files / gh_tokens_loader-0.1.2-py3-none-any.whl

Download URL gh_tokens_loader-0.1.2-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3de29fa3107fcbddc2df40c04cf251d46a7fcecda30d02aedcb53f213a5803f
BLAKE2b-256 checksum
How to use checksums
4d8b7db208454536dc715baff1db4a6de446b2cb1ebe96413887de462df9b2da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 14, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page