Branch: refs/heads/master Author: Enrico Tröger enrico.troeger@uvena.de Committer: Enrico Tröger enrico.troeger@uvena.de Date: Sun, 30 Jun 2019 21:34:26 UTC Commit: 16a8f5c38e7213b7fb07cb79e91d81763320bb9c https://github.com/geany/www.geany.org/commit/16a8f5c38e7213b7fb07cb79e91d81...
Log Message: ----------- Improve error handling on Github API requests
Before any HTTP or Github API errors were ignored and the code crashed later on processing the missing responses. Now, "requests" raises an appropriate error and we stop trying to process a missing response.
Also use a common method for making requests to reduce code duplication.
Modified Paths: -------------- static_docs/github_client.py
Modified: static_docs/github_client.py 54 lines changed, 34 insertions(+), 20 deletions(-) =================================================================== @@ -33,19 +33,33 @@ class GitHubApiClient: def get_file_contents(self, filename, user=None, repository=None): user = user or GITHUB_USER repository = repository or GITHUB_REPOSITORY - url_parameters = dict(user=user, - repository=repository, - filename=filename) - url = 'https://api.github.com/repos/%(user)s/%(repository)s/contents/%(filename)s' % \ - url_parameters - with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response: - response_json = response.json() - self._log_rate_limit(response) - self._log_request(response) + url = '{api_base_url}repos/{user}/{repository}/contents/{filename}'.format( + api_base_url=GITHUB_API_URL, + user=user, + repository=repository, + filename=filename) + response = self._request(url) + response_json = response.json()
# parse response return self._parse_fetch_file_response(response_json)
+ # ---------------------------------------------------------------------- + def _request(self, url, status_404_expected=False): + try: + with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response: + self._log_rate_limit(response) + self._log_request(response) + # error out on 4xx and 5xx status codes + response.raise_for_status() + except requests.exceptions.HTTPError as exc: + if exc.response.status_code == 404 and status_404_expected: + return None + else: + raise + + return response + # ---------------------------------------------------------------------- def _log_rate_limit(self, response): rate_limit_remaining = response.headers['X-RateLimit-Remaining'] @@ -71,26 +85,26 @@ def _parse_fetch_file_response(self, response_json):
# ---------------------------------------------------------------------- def get_release_by_tag(self, tag_name): - url = 'https://api.github.com/repos/%7Buser%7D/%7Brepository%7D/releases/tags/%7Bta... + url = '{api_base_url}repos/{user}/{repository}/releases/tags/{tag_name}'.format( + api_base_url=GITHUB_API_URL, user=GITHUB_USER, repository=GITHUB_REPOSITORY, tag_name=tag_name) - with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response: - response_json = response.json() - self._log_rate_limit(response)
- if response.status_code == 404: - return None + response = self._request(url, status_404_expected=True) + if response: + response_json = response.json() + return response_json
- return response_json + return None
# ---------------------------------------------------------------------- def get_latest_release(self): - url = 'https://api.github.com/repos/%7Buser%7D/%7Brepository%7D/releases/latest%27.... + url = '{api_base_url}repos/{user}/{repository}/releases/latest'.format( + api_base_url=GITHUB_API_URL, user=GITHUB_USER, repository=GITHUB_REPOSITORY) - with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response: - response_json = response.json() - self._log_rate_limit(response)
+ response = self._request(url) + response_json = response.json() return response_json
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).