Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #590 - Fix conditional proxy requests #608

Merged
merged 3 commits into from
Apr 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions webcompat/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def proxy_issue(number):

either as an authed user, or as one of our proxy bots.
'''
request_headers = get_request_headers(g.request_headers)
if g.user:
request_headers = get_request_headers(g.request_headers)
issue = github.raw_request('GET', 'repos/{0}/{1}'.format(
ISSUES_PATH, number), headers=request_headers)
else:
issue = proxy_request('get', '/{0}'.format(number))
issue = proxy_request('get', '/{0}'.format(number),
headers=request_headers)
return (issue.content, issue.status_code, get_headers(issue))


Expand Down Expand Up @@ -167,13 +168,14 @@ def get_search_results(query_string=None, params=None):

# convert issues api to search api params here.
params = normalize_api_params(params)
request_headers = get_request_headers(g.request_headers)

if g.user:
request_headers = get_request_headers(g.request_headers)
results = github.raw_request('GET', 'search/issues', params=params,
headers=request_headers)
else:
results = proxy_request('get', params=params, uri=search_uri)
results = proxy_request('get', params=params, uri=search_uri,
headers=request_headers)
return (results.content, results.status_code, get_headers(results))


Expand Down Expand Up @@ -219,16 +221,18 @@ def proxy_comments(number):
print('GitHubError: ', e.response.status_code)
return (':(', e.response.status_code)
else:
request_headers = get_request_headers(g.request_headers)

if g.user:
request_headers = get_request_headers(g.request_headers)
comments = github.raw_request(
'GET',
'repos/{0}/{1}/comments'.format(
ISSUES_PATH, number),
headers=request_headers
)
else:
comments = proxy_request('get', '/{0}/comments'.format(number))
comments = proxy_request('get', '/{0}/comments'.format(number),
headers=request_headers)
return (comments.content, comments.status_code, get_headers(comments))


Expand Down Expand Up @@ -275,9 +279,9 @@ def get_rate_limit():
'''

rate_limit_uri = 'https://api.github.com/rate_limit'
request_headers = get_request_headers(g.request_headers)
if g.user:
request_headers = get_request_headers(g.request_headers)
rl = github.raw_request('GET', 'rate_limit', headers=request_headers)
else:
rl = proxy_request('get', uri=rate_limit_uri)
rl = proxy_request('get', uri=rate_limit_uri, headers=request_headers)
return rl.content
20 changes: 12 additions & 8 deletions webcompat/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,34 @@
from webcompat import github

REPO_URI = app.config['ISSUES_REPO_URI']
headers = {'Authorization': 'token {0}'.format(app.config['BOT_OAUTH_TOKEN']),
'User-Agent': 'webcompat/webcompat-bot'}
BOT_TOKEN = app.config['BOT_OAUTH_TOKEN']
AUTH_HEADERS = {'Authorization': 'token {0}'.format(BOT_TOKEN),
'User-Agent': 'webcompat/webcompat-bot'}


def proxy_request(method, path_mod='', data=None, params=None, uri=None):
def proxy_request(method, path_mod='', data=None, params=None, uri=None,
headers=None):
'''Make a GitHub API request with a bot's OAuth token.

Necessary for non-logged in users.
* `path`, if included, will be appended to the end of the URI.
* Optionally pass in POST data via the `data` arg.
* Optionally point to a different URI via the `uri` arg.
* Optionally pass in HTTP headers to forward.
'''
# We capture the etag of the request and sends it back to github
if 'If-None-Match' in headers:
etag = g.request_headers['If-None-Match'].encode('utf-8')
headers['If-None-Match'] = etag
# Merge passed in headers with AUTH_HEADERS, and add the etag of the
# request, if it exists, to be sent back to GitHub.
auth_headers = AUTH_HEADERS.copy()
if headers:
auth_headers.update(headers)
# Preparing the requests
req = getattr(requests, method)
if uri:
req_uri = uri
else:
req_uri = 'https://api.github.com/repos/{0}{1}'.format(REPO_URI,
path_mod)
return req(req_uri, data=data, params=params, headers=headers)
return req(req_uri, data=data, params=params, headers=auth_headers)


def report_issue(form, proxy=False):
Expand Down