add option to add filepath to the current session

This commit is contained in:
Florian Förster 2025-03-26 14:00:30 +01:00
parent 6deda74a80
commit 72ede57a82
3 changed files with 66 additions and 0 deletions

View File

@ -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,

View File

@ -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

View File

@ -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"],