Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions dfh/dspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time

from dspace_rest_client.client import DSpaceClient
from requests import Response

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -30,6 +31,23 @@ def get_dspace_client(
return client


def api_get_with_auth_refresh(dspace_client: DSpaceClient, url: str) -> Response:
"""Perform GET routes via DSpace API, re-authenticating if auth is stale.

Note: many other POST, PUT, DELETE routes in the dspace-rest-python client library
perform automatic re-authentication / token refresh, but not GET routes. Testing
confirms that this is required for long running harvests where the connection got
stale.
"""
Comment thread
ghukill marked this conversation as resolved.
response = dspace_client.api_get(url)
if response.status_code == 401: # noqa: PLR2004
logger.debug("DSpace client auth appears stale; re-authenticating")
if not dspace_client.authenticate():
raise RuntimeError("Could not re-authenticate DSpaceClient")
response = dspace_client.api_get(url)
return response


def warm_dspace_auth(
*,
retry_attempts: int = 5,
Expand Down Expand Up @@ -87,8 +105,9 @@ def get_presigned_url_for_bitstream(

The URL returned is valid for a limited amount of time and a single request.
"""
response = dspace_client.api_get(
f"{dspace_client.API_ENDPOINT}/core/bitstreams/{bitstream_uuid}/signedurl"
response = api_get_with_auth_refresh(
dspace_client,
f"{dspace_client.API_ENDPOINT}/core/bitstreams/{bitstream_uuid}/signedurl",
)
if response.status_code != 200: # noqa: PLR2004
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ dfh = "dfh.cli:main"
include = ["dfh", "dfh.*"]

[tool.uv.sources]
timdex-dataset-api = { git = "https://github.com/MITLibraries/timdex-dataset-api", rev = "USE-531-fulltext-data-type-add" }
timdex-dataset-api = { git = "https://github.com/MITLibraries/timdex-dataset-api" }

[build-system]
requires = ["setuptools>=61"]
Expand Down
Loading
Loading