halfapi/halfapi/lib/routes.py

203 lines
5.4 KiB
Python
Raw Normal View History

[release][0.1.0] First HalfAPI release Squashed commit of the following: commit 68032dc55a07565ccd17a188407d9ac2537b62e6 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:40:26 2020 +0200 [release][0.1.0] First HalfAPI release commit a046a81114a3ae22bbc84a53f1e85217a0954dbc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:26:15 2020 +0200 [doc] màj du readme commit ed45b3011125c071aa53df8087d28bfa1150b373 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 11:03:17 2020 +0200 [wip] rm apidb commit 7df4b9bacf3d26f09ea07856587505f74284cab4 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:58:46 2020 +0200 [db] forgot foreign key router->domain commit 604b9a90f405121725e4b2126d73a5a83eec398f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:51:01 2020 +0200 [wip] routes mounting fixed commit c50a5572633d7dcc3cad48ef79c5ea1ca098284d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:29:09 2020 +0200 [wip][db][nf] http_verb, fct_name are in api.route commit 2b5b78db2f9c280dd5eb9d8bafc9174d9afc092f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 17:37:21 2020 +0200 [wip] refactor du 22 juillet 2222 commit d019b7e333ab37f106895c84521cef1bfe768caa Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 14:48:51 2020 +0200 [wip] remove "version" from app commit 98ccd61dcf369b8c4aac817a0c8409b6fed00f50 Author: Joël Maïzi <joel.maizi@lirmm.fr> Date: Wed Jul 22 14:45:28 2020 +0200 Remove api.version from database. commit aa0d4f8dbba8b8ec878835bb58b69facff7e675e Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:46:22 2020 +0200 [db] added router as api.acl primary_key part commit a97984e9de0e6a00bddca7dece0c715c9c16cbe1 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:41:31 2020 +0200 [wip] moved all acl treatment to acl_caller_middleware, fix route mounting, fix typo in logs commit 3dd310e80aaf6cb32f6c4ac23c1e2a924cebfde1 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 13:10:26 2020 +0200 [wip][nf] gestion des routes avec routers commit c2687c4a24126fbc3e57257bf23c267b334df522 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:39:53 2020 +0200 [db] ajout de la table api.router commit 9a10f76cf7790f75f23b72e19b0a58978752565c Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:19:53 2020 +0200 [db] renommage des champs d'acl commit c4e8c26a24835559d2e9b251df0eb462fe7e667d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:13:38 2020 +0200 [wip][nf] modification du systeme de montage des routes commit b7e8352ba1e427e9883797a44bb0f3da5edd576d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 11:15:30 2020 +0200 [wip][dbupdate] insertion des routes au nouveau format commit 28947444c4c062e6ced74f9bfdef11a36ddc438b Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 10:32:57 2020 +0200 [wip][dbupdate] Suppression des routes hors domaine
2020-07-23 17:54:47 +02:00
#!/usr/bin/env python3
"""
Routes module
Fonctions :
- route_acl_decorator
- gen_starlette_routes
- api_routes
- api_acls
- debug_routes
Exception :
- DomainNotFoundError
"""
from datetime import datetime
from functools import partial, wraps
2020-11-04 13:32:33 +01:00
import logging
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
from typing import Callable, List, Dict, Generator, Tuple
from types import ModuleType, FunctionType
[release][0.1.0] First HalfAPI release Squashed commit of the following: commit 68032dc55a07565ccd17a188407d9ac2537b62e6 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:40:26 2020 +0200 [release][0.1.0] First HalfAPI release commit a046a81114a3ae22bbc84a53f1e85217a0954dbc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:26:15 2020 +0200 [doc] màj du readme commit ed45b3011125c071aa53df8087d28bfa1150b373 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 11:03:17 2020 +0200 [wip] rm apidb commit 7df4b9bacf3d26f09ea07856587505f74284cab4 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:58:46 2020 +0200 [db] forgot foreign key router->domain commit 604b9a90f405121725e4b2126d73a5a83eec398f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:51:01 2020 +0200 [wip] routes mounting fixed commit c50a5572633d7dcc3cad48ef79c5ea1ca098284d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:29:09 2020 +0200 [wip][db][nf] http_verb, fct_name are in api.route commit 2b5b78db2f9c280dd5eb9d8bafc9174d9afc092f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 17:37:21 2020 +0200 [wip] refactor du 22 juillet 2222 commit d019b7e333ab37f106895c84521cef1bfe768caa Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 14:48:51 2020 +0200 [wip] remove "version" from app commit 98ccd61dcf369b8c4aac817a0c8409b6fed00f50 Author: Joël Maïzi <joel.maizi@lirmm.fr> Date: Wed Jul 22 14:45:28 2020 +0200 Remove api.version from database. commit aa0d4f8dbba8b8ec878835bb58b69facff7e675e Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:46:22 2020 +0200 [db] added router as api.acl primary_key part commit a97984e9de0e6a00bddca7dece0c715c9c16cbe1 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:41:31 2020 +0200 [wip] moved all acl treatment to acl_caller_middleware, fix route mounting, fix typo in logs commit 3dd310e80aaf6cb32f6c4ac23c1e2a924cebfde1 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 13:10:26 2020 +0200 [wip][nf] gestion des routes avec routers commit c2687c4a24126fbc3e57257bf23c267b334df522 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:39:53 2020 +0200 [db] ajout de la table api.router commit 9a10f76cf7790f75f23b72e19b0a58978752565c Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:19:53 2020 +0200 [db] renommage des champs d'acl commit c4e8c26a24835559d2e9b251df0eb462fe7e667d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:13:38 2020 +0200 [wip][nf] modification du systeme de montage des routes commit b7e8352ba1e427e9883797a44bb0f3da5edd576d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 11:15:30 2020 +0200 [wip][dbupdate] insertion des routes au nouveau format commit 28947444c4c062e6ced74f9bfdef11a36ddc438b Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 10:32:57 2020 +0200 [wip][dbupdate] Suppression des routes hors domaine
2020-07-23 17:54:47 +02:00
from starlette.exceptions import HTTPException
from starlette.routing import Route
[release][0.1.0] First HalfAPI release Squashed commit of the following: commit 68032dc55a07565ccd17a188407d9ac2537b62e6 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:40:26 2020 +0200 [release][0.1.0] First HalfAPI release commit a046a81114a3ae22bbc84a53f1e85217a0954dbc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:26:15 2020 +0200 [doc] màj du readme commit ed45b3011125c071aa53df8087d28bfa1150b373 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 11:03:17 2020 +0200 [wip] rm apidb commit 7df4b9bacf3d26f09ea07856587505f74284cab4 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:58:46 2020 +0200 [db] forgot foreign key router->domain commit 604b9a90f405121725e4b2126d73a5a83eec398f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:51:01 2020 +0200 [wip] routes mounting fixed commit c50a5572633d7dcc3cad48ef79c5ea1ca098284d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:29:09 2020 +0200 [wip][db][nf] http_verb, fct_name are in api.route commit 2b5b78db2f9c280dd5eb9d8bafc9174d9afc092f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 17:37:21 2020 +0200 [wip] refactor du 22 juillet 2222 commit d019b7e333ab37f106895c84521cef1bfe768caa Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 14:48:51 2020 +0200 [wip] remove "version" from app commit 98ccd61dcf369b8c4aac817a0c8409b6fed00f50 Author: Joël Maïzi <joel.maizi@lirmm.fr> Date: Wed Jul 22 14:45:28 2020 +0200 Remove api.version from database. commit aa0d4f8dbba8b8ec878835bb58b69facff7e675e Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:46:22 2020 +0200 [db] added router as api.acl primary_key part commit a97984e9de0e6a00bddca7dece0c715c9c16cbe1 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:41:31 2020 +0200 [wip] moved all acl treatment to acl_caller_middleware, fix route mounting, fix typo in logs commit 3dd310e80aaf6cb32f6c4ac23c1e2a924cebfde1 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 13:10:26 2020 +0200 [wip][nf] gestion des routes avec routers commit c2687c4a24126fbc3e57257bf23c267b334df522 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:39:53 2020 +0200 [db] ajout de la table api.router commit 9a10f76cf7790f75f23b72e19b0a58978752565c Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:19:53 2020 +0200 [db] renommage des champs d'acl commit c4e8c26a24835559d2e9b251df0eb462fe7e667d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:13:38 2020 +0200 [wip][nf] modification du systeme de montage des routes commit b7e8352ba1e427e9883797a44bb0f3da5edd576d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 11:15:30 2020 +0200 [wip][dbupdate] insertion des routes au nouveau format commit 28947444c4c062e6ced74f9bfdef11a36ddc438b Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 10:32:57 2020 +0200 [wip][dbupdate] Suppression des routes hors domaine
2020-07-23 17:54:47 +02:00
from starlette.requests import Request
from starlette.responses import Response, PlainTextResponse
[release][0.1.0] First HalfAPI release Squashed commit of the following: commit 68032dc55a07565ccd17a188407d9ac2537b62e6 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:40:26 2020 +0200 [release][0.1.0] First HalfAPI release commit a046a81114a3ae22bbc84a53f1e85217a0954dbc Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 15:26:15 2020 +0200 [doc] màj du readme commit ed45b3011125c071aa53df8087d28bfa1150b373 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 11:03:17 2020 +0200 [wip] rm apidb commit 7df4b9bacf3d26f09ea07856587505f74284cab4 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:58:46 2020 +0200 [db] forgot foreign key router->domain commit 604b9a90f405121725e4b2126d73a5a83eec398f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:51:01 2020 +0200 [wip] routes mounting fixed commit c50a5572633d7dcc3cad48ef79c5ea1ca098284d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Thu Jul 23 10:29:09 2020 +0200 [wip][db][nf] http_verb, fct_name are in api.route commit 2b5b78db2f9c280dd5eb9d8bafc9174d9afc092f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 17:37:21 2020 +0200 [wip] refactor du 22 juillet 2222 commit d019b7e333ab37f106895c84521cef1bfe768caa Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jul 22 14:48:51 2020 +0200 [wip] remove "version" from app commit 98ccd61dcf369b8c4aac817a0c8409b6fed00f50 Author: Joël Maïzi <joel.maizi@lirmm.fr> Date: Wed Jul 22 14:45:28 2020 +0200 Remove api.version from database. commit aa0d4f8dbba8b8ec878835bb58b69facff7e675e Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:46:22 2020 +0200 [db] added router as api.acl primary_key part commit a97984e9de0e6a00bddca7dece0c715c9c16cbe1 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jul 21 21:41:31 2020 +0200 [wip] moved all acl treatment to acl_caller_middleware, fix route mounting, fix typo in logs commit 3dd310e80aaf6cb32f6c4ac23c1e2a924cebfde1 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 13:10:26 2020 +0200 [wip][nf] gestion des routes avec routers commit c2687c4a24126fbc3e57257bf23c267b334df522 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:39:53 2020 +0200 [db] ajout de la table api.router commit 9a10f76cf7790f75f23b72e19b0a58978752565c Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:19:53 2020 +0200 [db] renommage des champs d'acl commit c4e8c26a24835559d2e9b251df0eb462fe7e667d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 12:13:38 2020 +0200 [wip][nf] modification du systeme de montage des routes commit b7e8352ba1e427e9883797a44bb0f3da5edd576d Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 11:15:30 2020 +0200 [wip][dbupdate] insertion des routes au nouveau format commit 28947444c4c062e6ced74f9bfdef11a36ddc438b Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jul 21 10:32:57 2020 +0200 [wip][dbupdate] Suppression des routes hors domaine
2020-07-23 17:54:47 +02:00
from halfapi.lib.domain import gen_router_routes, VERBS, domain_acls
from ..conf import DOMAINSDICT
2020-11-04 13:32:33 +01:00
logger = logging.getLogger('uvicorn.asgi')
class DomainNotFoundError(Exception):
""" Exception when a domain is not importable
"""
def route_acl_decorator(fct: Callable = None, params: List[Dict] = []):
"""
Decorator for async functions that calls pre-conditions functions
and appends kwargs to the target function
Parameters:
fct (Callable):
The function to decorate
params List[Dict]:
A list of dicts that have an "acl" key that points to a function
Returns:
async function
"""
if not fct:
return partial(route_acl_decorator, params=params)
@wraps(fct)
async def caller(req: Request, *args, **kwargs):
for param in params:
if param.get('acl'):
passed = param['acl'](req, *args, **kwargs)
if isinstance(passed, FunctionType):
passed = param['acl']()(req, *args, **kwargs)
if not passed:
2020-11-04 13:32:33 +01:00
logger.debug(
'ACL FAIL for current route (%s - %s)', fct, param.get('acl'))
continue
logger.debug(
'ACL OK for current route (%s - %s)', fct, param.get('acl'))
2020-11-04 13:32:33 +01:00
req.scope['acl_pass'] = param['acl'].__name__
if 'args' in param:
req.scope['args'] = param['args']
if 'check' in req.query_params:
return PlainTextResponse(param['acl'].__name__)
return await fct(
req, *args,
**{
**kwargs,
**param
})
if 'check' in req.query_params:
return PlainTextResponse('')
raise HTTPException(401)
return caller
def gen_starlette_routes(d_domains: Dict[str, ModuleType]) -> Generator:
"""
Yields the Route objects for HalfAPI app
Parameters:
d_domains (dict[str, ModuleType])
Returns:
Generator(Route)
"""
for domain_name, m_domain in d_domains.items():
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
for path, verb, fct, params in gen_router_routes(m_domain, []):
yield (
Route(f'/{domain_name}/{path}',
route_acl_decorator(
fct,
params
),
methods=[verb])
)
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
def api_routes(m_dom: ModuleType) -> Tuple[Dict, Dict]:
"""
Yields the description objects for HalfAPI app routes
Parameters:
m_dom (ModuleType): the halfapi module
Returns:
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
(Dict, Dict)
"""
2020-09-30 08:07:55 +02:00
d_acls = {}
def str_acl(params):
2020-09-29 15:08:35 +02:00
l_params = []
2020-09-30 08:07:55 +02:00
for param in params:
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
2020-10-07 15:50:50 +02:00
if 'acl' not in param.keys() or not param['acl']:
continue
2020-09-30 08:07:55 +02:00
l_params.append(param.copy())
l_params[-1]['acl'] = param['acl'].__name__
if param['acl'] not in d_acls.keys():
d_acls[param['acl'].__name__] = param['acl']
2020-09-29 15:08:35 +02:00
return l_params
d_res = {}
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
for path, verb, fct, params in gen_router_routes(m_dom, []):
if path not in d_res:
d_res[path] = {}
d_res[path][verb] = str_acl(params)
2020-09-30 08:07:55 +02:00
return d_res, d_acls
def api_acls(request):
""" Returns the list of possible ACLs
"""
2020-09-30 08:07:55 +02:00
res = {}
domains = DOMAINSDICT()
doc = 'doc' in request.query_params
for domain, m_domain in domains.items():
2020-09-30 08:07:55 +02:00
res[domain] = {}
for acl_name, fct in domain_acls(m_domain, [domain]):
if not isinstance(fct, FunctionType):
continue
fct_result = fct.__doc__.strip() if doc and fct.__doc__ else fct(request)
if acl_name in res[domain]:
continue
if isinstance(fct_result, FunctionType):
fct_result = fct()(request)
res[domain][acl_name] = fct_result
2020-09-30 08:07:55 +02:00
return res
def debug_routes():
""" Halfapi debug routes definition
"""
async def debug_log(request: Request, *args, **kwargs):
logger.debug('debuglog# %s', {datetime.now().isoformat()})
logger.info('debuglog# %s', {datetime.now().isoformat()})
logger.warning('debuglog# %s', {datetime.now().isoformat()})
logger.error('debuglog# %s', {datetime.now().isoformat()})
logger.critical('debuglog# %s', {datetime.now().isoformat()})
return Response('')
yield Route('/halfapi/log', debug_log)
async def error_code(request: Request, *args, **kwargs):
[0.5.3] Squashed commit of the following: commit ac935db6d62656713183707707d083298c1f34b0 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:49 2021 +0200 [tests] remove dummy-domain from dependencies commit 4d50363c9b1502d1d8b7cbafc207e80cdbe247a4 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:52:18 2021 +0200 [tests] update tests for 0.5.3 commit 6181592692464d21de9807e1e890b4ac92efc387 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:17:51 2021 +0200 [lib.*] Refactor libs commit ed7485a8a16b60dde8acc0d2b9afca00a75fce3c Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Thu Jun 17 18:15:10 2021 +0200 [app] Use HalfAPI class to be able to use custom configuration à commit fa1ca6bf9df4ea17d7fa7dfdf3694c56746e9c7f Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Wed Jun 16 15:34:25 2021 +0200 [wip] tests dummy_domain commit 86e8dd3465e0bd0f3d49f28fd9e05a52874e969a Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 18:12:13 2021 +0200 [0.5.3] ajout de la config actuelle dans les arguments des routes commit aa7ec62c7a3b5a0ae0dc0cc79bd509eece44d5ff Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 11:16:23 2021 +0200 [lib.jwtMw] verify signature even if halfapi is in DEBUG mode commit e208728d7ec61b0de583c66c348f73ac7a884108 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 10:49:46 2021 +0200 [lib.acl] args_check doesn't check required/optional arguments if "args" is not specified in request, if the target function is not async commit aa4c309778e4c969fe1314da305e02112d4641b7 Author: Maxime Alves LIRMM <maxime.alves@lirmm.fr> Date: Tue Jun 15 09:45:37 2021 +0200 [lib.domain] SUBROUTER can be a path parameter if including ":" commit 138420461d5c1ba0c7379dcda64e9a38aa5d768d Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:32 2021 +0200 [gitignore] *.swp commit 0c1e2849bad591d62f7399d820a044cf4e06de12 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Tue Jun 15 07:24:14 2021 +0200 [tests] test get route with dummy projects commit 7227e2d7f105516b67f3acac27b242fe01b59215 Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 17:18:47 2021 +0200 [lib.domain] handle modules without ROUTES attribute commit 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Author: Maxime Alves LIRMM@home <maxime.alves@lirmm.fr> Date: Mon Jun 14 16:34:58 2021 +0200 [tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
2021-06-17 18:53:23 +02:00
code = request.path_params['code']
raise HTTPException(code)
yield Route('/halfapi/error/{code:int}', error_code)
async def exception(request: Request, *args, **kwargs):
raise Exception('Test exception')
yield Route('/halfapi/exception', exception)