2727 ApiResource ,
2828 ApiScopes ,
2929 ApiSecurityDefinition ,
30- ApiSecurityDefinitionJwt , ResourceDeclareRequest
30+ ApiSecurityDefinitionJwt ,
31+ ResourceDeclareRequest ,
3132)
3233from grpclib import GRPCError
3334from nitric .api .exception import exception_from_grpc_error
@@ -55,11 +56,11 @@ class ApiOptions:
5556 security : Union [dict [str , List [str ]], None ]
5657
5758 def __init__ (
58- self ,
59- path : str = "" ,
60- middleware : List [Middleware ] = None ,
61- security_definitions : dict [str , SecurityDefinition ] = None ,
62- security : dict [str , List [str ]] = None ,
59+ self ,
60+ path : str = "" ,
61+ middleware : List [Middleware ] = None ,
62+ security_definitions : dict [str , SecurityDefinition ] = None ,
63+ security : dict [str , List [str ]] = None ,
6364 ):
6465 """Construct a new API options object."""
6566 self .middleware = middleware
@@ -82,23 +83,21 @@ def _to_resource(b: Api) -> Resource:
8283 return Resource (name = b .name , type = ResourceType .Api )
8384
8485
85- def security_definition_to_grpc_declaration (security_definitions : dict [str , SecurityDefinition ]) -> Union [
86- dict [str , ApiSecurityDefinition ], None ]:
86+ def _security_definition_to_grpc_declaration (
87+ security_definitions : dict [str , SecurityDefinition ]
88+ ) -> Union [dict [str , ApiSecurityDefinition ], None ]:
8789 if security_definitions is None or len (security_definitions ) == 0 :
8890 return None
8991 return {
90- k : ApiSecurityDefinition (
91- jwt = ApiSecurityDefinitionJwt (issuer = v .issuer , audiences = v .audiences )
92- ) for k , v in security_definitions .items ()
92+ k : ApiSecurityDefinition (jwt = ApiSecurityDefinitionJwt (issuer = v .issuer , audiences = v .audiences ))
93+ for k , v in security_definitions .items ()
9394 }
9495
9596
96- def security_to_grpc_declaration (security : dict [str , List [str ]]) -> dict [str , ApiScopes ]:
97+ def _security_to_grpc_declaration (security : dict [str , List [str ]]) -> dict [str , ApiScopes ] | None :
9798 if security is None or len (security ) == 0 :
9899 return None
99- return {
100- k : ApiScopes (v ) for k , v in security .items ()
101- }
100+ return {k : ApiScopes (v ) for k , v in security .items ()}
102101
103102
104103class Api (BaseResource ):
@@ -131,9 +130,9 @@ async def _register(self):
131130 resource_declare_request = ResourceDeclareRequest (
132131 resource = _to_resource (self ),
133132 api = ApiResource (
134- security_definitions = security_definition_to_grpc_declaration (self .security_definitions ),
135- security = security_to_grpc_declaration (self .security )
136- )
133+ security_definitions = _security_definition_to_grpc_declaration (self .security_definitions ),
134+ security = _security_to_grpc_declaration (self .security ),
135+ ),
137136 )
138137 )
139138 except GRPCError as grpc_err :
@@ -155,8 +154,18 @@ def all(self, match: str, opts: MethodOptions = None):
155154
156155 def decorator (function : HttpMiddleware ):
157156 r = self ._route (match )
158- r .method ([HttpMethod .GET , HttpMethod .POST , HttpMethod .PATCH , HttpMethod .PUT , HttpMethod .DELETE ,
159- HttpMethod .OPTIONS ], function , opts )
157+ r .method (
158+ [
159+ HttpMethod .GET ,
160+ HttpMethod .POST ,
161+ HttpMethod .PATCH ,
162+ HttpMethod .PUT ,
163+ HttpMethod .DELETE ,
164+ HttpMethod .OPTIONS ,
165+ ],
166+ function ,
167+ opts = opts ,
168+ )
160169
161170 return decorator
162171
@@ -167,7 +176,7 @@ def methods(self, methods: List[HttpMethod], match: str, opts: MethodOptions = N
167176
168177 def decorator (function : HttpMiddleware ):
169178 r = self ._route (match )
170- r .method (methods , function , opts )
179+ r .method (methods , function , opts = opts )
171180
172181 return decorator
173182
@@ -289,7 +298,7 @@ class Method:
289298 opts : MethodOptions
290299
291300 def __init__ (
292- self , route : Route , methods : List [HttpMethod ], * middleware : HttpMiddleware , opts : MethodOptions = None
301+ self , route : Route , methods : List [HttpMethod ], * middleware : HttpMiddleware , opts : MethodOptions = None
293302 ):
294303 """Construct a method handler for the specified route."""
295304 self .route = route
@@ -302,6 +311,6 @@ def start(self):
302311 Nitric ._register_worker (self .server )
303312
304313
305- def api (name : str ) -> Api :
306- """Create a new API resource"""
307- return Nitric ._create_resource (Api , name )
314+ def api (name : str , opts : ApiOptions = None ) -> Api :
315+ """Create a new API resource. """
316+ return Nitric ._create_resource (Api , name , opts = opts )
0 commit comments