Skip to content

Migrate booknotes endpoint to FastAPI#12007

Merged
RayBB merged 10 commits intointernetarchive:masterfrom
Armansiddiqui9:migrate-to-fastapi
Mar 23, 2026
Merged

Migrate booknotes endpoint to FastAPI#12007
RayBB merged 10 commits intointernetarchive:masterfrom
Armansiddiqui9:migrate-to-fastapi

Conversation

@Armansiddiqui9
Copy link
Copy Markdown
Contributor

Closes #11999

Migrates class booknotes(delegate.page) from openlibrary/plugins/openlibrary/api.py to FastAPI.

Technical

  • Added POST /works/OL{work_id}W/notes in openlibrary/fastapi/internal/api.py
  • Uses require_authenticated_user dependency→returns 401 instead of legacy browser redirect
  • Dropped redir param (not applicable for a JSON API)
  • Added BooknoteResponse Pydantic model
  • DB failures return 502 Bad Gateway

Testing

  • Use this command docker compose exec web pytest openlibrary/tests/fastapi/test_booknotes.py -v
  • 10/10 passed

Screenshot

Stakeholders

@RayBB

@github-project-automation github-project-automation bot moved this to Waiting Review/Merge from Staff in Ray's Project Mar 4, 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.

Overall, this is a great start. There's a few minor things to fix, and please fix them and then request another review, and please be sure to test thoroughly, but overall I think it looks about right!



class BooknoteResponse(BaseModel):
"""Response model for booknote POST endpoint."""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We don't need this comment.

Comment on lines +133 to +137
except Exception: # noqa: BLE001
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail="Error communicating with the database",
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We shouldn't need this try except it should be handled automatically by FastAPI.

- If `notes` is provided: create or update the note.
- If `notes` is omitted: remove the existing note.
"""
from openlibrary.core.models import Booknotes
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This import needs to be at the top of the file.

include_in_schema=SHOW_INTERNAL_IN_SCHEMA,
)
async def booknotes_post(
work_id: str,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be an integer.

"""
from openlibrary.core.models import Booknotes

numeric_work_id = int(work_id)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You don't need to cast it as an int if you make it required as an integer above.

@Armansiddiqui9
Copy link
Copy Markdown
Contributor Author

@RayBB
Resolved all requested changes:

  • Removed docstring from BooknoteResponse
  • Moved `booknotes_post to replace the original stub position to avoid merge conflicts
  • Removed try/except block as fastAPI handles unhandled exceptions automatically
  • Moved from openlibrary.core.models import Booknotes to top of file
  • Changed work_id from str to int as fastAPI validates and converts automatically
  • Removed numeric_work_id = int(work_id) cast as not needed
  • Removed test_db_error_returns_502 test not applicable without try/except

@Armansiddiqui9 Armansiddiqui9 requested a review from RayBB March 5, 2026 08:47
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.

Sorry for the long delay in reviewing here. You did excellent work. It was a little tricky to test but ultimately I used http://localhost:18080/docs#/internal/booknotes_post_works_OL_work_id_W_notes_post to test several cases and then added them to the test file.

Really appreciate your work and patience on this one!

@RayBB RayBB merged commit f397fe1 into internetarchive:master Mar 23, 2026
3 checks passed
@github-project-automation github-project-automation bot moved this from Waiting Review/Merge from Staff to Done in Ray's Project Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Migrate internal endpoints to FastAPI

2 participants