diff --git a/halfapi/app.py b/halfapi/app.py index 13f77eb..f05c197 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -17,7 +17,7 @@ from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.responses import * from halfapi.lib.routes import gen_starlette_routes -from halfapi.lib.schemas import schema_json +from halfapi.lib.schemas import get_api_routes, schema_json """ @@ -25,19 +25,17 @@ Base routes definition Only debug or doc routes, that should not be available in production """ -routes = [ - Route('/', lambda request, *args, **kwargs: ORJSONResponse('It Works!')), +routes = [ Route('/', get_api_routes) ] - Route('/user', lambda request, *args, **kwargs: - ORJSONResponse({'user':request.user.json}) - if type(request.user) != UnauthenticatedUser - else ORJSONResponse({'user':False})), - Route('/payload', lambda request, *args, **kwargs: - ORJSONResponse({'payload':str(request.payload)})), - - Route('/schema', schema_json) -] if not PRODUCTION else [] +if not PRODUCTION: + routes += [ + Route('/halfapi/current_user', lambda request, *args, **kwargs: + ORJSONResponse({'user':request.user.json}) + if type(request.user) != UnauthenticatedUser + else ORJSONResponse({'user': None})), + Route('/halfapi/schema', schema_json) + ] for domain, m_domain in DOMAINSDICT.items(): for route in gen_starlette_routes(m_domain): diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index 526c758..3e2ddd5 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -49,6 +49,8 @@ def get_fct_name(http_verb, path: str): return '_'.join(fct_name) def gen_routes(route_params, path, m_router): + fqtn = route_params.get('FQTN') + for verb in VERBS: params = route_params.get(verb) if params is None: @@ -67,13 +69,14 @@ def gen_routes(route_params, path, m_router): 'verb':verb, 'path':f"/{'/'.join([ elt for elt in path if elt ])}", 'params':params, - 'fct': fct } + 'fct': fct, + 'fqtn': fqtn } def gen_router_routes(m_router, path=[]): """ [ - ('path', [acl], fct) + ('path', [acl], fct, fqtn) ] """ diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index 5807c38..6450462 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -91,3 +91,33 @@ def gen_starlette_routes(m_dom): ), methods=[route['verb']]) ) + + +def api_routes(m_dom): + """ + Yields the description objects for HalfAPI app routes + + Parameters: + m_dom (module): the halfapi module + + Returns: + Generator[Dict] + """ + + m_dom_acl = importlib.import_module('.acl', m_dom.__name__) + + def pop_acl(r): + if 'acl' in r.keys(): + r.pop('acl') + print(r) + return r + + return { + route['path']: { + 'params': list(map(pop_acl, route['params'])), + 'verb': route['verb'], + 'fqtn': route['fqtn'] + } + for route in gen_domain_routes(m_dom.__name__) + } + \ No newline at end of file diff --git a/halfapi/lib/schemas.py b/halfapi/lib/schemas.py index 626adca..6fd658c 100644 --- a/halfapi/lib/schemas.py +++ b/halfapi/lib/schemas.py @@ -1,4 +1,5 @@ -from .routes import gen_starlette_routes +from ..conf import DOMAINSDICT +from .routes import gen_starlette_routes, api_routes from .responses import * from starlette.schemas import SchemaGenerator from starlette.routing import Router @@ -6,6 +7,11 @@ schemas = SchemaGenerator( {"openapi": "3.0.0", "info": {"title": "HalfAPI", "version": "1.0"}} ) +async def get_api_routes(request, *args, **kwargs): + return ORJSONResponse({ + domain: api_routes(m_domain) + for domain, m_domain in DOMAINSDICT.items() + }) async def schema_json(request, *args, **kwargs): return ORJSONResponse( diff --git a/pytest.ini b/pytest.ini index 0400395..25c7847 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,4 @@ [pytest] testpaths = tests halfapi -addopts = --doctest-modules -rP --log-level debug +addopts = --doctest-modules doctest_optionflags = ELLIPSIS diff --git a/tests/test_debug_routes.py b/tests/test_debug_routes.py index 267fba0..c51f124 100644 --- a/tests/test_debug_routes.py +++ b/tests/test_debug_routes.py @@ -5,17 +5,14 @@ from starlette.testclient import TestClient from halfapi.app import application import json -def test_itworks(): +def test_get_api_routes(): c = TestClient(application) - r = json.loads(c.get('/').text) - assert r == 'It Works!' + r = c.get('/') + d_r = r.json() + assert isinstance(d_r, dict) -def test_user(): - c = TestClient(application) - r = c.get('/user') - assert r.status_code == 200 -def test_user(): +def test_current_user(): c = TestClient(application) - r = c.get('/schema') + r = c.get('/halfapi/current_user') assert r.status_code == 200