From 7a6fda1e269b4ebcdbce17750cea540795f56845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Fri, 21 Nov 2014 10:07:06 +0100 Subject: [PATCH] [enh] Support list arguments from HTTP requests in the api --- moulinette/interfaces/api.py | 40 ++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/moulinette/interfaces/api.py b/moulinette/interfaces/api.py index 2dc3baf0..17b79ef8 100644 --- a/moulinette/interfaces/api.py +++ b/moulinette/interfaces/api.py @@ -65,7 +65,6 @@ class _HTTPArgumentParser(object): ## Append an argument to the current one def append(arg_strings, value, option_string=None): - # TODO: Process list arguments if isinstance(value, bool): # Append the option string only if option_string is not None: @@ -78,6 +77,19 @@ class _HTTPArgumentParser(object): arg_strings.append(value) else: arg_strings.append(value) + elif isinstance(value, list): + if option_string is not None: + arg_strings.append(option_string) + for v in value: + if isinstance(v, str): + arg_strings.append(value) + else: + logger.warning("unsupported argument value type %r " \ + "in %s for option string %s", v, value, + option_string) + else: + logger.warning("unsupported argument type %r for option " \ + "string %s", value, option_string) return arg_strings @@ -185,13 +197,33 @@ class _ActionsMapPlugin(object): context -- An instance of Route """ + def _format(value): + if isinstance(value, list) and len(value) == 1: + return value[0] + return value + def wrapper(*args, **kwargs): - # Bring arguments together params = kwargs + # Format boolean params for a in args: params[a] = True - for k, v in request.params.items(): - params[k] = v + # Append other request params + for k, v in request.params.dict.items(): + v = _format(v) + try: + curr_v = params[k] + except KeyError: + params[k] = v + else: + # Append param value to the list + if not isinstance(curr_v, list): + curr_v = [curr_v] + if isinstance(v, list): + for i in v: + curr_v.append(i) + else: + curr_v.append(v) + params[k] = curr_v # Process the action return callback((request.method, context.rule), params)