Skip to content
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

chore: refactor in Squeeze API endpoints #1145

Open
kazeemj565 opened this issue Feb 28, 2025 · 0 comments
Open

chore: refactor in Squeeze API endpoints #1145

kazeemj565 opened this issue Feb 28, 2025 · 0 comments

Comments

@kazeemj565
Copy link

Name: Bug report  
About: Create a report to help us improve  
Title: "chore: refactor in Squeeze API endpoints"  
Labels: "modification, enhancement

# Describe the modification
The Squeeze API endpoints defined in the `squeeze` router have several issues and areas for improvement:

1. Incorrect `response_model` Usage:  
   The endpoints use `response_model=success_response`, but `success_response` is a function (or utility) that returns a response dictionary rather than a Pydantic model. This might lead to unexpected behavior in automatic API documentation and response validation.

2. Query Parameter Dependency for Filters:
   The `filter` parameter in the GET endpoints (`get_all_squeeze` and `get_squeeze`) is defined as `filter: FilterSqueeze = None`. If `FilterSqueeze` is a Pydantic model intended to parse query parameters, it should be provided as a dependency (e.g., using `Depends()`) so that FastAPI can correctly extract and validate query parameters.

3. Inconsistent Error Handling:
   In `create_squeeze`, if the user is not found, the endpoint returns a success response with a 404 status code rather than raising an HTTPException. This may not integrate well with global error handlers.

4. Wildcard Imports: 
   The code imports models using `from api.v1.models import *`. This can lead to namespace pollution and makes it less clear which models are in use. Explicit imports are preferred for maintainability and clarity.



# To Reproduce 
Steps to reproduce the behavior:
1. Call the POST endpoint to create a squeeze page with an email that does not exist in the database and observe that it returns a success response with a 404 status code instead of raising an HTTPException.
2. Examine the generated API documentation and notice that the `response_model` does not reflect a proper Pydantic model schema.
3. Use the GET endpoints with query parameters for filtering and observe that the `filter` parameter may not be correctly parsed if sent as query parameters.



# Expected behavior
- The endpoints should specify a proper Pydantic response model in the `response_model` parameter.
- The `filter` parameter should be injected as a dependency using `Depends()` so that query parameters are correctly parsed and validated.
- Error cases (such as a missing user in `create_squeeze`) should raise HTTPException to allow for consistent error handling.
- Imports should be explicit to improve clarity and maintainability.



# Proposed Fixes

1. Fix `response_model` Usage  
Instead of using `success_response` as the `response_model`, define a Pydantic schema (for example, `SuccessResponseSchema`) that represents the success response structure. Then update the endpoint decorators accordingly.

Before:
```python
@squeeze.post("", response_model=success_response, status_code=201)
def create_squeeze(...):
    ...
    return success_response(status.HTTP_201_CREATED, SUCCESS, new_squeeze.to_dict())

After:

from api.v1.schemas.responses import SuccessResponseSchema

@squeeze.post("", response_model=SuccessResponseSchema, status_code=201)
def create_squeeze(...):
    ...
    return SuccessResponseSchema(
        status=status.HTTP_201_CREATED,
        message=SUCCESS,
        data=new_squeeze.to_dict()
    )
  1. Fix Query Parameter Dependency for Filters
    If FilterSqueeze is a Pydantic model, inject it via Depends() to ensure proper validation.

Before:

@squeeze.get("", response_model=success_response, status_code=200)
def get_all_squeeze(filter: FilterSqueeze = None, ...):
    squeeze_pages = squeeze_service.fetch_all(db, filter)
    ...

After:

@squeeze.get("", response_model=SuccessResponseSchema, status_code=200)
def get_all_squeeze(filter: FilterSqueeze = Depends(), ...):
    squeeze_pages = squeeze_service.fetch_all(db, filter)
    ...
  1. Improve Error Handling in create_squeeze
    Instead of returning a success response for a 404 error, raise an HTTPException.

Before:

user = user_service.fetch_by_email(db, data.email)
if not user:
    return success_response(status.HTTP_404_NOT_FOUND, "User not found!")

After:

user = user_service.fetch_by_email(db, data.email)
if not user:
    raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found!")
  1. Avoid Wildcard Imports
    Replace the wildcard import with explicit model imports.

Before:

from api.v1.models import 

After:

from api.v1.models.user import User
from api.v1.models.squeeze import Squeeze  # Example, adjust as needed.

Additional context

Addressing these issues will improve response validation, API documentation clarity, and error handling consistency. This will ultimately lead to a more maintainable and robust API implementation for squeeze page management.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant