Skip to content

Commit

Permalink
possibility for custom endpoints and selecting the crud endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Jan 28, 2024
1 parent 524bf47 commit eefc3a0
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 81 deletions.
79 changes: 75 additions & 4 deletions fastcrud/endpoint/crud_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@ def crud_router(
delete_schema: Optional[Type[DeleteSchemaType]] = None,
path: str = "",
tags: Optional[List[str]] = None,
include_in_schema: bool = True,
create_deps: List[Callable] = [],
read_deps: List[Callable] = [],
read_multi_deps: List[Callable] = [],
update_deps: List[Callable] = [],
delete_deps: List[Callable] = [],
db_delete_deps: List[Callable] = [],
included_methods: Optional[list[str]] = None,
deleted_methods: Optional[list[str]] = None,
endpoint_creator: Optional[Type[EndpointCreator]] = None,
) -> APIRouter:
"""
Creates and configures a FastAPI router with CRUD endpoints for a given model.
This utility function streamlines the process of setting up a router for CRUD operations,
using a custom `EndpointCreator` if provided, and managing dependency injections as well
as selective method inclusions or exclusions.
Args:
session: The SQLAlchemy async session.
model: The SQLAlchemy model.
Expand All @@ -39,15 +47,22 @@ def crud_router(
delete_schema: Optional Pydantic schema for deleting an item.
path: Base path for the CRUD endpoints.
tags: Optional list of tags for grouping endpoints in the documentation.
include_in_schema: Whether to include the created endpoints in the OpenAPI schema.
create_deps: Optional list of dependency injection functions for the create endpoint.
read_deps: Optional list of dependency injection functions for the read endpoint.
read_multi_deps: Optional list of dependency injection functions for the read multiple items endpoint.
update_deps: Optional list of dependency injection functions for the update endpoint.
delete_deps: Optional list of dependency injection functions for the delete endpoint.
db_delete_deps: Optional list of dependency injection functions for the hard delete endpoint.
included_methods: Optional list of CRUD methods to include. If None, all methods are included.
deleted_methods: Optional list of CRUD methods to exclude.
endpoint_creator: Optional custom class derived from EndpointCreator for advanced customization.
Returns:
An APIRouter object with configured CRUD endpoints.
Configured APIRouter instance with the CRUD endpoints.
Raises:
ValueError: If both 'included_methods' and 'deleted_methods' are provided.
Examples:
Basic Setup:
Expand Down Expand Up @@ -131,25 +146,81 @@ def get_current_user(token: str = Depends(oauth2_scheme)):
tags=["CRM"]
)
```
With Selective CRUD Methods:
```python
# Only include 'create' and 'read' methods
router = crud_router(
session=async_session,
model=MyModel,
crud=CRUDMyModel(MyModel),
create_schema=CreateMyModel,
update_schema=UpdateMyModel,
included_methods=["create", "read"],
path="/mymodel",
tags=["MyModel"]
)
```
Using a Custom EndpointCreator:
```python
class CustomEndpointCreator(EndpointCreator):
def _custom_route(self):
async def custom_endpoint():
# Custom endpoint logic
return {"message": "Custom route"}
return custom_endpoint
async def add_routes_to_router(self, ...):
# First, add standard CRUD routes
super().add_routes_to_router(...)
# Now, add custom routes
self.router.add_api_route(
path="/custom",
endpoint=self._custom_route(),
methods=["GET"],
tags=self.tags,
# Other parameters as needed
)
router = crud_router(
session=async_session,
model=MyModel,
crud=CRUDMyModel(MyModel),
create_schema=CreateMyModel,
update_schema=UpdateMyModel,
endpoint_creator=CustomEndpointCreator,
path="/mymodel",
tags=["MyModel"]
)
app.include_router(my_router)
```
"""
endpoint_creator = EndpointCreator(
endpoint_creator_class = endpoint_creator or EndpointCreator
endpoint_creator_instance = endpoint_creator_class(
session=session,
model=model,
crud=crud,
create_schema=create_schema,
update_schema=update_schema,
include_in_schema=include_in_schema,
delete_schema=delete_schema,
path=path,
tags=tags,
)

endpoint_creator.add_routes_to_router(
endpoint_creator_instance.add_routes_to_router(
create_deps=create_deps,
read_deps=read_deps,
read_multi_deps=read_multi_deps,
update_deps=update_deps,
delete_deps=delete_deps,
db_delete_deps=db_delete_deps,
included_methods=included_methods,
deleted_methods=deleted_methods,
)

return endpoint_creator.router
return endpoint_creator_instance.router
Loading

0 comments on commit eefc3a0

Please sign in to comment.