Refactor API Endpoints into Independent Routers
In previous issues (#716 and #723), we successfully separated the routers into individual modules to improve organization and maintainability. To further enhance our code structure, we propose refactoring the API endpoints that share the same URL pattern into independent routers.
Proposed Changes
For an instance, we suggest moving the following API endpoints to a dedicated router (e.g., routers/ask module under the routers package):
- POST
/asks - PATCH
/asks/{query_id} - GET
/asks/{query_id}/result
Additionally, we will implement request and response classes to encapsulate the data for these endpoints. For example:
class PostRequest(BaseModel):
...
class PostResponse(BaseModel):
...
@router.post(
"/asks",
response_model=PostResponse,
)
async def ask(
request: PostRequest,
background_tasks: BackgroundTasks,
service_container: ServiceContainer = Depends(get_service_container),
service_metadata: ServiceMetadata = Depends(get_service_metadata),
) -> PostResponse:
...
return PostResponse(id=id)
We will also add a module-level docstring to explain how to use the endpoints, similar to the example found as https://github.com/Canner/WrenAI/blob/42d2e7413bd026cc5793e3cf47073f09cd59510a/wren-ai-service/src/web/v1/routers/semantics_description.py#L18-L69
Benefits
- Improved Organization: Grouping related endpoints into a single router enhances the clarity and maintainability of our codebase.
- Easier Navigation: Developers will find it easier to locate and manage related endpoints, leading to a more efficient development process.
- Scalability: This refactor will lay the groundwork for future enhancements and additions to the API.
Next Steps
- Create new router files for the
/asks,/sql-answers,/sql-expansions,/ask-details, and other related endpoints. - Move the specified endpoints to the new routers.
- Update the main router in
routersto include the new independent routers. - Test the endpoints to ensure they function correctly after the refactor.
Hi, can I work on this issue?
@Spirizeon sure, I've assigned you to this issue!
Hi again! I have created a new router file for the /asks with changes. Will implement similar changes for other sets of endpoints soon.
@Spirizeon Good to hear the update! Thanks for the work! when the PR is ready to review, you can ping me in reviewers.
Thanks for the support, I've implemented all the necessary changes but I want to test the endpoints before marking ready for review. Any documentation I can refer to for that?
Thanks for the support, I've implemented all the necessary changes but I want to test the endpoints before marking ready for review. Any documentation I can refer to for that?
@Spirizeon Thanks for working on this issue again! I think following https://github.com/Canner/WrenAI/tree/main/wren-ai-service#start-the-service-for-development to start the services and you can try the endpoints on Wren UI or Swagger UI directly.
By the way, I think we could add some endpoint-level tests in the future. For now, directly testing them to ensure they work is also a good approach!