PyGithub Programming Model: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 16: Line 16:
==Authentication with Username and Password==
==Authentication with Username and Password==
==Authentication with JWT==
==Authentication with JWT==
=Authenticated SSL Client=
If the Github server requires the client to authenticate, and the client does not, or uses a self-signed certificate, an invocation fails with:
<syntaxhighlight lang='text'>
requests.exceptions.SSLError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /api/v3/user/repos?per_page=100 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)')))
</syntaxhighlight>
=Configuring Retry=
=Configuring Retry=
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>

Revision as of 23:35, 15 May 2023

Internal

Authentication

Various authentication mechanism are invoked by appropriately configuring the main Github class.

Authentication with Personal Access Token (PAT)

github_pat = os.environ.get('GITHUB_PAT')
if not github_pat:
    raise ValueError("'GITHUB_PAT' not found in environment")
host_url = 'https://github.com'
api_endpoint = 'api/v3'
base_url = f'{host_url}/{api_endpoint}'
github = Github(base_url=f'{base_url}', login_or_token=github_pat)

Authentication with Username and Password

Authentication with JWT

Configuring Retry

import os
from github import Github
from urllib3 import Retry


def access_github():
    # github = Github('user', 'password')
    github_pat = os.environ.get('GITHUB_PAT')
    if not github_pat:
        raise ValueError("'GITHUB_PAT' not setup")
    base_url = 'https://github.com'
    api_endpoint = 'api/v3'
    status_forcelist = [403, 500, 502, 504]  # retry 403s, 5XX from GitHub
    retry = Retry(total=10, backoff_factor=0.2, raise_on_status=True, status_forcelist=status_forcelist)
    github = Github(base_url=f'{base_url}/{api_endpoint}', login_or_token=github_pat, retry=retry, per_page=100)

    for repo in github.get_user().get_repos():
        print(repo.name)