[geany/www.geany.org] 087e16: Support a GitHub API token for API requests

Enrico Tröger git-noreply at xxxxx
Thu Feb 20 15:49:28 UTC 2020


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Thu, 20 Feb 2020 15:49:28 UTC
Commit:      087e16acfd98e11c9f228a6bc43ea3f69d5fc6f3
             https://github.com/geany/www.geany.org/commit/087e16acfd98e11c9f228a6bc43ea3f69d5fc6f3

Log Message:
-----------
Support a GitHub API token for API requests

With authenticated requests, the request limit is much higher and
so we can avoid hitting the anonymous request limit.


Modified Paths:
--------------
    geany/settings.py
    static_docs/github_client.py
    static_docs/views.py

Modified: geany/settings.py
1 lines changed, 1 insertions(+), 0 deletions(-)
===================================================================
@@ -448,6 +448,7 @@
 
 NIGHTLYBUILDS_BASE_DIR = '/path/to/nightly/builds'
 
+STATIC_DOCS_GITHUB_API_TOKEN = None
 STATIC_DOCS_GEANY_SOURCE_TARBALL = '/srv/www/download.geany.org/geany_git.tar.gz'
 STATIC_DOCS_GEANY_DESTINATION_DIR = os.path.join(MEDIA_ROOT, 'i18n')
 STATIC_DOCS_GEANY_DESTINATION_URL = os.path.join(MEDIA_URL, 'i18n')


Modified: static_docs/github_client.py
22 lines changed, 19 insertions(+), 3 deletions(-)
===================================================================
@@ -12,7 +12,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from base64 import standard_b64decode
+from base64 import standard_b64decode, standard_b64encode
 import logging
 
 import requests
@@ -29,6 +29,10 @@
 class GitHubApiClient:
     """"""
 
+    # ----------------------------------------------------------------------
+    def __init__(self, auth_token=None):
+        self._auth_token = auth_token
+
     # ----------------------------------------------------------------------
     def get_file_contents(self, filename, user=None, repository=None):
         user = user or GITHUB_USER
@@ -45,9 +49,14 @@ def get_file_contents(self, filename, user=None, repository=None):
         return self._parse_fetch_file_response(response_json)
 
     # ----------------------------------------------------------------------
-    def _request(self, url, status_404_expected=False):
+    def _request(self, url, status_404_expected=False, auth_token=None):
+        request_args = dict(timeout=HTTP_REQUEST_TIMEOUT, stream=False)
+        if self._auth_token is not None:
+            authorization_header = self._factor_authorization_header()
+            request_args['headers'] = authorization_header
+
         try:
-            with requests.get(url, timeout=HTTP_REQUEST_TIMEOUT, stream=False) as response:
+            with requests.get(url, **request_args) as response:
                 self._log_request(response, status_404_expected)
                 self._log_rate_limit(response)
                 # error out on 4xx and 5xx status codes
@@ -60,6 +69,13 @@ def _request(self, url, status_404_expected=False):
 
         return response
 
+    # ----------------------------------------------------------------------
+    def _factor_authorization_header(self):
+        auth = '{}:x-oauth-basic'.format(self._auth_token)
+        auth = auth.encode('ascii')
+        basic_auth_value = 'Basic {}'.format(standard_b64encode(auth).decode())
+        return {'Authorization': basic_auth_value}
+
     # ----------------------------------------------------------------------
     def _log_rate_limit(self, response):
         rate_limit_remaining = int(response.headers['X-RateLimit-Remaining'])


Modified: static_docs/views.py
4 lines changed, 2 insertions(+), 2 deletions(-)
===================================================================
@@ -61,7 +61,7 @@ def __init__(self, *args, **kwargs):
 
     # ----------------------------------------------------------------------
     def _fetch_file_via_github_api(self, filename, user=None, repository=None):
-        client = GitHubApiClient()
+        client = GitHubApiClient(auth_token=settings.STATIC_DOCS_GITHUB_API_TOKEN)
         self._file_contents = client.get_file_contents(filename, user=user, repository=repository)
 
 
@@ -169,7 +169,7 @@ def _get_release_notes_for_version(self, releases, version=None):
     # ----------------------------------------------------------------------
     @cache_function(CACHE_TIMEOUT_24HOURS)
     def _get_release_from_github(self, version=None):
-        client = GitHubApiClient()
+        client = GitHubApiClient(auth_token=settings.STATIC_DOCS_GITHUB_API_TOKEN)
         if version is None:
             github_release = client.get_latest_release()
         else:



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list