PyGithub Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 46: Line 46:


=User=
=User=
There are <code>AuthenticatedUser</code>s and <code>NamedUser</code>s.
There are <code>[[#AuthenticatedUser|AuthenticatedUser]]</code>s and <code>[[#NamedUser|NamedUser]]</code>s.


All users <font color=darkkhaki>from where? entire site? organization?</font> are returned with <code>Github#get_users()</code>.
All users <font color=darkkhaki>from where? entire site? organization?</font> are returned with <code>Github#get_users()</code>.

Revision as of 00:30, 16 May 2023

Internal

Github

https://pygithub.readthedocs.io/en/latest/github.html

The main class to instantiate to access the GitHub API. It is used to configure authentication, via username and password, personal access token (PAT) or JWT, the base URL, the timeout, the user agent, the Retry strategy, the page size and the pool size.

Base URL

The base_url consists of the host URL and API endpoint:

host_url = 'https://github.com'
api_endpoint = 'api/v3'
base_url = f'{host_url}/{api_endpoint}'
github = Github(base_url=base_url,, ...)

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")
github = Github(base_url='https://github.com/api/v3', login_or_token=github_pat)

Authentication with Username and Password

github = Github(base_url='https://github.com/api/v3', login_or_token='someusername', password='somepassword')

Note that the invocation does not fail if the username or the password is invalid. Subsequent calls are made as unauthenticated user.

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:

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

The Retry Strategy

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='...', retry=retry, ...)

Pagination

Responses that potentially may include a large number of elements are returned as PaginatedLists. The page size is controlled by the value of the per_page Github constructor parameter. If not specified the default value is ?

User

There are AuthenticatedUsers and NamedUsers.

All users from where? entire site? organization? are returned with Github#get_users().

The current user, even if the authentication failed, is returned as an AuthenticatedUser instance, in response to Github#get_user(). However, the instance is lazy instantiated, so the attempt to get a specific attribute will end up in invoking _requester.requestJsonAndCheck(...), which will cause a 403 "API rate limit exceeded [...] Authenticated requests get a higher rate limit."

AuthenticatedUser

NamedUser