Skip to content

Commit

Permalink
chore: get rid of shared.login() function (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jan 4, 2024
1 parent 1c13f5f commit 28806a0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 51 deletions.
10 changes: 4 additions & 6 deletions src/dsp_tools/commands/fast_xmlupload/upload_files.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/dsp_tools/commands/project/create/project_create.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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})...")
Expand Down
6 changes: 4 additions & 2 deletions src/dsp_tools/commands/project/create/project_create_lists.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 4 additions & 5 deletions 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
Expand Down Expand Up @@ -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:
"""
Expand Down
34 changes: 0 additions & 34 deletions src/dsp_tools/utils/shared.py
Expand Up @@ -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__)
Expand All @@ -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.
Expand Down

0 comments on commit 28806a0

Please sign in to comment.