Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

views: FAIR signposting level 1 support (HTTP Link headers) #2938

Merged

Conversation

ptamarit
Copy link
Member

@ptamarit ptamarit commented Dec 10, 2024

❤️ Thank you for your contribution!

Description

Checklist

Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:

Frontend

Reminder

By using GitHub, you have already agreed to the GitHub’s Terms of Service including that:

  1. You license your contribution under the same terms as the current repository’s license.
  2. You agree that you have the right to license your contribution under the current repository’s license.

# then try to get the optional `link` from the custom license.
url = right.get("props", {}).get("url") or right.get("link")
if url:
licenses.append(_get_header("license", url))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FAIR Signposting docs recommends to use SPDX license identifier (e.g. https://spdx.org/licenses/CC0-1.0).
However, in Zenodo we store URLs like https://creativecommons.org/publicdomain/zero/1.0/legalcode and not spdx.org URLs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If props["scheme"] == "spdx" I think we can safely generate the URL like https://spdx.org/licenses/{right["id"]}. We might have licenses (or even non-SPDX licenses), in which case just using url like here would be ok.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately our IDs are lower-cased (e.g. antlr-pd-fallback) while the SPDX URLs are are mixed-cased and case-sensitive (e.g. https://spdx.org/licenses/ANTLR-PD-fallback.html).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, I tried in the browser and copy-pasting URLs for some reason kept the original case... Ok, this is a bummer, I think we'll have to add the original spdx ID with the exact case as a props.spdx_id field or similar...

I think it would be fine to shelve this and just use the url, depends on whether we want to spend more time to re-import SPDX and update the existing license vocabulary (funnily, the dump we have is from more than a year ago).


def _get_signposting_linkset(pid_value):
api_url = record_url_for(_app="api", pid_value=pid_value)
return _get_header("linkset", api_url, "application/linkset+json")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this is required for level 2 support and was already added in a previous pull request.
Here we only include a link of the type "application/linkset+json", but the docs requires to also include a link of type "application/linkset".

@ptamarit ptamarit force-pushed the 2937-fair-signposting-level-1 branch from 0929cec to 15672de Compare December 10, 2024 15:55
@ptamarit ptamarit changed the title views: FAIR signposting level 1 support views: FAIR signposting level 1 support (HTTP Link headers) Dec 10, 2024
Copy link
Member

@slint slint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some minor comments only

# then try to get the optional `link` from the custom license.
url = right.get("props", {}).get("url") or right.get("link")
if url:
licenses.append(_get_header("license", url))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If props["scheme"] == "spdx" I think we can safely generate the URL like https://spdx.org/licenses/{right["id"]}. We might have licenses (or even non-SPDX licenses), in which case just using url like here would be ok.

Comment on lines 410 to 452
def add_signposting_content_resources(f):
"""Add signposting links to the content resources view's response headers."""

@wraps(f)
def view(*args, **kwargs):
response = make_response(f(*args, **kwargs))

# Relies on other decorators having operated before it
pid_value = kwargs["pid_value"]
signposting_link = record_url_for(_app="api", pid_value=pid_value)

response.headers["Link"] = (
f'<{signposting_link}> ; rel="linkset" ; type="application/linkset+json"' # fmt: skip
)
signposting_headers = [
_get_signposting_collection(pid_value),
_get_signposting_linkset(pid_value),
]

response.headers["Link"] = " , ".join(signposting_headers)

return response

return view


def add_signposting_metadata_resources(f):
"""Add signposting links to the metadata resources view's response headers."""

@wraps(f)
def view(*args, **kwargs):
response = make_response(f(*args, **kwargs))

# Relies on other decorators having operated before it
pid_value = kwargs["pid_value"]

signposting_headers = [
_get_signposting_describes(pid_value),
_get_signposting_linkset(pid_value),
]

response.headers["Link"] = " , ".join(signposting_headers)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that, unlike the Landing Page which relies on invenio_rdm_records.resources.serializers.signposting, the Content Resources and Metadata Resources are not relying on invenio_rdm_records.resources.serializers.signposting because:

  1. ContentResourceSchema and ContentResourceSchema expect the record to be passed via context={"record_dict"} which makes it more difficult to reuse here.
  2. The logic is pretty simple to add only the collection, describes and linkset headers, so re-implementing it here is not that bad.

# The test record does not have a license.
'<https://schema.org/Photograph> ; rel="type"',
'<https://schema.org/AboutPage> ; rel="type"',
f'<{api_url}> ; rel="linkset" ; type="application/linkset+json"',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for the landing page is implemented in FAIRSignpostingProfileLvl1Serializer in invenio-rdm-records and is already tested there (see inveniosoftware/invenio-rdm-records#1908).
It stills makes sense to at least issue the HTTP call to the endpoint here, to make sure that the decorator is working properly, but maybe the assertion should be less detailed to avoid having to adapt this test every time we modify the other module?

Copy link
Member Author

@ptamarit ptamarit Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agreed with myself and changed the test to write more generic assertions like:

    # ...

    # There should be at least 10 export formats supported (e.g. "application/dcat+xml", "application/x-bibtex", etc.).
    assert sum('; rel="describedby" ;' in header for header in link_headers) >= 10

    # ...

@ptamarit ptamarit marked this pull request as draft January 7, 2025 08:31
@ptamarit ptamarit marked this pull request as ready for review February 17, 2025 15:58
@ptamarit ptamarit force-pushed the 2937-fair-signposting-level-1 branch from 2e533ed to 7861a81 Compare February 21, 2025 12:09
@ptamarit
Copy link
Member Author

Just rebased to trigger the tests now that inveniosoftware/invenio-rdm-records#1908 is merged and that invenio-rdm-records v17.1.0 is released.

@ptamarit ptamarit force-pushed the 2937-fair-signposting-level-1 branch from 7861a81 to 463c09c Compare February 21, 2025 12:22
@ptamarit
Copy link
Member Author

Also squashed all commits into one, since the first implementation was then replaced by one relying on FAIRSignpostingProfileLvl1Serializer.

@ptamarit ptamarit force-pushed the 2937-fair-signposting-level-1 branch 2 times, most recently from 8d64b9e to 195f53f Compare February 21, 2025 13:19
@ptamarit ptamarit force-pushed the 2937-fair-signposting-level-1 branch from 195f53f to 19dd2fe Compare February 21, 2025 13:19
@ptamarit ptamarit merged commit d21af4e into inveniosoftware:master Feb 21, 2025
4 checks passed
@ptamarit ptamarit deleted the 2937-fair-signposting-level-1 branch February 21, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FAIR signposting level 1 support (HTTP Link headers & link rel item)
3 participants