From 8afab84c2fd72b89a78b550beaa420109989f842 Mon Sep 17 00:00:00 2001 From: Lefteris Chatzimparmpas Date: Wed, 7 Aug 2024 13:51:19 +0300 Subject: [PATCH] feat: TodoistAPIAsync accepts a session (#148) --- tests/test_api_async.py | 12 +++++++++++- todoist_api_python/api_async.py | 6 ++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_api_async.py b/tests/test_api_async.py index ac0619a..a2fc312 100644 --- a/tests/test_api_async.py +++ b/tests/test_api_async.py @@ -2,6 +2,8 @@ from unittest.mock import MagicMock, patch +import requests + from tests.data.test_defaults import DEFAULT_TOKEN from tests.utils.test_utils import get_todoist_api_patch from todoist_api_python.api import TodoistAPI @@ -13,4 +15,12 @@ def test_constructs_api_with_token(sync_api_constructor: MagicMock): sync_api_constructor.return_value = None TodoistAPIAsync(DEFAULT_TOKEN) - sync_api_constructor.assert_called_once_with(DEFAULT_TOKEN) + sync_api_constructor.assert_called_once_with(DEFAULT_TOKEN, None) + + +@patch(get_todoist_api_patch(TodoistAPI.__init__)) +def test_constructs_api_with_token_and_session(sync_api_constructor: MagicMock): + sync_api_constructor.return_value = None + session = requests.Session() + TodoistAPIAsync(DEFAULT_TOKEN, session) + sync_api_constructor.assert_called_once_with(DEFAULT_TOKEN, session) diff --git a/todoist_api_python/api_async.py b/todoist_api_python/api_async.py index 17fd6ce..3db285a 100644 --- a/todoist_api_python/api_async.py +++ b/todoist_api_python/api_async.py @@ -6,6 +6,8 @@ from todoist_api_python.utils import run_async if TYPE_CHECKING: + import requests + from todoist_api_python.models import ( Collaborator, Comment, @@ -19,8 +21,8 @@ class TodoistAPIAsync: - def __init__(self, token: str) -> None: - self._api = TodoistAPI(token) + def __init__(self, token: str, session: requests.Session | None = None) -> None: + self._api = TodoistAPI(token, session) async def get_task(self, task_id: str) -> Task: return await run_async(lambda: self._api.get_task(task_id))