diff --git a/quizler/lib.py b/quizler/lib.py index 28c3ea1..c8ba2b1 100644 --- a/quizler/lib.py +++ b/quizler/lib.py @@ -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: diff --git a/quizler/utils.py b/quizler/utils.py index bcb1edf..3495fc8 100644 --- a/quizler/utils.py +++ b/quizler/utils.py @@ -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] @@ -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') diff --git a/tests/quizler/test_lib.py b/tests/quizler/test_lib.py index 2f8868a..ddeb0f6 100644 --- a/tests/quizler/test_lib.py +++ b/tests/quizler/test_lib.py @@ -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')