Skip to content

Migrate browse endpoint to FastAPI#12006

Merged
RayBB merged 9 commits intointernetarchive:masterfrom
Sanket17052006:migrate-browse-fastapi
Mar 23, 2026
Merged

Migrate browse endpoint to FastAPI#12006
RayBB merged 9 commits intointernetarchive:masterfrom
Sanket17052006:migrate-browse-fastapi

Conversation

@Sanket17052006
Copy link
Copy Markdown
Contributor

@Sanket17052006 Sanket17052006 commented Mar 4, 2026

Related to #11999
This PR migrates the browse endpoint from web.py to FastAPI.

Changes Made :-

  • openlibrary/fastapi/internal/api.py - Added the FastAPI browse endpoint.
  • openlibrary/plugins/openlibrary/api.py - Deprecated the web.py endpoint.

Testing

Test command -

  • ./tests/integration/temporary/test_browse_migration.sh http://localhost:8080 http://localhost:18080

Stakeholders

@RayBB

@Sanket17052006 Sanket17052006 force-pushed the migrate-browse-fastapi branch 2 times, most recently from 65bf9dd to 8b17920 Compare March 4, 2026 07:35
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.

It's a good start, but you need to follow the best practices per the comment I left.

@RayBB
Copy link
Copy Markdown
Collaborator

RayBB commented Mar 4, 2026 via email

@RayBB RayBB self-assigned this Mar 5, 2026
@github-project-automation github-project-automation bot moved this to Waiting Review/Merge from Staff in Ray's Project Mar 5, 2026
@Sanket17052006
Copy link
Copy Markdown
Contributor Author

Sanket17052006 commented Mar 6, 2026

Hey @RayBB , I think the parse_comma_separated_list function is not defined in the models.py and also while taking a look at Parth's PR #12000 I saw that he defined the parse_comma_separated_list function locally and discussed about a existing identical function in fastapi/search.py and potential move. So for now, I kept conversion of the comma separated string to list[str] as it is.
I have made all the other requested changes.

@github-actions github-actions bot added the Needs: Response Issues which require feedback from lead label Mar 7, 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.

Sorry for the delay in feedback. It's a good start!
I understand this one is tricky because it can't be tested locally.
In this case, I think it's best to just return the exact same thing and not try to form it into a special response object.

response_model_exclude_none=False,
)
async def browse(request: Annotated[BrowseRequest, Depends()], pagination: Annotated[Pagination, Depends()]) -> BrowseResponse:
"""Browse endpoint (migrated from openlibrary.plugins.openlibrary.api)."""
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.

Please add a short description of what it does

if not url:
works = []
else:
works = await asyncio.to_thread(lending.get_available, url=url)
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 want to put this on a thread. Generally we should avoid doing that. In this case it doesn't work on testing and it's hard to see the errors when it's on a thread.

Comment on lines +78 to +92
if isinstance(works, list):
for work in works:
if isinstance(work, str) and (work.lower() == "error" or not work):
continue

if isinstance(work, str):
processed_works.append(WorkModel(key=work))
elif hasattr(work, "dict"):
processed_works.append(WorkModel(**work.dict()))
elif isinstance(work, dict):
if work.get("key") == "error":
continue
processed_works.append(WorkModel(**work))
elif hasattr(work, "key"):
processed_works.append(WorkModel(key=work.key))
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.

Why do we need to do this? I don't see anything similar in the old endpoint.

@Sanket17052006 Sanket17052006 force-pushed the migrate-browse-fastapi branch from 9e50d64 to 47aa052 Compare March 15, 2026 12:17
@Sanket17052006
Copy link
Copy Markdown
Contributor Author

Thanks @RayBB for the review! I have removed the threading and those messy if-else checks. Do the changes look good?
I am up for any more changes you think are required.

@RayBB RayBB self-requested a review March 23, 2026 18:18
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.

Great job on this on. I think it's exactly what we need.

I put it on testing and tested with this url and quite a few others and it works perfectly.

http://testing.openlibrary.org/browse.json?subject=openlibrary_staff_picks&sorts=lending___last_browse%20desc&limit=18

@RayBB RayBB merged commit ba219c4 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
@Sanket17052006
Copy link
Copy Markdown
Contributor Author

Thanks @RayBB , for those detailed reviews and looking forward to any suitable tasks you have for me. :)

@RayBB
Copy link
Copy Markdown
Collaborator

RayBB commented Mar 24, 2026

@Sanket17052006 would you like to migrate the public_observations endpoint as well now?

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: Done

Development

Successfully merging this pull request may close these issues.

2 participants