-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: log every request to DSP-API (#76)
- Loading branch information
Showing
15 changed files
with
506 additions
and
470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,35 @@ | ||
from urllib.parse import quote_plus | ||
|
||
import requests | ||
|
||
from dsp_permissions_scripts.ap.ap_model import Ap | ||
from dsp_permissions_scripts.models.api_error import ApiError | ||
from dsp_permissions_scripts.utils.authentication import get_protocol | ||
from dsp_permissions_scripts.models.errors import ApiError | ||
from dsp_permissions_scripts.utils.dsp_client import DspClient | ||
from dsp_permissions_scripts.utils.get_logger import get_logger | ||
from dsp_permissions_scripts.utils.try_request import http_call_with_retry | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
def _delete_ap_on_server( | ||
ap: Ap, | ||
host: str, | ||
token: str, | ||
) -> None: | ||
headers = {"Authorization": f"Bearer {token}"} | ||
def _delete_ap_on_server(ap: Ap, dsp_client: DspClient) -> None: | ||
ap_iri = quote_plus(ap.iri, safe="") | ||
protocol = get_protocol(host) | ||
url = f"{protocol}://{host}/admin/permissions/{ap_iri}" | ||
response = http_call_with_retry( | ||
action=lambda: requests.delete(url, headers=headers, timeout=20), | ||
err_msg=f"Could not delete Administrative Permission {ap.iri}", | ||
) | ||
if response.status_code != 200: | ||
raise ApiError(f"Could not delete Administrative Permission {ap.iri}", response.text, response.status_code) | ||
try: | ||
dsp_client.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( | ||
host: str, | ||
token: str, | ||
existing_aps: list[Ap], | ||
forGroup: str, | ||
dsp_client: DspClient, | ||
) -> list[Ap]: | ||
aps_to_delete = [ap for ap in existing_aps if ap.forGroup == forGroup] | ||
if not aps_to_delete: | ||
logger.warning(f"There are no APs to delete on {host} for group {forGroup}") | ||
return existing_aps | ||
logger.info(f"Deleting the Administrative Permissions for group {forGroup} on server {host}") | ||
for ap in aps_to_delete: | ||
_delete_ap_on_server( | ||
ap=ap, | ||
host=host, | ||
token=token, | ||
) | ||
_delete_ap_on_server(ap, dsp_client) | ||
existing_aps.remove(ap) | ||
logger.info(f"Deleted Administrative Permission {ap.iri}") | ||
return existing_aps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
dsp_permissions_scripts/models/api_error.py → dsp_permissions_scripts/models/errors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import pprint | ||
from dataclasses import dataclass, field | ||
from typing import Any | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass(frozen=True) | ||
@dataclass | ||
class ApiError(Exception): | ||
"""Exception raised when an error occurs while calling DSP-API.""" | ||
|
||
message: str | ||
response_text: str | None = None | ||
status_code: int | None = None | ||
payload: dict[str, Any] = field(default_factory=dict) | ||
|
||
def __str__(self) -> str: | ||
return pprint.pformat(vars(self)) | ||
|
||
|
||
@dataclass | ||
class PermissionsAlreadyUpToDate(Exception): | ||
message: str = "The submitted permissions are the same as the current ones" |
Oops, something went wrong.