feat: migrate list_view_json endpoint to FastAPI#12556
Conversation
Migrates the list_view_json endpoint from web.py to FastAPI as part of issue internetarchive#12487 (Migrate lists endpoints to FastAPI). Changes: - openlibrary/fastapi/lists.py: - Add _get_list_or_404() helper that calls get_list() and raises HTTP 404 if the list is missing or has been deleted (/type/delete) - Add list_view_json_user() -> GET /people/{username}/lists/{list_id}.json - Add list_view_json_public() -> GET /lists/{list_id}.json - Add list_view_json_series() -> GET /series/{list_id}.json - All three routes accept ?_raw=true to return the raw DB record - Remove unused stale list_view_json() stub - openlibrary/plugins/openlibrary/lists.py: - Mark legacy list_view_json class with @deprecated('migrated to fastapi') - Fix import ordering: move typing_extensions to third-party group - openlibrary/tests/fastapi/test_list_view_json.py (new file): - 6 tests covering: user list, 404, deleted list, public list, series, and the ?_raw=true query param passthrough
There was a problem hiding this comment.
Pull request overview
This PR migrates the list_view_json endpoint from the legacy web.py implementation to FastAPI, continuing the repo’s incremental lists-endpoint migration for issue #12487. It adds FastAPI routes for user, public, and series list JSON views while keeping the shared list-fetching logic in the existing lists module.
Changes:
- Added FastAPI
*.jsonroutes for user lists, public lists, and series lists, plus a shared_get_list_or_404()helper. - Reused the existing
get_list()helper fromopenlibrary.plugins.openlibrary.listsand marked the legacy web.py endpoint as deprecated. - Added a new FastAPI test file covering basic success, missing-list, deleted-list, series/public variants, and
_raw=true.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
openlibrary/fastapi/lists.py |
Adds the new FastAPI list-view routes and 404 helper logic. |
openlibrary/plugins/openlibrary/lists.py |
Reuses/refactors legacy list retrieval and deprecates the old web.py endpoint class. |
openlibrary/tests/fastapi/test_list_view_json.py |
Adds route-level tests for the migrated endpoint behavior. |
| return FAKE_LIST | ||
|
|
||
| monkeypatch.setattr("openlibrary.fastapi.lists.get_list", fake_get_list) | ||
| resp = client.get("/people/testuser/lists/OL123L.json?_raw=true") |
|
|
||
| def get_list(key: str, raw: bool = False) -> dict | None: | ||
| lst = web.ctx.site.get(key) | ||
| lst = site.get(key) |
| def _get_list_or_404(key: str, raw: bool) -> dict: | ||
| """Fetch a list by key and raise 404 if not found or deleted.""" | ||
| lst = get_list(key, raw=raw) | ||
| if not lst or (not raw and lst.get("type", {}).get("key") == "/type/delete"): |
|
Thanks for the PR, @shoaib-inamdar! Copilot has been assigned for an initial review. @RayBB is assigned to this PR and currently has:
The linked issue (#12487) has not yet been triaged — triage happens on Mondays and Fridays. Possible improvements for this PR
PR triage checklist (maintainers / Pam)
Note This comment was automatically generated by Pam, Open Library's Project AI Manager, on behalf of @mekarpeles. Pam is designed to provide status visibility, perform basic project management functions and relevant codebase research, and provide actionable feedback so contributors aren't left waiting. |
mypy [union-attr] (25 errors): - cast(List | None, site.get(key)) in get_list() to tell mypy the result is a List object (not str | Site); List is already imported from openlibrary.core.lists.model copilot suggestion internetarchive#2 (lists.py): - Already resolved: site.get(key) instead of web.ctx.site.get(key) copilot suggestion internetarchive#3 (fastapi/lists.py): - Use walrus operator in _get_list_or_404() to combine the get_list() call and None-check into one expression copilot suggestion internetarchive#1 (test file): - Capture and assert the key argument in test_list_view_json_raw_param to verify the correct list key is forwarded to get_list()
36efcd0 to
b6c786f
Compare
|
@Sanket17052006 would you be willing to review/test this one thoroughly in your local env? |
|
Yes, I would like to give it a shot |
There was a problem hiding this comment.
Hey @shoaib-inamdar, tested this locally — here's what I found.
master branch
curl "http://localhost:8080/people/openlibrary/lists/OL15L.json?_raw=true" returns 200 OK with correct JSON (tested with list OL15L - created locally for this review)
PR branch (8080)
curl "http://localhost:8080/people/openlibrary/lists/OL15L.json?_raw=true"
returns 500 AttributeError: 'Site' object has no attribute 'dict'
PR branch (port 18080)
curl "http://localhost:18080/people/openlibrary/lists/OL15L.json?_raw=true" returns Same AttributeError originating from fastapi/lists.py -> get_list
Root cause: The change from web.ctx.site.get(key) to site.get(key) in get_list is the problem as per my findings.
Also worth noting: once this is fixed, the ?_raw=true + deleted list case will need attention too. Happy to test after you input, and do point out if I missed out on something. :)
43e0f60 to
b919341
Compare
for more information, see https://pre-commit.ci
|
@Sanket17052006 , i have fixed the issue |
|
@shoaib-inamdar the changes look good, but are you able to hit port 18080 successfully with ?_raw=true in your local setup? I'm still getting a 404 for a list that exists on port 8080, so want to make sure I'm not missing something in my env. |
|
@shoaib-inamdar sorry there was a bit confusion on my part, I’ve re‑tested after the latest fixes
@RayBB LGTM, can you take a look? |
RayBB
left a comment
There was a problem hiding this comment.
@shoaib-inamdar and @Sanket17052006 thank you very much for submitting a high quality PR that was well tested.
I went ahead and pushed up some very small refactors to simplify things and ensure we support the /people/username/series case but overall this is very good.
Handles raw and 404 perfectly and has good validation.
Thanks for this!
@Sanket17052006 I hope you can rebase your PR off this one and follow similar patterns when it makes sense.
Migrates the list_view_json endpoint from web.py to FastAPI as part of issue #12487 (Migrate lists endpoints to FastAPI).
Changes:
openlibrary/fastapi/lists.py:
openlibrary/plugins/openlibrary/lists.py:
openlibrary/tests/fastapi/test_list_view_json.py (new file):
Closes #12487
Technical
Testing
Screenshot
Stakeholders
@RayBB