Utilities¶
Authentication¶
-
class
github.Auth.Login(login: str, password: str)¶ This class is used to authenticate with login and password.
-
property
username¶ The username.
-
property
password¶ The password.
-
property
-
class
github.Auth.Token(token: str)¶ This class is used to authenticate with a single constant token.
-
property
token_type¶ The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic.
- Returns
token type
-
property
token¶ The auth token as used in the HTTP Authorization header.
- Returns
token
-
property
-
class
github.Auth.JWT¶ This class is the base class to authenticate with a JSON Web Token (JWT).
-
property
token_type¶ The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic.
- Returns
token type
-
property
-
class
github.Auth.AppAuth(app_id: Union[int, str], private_key: str, jwt_expiry: int = 300, jwt_issued_at: int = -60, jwt_algorithm: str = 'RS256')¶ This class is used to authenticate as a GitHub App.
-
property
token¶ The auth token as used in the HTTP Authorization header.
- Returns
token
-
get_installation_auth(installation_id: int, token_permissions: Optional[Dict[str, str]] = None, requester: Optional[github.Requester.Requester] = None) → github.Auth.AppInstallationAuth¶ Creates a github.Auth.AppInstallationAuth instance for an installation.
- Parameters
installation_id – installation id
token_permissions – optional permissions
requester – optional requester with app authentication
- Returns
-
create_jwt(expiration: Optional[int] = None) → str¶ Create a signed JWT https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app
- Return string
jwt
-
property
-
class
github.Auth.AppAuthToken(token: str)¶ This class is used to authenticate as a GitHub App with a single constant JWT.
-
property
token¶ The auth token as used in the HTTP Authorization header.
- Returns
token
-
property
-
class
github.Auth.AppInstallationAuth(app_auth: github.Auth.AppAuth, installation_id: int, token_permissions: Optional[Dict[str, str]] = None, requester: Optional[github.Requester.Requester] = None)¶ This class is used to authenticate as a GitHub App Installation.
-
property
token_type¶ The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic.
- Returns
token type
-
property
token¶ The auth token as used in the HTTP Authorization header.
- Returns
token
-
property
-
class
github.Auth.AppUserAuth(client_id: str, client_secret: str, token: str, token_type: Optional[str] = None, expires_at: Optional[datetime.datetime] = None, refresh_token: Optional[str] = None, refresh_expires_at: Optional[datetime.datetime] = None, requester: Optional[github.Requester.Requester] = None)¶ This class is used to authenticate as a GitHub App on behalf of a user.
-
class
ApplicationOAuth(requester: Requester, headers: dict[str, Any], attributes: Any, completed: bool)¶ This class is used for identifying and authorizing users for Github Apps.
The reference can be found at https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps
-
get_access_token(code: str, state: str | None = None) → AccessToken¶
-
get_login_url(redirect_uri: str | None = None, state: str | None = None, login: str | None = None) → str¶ Return the URL you need to redirect a user to in order to authorize your App.
-
refresh_access_token(refresh_token: str) → AccessToken¶ - Calls
- Parameters
refresh_token – string
-
-
property
token_type¶ The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic.
- Returns
token type
-
property
token¶ The auth token as used in the HTTP Authorization header.
- Returns
token
-
class
Logging¶
-
github.enable_console_debug_logging() → None¶ This function sets up a very simple logging configuration (log everything on standard output) that is useful for troubleshooting.
Error Handling¶
-
exception
github.GithubException.GithubException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Error handling in PyGithub is done with exceptions. This class is the base of all exceptions raised by PyGithub (but
github.GithubException.BadAttributeException).Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.
-
property
status¶ The status returned by the Github API.
-
property
data¶ The (decoded) data returned by the Github API.
-
property
headers¶ The headers returned by the Github API.
-
property
-
exception
github.GithubException.BadCredentialsException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised in case of bad credentials (when Github API replies with a 401 or 403 HTML status)
-
exception
github.GithubException.UnknownObjectException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised when a non-existing object is requested (when Github API replies with a 404 HTML status)
-
exception
github.GithubException.BadUserAgentException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised when request is sent with a bad user agent header (when Github API replies with a 403 bad user agent HTML status)
-
exception
github.GithubException.RateLimitExceededException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised when the rate limit is exceeded (when Github API replies with a 403 rate limit exceeded HTML status)
-
exception
github.GithubException.BadAttributeException(actualValue: Any, expectedType: Union[Dict[Tuple[Type[str], Type[str]], Type[dict]], Tuple[Type[str], Type[str]], List[Type[dict]], List[Tuple[Type[str], Type[str]]]], transformationException: Optional[Exception])¶ Exception raised when Github returns an attribute with the wrong type.
-
property
actual_value¶ The value returned by Github.
-
property
expected_type¶ The type PyGithub expected.
-
property
transformation_exception¶ The exception raised when PyGithub tried to parse the value.
-
property
-
exception
github.GithubException.TwoFactorException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised when Github requires a onetime password for two-factor authentication.
-
exception
github.GithubException.IncompletableObject(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)¶ Exception raised when we can not request an object from Github because the data returned did not include a URL.
Default argument¶
github.NotSet is a special value for arguments you don’t want to provide. You should not have to manipulate it directly, because it’s the default value of all parameters accepting it. Just note that it is different from None, which is an allowed value for some parameters.
Pagination¶
-
class
github.PaginatedList.PaginatedList¶ This class abstracts the pagination of the REST API and the GraphQl API <https://docs.github.com/en/graphql/guides/using-pagination-in-the-graphql-api>`_.
You can simply enumerate through instances of this class:
for repo in user.get_repos(): print(repo.name)
If you want to know the total number of items in the list:
print(user.get_repos().totalCount)
You can also index them or take slices:
second_repo = user.get_repos()[1] first_repos = user.get_repos()[:10]
If you want to iterate in reversed order, just do:
for repo in user.get_repos().reversed: print(repo.name)
And if you really need it, you can explicitly access a specific page:
repos = user.get_repos() assert repos.is_rest, "get_page not supported by the GraphQL API" some_repos = repos.get_page(0) some_other_repos = repos.get_page(3)
Input classes¶
-
class
github.InputFileContent.InputFileContent(content: str, new_name: Union[str, github.GithubObject._NotSetType] = NotSet)¶ This class represents InputFileContents.
-
class
github.InputGitAuthor.InputGitAuthor(name: str, email: str, date: Union[str, github.GithubObject._NotSetType] = NotSet)¶ This class represents InputGitAuthors.
-
class
github.InputGitTreeElement.InputGitTreeElement(path: str, mode: str, type: str, content: Opt[str] = NotSet, sha: Opt[str | None] = NotSet)¶ This class represents InputGitTreeElements.
Raw Requests¶
If you need to make requests to APIs not yet supported by PyGithub,
you can use the Requester object directly, available as object.requester on most PyGithub objects.
-
class
github.Requester.Requester(auth: Optional[Auth], base_url: str, timeout: int, user_agent: str, per_page: int, verify: Union[bool, str], retry: Union[int, urllib3.util.retry.Retry, None], pool_size: Optional[int], seconds_between_requests: Optional[float] = None, seconds_between_writes: Optional[float] = None)¶ -
NEW_DEBUG_FRAME(requestHeader: Dict[str, str]) → None¶ Initialize a debug frame with requestHeader Frame count is updated and will be attached to respond header The structure of a frame: [requestHeader, statusCode, responseHeader, raw_data] Some of them may be None
-
DEBUG_ON_RESPONSE(statusCode: int, responseHeader: Dict[str, Union[str, int]], data: str) → None¶ Update current frame with response Current frame index will be attached to responseHeader.
-
close() → None¶ Close the connection to the server.
-
property
kwargs¶ Returns arguments required to recreate this Requester with Requester.__init__, as well as with MainClass.__init__ and GithubIntegration.__init__.
-
withAuth(auth: Optional[Auth]) → Requester¶ Create a new requester instance with identical configuration but the given authentication method.
- Parameters
auth – authentication method
- Returns
new Requester implementation
-
requestJsonAndCheck(verb: str, url: str, parameters: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, str]] = None, input: Optional[Any] = None) → Tuple[Dict[str, Any], Any]¶ Send a request with JSON body.
- Parameters
input – request body, serialized to JSON if specified
- Returns
(headers: dict, JSON Response: Any)- Raises
GithubExceptionfor error status codes
-
requestMultipartAndCheck(verb: str, url: str, parameters: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, Any]] = None, input: Optional[Dict[str, str]] = None) → Tuple[Dict[str, Any], Optional[Dict[str, Any]]]¶ Send a request with multi-part-encoded body.
- Parameters
input – request body, will be multi-part encoded if specified
- Returns
(headers: dict, JSON Response: Any)- Raises
GithubExceptionfor error status codes
-
requestBlobAndCheck(verb: str, url: str, parameters: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None, input: Optional[str] = None, cnx: Union[github.Requester.HTTPRequestsConnectionClass, github.Requester.HTTPSRequestsConnectionClass, None] = None) → Tuple[Dict[str, Any], Dict[str, Any]]¶ Send a request with a file for the body.
- Parameters
input – path to a file to use for the request body
- Returns
(headers: dict, JSON Response: Any)- Raises
GithubExceptionfor error status codes
-
graphql_query(query: str, variables: Dict[str, Any]) → Tuple[Dict[str, Any], Dict[str, Any]]¶ - Calls
-
graphql_node(node_id: str, graphql_schema: str, node_type: str) → Tuple[Dict[str, Any], Dict[str, Any]]¶ - Calls
-
graphql_node_class(node_id: str, graphql_schema: str, klass: Type[T_gh], node_type: Optional[str] = None) → T_gh¶ - Calls
-
graphql_query_class(query: str, variables: Dict[str, Any], data_path: List[str], klass: Type[T_gh]) → T_gh¶ - Calls
-
graphql_named_mutation(mutation_name: str, mutation_input: Dict[str, Any], output_schema: str) → Tuple[Dict[str, Any], Dict[str, Any]]¶ - Create a mutation in the format:
- mutation Mutation($input: MutationNameInput!) {
mutationName(input: $input) { <output_schema> }
}
and call the self.graphql_query method.
Returns the response data according to given output schema.
-
graphql_named_mutation_class(mutation_name: str, mutation_input: Dict[str, Any], output_schema: str, item: str, klass: Type[T_gh]) → T_gh¶ Executes a mutation and returns the output object as the given GithubObject.
See {@link graphql_named_mutation}.
-
requestJson(verb: str, url: str, parameters: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, Any]] = None, input: Optional[Any] = None, cnx: Union[github.Requester.HTTPRequestsConnectionClass, github.Requester.HTTPSRequestsConnectionClass, None] = None) → Tuple[int, Dict[str, Any], str]¶ Send a request with JSON input.
- Parameters
input – request body, will be serialized as JSON
:returns:
(status, headers, body)
-
requestMultipart(verb: str, url: str, parameters: Optional[Dict[str, Any]] = None, headers: Optional[Dict[str, Any]] = None, input: Optional[Dict[str, str]] = None, cnx: Union[github.Requester.HTTPRequestsConnectionClass, github.Requester.HTTPSRequestsConnectionClass, None] = None) → Tuple[int, Dict[str, Any], str]¶ Send a request with multi-part encoding.
- Parameters
input – request body, will be serialized as multipart form data
:returns:
(status, headers, body)
-
requestBlob(verb: str, url: str, parameters: Optional[Dict[str, str]] = None, headers: Optional[Dict[str, str]] = None, input: Optional[str] = None, cnx: Union[github.Requester.HTTPRequestsConnectionClass, github.Requester.HTTPSRequestsConnectionClass, None] = None) → Tuple[int, Dict[str, Any], str]¶ Send a request with a file as request body.
- Parameters
input – path to a local file to use for request body
:returns:
(status, headers, body)
-
requestMemoryBlobAndCheck(verb: str, url: str, parameters: Any, headers: Dict[str, Any], file_like: BinaryIO, cnx: Union[github.Requester.HTTPRequestsConnectionClass, github.Requester.HTTPSRequestsConnectionClass, None] = None) → Tuple[Dict[str, Any], Any]¶ Send a request with a binary file-like for the body.
- Parameters
file_like – file-like object to use for the request body
- Returns
(headers: dict, JSON Response: Any)- Raises
GithubExceptionfor error status codes
-