asterisk/rest-api-templates/param_parsing.mustache
Kinsey Moore 1590d32ab0 ARI: Support channel variables in originate
This adds back in support for specifying channel variables during an
originate without compromising the ability to specify query parameters
in the JSON body. This was accomplished by generating the body-parsing
code in a separate function instead of being integrated with the URI
query parameter parsing code such that it could be called by paths with
body parameters. This is transparent to the user of the API and
prevents manual duplication of code or data structures.

(closes issue ASTERISK-23051)
Review: https://reviewboard.asterisk.org/r/3122/
Reported by: Matt Jordan
........

Merged revisions 406003 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@406006 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2014-01-21 14:27:21 +00:00

114 lines
3 KiB
Text

{{!
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* David M. Lee, II <dlee@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.
}}
{{#has_query_parameters}}
for (i = get_params; i; i = i->next) {
{{#query_parameters}}
if (strcmp(i->name, "{{name}}") == 0) {
{{^allow_multiple}}
args.{{c_name}} = {{c_convert}}(i->value);
{{/allow_multiple}}
{{#allow_multiple}}
/* Parse comma separated list */
char *vals[MAX_VALS];
size_t j;
args.{{c_name}}_parse = ast_strdup(i->value);
if (!args.{{c_name}}_parse) {
ast_ari_response_alloc_failed(response);
goto fin;
}
if (strlen(args.{{c_name}}_parse) == 0) {
/* ast_app_separate_args can't handle "" */
args.{{c_name}}_count = 1;
vals[0] = args.{{c_name}}_parse;
} else {
args.{{c_name}}_count = ast_app_separate_args(
args.{{c_name}}_parse, ',', vals,
ARRAY_LEN(vals));
}
if (args.{{c_name}}_count == 0) {
ast_ari_response_alloc_failed(response);
goto fin;
}
if (args.{{c_name}}_count >= MAX_VALS) {
ast_ari_response_error(response, 400,
"Bad Request",
"Too many values for {{c_name}}");
goto fin;
}
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 (j = 0; j < args.{{c_name}}_count; ++j) {
args.{{c_name}}[j] = {{c_convert}}(vals[j]);
}
{{/allow_multiple}}
} else
{{/query_parameters}}
{}
}
{{/has_query_parameters}}
{{#has_path_parameters}}
for (i = path_vars; i; i = i->next) {
{{#path_parameters}}
if (strcmp(i->name, "{{name}}") == 0) {
args.{{c_name}} = {{c_convert}}(i->value);
} else
{{/path_parameters}}
{}
}
{{/has_path_parameters}}
{{^is_websocket}}
{{#parse_body}}
/* Look for a JSON request entity */
body = ast_http_get_json(ser, headers);
if (!body) {
switch (errno) {
case EFBIG:
ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
goto fin;
case ENOMEM:
ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
goto fin;
case EIO:
ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
goto fin;
}
}
{{#body_parameter}}
args.{{c_name}} = ast_json_ref(body);
{{/body_parameter}}
{{^body_parameter}}
if (ast_ari_{{c_name}}_{{c_nickname}}_parse_body(body, &args)) {
ast_ari_response_alloc_failed(response);
goto fin;
}
{{/body_parameter}}
{{/parse_body}}
{{/is_websocket}}