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