hoster tasks: return dictionaries instead of intransparent tuples

This commit is contained in:
Laura Klünder 2016-10-04 18:59:54 +02:00
parent 1a6ee4c2d5
commit 1d56f10ec1
3 changed files with 30 additions and 20 deletions

View file

@ -99,13 +99,8 @@ class Hoster(ABC):
session_data['state'] = 'logged_out' session_data['state'] = 'logged_out'
session_data['error'] = _('Internal error.') session_data['error'] = _('Internal error.')
else: else:
state, content = task.result result = task.result
if content: session_data.update(result) # updates 'state' key and optional 'error' and 'access_tokenÄ keys.
if state == 'logged_out':
session_data['error'] = content
else:
session_data['access_token'] = content
session_data['state'] = state
session_data.pop('checking_progress_id') session_data.pop('checking_progress_id')
def request_access_token(self, request, *args, **kwargs): def request_access_token(self, request, *args, **kwargs):
@ -137,7 +132,8 @@ class Hoster(ABC):
def do_request_access_token(self, *args, **kwargs): def do_request_access_token(self, *args, **kwargs):
""" """
Task method for requesting the access token asynchroniously. 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 pass
@ -145,6 +141,6 @@ class Hoster(ABC):
def do_check_access_token(self, access_token): def do_check_access_token(self, access_token):
""" """
Task method for checking the access token asynchroniously. 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 pass

View file

@ -55,20 +55,28 @@ class GithubHoster(Hoster):
}, headers={'Accept': 'application/json'}).json() }, headers={'Accept': 'application/json'}).json()
if 'error' in response: if 'error' in response:
return ('logged_out', return {
'%s: %s %s' % (response['error'], response['error_description'], response['error_uri'])) 'state': 'logged_out',
'error': '%s: %s %s' % (response['error'], response['error_description'], response['error_uri'])
}
if 'public_repo' not in response['scope'].split(','): 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): def do_check_access_token(self, access_token):
response = requests.get('https://api.github.com/rate_limit', headers={'Authorization': 'token '+access_token}) response = requests.get('https://api.github.com/rate_limit', headers={'Authorization': 'token '+access_token})
if response.status_code != 200: 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(',')): 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'}

View file

@ -59,13 +59,19 @@ class GitlabHoster(Hoster):
}).json() }).json()
if 'error' in response: 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): def do_check_access_token(self, access_token):
response = requests.get(self.get_endpoint('/user'), headers={'Authorization': 'Bearer '+access_token}) response = requests.get(self.get_endpoint('/user'), headers={'Authorization': 'Bearer '+access_token})
if response.status_code != 200: if response.status_code != 200:
return ('logged_out', '') return {'state': 'logged_out'}
return ('logged_in', None) return {'state': 'logged_in'}