simplifier l'API
This commit is contained in:
parent
dc7d081608
commit
32b83f496b
@ -38,9 +38,7 @@ def get_config():
|
|||||||
'sql_dir': SQL_DIR},
|
'sql_dir': SQL_DIR},
|
||||||
'cache': {'root_path': CACHE_ROOT_PATH},
|
'cache': {'root_path': CACHE_ROOT_PATH},
|
||||||
'servermodel': {'internal_source_path': SRV_SEED_PATH,
|
'servermodel': {'internal_source_path': SRV_SEED_PATH,
|
||||||
'internal_source': 'internal',
|
'internal_source': 'internal'},
|
||||||
'internal_distribution': 'last',
|
|
||||||
'internal_release_name': 'none'},
|
|
||||||
'submodule': {'allow_insecure_https': False,
|
'submodule': {'allow_insecure_https': False,
|
||||||
'pki': '192.168.56.112'},
|
'pki': '192.168.56.112'},
|
||||||
'provider': {'factory_configuration_dir': PROVIDER_FACTORY_CONFIG_DIR,
|
'provider': {'factory_configuration_dir': PROVIDER_FACTORY_CONFIG_DIR,
|
||||||
|
@ -249,9 +249,9 @@ class Dispatcher(register.RegisterDispatcher,
|
|||||||
async with await Config(self.option) as config:
|
async with await Config(self.option) as config:
|
||||||
await config.property.read_write()
|
await config.property.read_write()
|
||||||
# set message's option
|
# set message's option
|
||||||
await config.option('message').value.set(risotto_context.message)
|
await config.option('message').value.set(uri)
|
||||||
# store values
|
# store values
|
||||||
subconfig = config.option(risotto_context.message)
|
subconfig = config.option(uri)
|
||||||
extra_parameters = {}
|
extra_parameters = {}
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
if not internal or not key.startswith('_'):
|
if not internal or not key.startswith('_'):
|
||||||
|
@ -23,7 +23,8 @@ extra_routes = {}
|
|||||||
def create_context(request):
|
def create_context(request):
|
||||||
risotto_context = Context()
|
risotto_context = Context()
|
||||||
risotto_context.username = request.match_info.get('username',
|
risotto_context.username = request.match_info.get('username',
|
||||||
get_config()['http_server']['default_user'])
|
get_config()['http_server']['default_user'],
|
||||||
|
)
|
||||||
return risotto_context
|
return risotto_context
|
||||||
|
|
||||||
|
|
||||||
@ -117,7 +118,8 @@ async def api(request,
|
|||||||
async with await Config(get_messages(current_module_names=risotto_modules,
|
async with await Config(get_messages(current_module_names=risotto_modules,
|
||||||
load_shortarg=True,
|
load_shortarg=True,
|
||||||
current_version=risotto_context.version,
|
current_version=risotto_context.version,
|
||||||
uris=uris)[1],
|
uris=uris,
|
||||||
|
)[1],
|
||||||
display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
|
display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
|
||||||
await config.property.read_write()
|
await config.property.read_write()
|
||||||
TIRAMISU = await config.option.dict(remotable='none')
|
TIRAMISU = await config.option.dict(remotable='none')
|
||||||
@ -153,6 +155,9 @@ async def get_app(loop):
|
|||||||
extra_handler = type(api_route['path'], (extra_route_handler,), api_route)
|
extra_handler = type(api_route['path'], (extra_route_handler,), api_route)
|
||||||
routes.append(get(api_route['path'], extra_handler))
|
routes.append(get(api_route['path'], extra_handler))
|
||||||
print(f' - {api_route["path"]} (http_get)')
|
print(f' - {api_route["path"]} (http_get)')
|
||||||
|
# last version is default version
|
||||||
|
routes.append(get('/api', extra_handler))
|
||||||
|
print(f' - /api (http_get)')
|
||||||
print()
|
print()
|
||||||
if extra_routes:
|
if extra_routes:
|
||||||
print(_('======== Registered extra routes ========'))
|
print(_('======== Registered extra routes ========'))
|
||||||
|
@ -248,7 +248,8 @@ def get_message_file_path(version,
|
|||||||
|
|
||||||
def list_messages(uris,
|
def list_messages(uris,
|
||||||
current_module_names,
|
current_module_names,
|
||||||
current_version):
|
current_version,
|
||||||
|
):
|
||||||
def get_module_paths(current_module_names):
|
def get_module_paths(current_module_names):
|
||||||
if current_module_names is None:
|
if current_module_names is None:
|
||||||
current_module_names = listdir(join(MESSAGE_ROOT_PATH, version))
|
current_module_names = listdir(join(MESSAGE_ROOT_PATH, version))
|
||||||
@ -431,9 +432,9 @@ def _get_description(description,
|
|||||||
|
|
||||||
def _get_option(name,
|
def _get_option(name,
|
||||||
arg,
|
arg,
|
||||||
file_path,
|
uri,
|
||||||
select_option,
|
select_option,
|
||||||
optiondescription):
|
):
|
||||||
"""generate option
|
"""generate option
|
||||||
"""
|
"""
|
||||||
props = []
|
props = []
|
||||||
@ -443,7 +444,7 @@ def _get_option(name,
|
|||||||
props.append(Calculation(calc_value,
|
props.append(Calculation(calc_value,
|
||||||
Params(ParamValue('disabled'),
|
Params(ParamValue('disabled'),
|
||||||
kwargs={'condition': ParamOption(select_option, todict=True),
|
kwargs={'condition': ParamOption(select_option, todict=True),
|
||||||
'expected': ParamValue(optiondescription),
|
'expected': ParamValue(uri),
|
||||||
'reverse_condition': ParamValue(True)}),
|
'reverse_condition': ParamValue(True)}),
|
||||||
calc_value_property_help))
|
calc_value_property_help))
|
||||||
|
|
||||||
@ -472,25 +473,25 @@ def _get_option(name,
|
|||||||
elif type_ == 'Float':
|
elif type_ == 'Float':
|
||||||
obj = FloatOption(**kwargs)
|
obj = FloatOption(**kwargs)
|
||||||
else:
|
else:
|
||||||
raise Exception('unsupported type {} in {}'.format(type_, file_path))
|
raise Exception('unsupported type {} in {}'.format(type_, uri))
|
||||||
obj.impl_set_information('ref', arg.ref)
|
obj.impl_set_information('ref', arg.ref)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def get_options(message_def,
|
def get_options(message_def,
|
||||||
file_path,
|
uri,
|
||||||
select_option,
|
select_option,
|
||||||
optiondescription,
|
load_shortarg,
|
||||||
load_shortarg):
|
):
|
||||||
"""build option with args/kwargs
|
"""build option with args/kwargs
|
||||||
"""
|
"""
|
||||||
options =[]
|
options =[]
|
||||||
for name, arg in message_def.parameters.items():
|
for name, arg in message_def.parameters.items():
|
||||||
current_opt = _get_option(name,
|
current_opt = _get_option(name,
|
||||||
arg,
|
arg,
|
||||||
file_path,
|
uri,
|
||||||
select_option,
|
select_option,
|
||||||
optiondescription)
|
)
|
||||||
options.append(current_opt)
|
options.append(current_opt)
|
||||||
if hasattr(arg, 'shortarg') and arg.shortarg and load_shortarg:
|
if hasattr(arg, 'shortarg') and arg.shortarg and load_shortarg:
|
||||||
options.append(SymLinkOption(arg.shortarg, current_opt))
|
options.append(SymLinkOption(arg.shortarg, current_opt))
|
||||||
@ -498,17 +499,18 @@ def get_options(message_def,
|
|||||||
|
|
||||||
|
|
||||||
def _parse_responses(message_def,
|
def _parse_responses(message_def,
|
||||||
file_path):
|
uri,
|
||||||
|
):
|
||||||
"""build option with returns
|
"""build option with returns
|
||||||
"""
|
"""
|
||||||
if message_def.response.parameters is None:
|
if message_def.response.parameters is None:
|
||||||
raise Exception('message "{}" did not returned any valid parameters.'.format(message_def.message))
|
raise Exception(f'message "{message_def.message}" did not returned any valid parameters')
|
||||||
|
|
||||||
options = []
|
options = []
|
||||||
names = []
|
names = []
|
||||||
for name, obj in message_def.response.parameters.items():
|
for name, obj in message_def.response.parameters.items():
|
||||||
if name in names:
|
if name in names:
|
||||||
raise Exception('multi response with name {} in {}'.format(name, file_path))
|
raise Exception(f'multi response with name "{name}" in "{uri}"')
|
||||||
names.append(name)
|
names.append(name)
|
||||||
|
|
||||||
kwargs = {'name': name,
|
kwargs = {'name': name,
|
||||||
@ -531,15 +533,17 @@ def _parse_responses(message_def,
|
|||||||
else:
|
else:
|
||||||
kwargs['properties'] = ('mandatory',)
|
kwargs['properties'] = ('mandatory',)
|
||||||
options.append(option(**kwargs))
|
options.append(option(**kwargs))
|
||||||
od = OptionDescription(message_def.message,
|
od = OptionDescription(uri,
|
||||||
message_def.response.description,
|
message_def.response.description,
|
||||||
options)
|
options,
|
||||||
|
)
|
||||||
od.impl_set_information('multi', message_def.response.multi)
|
od.impl_set_information('multi', message_def.response.multi)
|
||||||
return od
|
return od
|
||||||
|
|
||||||
|
|
||||||
def _get_root_option(select_option,
|
def _get_root_option(select_option,
|
||||||
optiondescriptions):
|
optiondescriptions,
|
||||||
|
):
|
||||||
"""get root option
|
"""get root option
|
||||||
"""
|
"""
|
||||||
def _get_od(curr_ods):
|
def _get_od(curr_ods):
|
||||||
@ -581,19 +585,21 @@ def _get_root_option(select_option,
|
|||||||
def get_messages(current_module_names,
|
def get_messages(current_module_names,
|
||||||
load_shortarg=False,
|
load_shortarg=False,
|
||||||
current_version=None,
|
current_version=None,
|
||||||
uris=None):
|
uris=None,
|
||||||
|
):
|
||||||
"""generate description from yml files
|
"""generate description from yml files
|
||||||
"""
|
"""
|
||||||
optiondescriptions = {}
|
optiondescriptions = {}
|
||||||
optiondescriptions_info = {}
|
optiondescriptions_info = {}
|
||||||
messages = list(list_messages(uris,
|
messages = list(list_messages(uris,
|
||||||
current_module_names,
|
current_module_names,
|
||||||
current_version))
|
current_version,
|
||||||
|
))
|
||||||
messages.sort()
|
messages.sort()
|
||||||
optiondescriptions_name = [message_name.split('.', 1)[1] for message_name in messages]
|
# optiondescriptions_name = [message_name.split('.', 1)[1] for message_name in messages]
|
||||||
select_option = ChoiceOption('message',
|
select_option = ChoiceOption('message',
|
||||||
'Nom du message.',
|
'Nom du message.',
|
||||||
tuple(optiondescriptions_name),
|
tuple(messages),
|
||||||
properties=frozenset(['mandatory', 'positional']))
|
properties=frozenset(['mandatory', 'positional']))
|
||||||
for uri in messages:
|
for uri in messages:
|
||||||
message_def = get_message(uri,
|
message_def = get_message(uri,
|
||||||
@ -601,23 +607,26 @@ def get_messages(current_module_names,
|
|||||||
)
|
)
|
||||||
optiondescriptions_info[message_def.message] = {'pattern': message_def.pattern,
|
optiondescriptions_info[message_def.message] = {'pattern': message_def.pattern,
|
||||||
'default_roles': message_def.default_roles,
|
'default_roles': message_def.default_roles,
|
||||||
'version': message_def.version}
|
'version': message_def.version,
|
||||||
|
}
|
||||||
if message_def.pattern == 'rpc':
|
if message_def.pattern == 'rpc':
|
||||||
if not message_def.response:
|
if not message_def.response:
|
||||||
raise Exception(f'rpc without response is not allowed {uri}')
|
raise Exception(f'rpc without response is not allowed {uri}')
|
||||||
optiondescriptions_info[message_def.message]['response'] = _parse_responses(message_def,
|
optiondescriptions_info[message_def.message]['response'] = _parse_responses(message_def,
|
||||||
uri)
|
uri,
|
||||||
|
)
|
||||||
elif message_def.response:
|
elif message_def.response:
|
||||||
raise Exception(f'response is not allowed for {uri}')
|
raise Exception(f'response is not allowed for {uri}')
|
||||||
message_def.options = get_options(message_def,
|
message_def.options = get_options(message_def,
|
||||||
uri,
|
uri,
|
||||||
select_option,
|
select_option,
|
||||||
message_def.message,
|
load_shortarg,
|
||||||
load_shortarg)
|
)
|
||||||
optiondescriptions[message_def.message] = (message_def.description, message_def.options)
|
optiondescriptions[uri] = (message_def.description, message_def.options)
|
||||||
|
|
||||||
root = _get_root_option(select_option,
|
root = _get_root_option(select_option,
|
||||||
optiondescriptions)
|
optiondescriptions,
|
||||||
|
)
|
||||||
return optiondescriptions_info, root
|
return optiondescriptions_info, root
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,25 +113,31 @@ class RegisterDispatcher:
|
|||||||
return {param.name for param in list(signature(function).parameters.values())[first_argument_index:]}
|
return {param.name for param in list(signature(function).parameters.values())[first_argument_index:]}
|
||||||
|
|
||||||
async def get_message_args(self,
|
async def get_message_args(self,
|
||||||
message: str):
|
message: str,
|
||||||
|
version: str,
|
||||||
|
):
|
||||||
# load config
|
# load config
|
||||||
async with await Config(self.option, display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
|
async with await Config(self.option, display_name=lambda self, dyn_name, suffix: self.impl_getname()) as config:
|
||||||
|
uri = f'{version}.{message}'
|
||||||
await config.property.read_write()
|
await config.property.read_write()
|
||||||
# set message to the message name
|
# set message to the message name
|
||||||
await config.option('message').value.set(message)
|
await config.option('message').value.set(uri)
|
||||||
# get message argument
|
# get message argument
|
||||||
dico = await config.option(message).value.dict()
|
dico = await config.option(uri).value.dict()
|
||||||
return set(dico.keys())
|
return set(dico.keys())
|
||||||
|
|
||||||
async def valid_rpc_params(self,
|
async def valid_rpc_params(self,
|
||||||
version: str,
|
version: str,
|
||||||
message: str,
|
message: str,
|
||||||
function: Callable,
|
function: Callable,
|
||||||
module_name: str):
|
module_name: str,
|
||||||
|
):
|
||||||
""" parameters function must have strictly all arguments with the correct name
|
""" parameters function must have strictly all arguments with the correct name
|
||||||
"""
|
"""
|
||||||
# get message arguments
|
# get message arguments
|
||||||
message_args = await self.get_message_args(message)
|
message_args = await self.get_message_args(message,
|
||||||
|
version,
|
||||||
|
)
|
||||||
# get function arguments
|
# get function arguments
|
||||||
function_args = self.get_function_args(function)
|
function_args = self.get_function_args(function)
|
||||||
# compare message arguments with function parameter
|
# compare message arguments with function parameter
|
||||||
@ -156,11 +162,14 @@ class RegisterDispatcher:
|
|||||||
version: str,
|
version: str,
|
||||||
message: str,
|
message: str,
|
||||||
function: Callable,
|
function: Callable,
|
||||||
module_name: str):
|
module_name: str,
|
||||||
|
):
|
||||||
""" parameters function validation for event messages
|
""" parameters function validation for event messages
|
||||||
"""
|
"""
|
||||||
# get message arguments
|
# get message arguments
|
||||||
message_args = await self.get_message_args(message)
|
message_args = await self.get_message_args(message,
|
||||||
|
version,
|
||||||
|
)
|
||||||
# get function arguments
|
# get function arguments
|
||||||
function_args = self.get_function_args(function)
|
function_args = self.get_function_args(function)
|
||||||
# compare message arguments with function parameter
|
# compare message arguments with function parameter
|
||||||
|
Loading…
Reference in New Issue
Block a user