Debian Codegraph @

aiohttp_apispec/middlewares.py

raw symbols SCIP
1 from aiohttp import web
2
3 from .utils import issubclass_py37fix
4
5
6 @web.middleware
7 async def validation_middleware(request: web.Request, handler) -> web.Response:
8 """
9 Validation middleware for aiohttp web app
10
11 Usage:
12
13 .. code-block:: python
14
15 app.middlewares.append(validation_middleware)
16
17
18 """
19 orig_handler = request.match_info.handler
20 if not hasattr(orig_handler, "__schemas__"):
21 if not issubclass_py37fix(orig_handler, web.View):
22 return await handler(request)
23 sub_handler = getattr(orig_handler, request.method.lower(), None)
24 if sub_handler is None:
25 return await handler(request)
26 if not hasattr(sub_handler, "__schemas__"):
27 return await handler(request)
28 schemas = sub_handler.__schemas__
29 else:
30 schemas = orig_handler.__schemas__
31 result = {}
32 for schema in schemas:
33 data = await request.app["_apispec_parser"].parse(
34 schema["schema"],
35 request,
36 location=schema["location"],
37 unknown=None, # Pass None to use the schema’s setting instead.
38 )
39 if schema["put_into"]:
40 request[schema["put_into"]] = data
41 elif data:
42 try:
43 result.update(data)
44 except (ValueError, TypeError):
45 result = data
46 break
47 request[request.app["_apispec_request_data_name"]] = result
48 return await handler(request)