Debian Codegraph @

res/res_ari_sounds.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 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 /*
20 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27 /*! \file
28 *
29 * \brief Sound resources
30 *
31 * \author David M. Lee, II <[email protected]>
32 */
33
34 /*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <support_level>core</support_level>
39 ***/
40
41 #include "asterisk.h"
42
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
45 #include "asterisk/stasis_app.h"
46 #include "ari/resource_sounds.h"
47 #if defined(AST_DEVMODE)
48 #include "ari/ari_model_validators.h"
49 #endif
50
51 #define MAX_VALS 128
52
53 int ast_ari_sounds_list_parse_body(
54 struct ast_json *body,
55 struct ast_ari_sounds_list_args *args)
56 {
57 struct ast_json *field;
58 /* Parse query parameters out of it */
59 field = ast_json_object_get(body, "lang");
60 if (field) {
61 args->lang = ast_json_string_get(field);
62 }
63 field = ast_json_object_get(body, "format");
64 if (field) {
65 args->format = ast_json_string_get(field);
66 }
67 return 0;
68 }
69
70 /*!
71 * \brief Parameter parsing callback for /sounds.
72 * \param ser TCP/TLS session object
73 * \param get_params GET parameters in the HTTP request.
74 * \param path_vars Path variables extracted from the request.
75 * \param headers HTTP headers.
76 * \param body
77 * \param[out] response Response to the HTTP request.
78 */
79 static void ast_ari_sounds_list_cb(
80 struct ast_tcptls_session_instance *ser,
81 struct ast_variable *get_params, struct ast_variable *path_vars,
82 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
83 {
84 struct ast_ari_sounds_list_args args = {};
85 struct ast_variable *i;
86 #if defined(AST_DEVMODE)
87 int is_valid;
88 int code;
89 #endif /* AST_DEVMODE */
90
91 for (i = get_params; i; i = i->next) {
92 if (strcmp(i->name, "lang") == 0) {
93 args.lang = (i->value);
94 } else
95 if (strcmp(i->name, "format") == 0) {
96 args.format = (i->value);
97 } else
98 {}
99 }
100 if (ast_ari_sounds_list_parse_body(body, &args)) {
101 ast_ari_response_alloc_failed(response);
102 goto fin;
103 }
104 ast_ari_sounds_list(headers, &args, response);
105 #if defined(AST_DEVMODE)
106 code = response->response_code;
107
108 switch (code) {
109 case 0: /* Implementation is still a stub, or the code wasn't set */
110 is_valid = response->message == NULL;
111 break;
112 case 500: /* Internal Server Error */
113 case 501: /* Not Implemented */
114 is_valid = 1;
115 break;
116 default:
117 if (200 <= code && code <= 299) {
118 is_valid = ast_ari_validate_list(response->message,
119 ast_ari_validate_sound_fn());
120 } else {
121 ast_log(LOG_ERROR, "Invalid error response %d for /sounds\n", code);
122 is_valid = 0;
123 }
124 }
125
126 if (!is_valid) {
127 ast_log(LOG_ERROR, "Response validation failed for /sounds\n");
128 ast_ari_response_error(response, 500,
129 "Internal Server Error", "Response validation failed");
130 }
131 #endif /* AST_DEVMODE */
132
133 fin: __attribute__((unused))
134 return;
135 }
136 /*!
137 * \brief Parameter parsing callback for /sounds/{soundId}.
138 * \param ser TCP/TLS session object
139 * \param get_params GET parameters in the HTTP request.
140 * \param path_vars Path variables extracted from the request.
141 * \param headers HTTP headers.
142 * \param body
143 * \param[out] response Response to the HTTP request.
144 */
145 static void ast_ari_sounds_get_cb(
146 struct ast_tcptls_session_instance *ser,
147 struct ast_variable *get_params, struct ast_variable *path_vars,
148 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
149 {
150 struct ast_ari_sounds_get_args args = {};
151 struct ast_variable *i;
152 #if defined(AST_DEVMODE)
153 int is_valid;
154 int code;
155 #endif /* AST_DEVMODE */
156
157 for (i = path_vars; i; i = i->next) {
158 if (strcmp(i->name, "soundId") == 0) {
159 args.sound_id = (i->value);
160 } else
161 {}
162 }
163 ast_ari_sounds_get(headers, &args, response);
164 #if defined(AST_DEVMODE)
165 code = response->response_code;
166
167 switch (code) {
168 case 0: /* Implementation is still a stub, or the code wasn't set */
169 is_valid = response->message == NULL;
170 break;
171 case 500: /* Internal Server Error */
172 case 501: /* Not Implemented */
173 is_valid = 1;
174 break;
175 default:
176 if (200 <= code && code <= 299) {
177 is_valid = ast_ari_validate_sound(
178 response->message);
179 } else {
180 ast_log(LOG_ERROR, "Invalid error response %d for /sounds/{soundId}\n", code);
181 is_valid = 0;
182 }
183 }
184
185 if (!is_valid) {
186 ast_log(LOG_ERROR, "Response validation failed for /sounds/{soundId}\n");
187 ast_ari_response_error(response, 500,
188 "Internal Server Error", "Response validation failed");
189 }
190 #endif /* AST_DEVMODE */
191
192 fin: __attribute__((unused))
193 return;
194 }
195
196 /*! \brief REST handler for /api-docs/sounds.json */
197 static struct stasis_rest_handlers sounds_soundId = {
198 .path_segment = "soundId",
199 .is_wildcard = 1,
200 .callbacks = {
201 [AST_HTTP_GET] = ast_ari_sounds_get_cb,
202 },
203 .num_children = 0,
204 .children = { }
205 };
206 /*! \brief REST handler for /api-docs/sounds.json */
207 static struct stasis_rest_handlers sounds = {
208 .path_segment = "sounds",
209 .callbacks = {
210 [AST_HTTP_GET] = ast_ari_sounds_list_cb,
211 },
212 .num_children = 1,
213 .children = { &sounds_soundId, }
214 };
215
216 static int unload_module(void)
217 {
218 ast_ari_remove_handler(&sounds);
219 return 0;
220 }
221
222 static int load_module(void)
223 {
224 int res = 0;
225
226
227 res |= ast_ari_add_handler(&sounds);
228 if (res) {
229 unload_module();
230 return AST_MODULE_LOAD_DECLINE;
231 }
232
233 return AST_MODULE_LOAD_SUCCESS;
234 }
235
236 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Sound resources",
237 .support_level = AST_MODULE_SUPPORT_CORE,
238 .load = load_module,
239 .unload = unload_module,
240 .requires = "res_ari,res_ari_model,res_stasis",
241 );