diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fc5c4c..56116023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,12 @@ 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)) +- `Client` can now be as 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 diff --git a/substra/sdk/backends/remote/rest_client.py b/substra/sdk/backends/remote/rest_client.py index 0c7fcbad..406a073e 100644 --- a/substra/sdk/backends/remote/rest_client.py +++ b/substra/sdk/backends/remote/rest_client.py @@ -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) diff --git a/substra/sdk/client.py b/substra/sdk/client.py index 54a29656..7cc30338 100644 --- a/substra/sdk/client.py +++ b/substra/sdk/client.py @@ -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): @@ -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")