Skip to content
This repository was archived by the owner on Nov 10, 2024. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Made the class method names into resource methods
  • Loading branch information
yuval9313 committed Jul 4, 2020
commit 8671815ecc0df63c1a869aa33a307eaee2a47158
12 changes: 8 additions & 4 deletions fastapi_utils/cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
CBV_CLASS_KEY = "__cbv_class__"


def cbv(router: APIRouter) -> Callable[[Type[T]], Type[T]]:
def cbv(router: APIRouter, base_path: str) -> Callable[[Type[T]], Type[T]]:
"""
This function returns a decorator that converts the decorated into a class-based view for the provided router.

Expand All @@ -21,21 +21,25 @@ def cbv(router: APIRouter) -> Callable[[Type[T]], Type[T]]:
For more detail, review the documentation at
https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/#the-cbv-decorator
"""

def decorator(cls: Type[T]) -> Type[T]:
return _cbv(router, cls)
return _cbv(router, cls, base_path)

return decorator


def _cbv(router: APIRouter, cls: Type[T]) -> Type[T]:
def _cbv(router: APIRouter, cls: Type[T], base_path: str) -> Type[T]:
"""
Replaces any methods of the provided class `cls` that are endpoints of routes in `router` with updated
function calls that will properly inject an instance of `cls`.
"""
_init_cbv(cls)
cbv_router = APIRouter()
function_members = inspect.getmembers(cls, inspect.isfunction)
for name, func in function_members:
if hasattr(router, name) and not name.startswith('__'):
response_model = func.__annotations__['return']
api_resource = router.api_route(base_path, methods=[name.capitalize()], response_model=response_model)
api_resource(func)
functions_set = set(func for _, func in function_members)
cbv_routes = [
route
Expand Down