ARI - implement allowMultiple for parameters
[asterisk/asterisk.git] / rest-api-templates / param_parsing.mustache
1 {{!
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
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                         args.{{c_name}}_count = ast_app_separate_args(
40                                 args.{{c_name}}_parse, ',', vals, ARRAY_LEN(vals));
41                         if (args.{{c_name}}_count == 0) {
42                                 ast_ari_response_alloc_failed(response);
43                                 goto fin;
44                         }
45
46                         if (args.{{c_name}}_count >= MAX_VALS) {
47                                 ast_ari_response_error(response, 400,
48                                         "Bad Request",
49                                         "Too many values for {{c_name}}");
50                                 goto fin;
51                         }
52
53                         args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
54                         if (!args.{{c_name}}) {
55                                 ast_ari_response_alloc_failed(response);
56                                 goto fin;
57                         }
58
59                         for (j = 0; j < args.{{c_name}}_count; ++j) {
60                                 args.{{c_name}}[j] = {{c_convert}}(vals[j]);
61                         }
62 {{/allow_multiple}}
63                 } else
64 {{/query_parameters}}
65                 {}
66         }
67 {{/has_query_parameters}}
68 {{#has_path_parameters}}
69         for (i = path_vars; i; i = i->next) {
70 {{#path_parameters}}
71                 if (strcmp(i->name, "{{name}}") == 0) {
72                         args.{{c_name}} = {{c_convert}}(i->value);
73                 } else
74 {{/path_parameters}}
75                 {}
76         }
77 {{/has_path_parameters}}