-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
migrate import_preview_json to FastAPI #12745
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
Open
Sanket17052006
wants to merge
5
commits into
internetarchive:master
Choose a base branch
from
Sanket17052006:migrate-import-preview-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+370
−1
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
76fd1df
migrate import_preview_json to FastAPI
Sanket17052006 03456ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba69cdd
preserve the legacy behaviour
Sanket17052006 d4c3260
Add web.ctx.site=site.get() and improve tests
Sanket17052006 3f0fd95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """ | ||
| FastAPI endpoints for import preview. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Depends, Form, HTTPException, Query, status | ||
|
|
||
| from openlibrary.accounts import get_current_user | ||
| from openlibrary.plugins.importapi.import_ui import ImportPreviewRequest | ||
|
|
||
| router = APIRouter(tags=["import"]) | ||
|
|
||
|
|
||
| def check_import_permission() -> None: | ||
| """Check that the current user has admin/librarian/super-librarian role.""" | ||
| user = get_current_user() | ||
| if user is None: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="Authentication required", | ||
| ) | ||
| if not (user.is_admin() or user.is_librarian() or user.is_super_librarian()): | ||
| raise HTTPException( | ||
| status_code=status.HTTP_403_FORBIDDEN, | ||
| detail="Insufficient permissions", | ||
| ) | ||
|
|
||
|
|
||
| def _build_preview_response( | ||
| source: str | None, | ||
| provider: str | None, | ||
| identifier: str | None, | ||
| save: bool, | ||
| ) -> dict: | ||
| """Shared logic for GET and POST import preview handlers.""" | ||
| try: | ||
| req = ImportPreviewRequest.from_input( | ||
| { | ||
| "source": source, | ||
| "provider": provider, | ||
| "identifier": identifier, | ||
| "save": "true" if save else "false", | ||
| } | ||
| ) | ||
| except ValueError as e: | ||
| return {"success": False, "error": str(e)} | ||
|
|
||
| return dict(req.metadata_provider.do_import(req.identifier, req.save)) | ||
|
|
||
|
|
||
| @router.get("/import/preview") | ||
| def import_preview_json_get( | ||
| source: Annotated[ | ||
| str | None, | ||
| Query( | ||
| description="Source in format 'provider:identifier' (e.g. 'amazon:ASIN')", | ||
| ), | ||
| ] = None, | ||
| provider: Annotated[ | ||
| str | None, | ||
| Query( | ||
| description="Metadata provider: amazon, ia, or marc", | ||
| ), | ||
| ] = None, | ||
| identifier: Annotated[ | ||
| str | None, | ||
| Query( | ||
| description="Identifier for the given provider", | ||
| ), | ||
| ] = None, | ||
| _: Annotated[None, Depends(check_import_permission)] = None, | ||
| ) -> dict: | ||
| """ | ||
| Preview import metadata without saving. | ||
|
|
||
| Requires admin, librarian, or super-librarian role. | ||
| """ | ||
| return _build_preview_response(source, provider, identifier, save=False) | ||
|
|
||
|
|
||
| @router.post("/import/preview") | ||
| def import_preview_json_post( | ||
| source: Annotated[str | None, Form()] = None, | ||
| provider: Annotated[str | None, Form()] = None, | ||
| identifier: Annotated[str | None, Form()] = None, | ||
| save: Annotated[bool, Form()] = False, | ||
| _: Annotated[None, Depends(check_import_permission)] = None, | ||
| ) -> dict: | ||
| """ | ||
| Import metadata, optionally saving it. | ||
|
|
||
| Requires admin, librarian, or super-librarian role. | ||
| """ | ||
| return _build_preview_response(source, provider, identifier, save=save) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.