From 1d56f10ec1564e23a9c80789f05337b3c85b83f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Tue, 4 Oct 2016 18:59:54 +0200 Subject: [PATCH] hoster tasks: return dictionaries instead of intransparent tuples --- src/c3nav/editor/hosters/base.py | 14 +++++--------- src/c3nav/editor/hosters/github.py | 22 +++++++++++++++------- src/c3nav/editor/hosters/gitlab.py | 14 ++++++++++---- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/c3nav/editor/hosters/base.py b/src/c3nav/editor/hosters/base.py index 62eca861..4e4b12ba 100644 --- a/src/c3nav/editor/hosters/base.py +++ b/src/c3nav/editor/hosters/base.py @@ -99,13 +99,8 @@ class Hoster(ABC): session_data['state'] = 'logged_out' session_data['error'] = _('Internal error.') else: - state, content = task.result - if content: - if state == 'logged_out': - session_data['error'] = content - else: - session_data['access_token'] = content - session_data['state'] = state + result = task.result + session_data.update(result) # updates 'state' key and optional 'error' and 'access_tokenÄ keys. session_data.pop('checking_progress_id') def request_access_token(self, request, *args, **kwargs): @@ -137,7 +132,8 @@ class Hoster(ABC): def do_request_access_token(self, *args, **kwargs): """ Task method for requesting the access token asynchroniously. - Return a tuple with a new state and the access_token, or an optional error string if the state is 'logged_out'. + Returns a dict with a 'state' key containing the new hoster state, an optional 'error' key containing an + error message and an optional 'access_token' keys containing a new access token. """ pass @@ -145,6 +141,6 @@ class Hoster(ABC): def do_check_access_token(self, access_token): """ Task method for checking the access token asynchroniously. - Return a tuple with a new state and None, or an optional error string if the state is 'logged_out'. + Returns a dict with a 'state' key containing the new hoster state. """ pass diff --git a/src/c3nav/editor/hosters/github.py b/src/c3nav/editor/hosters/github.py index 67ed960d..41b332b6 100644 --- a/src/c3nav/editor/hosters/github.py +++ b/src/c3nav/editor/hosters/github.py @@ -55,20 +55,28 @@ class GithubHoster(Hoster): }, headers={'Accept': 'application/json'}).json() if 'error' in response: - return ('logged_out', - '%s: %s %s' % (response['error'], response['error_description'], response['error_uri'])) + return { + 'state': 'logged_out', + 'error': '%s: %s %s' % (response['error'], response['error_description'], response['error_uri']) + } if 'public_repo' not in response['scope'].split(','): - return ('missing_permissions', response['access_token']) + return { + 'state': 'missing_permissions', + 'access_token': response['access_token'] + } - return ('logged_in', response['access_token']) + return { + 'state': 'logged_in', + 'access_token': response['access_token'] + } def do_check_access_token(self, access_token): response = requests.get('https://api.github.com/rate_limit', headers={'Authorization': 'token '+access_token}) if response.status_code != 200: - return ('logged_out', '') + return {'state': 'logged_out'} if 'public_repo' not in (s.strip() for s in response.headers.get('X-OAuth-Scopes').split(',')): - return ('missing_permissions', None) + return {'state': 'missing_permissions'} - return ('logged_in', None) + return {'state': 'logged_in'} diff --git a/src/c3nav/editor/hosters/gitlab.py b/src/c3nav/editor/hosters/gitlab.py index 60df55d4..d414cb1f 100644 --- a/src/c3nav/editor/hosters/gitlab.py +++ b/src/c3nav/editor/hosters/gitlab.py @@ -59,13 +59,19 @@ class GitlabHoster(Hoster): }).json() if 'error' in response: - return ('logged_out', '%s: %s' % (response['error'], response['error_description'])) + return { + 'state': 'logged_out', + 'error': '%s: %s' % (response['error'], response['error_description']) + } - return ('logged_in', response['access_token']) + return { + 'state': 'logged_in', + 'access_token': response['access_token'] + } def do_check_access_token(self, access_token): response = requests.get(self.get_endpoint('/user'), headers={'Authorization': 'Bearer '+access_token}) if response.status_code != 200: - return ('logged_out', '') + return {'state': 'logged_out'} - return ('logged_in', None) + return {'state': 'logged_in'}