Debian Codegraph @

rest-api-templates/param_parsing.mustache

raw
1 {{!
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[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 {{#has_query_parameters}}
22 for (i = get_params; i; i = i->next) {
23 {{#query_parameters}}
24 if (strcmp(i->name, "{{name}}") == 0) {
25 {{^allow_multiple}}
26 args.{{c_name}} = {{c_convert}}(i->value);
27 {{/allow_multiple}}
28 {{#allow_multiple}}
29 /* Parse comma separated list */
30 char *vals[MAX_VALS];
31 size_t j;
32
33 args.{{c_name}}_parse = ast_strdup(i->value);
34 if (!args.{{c_name}}_parse) {
35 ast_ari_response_alloc_failed(response);
36 goto fin;
37 }
38
39 if (strlen(args.{{c_name}}_parse) == 0) {
40 /* ast_app_separate_args can't handle "" */
41 args.{{c_name}}_count = 1;
42 vals[0] = args.{{c_name}}_parse;
43 } else {
44 args.{{c_name}}_count = ast_app_separate_args(
45 args.{{c_name}}_parse, ',', vals,
46 ARRAY_LEN(vals));
47 }
48
49 if (args.{{c_name}}_count == 0) {
50 ast_ari_response_alloc_failed(response);
51 goto fin;
52 }
53
54 if (args.{{c_name}}_count >= MAX_VALS) {
55 ast_ari_response_error(response, 400,
56 "Bad Request",
57 "Too many values for {{c_name}}");
58 goto fin;
59 }
60
61 args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
62 if (!args.{{c_name}}) {
63 ast_ari_response_alloc_failed(response);
64 goto fin;
65 }
66
67 for (j = 0; j < args.{{c_name}}_count; ++j) {
68 args.{{c_name}}[j] = {{c_convert}}(vals[j]);
69 }
70 {{/allow_multiple}}
71 } else
72 {{/query_parameters}}
73 {}
74 }
75 {{/has_query_parameters}}
76 {{#has_path_parameters}}
77 for (i = path_vars; i; i = i->next) {
78 {{#path_parameters}}
79 if (strcmp(i->name, "{{name}}") == 0) {
80 args.{{c_name}} = {{c_convert}}(i->value);
81 } else
82 {{/path_parameters}}
83 {}
84 }
85 {{/has_path_parameters}}
86 {{^is_websocket}}
87 {{#parse_body}}
88 {{#body_parameter}}
89 args.{{c_name}} = body;
90 {{/body_parameter}}
91 {{^body_parameter}}
92 if (ast_ari_{{c_name}}_{{c_nickname}}_parse_body(body, &args)) {
93 ast_ari_response_alloc_failed(response);
94 goto fin;
95 }
96 {{/body_parameter}}
97 {{/parse_body}}
98 {{/is_websocket}}