diff --git a/src/dsp_tools/commands/fast_xmlupload/upload_files.py b/src/dsp_tools/commands/fast_xmlupload/upload_files.py index 027f477e8..61ee89f77 100644 --- a/src/dsp_tools/commands/fast_xmlupload/upload_files.py +++ b/src/dsp_tools/commands/fast_xmlupload/upload_files.py @@ -12,8 +12,9 @@ from dsp_tools.models.exceptions import UserError from dsp_tools.utils.connection import Connection +from dsp_tools.utils.connection_live import ConnectionLive from dsp_tools.utils.create_logger import get_logger -from dsp_tools.utils.shared import login, make_chunks +from dsp_tools.utils.shared import make_chunks logger = get_logger(__name__) @@ -411,11 +412,8 @@ def upload_files( logger.info(f"Found {len(internal_filenames_of_processed_files)} files to upload...") # create connection to DSP - con = login( - server=dsp_url, - user=user, - password=password, - ) + con = ConnectionLive(dsp_url) + con.login(user, password) # upload files in parallel start_time = datetime.now() diff --git a/src/dsp_tools/commands/project/create/project_create.py b/src/dsp_tools/commands/project/create/project_create.py index d00cf7d11..93d00a752 100644 --- a/src/dsp_tools/commands/project/create/project_create.py +++ b/src/dsp_tools/commands/project/create/project_create.py @@ -20,8 +20,9 @@ from dsp_tools.models.helpers import Cardinality, Context, DateTimeStamp from dsp_tools.models.langstring import LangString from dsp_tools.utils.connection import Connection +from dsp_tools.utils.connection_live import ConnectionLive from dsp_tools.utils.create_logger import get_logger -from dsp_tools.utils.shared import login, parse_json_input +from dsp_tools.utils.shared import parse_json_input logger = get_logger(__name__) @@ -1048,7 +1049,8 @@ def create_project( ) # establish connection to DSP server - con = login(server=server, user=user_mail, password=password, dump=dump) + con = ConnectionLive(server, dump=dump) + con.login(user_mail, password) # create project on DSP server print(f"Create project '{proj_shortname}' ({proj_shortcode})...") diff --git a/src/dsp_tools/commands/project/create/project_create_lists.py b/src/dsp_tools/commands/project/create/project_create_lists.py index 3bc4d3a1d..f8d3b7cfd 100644 --- a/src/dsp_tools/commands/project/create/project_create_lists.py +++ b/src/dsp_tools/commands/project/create/project_create_lists.py @@ -6,8 +6,9 @@ from dsp_tools.commands.project.models.project import Project from dsp_tools.models.exceptions import BaseError, UserError from dsp_tools.utils.connection import Connection +from dsp_tools.utils.connection_live import ConnectionLive from dsp_tools.utils.create_logger import get_logger -from dsp_tools.utils.shared import login, parse_json_input +from dsp_tools.utils.shared import parse_json_input logger = get_logger(__name__) @@ -183,7 +184,8 @@ def create_lists( print("JSON project file is syntactically correct and passed validation.") # connect to the DSP server - con = login(server=server, user=user, password=password, dump=dump) + con = ConnectionLive(server, dump=dump) + con.login(user, password) # retrieve the project shortcode = project_definition["project"]["shortcode"] diff --git a/src/dsp_tools/commands/xmlupload/xmlupload.py b/src/dsp_tools/commands/xmlupload/xmlupload.py index 74e4baf54..fcd8fe2be 100644 --- a/src/dsp_tools/commands/xmlupload/xmlupload.py +++ b/src/dsp_tools/commands/xmlupload/xmlupload.py @@ -37,7 +37,6 @@ from dsp_tools.utils.connection_live import ConnectionLive from dsp_tools.utils.create_logger import get_logger from dsp_tools.utils.json_ld_util import get_json_ld_context_for_project -from dsp_tools.utils.shared import login logger = get_logger(__name__) @@ -84,7 +83,8 @@ def xmlupload( ) # establish connection to DSP server - con = login(server=server, user=user, password=password, dump=config.diagnostics.dump) + con = ConnectionLive(server, dump=config.diagnostics.dump) + con.login(user, password) sipi_con = ConnectionLive(sipi, dump=config.diagnostics.dump, token=con.get_token()) sipi_server = Sipi(sipi_con) diff --git a/src/dsp_tools/utils/connection_live.py b/src/dsp_tools/utils/connection_live.py index d90a3ed42..8b3073d53 100644 --- a/src/dsp_tools/utils/connection_live.py +++ b/src/dsp_tools/utils/connection_live.py @@ -1,6 +1,6 @@ import json import time -from dataclasses import dataclass +from dataclasses import dataclass, field from datetime import datetime from functools import partial from pathlib import Path @@ -106,18 +106,17 @@ class ConnectionLive: Attributes: server: address of the server, e.g https://api.dasch.swiss dump: if True, every request is written into a file - dump_directory: directory where the HTTP requests are written token: session token received by the server after login """ server: str dump: bool = False - dump_directory = Path("HTTP requests") + dump_directory: Path = field(init=False, default=Path("HTTP requests")) token: Optional[str] = None # downtimes of server-side services -> API still processes request # -> retry too early has side effects (e.g. duplicated resources) - timeout_put_post: int = 30 * 60 - timeout_get_delete: int = 20 + timeout_put_post: int = field(init=False, default=30 * 60) + timeout_get_delete: int = field(init=False, default=20) def __post_init__(self) -> None: """ diff --git a/src/dsp_tools/utils/shared.py b/src/dsp_tools/utils/shared.py index 80097af53..3fcb94b83 100644 --- a/src/dsp_tools/utils/shared.py +++ b/src/dsp_tools/utils/shared.py @@ -15,8 +15,6 @@ from dsp_tools.commands.excel2xml.propertyelement import PropertyElement from dsp_tools.models.exceptions import BaseError, UserError -from dsp_tools.utils.connection import Connection -from dsp_tools.utils.connection_live import ConnectionLive from dsp_tools.utils.create_logger import get_logger logger = get_logger(__name__) @@ -43,38 +41,6 @@ def make_chunks(lst: list[T], length: int) -> Iterable[list[T]]: yield lst[i : i + length] -def login( - server: str, - user: str, - password: str, - dump: bool = False, -) -> Connection: - """ - Creates a connection, - makes a login (handling temporary network interruptions), - and returns the active connection. - - Args: - server: URL of the DSP server to connect to - user: Username (e-mail) - password: Password of the user - dump: if True, every request is written into a file - - Raises: - UserError: if the login fails permanently - - Returns: - Connection instance - """ - con = ConnectionLive(server=server, dump=dump) - try: - con.login(email=user, password=password) - except BaseError: - logger.error("Cannot login to DSP server", exc_info=True) - raise UserError("Cannot login to DSP server") from None - return con - - def validate_xml_against_schema(input_file: Union[str, Path, etree._ElementTree[Any]]) -> bool: """ Validates an XML file against the DSP XSD schema.