WrenAI icon indicating copy to clipboard operation
WrenAI copied to clipboard

Refactor API Endpoints into Independent Routers

Open paopa opened this issue 1 year ago • 5 comments

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

  1. Create new router files for the /asks, /sql-answers, /sql-expansions, /ask-details, and other related endpoints.
  2. Move the specified endpoints to the new routers.
  3. Update the main router in routers to include the new independent routers.
  4. Test the endpoints to ensure they function correctly after the refactor.

paopa avatar Oct 08 '24 08:10 paopa

Hi, can I work on this issue?

Spirizeon avatar Oct 08 '24 16:10 Spirizeon

@Spirizeon sure, I've assigned you to this issue!

cyyeh avatar Oct 08 '24 16:10 cyyeh

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 avatar Oct 10 '24 16:10 Spirizeon

@Spirizeon Good to hear the update! Thanks for the work! when the PR is ready to review, you can ping me in reviewers.

paopa avatar Oct 11 '24 07:10 paopa

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 avatar Oct 12 '24 19:10 Spirizeon

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!

paopa avatar Oct 14 '24 15:10 paopa