From d5f39a7929285bbf1f8a12fd7f7029701c1c1b1d Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Wed, 8 Dec 2021 16:42:12 +0100 Subject: [PATCH] [schemas] always give a list of schemas --- CHANGELOG.md | 8 +++++ halfapi/half_domain.py | 72 +----------------------------------------- halfapi/halfapi.py | 27 ++++++++-------- 3 files changed, 22 insertions(+), 85 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ac252..a86ff12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # HalfAPI +## 0.6.2 + +- Domains now need to include the following variables in their __init__.py + - __name__ (str, optional) + - __id__ (str, optional) +- halfapi domain + + ## 0.1.0 - Mounts domain routers with their ACLs as decorator diff --git a/halfapi/half_domain.py b/halfapi/half_domain.py index 8860b6e..9ac314c 100644 --- a/halfapi/half_domain.py +++ b/halfapi/half_domain.py @@ -42,22 +42,6 @@ class HalfDomain(Starlette): self.config = config - """ - if domain: - m_domain = importlib.import_module(domain) - if not router: - router = getattr('__router__', domain, '.routers') - m_domain_router = importlib.import_module(router, domain) - m_domain_acl = importlib.import_module(f'{domain}.acl') - - if not(m_domain and m_domain_router and m_domain_acl): - raise Exception('Cannot import domain') - - self.schema = domain_schema(m_domain) - - routes = [ Route('/', JSONRoute(self.schema)) ] - """ - logger.info('HalfDomain creation %s %s', domain, config) super().__init__( routes=self.gen_domain_routes(), @@ -141,30 +125,6 @@ class HalfDomain(Starlette): else: return acl.args_check(fct), params - # @staticmethod - # def gen_routes(m_router): - #  """ - #  Yields the Route objects for a domain - - #  Parameters: - #  m_domains: ModuleType - - #  Returns: - #  Generator(HalfRoute) - #  """ - #  """ - #  yield HalfRoute('/', - #  JSONRoute(self.schema(self.m_domain)), - #  [{'acl': acl.public}], - #  'GET' - #  ) - #  """ - - #  for path, method, _, fct, params \ - #  in HalfDomain.gen_router_routes(m_router, []): - - #  yield HalfRoute(f'/{path}', fct, params, method) - @staticmethod def gen_router_routes(m_router, path: List[str]) -> \ @@ -292,7 +252,7 @@ class HalfDomain(Starlette): Generator(HalfRoute) """ yield HalfRoute('/', - JSONRoute(self.schema()), + JSONRoute([ self.schema() ]), [{'acl': acl.public}], 'GET' ) @@ -346,33 +306,3 @@ class HalfDomain(Starlette): } schema['paths'] = self.schema_dict() return schema - - - # def domain_schema_list(m_router: ModuleType) -> List: - # """ Schema as list, one row by route/acl - # Parameters: - # - # m_router (ModuleType): The domain routers' module - # - # Returns: - # - # List[Tuple]: (path, verb, callable, doc, acls) - # """ - # res = [] - # - # for path, verb, m_router, fct, parameters in gen_router_routes(m_router, []): - # for params in parameters: - # res.append(( - # path, - # verb, - # f'{m_router.__name__}:{fct.__name__}', - # params.get('acl').__name__, - # params.get('args', {}).get('required', []), - # params.get('args', {}).get('optional', []), - # params.get('out', []) - # )) - # - # return res - - - diff --git a/halfapi/halfapi.py b/halfapi/halfapi.py index b116914..db28859 100644 --- a/halfapi/halfapi.py +++ b/halfapi/halfapi.py @@ -104,6 +104,8 @@ class HalfAPI: on_startup=startup_fcts ) + schemas = [] + for key, domain in self.config.get('domain', {}).items(): if not isinstance(domain, dict): continue @@ -120,22 +122,19 @@ class HalfAPI: path = f'/{dom_name}' logger.debug('Mounting domain %s on %s', domain.get('name'), path) - self.__application.mount(path, - HalfDomain( - domain.get('name', key), - domain.get('router'), - domain.get('config', {}), - self.application - ) + + half_domain = HalfDomain( + domain.get('name', key), + domain.get('router'), + domain.get('config', {}), + self.application ) - """ - self.__application.add_middleware( - DomainMiddleware, - domain=domain, - config=CONFIG - ) - """ + schemas.append(half_domain.schema()) + + self.__application.mount(path, half_domain) + + self.__application.add_route('/', JSONRoute(schemas)) self.__application.add_middleware( AuthenticationMiddleware,