Skip to content

Commit

Permalink
resources: add etag headers
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed Sep 21, 2023
1 parent 7cca97d commit 29c61d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
50 changes: 37 additions & 13 deletions invenio_rdm_records/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from invenio_i18n import lazy_gettext as _
from invenio_records.systemfields.relations import InvalidRelationValue
from invenio_records_resources.resources.files import FileResourceConfig
from invenio_records_resources.resources.records.headers import etag_headers
from invenio_records_resources.services.base.config import ConfiguratorMixin, FromConfig
from invenio_requests.resources.requests.config import RequestSearchRequestArgsSchema

Expand Down Expand Up @@ -67,23 +68,42 @@ def csl_url_args_retriever():
#
# Response handlers
#
def _bibliography_headers(obj_or_list, code, many=False):
"""Override content type for 'text/x-bibliography'."""
_etag_headers = etag_headers(obj_or_list, code, many=False)
_etag_headers["content-type"] = "text/plain"
return _etag_headers


record_serializers = {
"application/json": ResponseHandler(JSONSerializer()),
"application/marcxml+xml": ResponseHandler(MARCXMLSerializer()),
"application/vnd.inveniordm.v1+json": ResponseHandler(UIJSONSerializer()),
"application/vnd.citationstyles.csl+json": ResponseHandler(CSLJSONSerializer()),
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers),
"application/marcxml+xml": ResponseHandler(
MARCXMLSerializer(), headers=etag_headers
),
"application/vnd.inveniordm.v1+json": ResponseHandler(
UIJSONSerializer(), headers=etag_headers
),
"application/vnd.citationstyles.csl+json": ResponseHandler(
CSLJSONSerializer(), headers=etag_headers
),
"application/vnd.datacite.datacite+json": ResponseHandler(
DataCite43JSONSerializer()
DataCite43JSONSerializer(), headers=etag_headers
),
"application/vnd.geo+json": ResponseHandler(
GeoJSONSerializer(), headers=etag_headers
),
"application/vnd.datacite.datacite+xml": ResponseHandler(
DataCite43XMLSerializer(), headers=etag_headers
),
"application/x-dc+xml": ResponseHandler(
DublinCoreXMLSerializer(), headers=etag_headers
),
"application/vnd.geo+json": ResponseHandler(GeoJSONSerializer()),
"application/vnd.datacite.datacite+xml": ResponseHandler(DataCite43XMLSerializer()),
"application/x-dc+xml": ResponseHandler(DublinCoreXMLSerializer()),
"text/x-bibliography": ResponseHandler(
StringCitationSerializer(url_args_retriever=csl_url_args_retriever),
headers={"content-type": "text/plain"},
headers=_bibliography_headers,
),
"application/x-bibtex": ResponseHandler(BibtexSerializer()),
"application/dcat+xml": ResponseHandler(DCATSerializer()),
"application/x-bibtex": ResponseHandler(BibtexSerializer(), headers=etag_headers),
"application/dcat+xml": ResponseHandler(DCATSerializer(), headers=etag_headers),
}


Expand Down Expand Up @@ -310,7 +330,9 @@ class RDMParentRecordLinksResourceConfig(RecordResourceConfig, ConfiguratorMixin
"link_id": ma.fields.Str(),
}

response_handlers = {"application/json": ResponseHandler(JSONSerializer())}
response_handlers = {
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers)
}

error_handlers = record_links_error_handlers

Expand Down Expand Up @@ -338,7 +360,9 @@ class RDMParentGrantsResourceConfig(RecordResourceConfig, ConfiguratorMixin):
}
request_extra_args = {"expand": ma.fields.Bool()}

response_handlers = {"application/json": ResponseHandler(JSONSerializer())}
response_handlers = {
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers)
}

error_handlers = grants_error_handlers

Expand Down
7 changes: 4 additions & 3 deletions invenio_rdm_records/resources/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from invenio_drafts_resources.resources import RecordResource
from invenio_drafts_resources.resources.records.errors import RedirectException
from invenio_records_resources.resources.errors import ErrorHandlersMixin
from invenio_records_resources.resources.records.headers import etag_headers
from invenio_records_resources.resources.records.resource import (
request_data,
request_extra_args,
Expand Down Expand Up @@ -649,7 +650,7 @@ def with_iiif_content_negotiation(serializer):
"""Always response as JSON LD regardless of the request type."""
return with_content_negotiation(
response_handlers={
"application/ld+json": ResponseHandler(serializer()),
"application/ld+json": ResponseHandler(serializer(), headers=etag_headers),
},
default_accept_mimetype="application/ld+json",
)
Expand Down Expand Up @@ -692,15 +693,15 @@ def _get_record_with_files(self):
@response_handler()
def manifest(self):
"""Manifest."""
return self._get_record_with_files(), 200
return self._get_record_with_files().to_dict(), 200

@cross_origin(origin="*", methods=["GET"])
@with_iiif_content_negotiation(IIIFSequenceV2JSONSerializer)
@iiif_request_view_args
@response_handler()
def sequence(self):
"""Sequence."""
return self._get_record_with_files(), 200
return self._get_record_with_files().to_dict(), 200

@cross_origin(origin="*", methods=["GET"])
@with_iiif_content_negotiation(IIIFCanvasV2JSONSerializer)
Expand Down

0 comments on commit 29c61d0

Please sign in to comment.