[routers] docstring is mandator in route methods. YAML is used for markup

This commit is contained in:
Maxime Alves LIRMM@home 2021-11-29 05:43:51 +01:00
parent 47d81c048f
commit c27ed3a966
7 changed files with 75 additions and 10 deletions

View File

@ -172,9 +172,9 @@ def gen_routes(m_router: ModuleType,
def gen_router_routes(m_router: ModuleType, path: List[str]) -> \ def gen_router_routes(m_router: ModuleType, path: List[str]) -> \
Iterator[Tuple[str, str, Coroutine, List]]: Iterator[Tuple[str, str, ModuleType, Coroutine, List]]:
""" """
Recursive generatore that parses a router (or a subrouter) Recursive generator that parses a router (or a subrouter)
and yields from gen_routes and yields from gen_routes
Parameters: Parameters:
@ -184,8 +184,10 @@ def gen_router_routes(m_router: ModuleType, path: List[str]) -> \
Yields: Yields:
(str, str, Coroutine, List): A tuple containing the path, verb, (str, str, ModuleType, Coroutine, List): A tuple containing the path, verb,
function and parameters of the route router module, function reference and parameters of the route.
Function and parameters are yielded from then gen_routes function,
that decorates the endpoint function.
""" """
for subpath, params in read_router(m_router).items(): for subpath, params in read_router(m_router).items():

View File

@ -122,10 +122,23 @@ def api_routes(m_dom: ModuleType) -> Tuple[Dict, Dict]:
return l_params return l_params
d_res = {} d_res = {}
for path, verb, _, _, params in gen_router_routes(m_dom, []): for path, verb, m_router, fct, params in gen_router_routes(m_dom, []):
if path not in d_res: try:
d_res[path] = {} if path not in d_res:
d_res[path][verb] = str_acl(params) d_res[path] = {}
d_res[path][verb] = {
'docs': yaml.load(fct.__doc__, Loader=yaml.FullLoader),
'acls': str_acl(params)
}
except Exception as exc:
logger.error("""Error in route generation
path:%s
verb:%s
router:%s
fct:%s
params:%s """, path, verb, m_router, fct, params)
raise exc
return d_res, d_acls return d_res, d_acls

View File

@ -8,16 +8,36 @@ ACLS = {
} }
def get(test): def get(test):
"""
description:
returns the path parameter
"""
return str(test) return str(test)
def post(test): def post(test):
"""
description:
returns the path parameter
"""
return str(test) return str(test)
def patch(test): def patch(test):
"""
description:
returns the path parameter
"""
return str(test) return str(test)
def put(test): def put(test):
"""
description:
returns the path parameter
"""
return str(test) return str(test)
def delete(test): def delete(test):
"""
description:
returns the path parameter
"""
return str(test) return str(test)

View File

@ -3,4 +3,8 @@ ACLS = {
'GET' : [{'acl':acl.public}] 'GET' : [{'acl':acl.public}]
} }
def get(): def get():
"""
description:
Not implemented
"""
raise NotImplementedError raise NotImplementedError

View File

@ -6,5 +6,9 @@ ACLS = {
} }
def get(halfapi): def get(halfapi):
"""
description:
returns the configuration of the domain
"""
logger.error('%s', halfapi) logger.error('%s', halfapi)
return halfapi['config'] return halfapi['config']

View File

@ -1,5 +1,5 @@
from starlette.routing import Route from starlette.routing import Route
from halfapi.lib.routes import gen_starlette_routes from halfapi.lib.routes import gen_starlette_routes, api_routes, gen_router_routes
def test_gen_starlette_routes(): def test_gen_starlette_routes():
from .dummy_domain import routers from .dummy_domain import routers
@ -7,3 +7,25 @@ def test_gen_starlette_routes():
'dummy_domain': routers }): 'dummy_domain': routers }):
assert isinstance(route, Route) assert isinstance(route, Route)
def test_api_routes():
from . import dummy_domain
d_res, d_acls = api_routes(dummy_domain)
assert isinstance(d_res, dict)
assert isinstance(d_acls, dict)
yielded = False
for path, verb, m_router, fct, params in gen_router_routes(dummy_domain, []):
if not yielded:
yielded = True
assert path in d_res
assert verb in d_res[path]
assert 'docs' in d_res[path][verb]
assert 'acls' in d_res[path][verb]
assert isinstance(d_res[path][verb]['docs'], dict)
assert isinstance(d_res[path][verb]['acls'], list)
assert len(d_res[path][verb]['acls']) == len(params)
assert yielded is True

View File

@ -41,4 +41,4 @@ def test_get_api_dummy_domain_routes(application_domain, routers):
assert 'abc/alphabet' in d_r assert 'abc/alphabet' in d_r
assert 'GET' in d_r['abc/alphabet'] assert 'GET' in d_r['abc/alphabet']
assert len(d_r['abc/alphabet']['GET']) > 0 assert len(d_r['abc/alphabet']['GET']) > 0
assert 'acl' in d_r['abc/alphabet']['GET'][0] assert 'acls' in d_r['abc/alphabet']['GET']