diff --git a/halfapi/__init__.py b/halfapi/__init__.py index 2d7d3d1..60d04d4 100644 --- a/halfapi/__init__.py +++ b/halfapi/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = '0.6.22-rc0' +__version__ = '0.6.22-rc1' def version(): return f'HalfAPI version:{__version__}' diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index b90b0f6..1c61d1a 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -52,7 +52,7 @@ class NoDomainsException(Exception): """ 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 """ @wraps(fct) @@ -60,9 +60,10 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine: async def wrapped(request, *args, **kwargs): fct_args_spec = inspect.getfullargspec(fct).args fct_args_defaults = inspect.getfullargspec(fct).defaults or [] - fct_args_defaults_dict = {} - for i in range(len(fct_args_defaults)): - fct_args_defaults_dict[fct_args_spec[-i]] = fct_args_defaults[-i] + fct_args_defaults_dict = dict(list(zip( + reversed(fct_args_spec), + reversed(fct_args_defaults) + ))) 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: - 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: fct_args['out'] = kwargs.get('out') @@ -88,6 +94,8 @@ def route_decorator(fct: FunctionType, ret_type: str = 'json') -> Coroutine: else: ret_type = fct_args.get('data', {}).get('format', 'json') + logger.debug('Return type {} (defaults: {})'.format(ret_type, + fct_args_defaults_dict)) try: if ret_type == 'json': return ORJSONResponse(fct(**fct_args)) diff --git a/tests/dummy_domain/routers/ret_type/EXT_str/__init__.py b/tests/dummy_domain/routers/ret_type/EXT_str/__init__.py new file mode 100644 index 0000000..380b048 --- /dev/null +++ b/tests/dummy_domain/routers/ret_type/EXT_str/__init__.py @@ -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')) diff --git a/tests/dummy_domain/routers/ret_type/EXT_str/config/__init__.py b/tests/dummy_domain/routers/ret_type/EXT_str/config/__init__.py new file mode 100644 index 0000000..639e3ea --- /dev/null +++ b/tests/dummy_domain/routers/ret_type/EXT_str/config/__init__.py @@ -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')) diff --git a/tests/test_domain.py b/tests/test_domain.py index e84a832..d3ad4f9 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -19,3 +19,20 @@ class TestDummyDomain(TestDomain): 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') + 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'