3
3
import threading
4
4
import typing as t
5
5
6
- from flama import asgi , exceptions , http , injection , types , url , validation , websockets
6
+ from flama import asgi , exceptions , http , injection , routing , types , url , validation , websockets
7
7
from flama .ddd .components import WorkerComponent
8
8
from flama .events import Events
9
9
from flama .middleware import MiddlewareStack
10
10
from flama .models .modules import ModelsModule
11
11
from flama .modules import Modules
12
12
from flama .pagination import paginator
13
13
from flama .resources import ResourcesModule
14
- from flama .routing import BaseRoute , Router
15
14
from flama .schemas .modules import SchemaModule
16
15
17
16
try :
23
22
from flama .middleware import Middleware
24
23
from flama .modules import Module
25
24
from flama .pagination .types import PaginationType
26
- from flama .routing import Mount , Route , WebSocketRoute
27
25
28
26
__all__ = ["Flama" ]
29
27
33
31
class Flama :
34
32
def __init__ (
35
33
self ,
36
- routes : t .Optional [t .Sequence [t . Union [ " BaseRoute", "Mount" ] ]] = None ,
34
+ routes : t .Optional [t .Sequence ["routing. BaseRoute" ]] = None ,
37
35
components : t .Optional [t .Union [t .Sequence [injection .Component ], set [injection .Component ]]] = None ,
38
36
modules : t .Optional [t .Union [t .Sequence ["Module" ], set ["Module" ]]] = None ,
39
37
middleware : t .Optional [t .Sequence ["Middleware" ]] = None ,
@@ -75,8 +73,7 @@ def __init__(
75
73
"send" : types .Send ,
76
74
"exc" : Exception ,
77
75
"app" : Flama ,
78
- "path_params" : types .PathParams ,
79
- "route" : BaseRoute ,
76
+ "route" : routing .BaseRoute ,
80
77
"request" : http .Request ,
81
78
"response" : http .Response ,
82
79
"websocket" : websockets .WebSocket ,
@@ -102,7 +99,7 @@ def __init__(
102
99
self .modules = Modules (app = self , modules = {* default_modules , * (modules or [])})
103
100
104
101
# Initialize router
105
- self .app = self .router = Router (
102
+ self .app = self .router = routing . Router (
106
103
routes = routes , components = [* default_components , * (components or [])], lifespan = lifespan
107
104
)
108
105
@@ -144,11 +141,12 @@ async def __call__(self, scope: types.Scope, receive: types.Receive, send: types
144
141
:param receive: ASGI receive event.
145
142
:param send: ASGI send event.
146
143
"""
147
- if scope ["type" ] != "lifespan" and self .status in (types .AppStatus .NOT_STARTED , types .AppStatus .STARTING ):
148
- raise exceptions .ApplicationError ("Application is not ready to process requests yet." )
144
+ if scope ["type" ] != "lifespan" :
145
+ if self .status in (types .AppStatus .NOT_STARTED , types .AppStatus .STARTING ):
146
+ raise exceptions .ApplicationError ("Application is not ready to process requests yet." )
149
147
150
- if scope [ "type" ] != "lifespan" and self .status in (types .AppStatus .SHUT_DOWN , types .AppStatus .SHUTTING_DOWN ):
151
- raise exceptions .ApplicationError ("Application is already shut down." )
148
+ elif self .status in (types .AppStatus .SHUT_DOWN , types .AppStatus .SHUTTING_DOWN ):
149
+ raise exceptions .ApplicationError ("Application is already shut down." )
152
150
153
151
scope ["app" ] = self
154
152
scope .setdefault ("root_app" , self )
@@ -182,7 +180,7 @@ def add_component(self, component: injection.Component):
182
180
self .router .build (self )
183
181
184
182
@property
185
- def routes (self ) -> list ["BaseRoute" ]:
183
+ def routes (self ) -> list ["routing. BaseRoute" ]:
186
184
"""List of registered routes.
187
185
188
186
:return: Routes.
@@ -196,10 +194,10 @@ def add_route(
196
194
methods : t .Optional [list [str ]] = None ,
197
195
name : t .Optional [str ] = None ,
198
196
include_in_schema : bool = True ,
199
- route : t .Optional ["Route" ] = None ,
197
+ route : t .Optional ["routing. Route" ] = None ,
200
198
pagination : t .Optional [t .Union [str , "PaginationType" ]] = None ,
201
199
tags : t .Optional [dict [str , t .Any ]] = None ,
202
- ) -> "Route" :
200
+ ) -> "routing. Route" :
203
201
"""Register a new HTTP route or endpoint under given path.
204
202
205
203
:param path: URL path.
@@ -257,10 +255,10 @@ def add_websocket_route(
257
255
path : t .Optional [str ] = None ,
258
256
endpoint : t .Optional [types .WebSocketHandler ] = None ,
259
257
name : t .Optional [str ] = None ,
260
- route : t .Optional ["WebSocketRoute" ] = None ,
258
+ route : t .Optional ["routing. WebSocketRoute" ] = None ,
261
259
pagination : t .Optional [t .Union [str , "PaginationType" ]] = None ,
262
260
tags : t .Optional [dict [str , t .Any ]] = None ,
263
- ) -> "WebSocketRoute" :
261
+ ) -> "routing. WebSocketRoute" :
264
262
"""Register a new websocket route or endpoint under given path.
265
263
266
264
:param path: URL path.
@@ -296,9 +294,9 @@ def mount(
296
294
path : t .Optional [str ] = None ,
297
295
app : t .Optional [types .App ] = None ,
298
296
name : t .Optional [str ] = None ,
299
- mount : t .Optional ["Mount" ] = None ,
297
+ mount : t .Optional ["routing. Mount" ] = None ,
300
298
tags : t .Optional [dict [str , t .Any ]] = None ,
301
- ) -> "Mount" :
299
+ ) -> "routing. Mount" :
302
300
"""Register a new mount point containing an ASGI app in this router under given path.
303
301
304
302
:param path: URL path.
@@ -366,7 +364,7 @@ def resolve_url(self, name: str, **path_params: t.Any) -> url.URL:
366
364
"""
367
365
return self .router .resolve_url (name , ** path_params )
368
366
369
- def resolve_route (self , scope : types .Scope ) -> tuple [BaseRoute , types .Scope ]:
367
+ def resolve_route (self , scope : types .Scope ) -> tuple [routing . BaseRoute , types .Scope ]:
370
368
"""Look for a route that matches given ASGI scope.
371
369
372
370
:param scope: ASGI scope.
0 commit comments