a c'@sddlZddlZddlmZmZddlmZddlmZddl m Z ddl m Z ddl mZddlmZdd lmZmZdd lmZmZmZmZGd d d ZdS) N)StateURLPath) Middleware)BaseHTTPMiddleware)ServerErrorMiddleware)ExceptionMiddleware)Request)Response) BaseRouteRouter)ASGIAppReceiveScopeSendc @sReZdZdZd7eejejeejeje ejej ej ej e egejeejefffejejej ejejej ejej dgejfddddZeddd Zeejedd d Zeej ed d dZeeeddddZeej dddZ d8eeejeddddZ!d9eeejeddddZ"e#ej ddddZ$eje%ej&efej dddd Z'eej dd!d"d#Z(d:eej ejejeejeedd%d&d'Z)d;eej ejedd(d)d*Z*eje%ej&efej d+d,d-Z+d Starletteah Creates an application instance. **Parameters:** * **debug** - Boolean indicating if debug tracebacks should be returned on errors. * **routes** - A list of routes to serve incoming HTTP and WebSocket requests. * **middleware** - A list of middleware to run for every request. A starlette application will always automatically include two middleware classes. `ServerErrorMiddleware` is added as the very outermost middleware, to handle any uncaught errors occurring anywhere in the entire stack. `ExceptionMiddleware` is added as the very innermost middleware, to deal with handled exception cases occurring in the routing or endpoints. * **exception_handlers** - A mapping of either integer status codes, or exception class types onto callables which handle the exceptions. Exception handler callables should be of the form `handler(request, exc) -> response` and may be be either standard functions, or async functions. * **on_startup** - A list of callables to run on application startup. Startup handler callables do not take any arguments, and may be be either standard functions, or async functions. * **on_shutdown** - A list of callables to run on application shutdown. Shutdown handler callables do not take any arguments, and may be be either standard functions, or async functions. FN)debugroutes middlewareexception_handlers on_startup on_shutdownlifespanreturncCsv|dus |dur|dus Jd||_t|_t||||d|_|durLint||_|durbgnt||_d|_ dS)Nz>Use either 'lifespan' or 'on_startup'/'on_shutdown', not both.)rrr) rrstater routerdictrlistuser_middlewaremiddleware_stack)selfrrrrrrrr c/workspaces/shunt/resources/test-fastapi/venv/lib/python3.9/site-packages/starlette/applications.py__init__*s zStarlette.__init__)rc Cs|j}d}i}|jD]"\}}|dtfvr2|}q|||<qtt||dg|jtt||dg}|j}t |D]\}} |fd|i| }qp|S)Ni)handlerr)handlersrapp) rritems Exceptionrrrrrreversed) rrZ error_handlerrkeyvaluerr%clsoptionsr r r!build_middleware_stackOs*   z Starlette.build_middleware_stackcCs|jjSN)rrrr r r!rkszStarlette.routes)name path_paramsrcKs|jj|fi|Sr.)r url_path_for)rr0r1r r r!r2oszStarlette.url_path_for)scopereceivesendrcs4||d<|jdur||_||||IdHdS)Nr%)rr-)rr3r4r5r r r!__call__rs  zStarlette.__call__) event_typercCs |j|Sr.)ron_event)rr7r r r!r8xszStarlette.on_event)pathr%r0rcCs|jj|||ddSN)r%r0)rmount)rr9r%r0r r r!r;{szStarlette.mount)hostr%r0rcCs|jj|||ddSr:)rr<)rr<r%r0r r r!r<szStarlette.host)middleware_classr,rcKs0|jdurtd|jdt|fi|dS)Nz6Cannot add middleware after an application has startedr)r RuntimeErrorrinsertr)rr=r,r r r!add_middlewares zStarlette.add_middleware)exc_class_or_status_coder#rcCs||j|<dSr.)r)rrAr#r r r!add_exception_handlerszStarlette.add_exception_handler)r7funcrcCs|j||dSr.)radd_event_handler)rr7rCr r r!rDszStarlette.add_event_handlerT)r9routemethodsr0include_in_schemarcCs|jj|||||ddSN)rFr0rGr add_route)rr9rErFr0rGr r r!rJs zStarlette.add_route)r9rEr0rcCs|jj|||ddSN)r0radd_websocket_route)rr9rEr0r r r!rMszStarlette.add_websocket_route)rArcs*tdttjtjdfdd }|S)NzThe `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/exceptions/ for the recommended approach.rCrcs||Sr.)rBrCrArr r! decorators z.Starlette.exception_handler..decoratorwarningswarnDeprecationWarningtypingCallable)rrArQr rPr!exception_handlers zStarlette.exception_handler)r9rFr0rGrcs0tdttjtjdfdd }|S)z We no longer document this decorator style API, and its usage is discouraged. Instead you should use the following approach: >>> routes = [Route(path, endpoint=...), ...] >>> app = Starlette(routes=routes) zThe `route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/routing/ for the recommended approach.rNcsjj|d|SrHrIrOrGrFr0r9rr r!rQsz"Starlette.route..decoratorrR)rr9rFr0rGrQr rYr!rEs  zStarlette.route)r9r0rcs,tdttjtjdfdd }|S)a We no longer document this decorator style API, and its usage is discouraged. Instead you should use the following approach: >>> routes = [WebSocketRoute(path, endpoint=...), ...] >>> app = Starlette(routes=routes) zThe `websocket_route` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/routing/#websocket-routing for the recommended approach.rNcsjj|d|SrKrLrOr0r9rr r!rQsz,Starlette.websocket_route..decoratorrR)rr9r0rQr rZr!websocket_routes zStarlette.websocket_route)middleware_typercs8tdt|dksJdtjtjdfdd }|S)z We no longer document this decorator style API, and its usage is discouraged. Instead you should use the following approach: >>> middleware = [Middleware(...), ...] >>> app = Starlette(middleware=middleware) zThe `middleware` decorator is deprecated, and will be removed in version 1.0.0. Refer to https://www.starlette.io/middleware/#using-middleware for recommended approach.httpz/Currently only middleware("http") is supported.rNcsjt|d|S)N)dispatch)r@rrOr/r r!rQsz'Starlette.middleware..decoratorrR)rr\rQr r/r!rszStarlette.middleware)FNNNNNN)N)N)NNT)N)NNT)N)/__name__ __module__ __qualname____doc__boolrVOptionalSequencer rMappingAnyrWrr'Unionr AwaitableAsyncContextManagerr"r r-propertyListrstrrr2rr rr6r8r;r<typer@intTyperBrDrJrMrXrEr[rr r r r!rs   %          !  r)rVrSZstarlette.datastructuresrrZstarlette.middlewarerZstarlette.middleware.baserZstarlette.middleware.errorsrZstarlette.middleware.exceptionsrZstarlette.requestsrZstarlette.responsesr Zstarlette.routingr r Zstarlette.typesr r rrrr r r r!s