Skip to content

Commit b362022

Browse files
authored
Merge pull request tobami#278 from Kami/support_github_auth
Support authenticated Github API requests
2 parents 237fe8c + 61e338b commit b362022

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

codespeed/commits/github.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
try:
1212
# Python 3
1313
from urllib.request import urlopen
14+
from urllib.request import Request
1415
except ImportError:
1516
# Python 2
16-
from urllib import urlopen
17+
from urllib2 import urlopen
18+
from urllib2 import Request
1719
import re
1820
import json
1921

2022
import isodate
23+
from django.conf import settings
2124
from django.core.cache import cache
2225

2326
from .exceptions import CommitLogError
@@ -42,8 +45,17 @@ def fetch_json(url):
4245
json_obj = cache.get(url)
4346

4447
if json_obj is None:
48+
github_oauth_token = getattr(settings, 'GITHUB_OAUTH_TOKEN', None)
49+
50+
if github_oauth_token:
51+
headers = {'Authorization': 'token %s' % (github_oauth_token)}
52+
else:
53+
headers = {}
54+
55+
request = Request(url=url, headers=headers)
56+
4557
try:
46-
json_obj = json.load(urlopen(url))
58+
json_obj = json.load(urlopen(request))
4759
except IOError as e:
4860
logger.exception("Unable to load %s: %s",
4961
url, e, exc_info=True)

codespeed/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@
8383

8484
ALLOW_ANONYMOUS_POST = True # Whether anonymous users can post results
8585
REQUIRE_SECURE_AUTH = True # Whether auth needs to be over a secure channel
86+
87+
GITHUB_OAUTH_TOKEN = None # Github oAuth token to use when using Github repo type. If not
88+
# specified, it will utilize unauthenticated requests which have
89+
# low rate limits.

0 commit comments

Comments
 (0)