Debian Codegraph @

rest-api-templates/body_parsing.mustache

raw
1 {{!
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * William Kinsey Moore, III <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 }}
18 {{!
19 * Snippet for decoding parameters into an _args struct.
20 }}
21 {{#parse_body}}
22 int ast_ari_{{c_name}}_{{c_nickname}}_parse_body(
23 struct ast_json *body,
24 struct ast_ari_{{c_name}}_{{c_nickname}}_args *args)
25 {
26 {{#has_query_parameters}}
27 struct ast_json *field;
28 {{/has_query_parameters}}
29 /* Parse query parameters out of it */
30 {{#query_parameters}}
31 {{^is_body_parameter}}
32 field = ast_json_object_get(body, "{{name}}");
33 if (field) {
34 {{^allow_multiple}}
35 args->{{c_name}} = {{json_convert}}(field);
36 {{/allow_multiple}}
37 {{#allow_multiple}}
38 /* If they were silly enough to both pass in a query param and a
39 * JSON body, free up the query value.
40 */
41 ast_free(args->{{c_name}});
42 if (ast_json_typeof(field) == AST_JSON_ARRAY) {
43 /* Multiple param passed as array */
44 size_t i;
45 args->{{c_name}}_count = ast_json_array_size(field);
46 args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count);
47
48 if (!args->{{c_name}}) {
49 return -1;
50 }
51
52 for (i = 0; i < args->{{c_name}}_count; ++i) {
53 args->{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i));
54 }
55 } else {
56 /* Multiple param passed as single value */
57 args->{{c_name}}_count = 1;
58 args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count);
59 if (!args->{{c_name}}) {
60 return -1;
61 }
62 args->{{c_name}}[0] = {{json_convert}}(field);
63 }
64 {{/allow_multiple}}
65 }
66 {{/is_body_parameter}}
67 {{/query_parameters}}
68 return 0;
69 }
70
71 {{/parse_body}}