Skip to content

Commit

Permalink
enrich raised errors with context message
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Mar 22, 2024
1 parent 1eb3f23 commit 32c093a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 10 deletions.
7 changes: 6 additions & 1 deletion dsp_permissions_scripts/ap/ap_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import quote_plus

from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.models.api_error import ApiError
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils import connection

Expand All @@ -10,7 +11,11 @@

def _delete_ap_on_server(ap: Ap) -> None:
ap_iri = quote_plus(ap.iri, safe="")
connection.con.delete(f"/admin/permissions/{ap_iri}")
try:
connection.con.delete(f"/admin/permissions/{ap_iri}")
except ApiError as err:
err.message = f"Could not delete Administrative Permission {ap.iri}"
raise err from None


def delete_ap_of_group_on_server(
Expand Down
7 changes: 6 additions & 1 deletion dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


from dsp_permissions_scripts.ap.ap_model import Ap, ApValue
from dsp_permissions_scripts.models.api_error import ApiError
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode
from dsp_permissions_scripts.utils import connection
Expand Down Expand Up @@ -37,7 +38,11 @@ def create_admin_route_object_from_ap(ap: Ap) -> dict[str, Any]:

def _get_all_aps_of_project(project_iri: str) -> list[Ap]:
project_iri = quote_plus(project_iri, safe="")
response = connection.con.get(f"/admin/permissions/ap/{project_iri}")
try:
response = connection.con.get(f"/admin/permissions/ap/{project_iri}")
except ApiError as err:
err.message = f"Could not get APs of project {project_iri}"
raise err from None
aps: list[dict[str, Any]] = response["administrative_permissions"]
ap_objects = [create_ap_from_admin_route_object(ap) for ap in aps]
return ap_objects
Expand Down
6 changes: 5 additions & 1 deletion dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
def _update_ap_on_server(ap: Ap) -> Ap:
iri = quote_plus(ap.iri, safe="")
payload = {"hasPermissions": create_admin_route_object_from_ap(ap)["hasPermissions"]}
response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
try:
response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
except ApiError as err:
err.message = f"Could not update Administrative Permission {ap.iri}"
raise err from None
ap_updated: dict[str, Any] = response["administrative_permission"]
ap_object_updated = create_ap_from_admin_route_object(ap_updated)
return ap_object_updated
Expand Down
7 changes: 6 additions & 1 deletion dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import quote_plus

from dsp_permissions_scripts.doap.doap_model import Doap, DoapTarget, DoapTargetType
from dsp_permissions_scripts.models.api_error import ApiError
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode
from dsp_permissions_scripts.utils.scope_serialization import (
Expand Down Expand Up @@ -34,7 +35,11 @@ def _filter_doaps_by_target(

def _get_all_doaps_of_project(project_iri: str) -> list[Doap]:
project_iri = quote_plus(project_iri, safe="")
response = connection.con.get(f"/admin/permissions/doap/{project_iri}")
try:
response = connection.con.get(f"/admin/permissions/doap/{project_iri}")
except ApiError as err:
err.message = f"Error while getting DOAPs of project {project_iri}"
raise err from None
doaps: list[dict[str, Any]] = response["default_object_access_permissions"]
doap_objects = [create_doap_from_admin_route_response(doap) for doap in doaps]
return doap_objects
Expand Down
6 changes: 5 additions & 1 deletion dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
def _update_doap_scope_on_server(doap_iri: str, scope: PermissionScope) -> Doap:
iri = quote_plus(doap_iri, safe="")
payload = {"hasPermissions": create_admin_route_object_from_scope(scope)}
response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
try:
response = connection.con.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
except ApiError as err:
err.message = f"Could not update scope of DOAP {doap_iri}"
raise err from None
new_doap = create_doap_from_admin_route_response(response["default_object_access_permission"])
return new_doap

Expand Down
12 changes: 10 additions & 2 deletions dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def _get_next_page(
This means that the page must be incremented until the response contains 0 or 1 resource.
"""
route = f"/v2/resources?resourceClass={quote_plus(resclass_iri)}&page={page}"
result = connection.con.get(route, headers=headers)
try:
result = connection.con.get(route, headers=headers)
except ApiError as err:
err.message = "Could not get next page"
raise err from None

# result contains several resources: return them, then continue with next page
if "@graph" in result:
Expand All @@ -77,7 +81,11 @@ def _get_next_page(
def get_resource(resource_iri: str) -> dict[str, Any]:
"""Requests the resource with the given IRI from DSP-API"""
iri = quote_plus(resource_iri, safe="")
return connection.con.get(f"/v2/resources/{iri}")
try:
return connection.con.get(f"/v2/resources/{iri}")
except ApiError as err:
err.message = f"Error while getting resource {resource_iri}"
raise err from None


def get_oap_by_resource_iri(resource_iri: str) -> Oap:
Expand Down
19 changes: 16 additions & 3 deletions dsp_permissions_scripts/utils/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from urllib.parse import quote_plus

from dsp_permissions_scripts.models.api_error import ApiError
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import dereference_prefix
from dsp_permissions_scripts.utils import connection
Expand All @@ -8,14 +9,22 @@


def _get_onto_iris_of_project(project_iri: str) -> list[str]:
response = connection.con.get("/v2/ontologies/metadata")
try:
response = connection.con.get("/v2/ontologies/metadata")
except ApiError as err:
err.message = f"Could not get onto IRIs of project {project_iri}"
raise err from None
all_ontologies = response["@graph"]
project_onto_iris = [o["@id"] for o in all_ontologies if o["knora-api:attachedToProject"]["@id"] == project_iri]
return project_onto_iris


def _get_class_iris_of_onto(onto_iri: str) -> list[str]:
response = connection.con.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}")
try:
response = connection.con.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}")
except ApiError as err:
err.message = f"Could not get class IRIs of onto {onto_iri}"
raise err from None
all_entities = response["@graph"]
context = response["@context"]
class_ids = [c["@id"] for c in all_entities if c.get("knora-api:isResourceClass")]
Expand All @@ -35,6 +44,10 @@ def get_all_resource_class_iris_of_project(project_iri: str) -> list[str]:


def get_project_iri_by_shortcode(shortcode: str) -> str:
response = connection.con.get(f"/admin/projects/shortcode/{shortcode}")
try:
response = connection.con.get(f"/admin/projects/shortcode/{shortcode}")
except ApiError as err:
err.message = f"Could not get project IRI by shortcode {shortcode}"
raise err from None
iri: str = response["project"]["id"]
return iri

0 comments on commit 32c093a

Please sign in to comment.