Skip to content

Commit

Permalink
Fixes #22 - Support for user-authenticated API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lourot committed Jan 2, 2018
1 parent d5e1298 commit 2246e4e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
6 changes: 4 additions & 2 deletions quizler/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def get_api_envs():
return client_id, user_id


def api_call(method: str, end_point: str, params: Dict[str, str], client_id: str):
def api_call(method: str, end_point: str, params: Dict[str, str], client_id: str,
access_token: str = None):
"""Call given API end_point with API keys."""
url = 'https://api.quizlet.com/2.0/{}'.format(end_point)
params['client_id'] = client_id
headers = {'Authorization': 'Bearer {}'.format(access_token)} if access_token else None
# pylint: disable=too-many-function-args
response = requests.request(method, url, params=params)
response = requests.request(method, url, params=params, headers=headers)
# pylint: enable=too-many-function-args
# pylint: disable=no-member
if response.status_code != requests.codes.ok:
Expand Down
14 changes: 7 additions & 7 deletions quizler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ def print_common_terms(common_terms: List[Tuple[str, str, Set[str]]]):
print(' {}'.format(term))


def delete_term(set_id, term_id, client_id):
def delete_term(set_id, term_id, client_id, access_token):
"""Delete the given term."""
api_call('delete', 'sets/{}/terms/{}'.format(set_id, term_id), {}, client_id)
api_call('delete', 'sets/{}/terms/{}'.format(set_id, term_id), {}, client_id, access_token)


def add_term(set_id, term: Term, client_id):
def add_term(set_id, term: Term, client_id, access_token):
"""Add the given term to the given set."""
api_call('post', 'sets/{}/terms'.format(set_id), term.to_dict(), client_id)
api_call('post', 'sets/{}/terms'.format(set_id), term.to_dict(), client_id, access_token)


def reset_term_stats(set_id, term_id, client_id, user_id):
def reset_term_stats(set_id, term_id, client_id, user_id, access_token):
"""Reset the stats of a term by deleting and re-creating it."""
found_sets = [user_set for user_set in get_user_sets(client_id, user_id)
if user_set.set_id == set_id]
Expand All @@ -81,9 +81,9 @@ def reset_term_stats(set_id, term_id, client_id, user_id):
raise NotImplementedError('"{}" has an image and is thus not supported'.format(term))

print('Deleting "{}"...'.format(term))
delete_term(set_id, term_id, client_id)
delete_term(set_id, term_id, client_id, access_token)
print('Re-creating "{}"...'.format(term))
add_term(set_id, term_id, client_id)
add_term(set_id, term_id, client_id, access_token)
print('Done')


Expand Down
3 changes: 2 additions & 1 deletion tests/quizler/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_correct_url_was_called(self, mock_request):
mock_request.assert_called_once_with(
'get',
'https://api.quizlet.com/2.0/end_point',
params={'client_id': 'client_id'}
params={'client_id': 'client_id'},
headers=None
)

@mock.patch('requests.request')
Expand Down

0 comments on commit 2246e4e

Please sign in to comment.