From f9e06a76447330234b24f17a82076fd91fd8a540 Mon Sep 17 00:00:00 2001 From: Luc Hermitte Date: Tue, 20 Aug 2024 03:40:56 +0200 Subject: [PATCH] Improve DbC doc for get_access_token() and fix post-condition on `get_netrc_credentials()` --- eof/_auth.py | 9 ++++++++- eof/dataspace_client.py | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/eof/_auth.py b/eof/_auth.py index db7016e..6d397bb 100644 --- a/eof/_auth.py +++ b/eof/_auth.py @@ -87,7 +87,12 @@ def _file_is_0600(filename: Filename): def get_netrc_credentials(host: str, netrc_file: Optional[Filename] = None) -> tuple[str, str]: - """Get username and password from netrc file for a given host.""" + """ + Get username and password from netrc file for a given host. + + :return: username and password found for host in netrc_file + :postcondition: username and password are non empty strings. + """ netrc_file = netrc_file or "~/.netrc" netrc_file = Path(netrc_file).expanduser() _logger.debug(f"Using {netrc_file=!r}") @@ -98,6 +103,8 @@ def get_netrc_credentials(host: str, netrc_file: Optional[Filename] = None) -> t username, _, password = auth if username is None or password is None: raise ValueError(f"No username/password found for {host} in ~/.netrc") + if not username or not password: + raise ValueError(f"Empty username/password found for {host} in ~/.netrc") return username, password diff --git a/eof/dataspace_client.py b/eof/dataspace_client.py index e0c3d55..53fcc73 100644 --- a/eof/dataspace_client.py +++ b/eof/dataspace_client.py @@ -291,10 +291,12 @@ def query_orbit_file_service(query: str) -> list[dict]: return query_results -def get_access_token(username, password, token_2fa) -> str: +def get_access_token(username: str, password: str, token_2fa: Optional[str]) -> str: """Get an access token for the Copernicus Data Space Ecosystem (CDSE) API. Code from https://documentation.dataspace.copernicus.eu/APIs/Token.html + + :precondition: username and password are non empty strings. """ assert username and password, "Username and password values are expected!"