From bcc4a3e9d844e81267a6dadb2f64ecf1658bd451 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Fri, 18 Sep 2020 11:47:43 +0200 Subject: [PATCH] =?UTF-8?q?[routing]=C2=A0gives=20"keys"=20of=20the=20Acl?= =?UTF-8?q?=20row=20to=20the=20endpoint=20as=20kwargs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- halfapi/__init__.py | 2 +- halfapi/lib/routes.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/halfapi/__init__.py b/halfapi/__init__.py index 13cbbb7..cc15c38 100644 --- a/halfapi/__init__.py +++ b/halfapi/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = '0.1.0' +__version__ = '0.2.0' def version(): return f'HalfAPI version:{__version__}' diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index bd6de54..e53bdf4 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -2,6 +2,7 @@ from functools import wraps import importlib import sys +from typing import Callable, List, Tuple from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT, PRODUCTION, DOMAINS) @@ -33,13 +34,13 @@ def get_routes(domains=None): """ - def route_decorator(fct, acls_mod, acls): + def route_decorator(fct: Callable, acls_mod, acls: List[Tuple]): @wraps(fct) async def caller(req: Request, *args, **kwargs): - for acl_fct_name in acls: + for acl_fct_name, keys in acls: acl_fct = getattr(acls_mod, acl_fct_name) if acl_fct(req, *args, **kwargs): - return await fct(req, *args, **kwargs) + return await fct(req, *args, **{ **kwargs, **{'keys': keys} }) raise HTTPException(401) @@ -64,8 +65,8 @@ def get_routes(domains=None): router=router['name']) as routes: for route in routes.select(): fct_name = route.pop('fct_name') - acls = [ list(elt.values()).pop() - for elt in Acl(**route).select('acl_fct_name') ] + acls = [ (elt['acl_fct_name'], elt['keys']) + for elt in Acl(**route).select('acl_fct_name', 'keys') ] router_routes.append( Route(route['path'], @@ -81,4 +82,4 @@ def get_routes(domains=None): app_routes.append(Mount('/{name}'.format(**domain), routes=domain_routes)) - return app_routes + return app_routes