1- from typing import Any , Callable , Generic , List , Optional , Type
1+ from typing import Any , Callable , Generic , List , Optional , Type , Union
22
33from fastapi import APIRouter , HTTPException
44from fastapi .types import DecoratedCallable
55
6- from ._types import T
6+ from ._types import T , DEPENDENCIES
77from ._utils import pagination_factory , schema_factory
88
99NOT_FOUND = HTTPException (404 , "Item not found" )
@@ -23,12 +23,12 @@ def __init__(
2323 prefix : Optional [str ] = None ,
2424 tags : Optional [List [str ]] = None ,
2525 paginate : Optional [int ] = None ,
26- get_all_route : bool = True ,
27- get_one_route : bool = True ,
28- create_route : bool = True ,
29- update_route : bool = True ,
30- delete_one_route : bool = True ,
31- delete_all_route : bool = True ,
26+ get_all_route : Union [ bool , DEPENDENCIES ] = True ,
27+ get_one_route : Union [ bool , DEPENDENCIES ] = True ,
28+ create_route : Union [ bool , DEPENDENCIES ] = True ,
29+ update_route : Union [ bool , DEPENDENCIES ] = True ,
30+ delete_one_route : Union [ bool , DEPENDENCIES ] = True ,
31+ delete_all_route : Union [ bool , DEPENDENCIES ] = True ,
3232 ** kwargs : Any ,
3333 ) -> None :
3434
@@ -53,59 +53,75 @@ def __init__(
5353 super ().__init__ (prefix = prefix , tags = tags , ** kwargs )
5454
5555 if get_all_route :
56- super (). add_api_route (
56+ self . _add_api_route (
5757 "" ,
5858 self ._get_all (),
5959 methods = ["GET" ],
6060 response_model = Optional [List [self .schema ]], # type: ignore
6161 summary = "Get All" ,
62+ dependencies = get_all_route ,
6263 )
6364
6465 if create_route :
65- super (). add_api_route (
66+ self . _add_api_route (
6667 "" ,
6768 self ._create (),
6869 methods = ["POST" ],
6970 response_model = self .schema ,
7071 summary = "Create One" ,
72+ dependencies = create_route ,
7173 )
7274
7375 if delete_all_route :
74- super (). add_api_route (
76+ self . _add_api_route (
7577 "" ,
7678 self ._delete_all (),
7779 methods = ["DELETE" ],
7880 response_model = Optional [List [self .schema ]], # type: ignore
7981 summary = "Delete All" ,
82+ dependencies = delete_all_route ,
8083 )
8184
8285 if get_one_route :
83- super (). add_api_route (
86+ self . _add_api_route (
8487 "/{item_id}" ,
8588 self ._get_one (),
8689 methods = ["GET" ],
8790 response_model = self .schema ,
8891 summary = "Get One" ,
92+ dependencies = get_one_route ,
8993 )
9094
9195 if update_route :
92- super (). add_api_route (
96+ self . _add_api_route (
9397 "/{item_id}" ,
9498 self ._update (),
9599 methods = ["PUT" ],
96100 response_model = self .schema ,
97101 summary = "Update One" ,
102+ dependencies = update_route ,
98103 )
99104
100105 if delete_one_route :
101- super (). add_api_route (
106+ self . _add_api_route (
102107 "/{item_id}" ,
103108 self ._delete_one (),
104109 methods = ["DELETE" ],
105110 response_model = self .schema ,
106111 summary = "Delete One" ,
112+ dependencies = delete_one_route ,
107113 )
108114
115+ def _add_api_route (
116+ self ,
117+ path : str ,
118+ endpoint : Callable [..., Any ],
119+ dependencies : Union [bool , DEPENDENCIES ],
120+ ** kwargs : Any ,
121+ ) -> None :
122+ dependencies = [] if isinstance (dependencies , bool ) else dependencies
123+ super ().add_api_route (path , endpoint , dependencies = dependencies , ** kwargs )
124+
109125 def api_route (
110126 self , path : str , * args : Any , ** kwargs : Any
111127 ) -> Callable [[DecoratedCallable ], DecoratedCallable ]:
0 commit comments