diff options
Diffstat (limited to 'rest-api-templates')
-rw-r--r-- | rest-api-templates/ari_resource.h.mustache | 13 | ||||
-rw-r--r-- | rest-api-templates/asterisk_processor.py | 2 | ||||
-rw-r--r-- | rest-api-templates/body_parsing.mustache | 71 | ||||
-rw-r--r-- | rest-api-templates/param_parsing.mustache | 41 | ||||
-rw-r--r-- | rest-api-templates/res_ari_resource.c.mustache | 6 |
5 files changed, 90 insertions, 43 deletions
diff --git a/rest-api-templates/ari_resource.h.mustache b/rest-api-templates/ari_resource.h.mustache index e389eb5d7..e66d9b604 100644 --- a/rest-api-templates/ari_resource.h.mustache +++ b/rest-api-templates/ari_resource.h.mustache @@ -62,6 +62,19 @@ struct ast_ari_{{c_name}}_{{c_nickname}}_args { {{/parameters}} }; {{#is_req}} +{{#parse_body}} +/*! + * \brief Body parsing function for {{path}}. + * \param body The JSON body from which to parse parameters. + * \param[out] args The args structure to parse into. + * \retval zero on success + * \retval non-zero on failure + */ +int ast_ari_{{c_name}}_{{c_nickname}}_parse_body( + struct ast_json *body, + struct ast_ari_{{c_name}}_{{c_nickname}}_args *args); + +{{/parse_body}} /*! * \brief {{summary}} {{#notes}} diff --git a/rest-api-templates/asterisk_processor.py b/rest-api-templates/asterisk_processor.py index 7eb5bff6f..ef0f1673a 100644 --- a/rest-api-templates/asterisk_processor.py +++ b/rest-api-templates/asterisk_processor.py @@ -203,8 +203,10 @@ class AsteriskProcessor(SwaggerPostProcessor): def process_parameter(self, parameter, context): if parameter.param_type == 'body': + parameter.is_body_parameter = True; parameter.c_data_type = 'struct ast_json *' else: + parameter.is_body_parameter = False; if not parameter.data_type in self.type_mapping: raise SwaggerError( "Invalid parameter type %s" % parameter.data_type, context) diff --git a/rest-api-templates/body_parsing.mustache b/rest-api-templates/body_parsing.mustache new file mode 100644 index 000000000..63cce0de8 --- /dev/null +++ b/rest-api-templates/body_parsing.mustache @@ -0,0 +1,71 @@ +{{! + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2014, Digium, Inc. + * + * William Kinsey Moore, III <kmoore@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. +}} +{{! + * Snippet for decoding parameters into an _args struct. +}} +{{#parse_body}} +int ast_ari_{{c_name}}_{{c_nickname}}_parse_body( + struct ast_json *body, + struct ast_ari_{{c_name}}_{{c_nickname}}_args *args) +{ +{{#has_query_parameters}} + struct ast_json *field; +{{/has_query_parameters}} + /* Parse query parameters out of it */ +{{#query_parameters}} +{{^is_body_parameter}} + field = ast_json_object_get(body, "{{name}}"); + if (field) { +{{^allow_multiple}} + args->{{c_name}} = {{json_convert}}(field); +{{/allow_multiple}} +{{#allow_multiple}} + /* If they were silly enough to both pass in a query param and a + * JSON body, free up the query value. + */ + ast_free(args->{{c_name}}); + if (ast_json_typeof(field) == AST_JSON_ARRAY) { + /* Multiple param passed as array */ + size_t i; + args->{{c_name}}_count = ast_json_array_size(field); + args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count); + + if (!args->{{c_name}}) { + return -1; + } + + for (i = 0; i < args->{{c_name}}_count; ++i) { + args->{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i)); + } + } else { + /* Multiple param passed as single value */ + args->{{c_name}}_count = 1; + args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count); + if (!args->{{c_name}}) { + return -1; + } + args->{{c_name}}[0] = {{json_convert}}(field); + } +{{/allow_multiple}} + } +{{/is_body_parameter}} +{{/query_parameters}} + return 0; +} + +{{/parse_body}} diff --git a/rest-api-templates/param_parsing.mustache b/rest-api-templates/param_parsing.mustache index 9d2073869..2dde4b33f 100644 --- a/rest-api-templates/param_parsing.mustache +++ b/rest-api-templates/param_parsing.mustache @@ -104,45 +104,10 @@ args.{{c_name}} = ast_json_ref(body); {{/body_parameter}} {{^body_parameter}} - /* Parse query parameters out of it */ -{{#query_parameters}} - field = ast_json_object_get(body, "{{name}}"); - if (field) { -{{^allow_multiple}} - args.{{c_name}} = {{json_convert}}(field); -{{/allow_multiple}} -{{#allow_multiple}} - /* If they were silly enough to both pass in a query param and a - * JSON body, free up the query value. - */ - ast_free(args.{{c_name}}); - if (ast_json_typeof(field) == AST_JSON_ARRAY) { - /* Multiple param passed as array */ - size_t i; - args.{{c_name}}_count = ast_json_array_size(field); - args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); - - if (!args.{{c_name}}) { - ast_ari_response_alloc_failed(response); - goto fin; - } - - for (i = 0; i < args.{{c_name}}_count; ++i) { - args.{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i)); - } - } else { - /* Multiple param passed as single value */ - args.{{c_name}}_count = 1; - args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); - if (!args.{{c_name}}) { - ast_ari_response_alloc_failed(response); - goto fin; - } - args.{{c_name}}[0] = {{json_convert}}(field); - } -{{/allow_multiple}} + if (ast_ari_{{c_name}}_{{c_nickname}}_parse_body(body, &args)) { + ast_ari_response_alloc_failed(response); + goto fin; } -{{/query_parameters}} {{/body_parameter}} {{/parse_body}} {{/is_websocket}} diff --git a/rest-api-templates/res_ari_resource.c.mustache b/rest-api-templates/res_ari_resource.c.mustache index d2823a877..74681fc29 100644 --- a/rest-api-templates/res_ari_resource.c.mustache +++ b/rest-api-templates/res_ari_resource.c.mustache @@ -66,6 +66,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") {{#apis}} {{#operations}} {{#is_req}} +{{> body_parsing}} /*! * \brief Parameter parsing callback for {{path}}. * \param get_params GET parameters in the HTTP request. @@ -83,11 +84,6 @@ static void ast_ari_{{c_name}}_{{c_nickname}}_cb( struct ast_variable *i; {{/has_parameters}} RAII_VAR(struct ast_json *, body, NULL, ast_json_unref); -{{^body_parameter}} -{{#has_query_parameters}} - struct ast_json *field; -{{/has_query_parameters}} -{{/body_parameter}} #if defined(AST_DEVMODE) int is_valid; int code; |