Skip to content

Commit

Permalink
feat: TodoistAPIAsync accepts a session (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
lefcha authored Aug 7, 2024
1 parent a813a5d commit 8afab84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 11 additions & 1 deletion tests/test_api_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
6 changes: 4 additions & 2 deletions todoist_api_python/api_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down

0 comments on commit 8afab84

Please sign in to comment.