summaryrefslogtreecommitdiff
path: root/rest-api-templates/param_parsing.mustache
blob: 9d207386935243c00e6222c9cafcb83ca6418ff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{{!
 * 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}}
	/* 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}}
	}
{{/query_parameters}}
{{/body_parameter}}
{{/parse_body}}
{{/is_websocket}}