fix ret_type feature

This commit is contained in:
Maxime Alves LIRMM@home 2022-08-31 00:12:37 +02:00
parent 8bdf5cab82
commit 5e21d4c24f
5 changed files with 75 additions and 6 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__version__ = '0.6.22-rc0' __version__ = '0.6.22-rc1'
def version(): def version():
return f'HalfAPI version:{__version__}' return f'HalfAPI version:{__version__}'

View File

@ -52,7 +52,7 @@ class NoDomainsException(Exception):
""" """
pass pass
def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine: def route_decorator(fct: FunctionType) -> Coroutine:
""" Returns an async function that can be mounted on a router """ Returns an async function that can be mounted on a router
""" """
@wraps(fct) @wraps(fct)
@ -60,9 +60,10 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine:
async def wrapped(request, *args, **kwargs): async def wrapped(request, *args, **kwargs):
fct_args_spec = inspect.getfullargspec(fct).args fct_args_spec = inspect.getfullargspec(fct).args
fct_args_defaults = inspect.getfullargspec(fct).defaults or [] fct_args_defaults = inspect.getfullargspec(fct).defaults or []
fct_args_defaults_dict = {} fct_args_defaults_dict = dict(list(zip(
for i in range(len(fct_args_defaults)): reversed(fct_args_spec),
fct_args_defaults_dict[fct_args_spec[-i]] = fct_args_defaults[-i] reversed(fct_args_defaults)
)))
fct_args = request.path_params.copy() fct_args = request.path_params.copy()
@ -76,7 +77,12 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine:
if 'data' in fct_args_spec: if 'data' in fct_args_spec:
fct_args['data'] = kwargs.get('data') if 'data' in fct_args_defaults_dict:
fct_args['data'] = fct_args_defaults_dict
else:
fct_args['data'] = {}
fct_args['data'].update(kwargs.get('data', {}))
if 'out' in fct_args_spec: if 'out' in fct_args_spec:
fct_args['out'] = kwargs.get('out') fct_args['out'] = kwargs.get('out')
@ -88,6 +94,8 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine:
else: else:
ret_type = fct_args.get('data', {}).get('format', 'json') ret_type = fct_args.get('data', {}).get('format', 'json')
logger.debug('Return type {} (defaults: {})'.format(ret_type,
fct_args_defaults_dict))
try: try:
if ret_type == 'json': if ret_type == 'json':
return ORJSONResponse(fct(**fct_args)) return ORJSONResponse(fct(**fct_args))

View File

@ -0,0 +1,13 @@
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}]
}
def get(ext, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return '\n'.join(('trololo', '', 'ololotr'))

View File

@ -0,0 +1,31 @@
from halfapi.lib import acl
ACLS = {
'GET': [{'acl':acl.public}],
'POST': [
{
'acl':acl.public,
'args': {
'required': {'trou'},
'optional': {'troo'}
}
}
]
}
def get(ext, halfapi={}, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
return '\n'.join(('trololo', '', 'ololotr'))
def post(ext, data={'troo': 'fidget'}, halfapi={}, ret_type='html'):
"""
responses:
200:
description: dummy abc.alphabet route
"""
print(data)
return '\n'.join(('trololo', '', 'ololotr'))

View File

@ -19,3 +19,20 @@ class TestDummyDomain(TestDomain):
assert res.status_code == 200 assert res.status_code == 200
assert isinstance(res.content.decode(), str) assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html' assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.get('/ret_type/h24')
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.get('/ret_type/h24/config')
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.post('/ret_type/h24/config', {
'trou': 'glet'
})
assert res.status_code == 200
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'