Skip to content

Add handler to associate IA ocaids with OL edition records#12429

Open
jimchamp wants to merge 9 commits intointernetarchive:masterfrom
jimchamp:ocaid-association-handler
Open

Add handler to associate IA ocaids with OL edition records#12429
jimchamp wants to merge 9 commits intointernetarchive:masterfrom
jimchamp:ocaid-association-handler

Conversation

@jimchamp
Copy link
Copy Markdown
Collaborator

@jimchamp jimchamp commented Apr 22, 2026

Closes #11490

Creates new /api/link.json handler for the purpose of syncing data between IA and OL. POST requests to this endpoint are expected to have the following query parameters:

  • msg : A colon delimited string in the format {ocaid}|{openlibrary_edition_id}|{expiry}
  • digest : The sha256 hashed value of msg

All inputs required for the operation are expected to be sent in the msg string.

The POST handler will, in order:

  1. Verify the digest and check the expiry
  2. Validate the request inputs
  3. Query for the OL edition that matches the Open Library edition ID given in the msg
  4. Adds given ocaid to the OL edition

Possible responses:

Status Condition Response Body
200 Success {"status": "ok"}
400 Missing/malformed ocaid or ts in msg {"error": "Invalid inputs"}
401 Invalid or expired HMAC digest (empty)
404 No edition found with given ocaid (empty)
500 DB/save failure {"error": "exception_message"}

Technical

Testing

Screenshot

Stakeholders

Copilot AI review requested due to automatic review settings April 22, 2026 00:29
@github-actions github-actions Bot added the Priority: 2 Important, as time permits. [managed] label Apr 22, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new internal API endpoint intended to let Internet Archive associate an IA ocaid with an Open Library edition record.

Changes:

  • Introduces a new POST /api/link (JSON) handler to validate an HMAC token + expiry and then update an edition record.
  • Updates the target edition’s ocaid and appends an ia:{ocaid} entry to source_records.

Comment thread openlibrary/plugins/openlibrary/api.py Outdated
Comment thread openlibrary/plugins/openlibrary/api.py Outdated
Comment thread openlibrary/plugins/openlibrary/api.py
Comment thread openlibrary/plugins/openlibrary/api.py Outdated
Comment on lines +1115 to +1118
i = web.input(digest="", msg="")
digest = i.digest
msg = i.msg

Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

PR description says digest is a SHA-256 hash of msg, but this endpoint uses HMACToken.verify, which expects an HMAC using a shared secret (and currently MD5-based). Please align the PR description and the IA client implementation with what the server actually verifies.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

HMAC.verify has been updated to use sha256 in #12402.

Comment on lines +1110 to +1114
class link_ia_ol(delegate.page):
path = "/api/link"
encoding = "json"

def POST(self):
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

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

This adds a new /api/link handler with multiple behaviors (401/400/404/500 + a successful update to ocaid/source_records), but there are no tests covering it. Please add unit tests following existing openlibrary/plugins/openlibrary/tests/* patterns to assert the expected status codes and that edition fields are updated correctly.

Copilot uses AI. Check for mistakes.
Comment thread openlibrary/plugins/openlibrary/api.py Outdated
@jimchamp jimchamp marked this pull request as draft April 22, 2026 00:49
@hornc
Copy link
Copy Markdown
Collaborator

hornc commented Apr 22, 2026

@jimchamp This should not add an archive.org source_record if the record's metadata was not sourced from archive.org. Doing so would be confusing to librarians who are tracking down metadata issues, and imply an import has occurred when only a 'link' has.

@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label Apr 22, 2026
@jimchamp
Copy link
Copy Markdown
Collaborator Author

jimchamp commented Apr 22, 2026

Thanks @hornc! I had already forgotten to ask about that assumption...

Will update shortly.

@jimchamp jimchamp added Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] and removed Needs: Response Issues which require feedback from lead labels Apr 22, 2026
@jimchamp jimchamp force-pushed the ocaid-association-handler branch from 2274ad7 to 05793a6 Compare April 22, 2026 20:57
@github-actions github-actions Bot removed the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Apr 22, 2026
Comment thread openlibrary/plugins/openlibrary/api.py
Comment thread openlibrary/plugins/openlibrary/api.py Outdated
@jimchamp jimchamp marked this pull request as ready for review April 22, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: 2 Important, as time permits. [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When Archive.org sets openlibrary_edition, set Open Library ocaid

4 participants