Skip to content

Commit

Permalink
Add token deletion in Client destructor
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Léobal <olivier.leobal@owkin.com>
  • Loading branch information
oleobal committed Aug 9, 2023
1 parent c518351 commit e8c5ac7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `wait_completion` param on `get_performances`, `list_task_output_assets` and `get_task_output_asset` to block execution until execution is over ([#368](https://github.com/Substra/substra/pull/368))
- `list_task_output_assets` and `get_task_output_asset` wait that the compute task is over before getting assets ([#369](https://github.com/Substra/substra/pull/369))
- warning and help message when logging in with username/password rather than token ([#378](https://github.com/Substra/substra/pull/378))
- new `Client.logout` function, mirrorring `Client.login` ([#381](https://github.com/Substra/substra/pull/381))
- `Client` can now be the expression in a `with` statement ([#381](https://github.com/Substra/substra/pull/381))

### Changed

- change how API responses are parsed to match server changes ([#379](https://github.com/Substra/substra/pull/379))
- `Client` will now terminate the sessions it starts when given username & password ([#381](https://github.com/Substra/substra/pull/381))

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion substra/sdk/backends/remote/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def login(self, username, password):

def logout(self) -> None:
if not self._token_id:
logger.debug("Logging out has no effect (did not call Client.login)")
return
try:
r = requests.delete(
f"{self._base_url}/active-api-tokens/", params={"id": self._token_id}, headers=self._headers
)
r.raise_for_status()
self._token_id = None
logger.info("Successfully logged out")
except requests.exceptions.ConnectionError as e:
raise exceptions.ConnectionError.from_request_exception(e)
Expand Down
6 changes: 4 additions & 2 deletions substra/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ def __init__(
self._token = self.login(config_dict["username"].value, config_dict["password"].value)

def __enter__(self):
# for use in a `with` statement
return self

def __exit__(self, exc_type, exc_value, traceback):
# for use in a `with` statement
self.logout()

def __del__(self):
self.logout()

def _get_backend(self, backend_type: schemas.BackendType):
Expand Down Expand Up @@ -381,6 +382,7 @@ def login(self, username, password):
def logout(self) -> None:
"""
Log out from a remote server, if Client.login was used
(otherwise, nothing happens)
"""
if not self._backend:
raise exceptions.SDKException("No backend found")
Expand Down

0 comments on commit e8c5ac7

Please sign in to comment.