Skip to content

migrate import_preview_json to FastAPI#12745

Open
Sanket17052006 wants to merge 5 commits into
internetarchive:masterfrom
Sanket17052006:migrate-import-preview-json
Open

migrate import_preview_json to FastAPI#12745
Sanket17052006 wants to merge 5 commits into
internetarchive:masterfrom
Sanket17052006:migrate-import-preview-json

Conversation

@Sanket17052006
Copy link
Copy Markdown
Contributor

Closes #

This PR migrates import_preview_json from web.py to FastAPI.

Technical

  • Added openlibrary/fastapi/importapi.py with GET /import/preview and POST /import/preview endpoints
  • Marked legacy import_preview_json class in import_ui.py with @deprecated("migrated to fastapi")
  • Registered the new router in asgi_app.py

Testing

docker compose run --rm home python -m pytest openlibrary/tests/fastapi/test_importapi.py -v

Screenshot

Stakeholders

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

This PR migrates the import preview JSON handler toward FastAPI by adding a new router, registering it in the ASGI app, marking the legacy JSON handler deprecated, and adding endpoint tests.

Changes:

  • Adds FastAPI GET/POST handlers for import preview JSON behavior.
  • Registers the import API router in the main ASGI app.
  • Adds FastAPI tests for auth, save behavior, and invalid source handling.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
openlibrary/fastapi/importapi.py Adds FastAPI import preview endpoints and shared request handling.
openlibrary/asgi_app.py Includes the new import API router.
openlibrary/plugins/importapi/import_ui.py Marks the legacy JSON preview handler as deprecated.
openlibrary/tests/fastapi/test_importapi.py Adds tests for the new FastAPI import preview endpoints.
Comments suppressed due to low confidence (1)

openlibrary/fastapi/importapi.py:84

  • Registering POST on /import/preview intercepts the existing HTML form posts from openlibrary/templates/import_preview.html. Those posts currently render a preview or redirect after saving, but this FastAPI handler always returns JSON, so the import preview UI's submit/import flow will break; use the legacy JSON endpoint URL (such as the .json route) or keep the HTML route handled by the legacy page.
@router.post("/import/preview")

Comment thread openlibrary/fastapi/importapi.py Outdated
@github-project-automation github-project-automation Bot moved this to Waiting Review/Merge from Staff in Ray's Project May 18, 2026
Copy link
Copy Markdown
Collaborator

@RayBB RayBB left a comment

Choose a reason for hiding this comment

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

In general it looks like it's in the right direction.

Doesn't seem to be working. Please test the endpoints before asking for review.
http://localhost:18080/import/preview.json?provider=ia&identifier=roughcutmystery00gorm

We'll have to use site.get() instead of web.ctx.site

The tests could also be DRY'd up with fixtures for mocking current user, admin user, import request.

So please go head and fix this and ensure it actually works locally for both new endpoints. Give the examples you used to test too.

@github-project-automation github-project-automation Bot moved this from Waiting Review/Merge from Staff to Someone else is working on it in Ray's Project May 28, 2026
@Sanket17052006
Copy link
Copy Markdown
Contributor Author

Thanks for the review! I'll fix the issues and test the endpoint thoroughly before updating.

@Sanket17052006 Sanket17052006 force-pushed the migrate-import-preview-json branch from e97c8ff to d4c3260 Compare May 28, 2026 17:13
@Sanket17052006
Copy link
Copy Markdown
Contributor Author

Hey @RayBB , I've fixed the issues locally - these are the examples I used :

  • curl -b cookies.txt "http://localhost:18080/import/preview.json?provider=ia&identifier=roughcutmystery00gorm"
  • {"success": true, "edition": {"key": "/books/OL10M", "status": "matched"}, "work": {"key": "/works/OL1W", "status": "matched"}, "preview": true}

POST with save:

  • curl -b cookies.txt -X POST "http://localhost:18080/import/preview.json" -d "provider=ia&identifier=roughcutmystery00gorm&save=true"
  • {"success": true, "edition": {"key": "/books/OL10M", "status": "matched"}, "work": {"key": "/works/OL1W", "status": "matched"}}

No auth:

  • curl "http://localhost:18080/import/preview.json?provider=ia&identifier=roughcutmystery00gorm"
  • {"detail": "Authentication required"} (401)

@github-actions github-actions Bot added the Needs: Response Issues which require feedback from lead label May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs: Response Issues which require feedback from lead

Projects

Status: Someone else is working on it

Development

Successfully merging this pull request may close these issues.

3 participants