Coverage for pyquickhelper/loghelper/github_api.py: 31%
13 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1"""
2@file
3@brief Calls :epkg:`github` API.
4"""
5import requests
8class GitHubApiException(Exception):
9 """
10 Exception raised when a call to github rest api failed.
11 """
13 def __init__(self, response, url, **kwargs):
14 """
15 Merges everything into a string.
16 """
17 msg = [f'{k}={v!r}' for k, v in sorted(kwargs.items())]
18 if msg:
19 msg = "\n" + "\n".join(msg)
20 Exception.__init__(
21 self,
22 "response={0}\nurl='{1}'\ntext='{2}'\nstatus={3}{4}".format(
23 response, url, response.text, response.status_code, msg))
26def call_github_api(owner, repo, ask, auth=None, headers=None):
27 """
28 Calls `GitHub REST API <https://developer.github.com/v3/>`_.
30 @param owner owner of the project
31 @param auth tuple *(user, password)*
32 @param repo repository name
33 @param ask query (see below)
34 @param header dictionary
35 @return json
37 Example for *ask*:
39 * ``commits``
40 * ``downloads``
41 * ``forks``
42 * ``issues``
43 * ``pulls``
44 * ``stats/code_frequency`` - Needs authentification
45 * ``stats/commit_activity`` - Needs authentification
46 * ``stats/punch_card`` - Needs authentification
47 * ``traffic/popular/referrers`` - Must have push access to repository
48 * ``traffic/popular/paths`` - Must have push access to repository
49 * ``traffic/views`` - Must have push access to repository
50 * ``traffic/clones`` - Must have push access to repository
52 GitHub limits the number of requets per hour:
53 `Rate Limiting <https://developer.github.com/v3/#rate-limiting>`_.
54 """
55 url = f"https://api.github.com/repos/{owner}/{repo}/{ask.strip('/')}"
56 if '...' in url:
57 raise ValueError( # pragma: no cover
58 f"Unexpected url={url!r}, owner={owner!r}, auth={auth!r}, repo={repo!r}.")
59 response = requests.get(url, auth=auth, headers=headers, timeout=10)
60 if response.status_code != 200:
61 raise GitHubApiException( # pragma: no cover
62 response, url, owner=owner, repo=repo, ask=ask, auth=auth)
63 return response.json()