Commit 6985bacd authored by Viacheslav Sukhovieiev's avatar Viacheslav Sukhovieiev
Browse files

feat: add handler decorator logic

parent 0225f1f8
Pipeline #35667 passed with stages
in 1 minute and 46 seconds
......@@ -15,6 +15,7 @@
"""CORS support for aiohttp.
"""
import functools
from typing import Mapping, Union, Any
from aiohttp import web
......@@ -65,3 +66,39 @@ def setup(app: web.Application, *,
cors = CorsConfig(app, defaults=defaults)
app[APP_CONFIG_KEY] = cors
return cors
def get_base_config() -> dict:
"""
returns base CORS config
{
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
allow_methods="*",
)
}
"""
return {
"*": ResourceOptions(
allow_credentials=True,
expose_headers="*",
allow_headers="*",
allow_methods="*",
)
}
def custom_handler(config=None, defaults=False):
if not config and defaults is True:
config = get_base_config()
def wrapper(func):
@functools.wraps(func)
def handler(*args, **kwargs):
return func(*args, **kwargs)
handler.apply_cors = True
handler.cors_config = config
return handler
return wrapper
......@@ -261,3 +261,12 @@ class CorsConfig:
stacklevel=2)
return self._cors_impl.add(routing_entity, config)
def apply(self, app: web.Application):
for route in list(app.router.routes()):
if hasattr(route.handler, "apply_cors"):
if hasattr(route.handler, "cors_config"):
config = route.handler.cors_config
else:
config = None
self.add(route, config)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment