From 72ede57a82eab9be5edbd6819b83b7481ca01a46 Mon Sep 17 00:00:00 2001 From: foefl Date: Wed, 26 Mar 2025 14:00:30 +0100 Subject: [PATCH] add option to add filepath to the current session --- src/delta_barth/api/common.py | 25 +++++++++++++++++++++++++ src/delta_barth/management.py | 7 +++++++ tests/api/test_common.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/delta_barth/api/common.py b/src/delta_barth/api/common.py index 6a02173..2a05f82 100644 --- a/src/delta_barth/api/common.py +++ b/src/delta_barth/api/common.py @@ -1,5 +1,6 @@ from __future__ import annotations +from pathlib import Path from typing import TYPE_CHECKING, Final import requests @@ -26,17 +27,29 @@ class Session: self, base_headers: HttpContentHeaders, ) -> None: + self._data_path: Path | None = None self._creds: ApiCredentials | None = None self._base_url: str | None = None self._headers = base_headers self._session_token: str | None = None self._logged_in: bool = False + @property + def data_path(self) -> Path: + assert self._data_path is not None, "accessed data path not set" + return self._data_path + @property def creds(self) -> ApiCredentials: assert self._creds is not None, "accessed credentials not set" return self._creds + def set_data_path( + self, + path: str, + ): + self._data_path = validate_path(path) + def set_credentials( self, username: str, @@ -185,6 +198,18 @@ class Session: return response, status +def validate_path( + str_path: str, +) -> Path: + path = Path(str_path).resolve() + if not path.exists(): + raise FileNotFoundError(f"Provided path >{path}< seems not to exist.") + elif not path.is_dir(): + raise FileNotFoundError(f"Provided path >{path}< seems not to be a directory.") + + return path + + def validate_credentials( username: str, password: str, diff --git a/src/delta_barth/management.py b/src/delta_barth/management.py index 8a0cf72..a7177bc 100644 --- a/src/delta_barth/management.py +++ b/src/delta_barth/management.py @@ -11,6 +11,12 @@ from delta_barth.constants import HTTP_BASE_CONTENT_HEADERS SESSION: Final[Session] = Session(HTTP_BASE_CONTENT_HEADERS) +def set_data_path( + path: str, +) -> None: + SESSION.set_data_path(path) + + def set_credentials( username: str, password: str, @@ -30,6 +36,7 @@ def get_credentials() -> str: # pragma: no cover return creds.model_dump_json() +# ** legacy: not part of external API def set_base_url( base_url: str, ) -> None: # pragma: no cover diff --git a/tests/api/test_common.py b/tests/api/test_common.py index cf52814..30440a4 100644 --- a/tests/api/test_common.py +++ b/tests/api/test_common.py @@ -1,3 +1,5 @@ +from pathlib import Path + import pytest from pydantic import ValidationError @@ -9,6 +11,38 @@ from delta_barth.errors import ( from delta_barth.types import HttpRequestTypes +def test_validate_path_Success(): + str_pth = str(Path.cwd()) + path = common.validate_path(str_pth) + assert path.name == Path.cwd().name + + +def test_validate_path_FailNotExisting(): + str_pth = str(Path.cwd() / "test") + with pytest.raises(FileNotFoundError, match=r"seems not to exist"): + _ = common.validate_path(str_pth) + + +def test_validate_path_FailNoDirectory(tmp_path): + file = tmp_path / "test.txt" + file.write_text("test", encoding="utf-8") + + str_pth = str(file) + with pytest.raises(FileNotFoundError, match=r"seems not to be a directory"): + _ = common.validate_path(str_pth) + + +def test_session_set_DataPath(tmp_path): + str_path = str(tmp_path) + session = common.Session(HTTP_BASE_CONTENT_HEADERS) + + assert session._data_path is None + + session.set_data_path(str_path) + assert session._data_path is not None + assert isinstance(session.data_path, Path) + + def test_validate_creds(credentials): creds = common.validate_credentials( username=credentials["user"],