[lib.domain] implement domain schema - try: halfapi domain dummy_domain
This commit is contained in:
parent
1ccfa0d10e
commit
49c13c56ac
@ -9,13 +9,16 @@ import importlib
|
|||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
import orjson
|
||||||
|
|
||||||
|
|
||||||
from .cli import cli
|
from .cli import cli
|
||||||
from ..conf import config, write_config, DOMAINSDICT
|
from ..conf import config, write_config, DOMAINSDICT
|
||||||
|
|
||||||
|
from ..lib.domain import domain_schema
|
||||||
from ..lib.schemas import schema_dict_dom
|
from ..lib.schemas import schema_dict_dom
|
||||||
from ..lib.routes import api_routes
|
from ..lib.routes import api_routes
|
||||||
|
from ..lib.responses import ORJSONResponse
|
||||||
|
|
||||||
|
|
||||||
from ..logging import logger
|
from ..logging import logger
|
||||||
@ -117,49 +120,39 @@ def list_api_routes():
|
|||||||
list_routes(domain, m_dom)
|
list_routes(domain, m_dom)
|
||||||
|
|
||||||
|
|
||||||
@click.option('--read',default=False, is_flag=True)
|
@click.option('--read',default=True, is_flag=True)
|
||||||
@click.option('--create',default=False, is_flag=True)
|
@click.option('--create',default=False, is_flag=True)
|
||||||
@click.option('--update',default=False, is_flag=True)
|
@click.option('--update',default=False, is_flag=True)
|
||||||
@click.option('--delete',default=False, is_flag=True)
|
@click.option('--delete',default=False, is_flag=True)
|
||||||
@click.option('--domains',default=None)
|
@click.argument('domain',default=None, required=False)
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def domain(domains, delete, update, create, read): #, domains, read, create, update, delete):
|
def domain(domain, delete, update, create, read): #, domains, read, create, update, delete):
|
||||||
"""
|
"""
|
||||||
The "halfapi domain" command
|
The "halfapi domain" command
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
domain (List[str]|None): The list of the domains to list/update
|
domain (str|None): The domain name
|
||||||
|
|
||||||
The parameter has a misleading name as it is a multiple option
|
The parameter has a misleading name as it is a multiple option
|
||||||
but this would be strange to use it several times named as "domains"
|
but this would be strange to use it several times named as "domains"
|
||||||
|
|
||||||
update (boolean): If set, update the database for the selected domains
|
update (boolean): If set, update the database for the selected domains
|
||||||
"""
|
"""
|
||||||
|
if not domain:
|
||||||
dom_dict = DOMAINSDICT()
|
|
||||||
|
|
||||||
if not domains:
|
|
||||||
if create:
|
if create:
|
||||||
# TODO: Connect to the create_domain function
|
# TODO: Connect to the create_domain function
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
else:
|
raise Exception('Missing domain name')
|
||||||
dom_dict_ = {}
|
|
||||||
|
|
||||||
for domain_name in domains.split(','):
|
|
||||||
if domain_name in dom_dict.keys():
|
|
||||||
dom_dict_[domain_name] = dom_dict[domain_name]
|
|
||||||
continue
|
|
||||||
|
|
||||||
click.echo(
|
|
||||||
f'Domain {domain_name}s is not activated in the configuration')
|
|
||||||
|
|
||||||
dom_dict = dom_dict_
|
|
||||||
|
|
||||||
for domain_name, m_dom in dom_dict.items():
|
|
||||||
if update:
|
if update:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
if delete:
|
if delete:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
if read:
|
if read:
|
||||||
list_routes(domain_name, m_dom)
|
m_domain = importlib.import_module(domain)
|
||||||
|
|
||||||
|
click.echo(orjson.dumps(
|
||||||
|
domain_schema(m_domain),
|
||||||
|
option=orjson.OPT_NON_STR_KEYS,
|
||||||
|
default=ORJSONResponse.default_cast)
|
||||||
|
)
|
||||||
|
|
||||||
|
@ -32,12 +32,11 @@ from timing_asgi.integrations import StarletteScopeToName
|
|||||||
from .lib.constants import API_SCHEMA_DICT
|
from .lib.constants import API_SCHEMA_DICT
|
||||||
from .lib.domain_middleware import DomainMiddleware
|
from .lib.domain_middleware import DomainMiddleware
|
||||||
from .lib.timing import HTimingClient
|
from .lib.timing import HTimingClient
|
||||||
from .lib.domain import NoDomainsException
|
|
||||||
from .lib.jwt_middleware import JWTAuthenticationBackend
|
from .lib.jwt_middleware import JWTAuthenticationBackend
|
||||||
from .lib.responses import (ORJSONResponse, UnauthorizedResponse,
|
from .lib.responses import (ORJSONResponse, UnauthorizedResponse,
|
||||||
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse,
|
NotFoundResponse, InternalServerErrorResponse, NotImplementedResponse,
|
||||||
ServiceUnavailableResponse)
|
ServiceUnavailableResponse)
|
||||||
from .lib.domain import domain_schema_dict
|
from .lib.domain import domain_schema_dict, NoDomainsException, domain_schema
|
||||||
from .lib.routes import gen_domain_routes, gen_schema_routes, JSONRoute
|
from .lib.routes import gen_domain_routes, gen_schema_routes, JSONRoute
|
||||||
from .lib.schemas import schema_json, get_acls
|
from .lib.schemas import schema_json, get_acls
|
||||||
from .logging import logger, config_logging
|
from .logging import logger, config_logging
|
||||||
@ -55,7 +54,7 @@ class HalfAPI:
|
|||||||
CONFIG = config.get('config', {})
|
CONFIG = config.get('config', {})
|
||||||
|
|
||||||
domain = config.get('domain')['name']
|
domain = config.get('domain')['name']
|
||||||
router = config.get('domain')['router']
|
router = config.get('domain').get('router', None)
|
||||||
|
|
||||||
if not (domain and router):
|
if not (domain and router):
|
||||||
raise NoDomainsException()
|
raise NoDomainsException()
|
||||||
@ -66,22 +65,18 @@ class HalfAPI:
|
|||||||
|
|
||||||
self.__application = None
|
self.__application = None
|
||||||
|
|
||||||
if domain and router:
|
m_domain = m_domain_router = m_domain_acl = None
|
||||||
|
if domain:
|
||||||
m_domain = importlib.import_module(f'{domain}')
|
m_domain = importlib.import_module(f'{domain}')
|
||||||
m_domain_router = importlib.import_module(f'{domain}.{router}')
|
if not router:
|
||||||
|
router = getattr('__router__', domain, '.routers')
|
||||||
|
m_domain_router = importlib.import_module(router)
|
||||||
m_domain_acl = importlib.import_module(f'{domain}.acl')
|
m_domain_acl = importlib.import_module(f'{domain}.acl')
|
||||||
|
|
||||||
self.schema = { **API_SCHEMA_DICT }
|
if not(m_domain and m_domain_router and m_domain_acl):
|
||||||
|
raise Exception('Cannot import domain')
|
||||||
self.schema['domain'] = {
|
|
||||||
'name': domain,
|
|
||||||
'version': getattr(m_domain, '__version__', ''),
|
|
||||||
'patch_release': getattr(m_domain, '__patch_release__', ''),
|
|
||||||
'acls': tuple(getattr(m_domain_acl, 'ACLS', ()))
|
|
||||||
}
|
|
||||||
|
|
||||||
self.schema['paths'] = domain_schema_dict(m_domain_router)
|
|
||||||
|
|
||||||
|
self.schema = domain_schema(m_domain)
|
||||||
|
|
||||||
routes = [ Route('/', JSONRoute(self.schema)) ]
|
routes = [ Route('/', JSONRoute(self.schema)) ]
|
||||||
|
|
||||||
|
@ -39,6 +39,7 @@ ROUTE_SCHEMA = Schema({
|
|||||||
|
|
||||||
DOMAIN_SCHEMA = Schema({
|
DOMAIN_SCHEMA = Schema({
|
||||||
'name': str,
|
'name': str,
|
||||||
|
Optional('routers'): str,
|
||||||
Optional('version'): str,
|
Optional('version'): str,
|
||||||
Optional('patch_release'): str,
|
Optional('patch_release'): str,
|
||||||
Optional('acls'): [
|
Optional('acls'): [
|
||||||
|
@ -230,6 +230,7 @@ def gen_router_routes(m_router: ModuleType, path: List[str]) -> \
|
|||||||
path.pop()
|
path.pop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def domain_schema_dict(m_router: ModuleType) -> Dict:
|
def domain_schema_dict(m_router: ModuleType) -> Dict:
|
||||||
""" gen_router_routes return values as a dict
|
""" gen_router_routes return values as a dict
|
||||||
Parameters:
|
Parameters:
|
||||||
@ -239,6 +240,8 @@ def domain_schema_dict(m_router: ModuleType) -> Dict:
|
|||||||
Returns:
|
Returns:
|
||||||
|
|
||||||
Dict: Schema of dict is halfapi.lib.constants.DOMAIN_SCHEMA
|
Dict: Schema of dict is halfapi.lib.constants.DOMAIN_SCHEMA
|
||||||
|
|
||||||
|
@TODO: Should be a "router_schema_dict" function
|
||||||
"""
|
"""
|
||||||
d_res = {}
|
d_res = {}
|
||||||
|
|
||||||
@ -256,6 +259,24 @@ def domain_schema_dict(m_router: ModuleType) -> Dict:
|
|||||||
|
|
||||||
return d_res
|
return d_res
|
||||||
|
|
||||||
|
from .constants import API_SCHEMA_DICT
|
||||||
|
def domain_schema(m_domain: ModuleType) -> Dict:
|
||||||
|
schema = { **API_SCHEMA_DICT }
|
||||||
|
routers_submod_str = getattr(m_domain, '__routers__', '.routers')
|
||||||
|
m_domain_acl = importlib.import_module('.acl', m_domain.__package__)
|
||||||
|
m_domain_routers = importlib.import_module(
|
||||||
|
routers_submod_str, m_domain.__package__
|
||||||
|
)
|
||||||
|
schema['domain'] = {
|
||||||
|
'name': getattr(m_domain, '__name__'),
|
||||||
|
'version': getattr(m_domain, '__version__', ''),
|
||||||
|
'patch_release': getattr(m_domain, '__patch_release__', ''),
|
||||||
|
'routers': routers_submod_str,
|
||||||
|
'acls': tuple(getattr(m_domain_acl, 'ACLS', ()))
|
||||||
|
}
|
||||||
|
schema['paths'] = domain_schema_dict(m_domain_routers)
|
||||||
|
return schema
|
||||||
|
|
||||||
def domain_schema_list(m_router: ModuleType) -> List:
|
def domain_schema_list(m_router: ModuleType) -> List:
|
||||||
""" Schema as list, one row by route/acl
|
""" Schema as list, one row by route/acl
|
||||||
Parameters:
|
Parameters:
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
__name__ = 'dummy_domain'
|
||||||
|
__version__ = '0.0.0'
|
||||||
|
__patch_release__ = '0.0.0'
|
||||||
|
__routers__ = '.routers'
|
||||||
|
|
@ -20,9 +20,16 @@ class TestCliProj():
|
|||||||
|
|
||||||
|
|
||||||
def test_domain_commands(self, project_runner):
|
def test_domain_commands(self, project_runner):
|
||||||
|
""" TODO: Test create command
|
||||||
|
"""
|
||||||
r = project_runner('domain')
|
r = project_runner('domain')
|
||||||
|
print(r.stdout)
|
||||||
|
assert r.exit_code == 1
|
||||||
|
r = project_runner('domain dummy_domain')
|
||||||
|
print(r.stdout)
|
||||||
assert r.exit_code == 0
|
assert r.exit_code == 0
|
||||||
|
|
||||||
|
|
||||||
def test_config_commands(self, project_runner):
|
def test_config_commands(self, project_runner):
|
||||||
try:
|
try:
|
||||||
r = project_runner('config')
|
r = project_runner('config')
|
||||||
|
Loading…
Reference in New Issue
Block a user