Skip to content

Commit

Permalink
chore: tidy up client magic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
giuppep committed Dec 28, 2021
1 parent c01c10d commit 00b3111
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions rstr/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,24 @@ def __repr__(self) -> str:
return f'Rstr("{self.url}")'

def _init_session(self) -> None:
if self._session is None:
self._session = Session()
self._session.auth = _TokenAuth(self._token)

def _close_session(self) -> None:
if self._session is not None:
raise RuntimeError("Session already initialised")
self._session = Session()
self._session.auth = _TokenAuth(self._token)
self._session.close()
self._session = None

def __enter__(self) -> "Rstr":
assert self._session is None
self._init_session()
return self

def __exit__(self, *_: Any) -> None:
assert self._session is not None
self._session.close()
self._session = None
self._close_session()

def __del__(self) -> None:
if self._session is not None:
self._session.close()
self._session = None
self._close_session()

def _request(
self, endpoint: str, method: RequestMethods, **kwargs: Any
Expand Down

0 comments on commit 00b3111

Please sign in to comment.