Skip to content

Commit

Permalink
fix: Properly close requests Session object (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
lefcha authored Aug 7, 2024
1 parent 0d2f31a commit 3529409
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions todoist_api_python/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import Any
from weakref import finalize

import requests

Expand Down Expand Up @@ -36,6 +37,13 @@ class TodoistAPI:
def __init__(self, token: str, session: requests.Session | None = None) -> None:
self._token: str = token
self._session = session or requests.Session()
self._finalizer = finalize(self, self._session.close)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self._finalizer()

def get_task(self, task_id: str) -> Task:
endpoint = get_rest_url(f"{TASKS_ENDPOINT}/{task_id}")
Expand Down

0 comments on commit 3529409

Please sign in to comment.