Skip to content

Commit

Permalink
chore: log all HTTP requests, get rid of --dump flag (DEV-3171) (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jan 12, 2024
1 parent d3e238b commit e2b0598
Show file tree
Hide file tree
Showing 28 changed files with 149 additions and 227 deletions.
3 changes: 0 additions & 3 deletions docs/cli-commands.md
Expand Up @@ -61,7 +61,6 @@ The following options are available:
- `-V` | `--validate-only` (optional): validate the JSON file without creating it on the DSP server
- `-l` | `--lists-only` (optional): create only the lists (prerequisite: the project exists on the server)
- `-v` | `--verbose` (optional): print more information about the progress to the console
- `-d` | `--dump` (optional): write every request to DSP-API into a file

The defaults are intended for local testing:

Expand Down Expand Up @@ -101,7 +100,6 @@ The following options are available:
- `-p` | `--password` (optional, default: `test`): password used for authentication with the DSP-API
- `-P` | `--project` (mandatory): shortcode, shortname or IRI of the project
- `-v` | `--verbose` (optional): print more information about the progress to the console
- `-d` | `--dump` (optional): write every request to DSP-API into a file

The defaults are intended for local testing:

Expand Down Expand Up @@ -141,7 +139,6 @@ The following options are available:
- `-i` | `--imgdir` (optional, default: `.`): folder from where the paths in the `<bitstream>` tags are evaluated
- `-V` | `--validate` (optional): validate the XML file without uploading it
- `-v` | `--verbose` (optional): print more information about the progress to the console
- `-d` | `--dump` (optional): write every request to DSP-API/SIPI into a file

Output:

Expand Down
6 changes: 1 addition & 5 deletions src/dsp_tools/cli/call_action.py
Expand Up @@ -156,7 +156,6 @@ def _call_ingest_xmlupload(args: argparse.Namespace) -> bool:
password=args.password,
dsp_url=args.server,
sipi_url=args.sipi_url,
dump=args.dump,
)
return True

Expand Down Expand Up @@ -202,7 +201,7 @@ def _call_xmlupload(args: argparse.Namespace) -> bool:
password=args.password,
imgdir=args.imgdir,
sipi=args.sipi_url,
config=UploadConfig(diagnostics=DiagnosticsConfig(verbose=args.verbose, dump=args.dump)),
config=UploadConfig(diagnostics=DiagnosticsConfig(verbose=args.verbose)),
)


Expand All @@ -214,7 +213,6 @@ def _call_get(args: argparse.Namespace) -> bool:
user=args.user,
password=args.password,
verbose=args.verbose,
dump=args.dump,
)


Expand All @@ -230,7 +228,6 @@ def _call_create(args: argparse.Namespace) -> bool:
server=args.server,
user=args.user,
password=args.password,
dump=args.dump,
)
case False, True:
success = validate_project(args.project_definition)
Expand All @@ -242,6 +239,5 @@ def _call_create(args: argparse.Namespace) -> bool:
user_mail=args.user,
password=args.password,
verbose=args.verbose,
dump=args.dump,
)
return success
4 changes: 0 additions & 4 deletions src/dsp_tools/cli/create_parsers.py
Expand Up @@ -194,7 +194,6 @@ def _add_ingest_xmlupload(
subparser.add_argument("-s", "--server", default=default_dsp_api_url, help=dsp_server_text)
subparser.add_argument("-u", "--user", default=root_user_email, help=username_text)
subparser.add_argument("-p", "--password", default=root_user_pw, help=password_text)
subparser.add_argument("-d", "--dump", action="store_true", help="write every request to DSP-API/SIPI into a file")
subparser.add_argument("xml_file", help="path to XML file containing the data")


Expand Down Expand Up @@ -267,7 +266,6 @@ def _add_xmlupload(
"-V", "--validate-only", action="store_true", help="validate the XML file without uploading it"
)
subparser.add_argument("-v", "--verbose", action="store_true", help=verbose_text)
subparser.add_argument("-d", "--dump", action="store_true", help="write every request to DSP-API/SIPI into a file")
subparser.add_argument("xmlfile", help="path to the XML file containing the data")


Expand All @@ -287,7 +285,6 @@ def _add_get(
subparser.add_argument("-p", "--password", default=root_user_pw, help=password_text)
subparser.add_argument("-P", "--project", help="shortcode, shortname or IRI of the project", required=True)
subparser.add_argument("-v", "--verbose", action="store_true", help=verbose_text)
subparser.add_argument("-d", "--dump", action="store_true", help="write every request to DSP-API into a file")
subparser.add_argument("project_definition", help="path to the file the project should be written to")


Expand Down Expand Up @@ -319,5 +316,4 @@ def _add_create(
help="create only the lists (prerequisite: the project exists on the server)",
)
subparser.add_argument("-v", "--verbose", action="store_true", help=verbose_text)
subparser.add_argument("-d", "--dump", action="store_true", help="write every request to DSP-API into a file")
subparser.add_argument("project_definition", help="path to the JSON project file")
6 changes: 2 additions & 4 deletions src/dsp_tools/commands/ingest_xmlupload/upload_xml.py
Expand Up @@ -8,7 +8,7 @@
get_mapping_dict_from_file,
replace_filepath_with_sipi_id,
)
from dsp_tools.commands.xmlupload.upload_config import DiagnosticsConfig, UploadConfig
from dsp_tools.commands.xmlupload.upload_config import UploadConfig
from dsp_tools.commands.xmlupload.xmlupload import xmlupload
from dsp_tools.models.exceptions import InputError
from dsp_tools.utils.create_logger import get_logger
Expand All @@ -23,7 +23,6 @@ def ingest_xmlupload(
password: str,
dsp_url: str,
sipi_url: str,
dump: bool = False,
) -> None:
"""
This function reads an XML file
Expand All @@ -39,7 +38,6 @@ def ingest_xmlupload(
password: the user's password for login into DSP
dsp_url: URL to the DSP server
sipi_url: URL to the Sipi server
dump: if True, write every request to DSP-API/SIPI into a file
Raises:
InputError: if any media was not uploaded or uploaded media was not referenced.
Expand Down Expand Up @@ -68,5 +66,5 @@ def ingest_xmlupload(
password=password,
imgdir=".",
sipi=sipi_url,
config=UploadConfig(media_previously_uploaded=True, diagnostics=DiagnosticsConfig(dump=dump)),
config=UploadConfig(media_previously_uploaded=True),
)
4 changes: 1 addition & 3 deletions src/dsp_tools/commands/project/create/project_create.py
Expand Up @@ -991,7 +991,6 @@ def create_project(
user_mail: str,
password: str,
verbose: bool = False,
dump: bool = False,
) -> bool:
"""
Creates a project from a JSON project file on a DSP server.
Expand All @@ -1006,7 +1005,6 @@ def create_project(
user_mail: a username (e-mail) who has the permission to create a project
password: the user's password
verbose: prints more information if set to True
dump: if True, write every request to DSP-API into a file
Raises:
UserError:
Expand Down Expand Up @@ -1049,7 +1047,7 @@ def create_project(
)

# establish connection to DSP server
con = ConnectionLive(server, dump=dump)
con = ConnectionLive(server)
con.login(user_mail, password)

# create project on DSP server
Expand Down
Expand Up @@ -135,7 +135,6 @@ def create_lists(
server: str,
user: str,
password: str,
dump: bool = False,
) -> tuple[dict[str, Any], bool]:
"""
This method accepts a JSON project definition,
Expand All @@ -151,7 +150,6 @@ def create_lists(
server: URL of the DSP server
user: Username (e-mail) for the DSP server, must have the permissions to create a project
password: Password of the user
dump: if True, write every request to DSP-API into a file
Raises:
UserError:
Expand Down Expand Up @@ -184,7 +182,7 @@ def create_lists(
print("JSON project file is syntactically correct and passed validation.")

# connect to the DSP server
con = ConnectionLive(server, dump=dump)
con = ConnectionLive(server)
con.login(user, password)

# retrieve the project
Expand Down
4 changes: 1 addition & 3 deletions src/dsp_tools/commands/project/get.py
Expand Up @@ -20,7 +20,6 @@ def get_project(
user: str | None = None,
password: str | None = None,
verbose: bool = False,
dump: bool = False,
) -> bool:
"""
This function writes a project from a DSP server into a JSON file.
Expand All @@ -32,15 +31,14 @@ def get_project(
user: the user (e-mail) who sends the request
password: the password of the user who sends the request
verbose: verbose option for the command, if used more output is given to the user
dump: if True, write every request to DSP-API into a file
Raises:
BaseError: if something went wrong
Returns:
True if the process finishes without errors
"""
con = ConnectionLive(server=server, dump=dump)
con = ConnectionLive(server)
if user and password:
try:
con.login(user, password)
Expand Down
11 changes: 4 additions & 7 deletions src/dsp_tools/commands/project/models/group.py
Expand Up @@ -23,7 +23,6 @@

from __future__ import annotations

import json
from typing import Any, Optional, Union
from urllib.parse import quote_plus

Expand Down Expand Up @@ -203,8 +202,7 @@ def toJsonObj(self, action: Actions) -> dict[str, Any]:

def create(self) -> Group:
jsonobj = self.toJsonObj(Actions.Create)
jsondata = json.dumps(jsonobj)
result = self._con.post(Group.ROUTE, jsondata)
result = self._con.post(Group.ROUTE, jsonobj)
return Group.fromJsonObj(self._con, result["group"])

def read(self) -> Group:
Expand All @@ -215,12 +213,11 @@ def update(self) -> Optional[Group]:
updated_group = None
jsonobj = self.toJsonObj(Actions.Update)
if jsonobj:
jsondata = json.dumps(jsonobj)
result = self._con.put(Group.ROUTE_SLASH + quote_plus(self._iri), jsondata)
result = self._con.put(Group.ROUTE_SLASH + quote_plus(self._iri), jsonobj)
updated_group = Group.fromJsonObj(self._con, result["group"])
if self._status is not None and "status" in self._changed:
jsondata = json.dumps({"status": self._status})
result = self._con.put(Group.ROUTE_SLASH + quote_plus(self._iri) + "/status", jsondata)
jsonobj = {"status": self._status}
result = self._con.put(Group.ROUTE_SLASH + quote_plus(self._iri) + "/status", jsonobj)
updated_group = Group.fromJsonObj(self._con, result["group"])
return updated_group

Expand Down
10 changes: 3 additions & 7 deletions src/dsp_tools/commands/project/models/listnode.py
Expand Up @@ -21,14 +21,12 @@
"""
from __future__ import annotations

import json
from pprint import pprint
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.commands.project.models.set_encoder import SetEncoder
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import LangString, Languages
Expand Down Expand Up @@ -419,12 +417,11 @@ def create(self) -> ListNode:
:return: JSON-object from DSP-API
"""
jsonobj = self.toJsonObj(Actions.Create)
jsondata = json.dumps(jsonobj, cls=SetEncoder)
if self._parent:
result = self._con.post(ListNode.ROUTE_SLASH + quote_plus(self._parent), jsondata)
result = self._con.post(ListNode.ROUTE_SLASH + quote_plus(self._parent), jsonobj)
return ListNode.fromJsonObj(self._con, result["nodeinfo"])
else:
result = self._con.post(ListNode.ROUTE, jsondata)
result = self._con.post(ListNode.ROUTE, jsonobj)
return ListNode.fromJsonObj(self._con, result["list"]["listinfo"])

def read(self) -> Any:
Expand All @@ -451,8 +448,7 @@ def update(self) -> Union[Any, None]:

jsonobj = self.toJsonObj(Actions.Update, self.iri)
if jsonobj:
jsondata = json.dumps(jsonobj, cls=SetEncoder)
result = self._con.put(ListNode.ROUTE_SLASH + quote_plus(self.iri), jsondata)
result = self._con.put(ListNode.ROUTE_SLASH + quote_plus(self.iri), jsonobj)
pprint(result)
return ListNode.fromJsonObj(self._con, result["listinfo"])
else:
Expand Down
5 changes: 1 addition & 4 deletions src/dsp_tools/commands/project/models/ontology.py
Expand Up @@ -27,7 +27,6 @@
from __future__ import annotations

import copy
import json
from typing import Any, Optional, Union
from urllib.parse import quote_plus

Expand All @@ -37,7 +36,6 @@
from dsp_tools.commands.project.models.project import Project
from dsp_tools.commands.project.models.propertyclass import PropertyClass
from dsp_tools.commands.project.models.resourceclass import ResourceClass
from dsp_tools.commands.project.models.set_encoder import SetEncoder
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions, Context, DateTimeStamp, WithId
from dsp_tools.utils.connection import Connection
Expand Down Expand Up @@ -301,8 +299,7 @@ def toJsonObj(self, action: Actions) -> Any:

def create(self) -> "Ontology":
jsonobj = self.toJsonObj(Actions.Create)
jsondata = json.dumps(jsonobj, cls=SetEncoder, indent=4)
result = self._con.post(Ontology.ROUTE, jsondata)
result = self._con.post(Ontology.ROUTE, jsonobj)
return Ontology.fromJsonObj(self._con, result)

def read(self) -> "Ontology":
Expand Down
8 changes: 2 additions & 6 deletions src/dsp_tools/commands/project/models/project.py
Expand Up @@ -24,12 +24,10 @@

from __future__ import annotations

import json
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.set_encoder import SetEncoder
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions
from dsp_tools.models.langstring import LangString, Languages
Expand Down Expand Up @@ -414,8 +412,7 @@ def create(self) -> Project:
:return: JSON-object from DSP
"""
jsonobj = self.toJsonObj(Actions.Create)
jsondata = json.dumps(jsonobj, cls=SetEncoder)
result = self._con.post(Project.ROUTE, jsondata)
result = self._con.post(Project.ROUTE, jsonobj)
return Project.fromJsonObj(self._con, result["project"])

def read(self) -> Project:
Expand Down Expand Up @@ -446,8 +443,7 @@ def update(self) -> Project:
Returns: JSON object returned as response from DSP reflecting the update
"""
jsonobj = self.toJsonObj(Actions.Update)
jsondata = json.dumps(jsonobj, cls=SetEncoder)
result = self._con.put(Project.IRI + quote_plus(self.iri), jsondata)
result = self._con.put(Project.IRI + quote_plus(self.iri), jsonobj)
return Project.fromJsonObj(self._con, result["project"])

def delete(self) -> Project:
Expand Down
11 changes: 3 additions & 8 deletions src/dsp_tools/commands/project/models/propertyclass.py
@@ -1,12 +1,10 @@
import json
from typing import Any, Optional, Sequence, Union
from urllib.parse import quote_plus

import regex

from dsp_tools.commands.project.models.listnode import ListNode
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.set_encoder import SetEncoder
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.helpers import Actions, Context, DateTimeStamp, WithId
from dsp_tools.models.langstring import LangString, Languages
Expand Down Expand Up @@ -333,8 +331,7 @@ def resolve_propref(resref: str) -> dict[str, str]:

def create(self, last_modification_date: DateTimeStamp) -> tuple[DateTimeStamp, "PropertyClass"]:
jsonobj = self.toJsonObj(last_modification_date, Actions.Create)
jsondata = json.dumps(jsonobj, cls=SetEncoder, indent=2)
result = self._con.post(PropertyClass.ROUTE, jsondata)
result = self._con.post(PropertyClass.ROUTE, jsonobj)
last_modification_date = DateTimeStamp(result["knora-api:lastModificationDate"])
return last_modification_date, PropertyClass.fromJsonObj(self._con, self._context, result["@graph"])

Expand All @@ -346,14 +343,12 @@ def update(self, last_modification_date: DateTimeStamp) -> tuple[DateTimeStamp,
something_changed = False
if "label" in self._changed:
jsonobj = self.toJsonObj(last_modification_date, Actions.Update, "label")
jsondata = json.dumps(jsonobj, cls=SetEncoder, indent=4)
result = self._con.put(PropertyClass.ROUTE, jsondata)
result = self._con.put(PropertyClass.ROUTE, jsonobj)
last_modification_date = DateTimeStamp(result["knora-api:lastModificationDate"])
something_changed = True
if "comment" in self._changed:
jsonobj = self.toJsonObj(last_modification_date, Actions.Update, "comment")
jsondata = json.dumps(jsonobj, cls=SetEncoder, indent=4)
result = self._con.put(PropertyClass.ROUTE, jsondata)
result = self._con.put(PropertyClass.ROUTE, jsonobj)
last_modification_date = DateTimeStamp(result["knora-api:lastModificationDate"])
something_changed = True
if something_changed:
Expand Down

0 comments on commit e2b0598

Please sign in to comment.