Add channel events for res_stasis apps
[asterisk/asterisk.git] / rest-api-templates / res_stasis_http_resource.c.mustache
1 {{#api_declaration}}
2 /*
3  * Asterisk -- An open source telephony toolkit.
4  *
5  * {{{copyright}}}
6  *
7  * {{{author}}}
8 {{! Template Copyright
9  * Copyright (C) 2013, Digium, Inc.
10  *
11  * David M. Lee, II <dlee@digium.com>
12 }}
13  *
14  * See http://www.asterisk.org for more information about
15  * the Asterisk project. Please do not directly contact
16  * any of the maintainers of this project for assistance;
17  * the project provides a web site, mailing lists and IRC
18  * channels for your use.
19  *
20  * This program is free software, distributed under the terms of
21  * the GNU General Public License Version 2. See the LICENSE file
22  * at the top of the source tree.
23  */
24
25 {{! Template for rendering the res_ module for an HTTP resource. }}
26 /*
27 {{> do-not-edit}}
28  * This file is generated by a mustache template. Please see the original
29  * template in rest-api-templates/res_stasis_http_resource.c.mustache
30  */
31
32 /*! \file
33  *
34  * \brief {{{description}}}
35  *
36  * \author {{{author}}}
37  */
38
39 /*** MODULEINFO
40         <depend type="module">res_stasis_http</depend>
41         <support_level>core</support_level>
42  ***/
43
44 #include "asterisk.h"
45
46 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
47
48 #include "asterisk/module.h"
49 #include "stasis_http/resource_{{name}}.h"
50 {{#has_events}}
51 #include "asterisk/stasis_channels.h"
52 {{/has_events}}
53
54 {{#apis}}
55 {{#operations}}
56 /*!
57  * \brief Parameter parsing callback for {{path}}.
58  * \param get_params GET parameters in the HTTP request.
59  * \param path_vars Path variables extracted from the request.
60  * \param headers HTTP headers.
61  * \param[out] response Response to the HTTP request.
62  */
63 static void stasis_http_{{c_nickname}}_cb(
64     struct ast_variable *get_params, struct ast_variable *path_vars,
65     struct ast_variable *headers, struct stasis_http_response *response)
66 {
67         struct ast_{{c_nickname}}_args args = {};
68 {{#has_parameters}}
69         struct ast_variable *i;
70
71 {{#has_query_parameters}}
72         for (i = get_params; i; i = i->next) {
73 {{#query_parameters}}
74                 if (strcmp(i->name, "{{name}}") == 0) {
75                         args.{{c_name}} = {{c_convert}}(i->value);
76                 } else
77 {{/query_parameters}}
78                 {}
79         }
80 {{/has_query_parameters}}
81 {{#has_path_parameters}}
82         for (i = path_vars; i; i = i->next) {
83 {{#path_parameters}}
84                 if (strcmp(i->name, "{{name}}") == 0) {
85                         args.{{c_name}} = {{c_convert}}(i->value);
86                 } else
87 {{/path_parameters}}
88                 {}
89         }
90 {{/has_path_parameters}}
91 {{/has_parameters}}
92         stasis_http_{{c_nickname}}(headers, &args, response);
93 }
94 {{/operations}}
95 {{/apis}}
96
97 {{! The rest_handler partial expands to the tree of stasis_rest_handlers }}
98 {{#root_path}}
99 {{> rest_handler}}
100 {{/root_path}}
101
102 {{#has_events}}
103 {{#events}}
104 {{> event_function_decl}}
105         )
106 {
107         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
108         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
109 {{#has_properties}}
110         struct ast_json *validator;
111 {{/has_properties}}
112 {{#channel}}
113         int ret;
114 {{/channel}}
115 {{#bridge}}
116 {{^channel}}
117         int ret;
118 {{/channel}}
119 {{/bridge}}
120
121 {{#channel}}
122         ast_assert(channel_snapshot != NULL);
123 {{/channel}}
124 {{#bridge}}
125         ast_assert(bridge_snapshot != NULL);
126 {{/bridge}}
127 {{#has_properties}}
128         ast_assert(blob != NULL);
129 {{#channel}}
130         ast_assert(ast_json_object_get(blob, "channel") == NULL);
131 {{/channel}}
132 {{#bridge}}
133         ast_assert(ast_json_object_get(blob, "bridge") == NULL);
134 {{/bridge}}
135         ast_assert(ast_json_object_get(blob, "type") == NULL);
136 {{#properties}}
137
138         validator = ast_json_object_get(blob, "{{name}}");
139         if (validator) {
140                 /* do validation? XXX */
141 {{#required}}
142         } else {
143                 /* fail message generation if the required parameter doesn't exist */
144                 return NULL;
145 {{/required}}
146         }
147 {{/properties}}
148
149         event = ast_json_deep_copy(blob);
150 {{/has_properties}}
151 {{^has_properties}}
152
153         event = ast_json_object_create();
154 {{/has_properties}}
155         if (!event) {
156                 return NULL;
157         }
158
159 {{#channel}}
160         ret = ast_json_object_set(event,
161                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
162         if (ret) {
163                 return NULL;
164         }
165
166 {{/channel}}
167 {{#bridge}}
168         ret = ast_json_object_set(event,
169                 "bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
170         if (ret) {
171                 return NULL;
172         }
173
174 {{/bridge}}
175         message = ast_json_pack("{s: o}", "{{c_id}}", ast_json_ref(event));
176         if (!message) {
177                 return NULL;
178         }
179
180         return ast_json_ref(message);
181 }
182
183 {{/events}}
184 {{/has_events}}
185 static int load_module(void)
186 {
187         return stasis_http_add_handler(&{{root_full_name}});
188 }
189
190 static int unload_module(void)
191 {
192         stasis_http_remove_handler(&{{root_full_name}});
193         return 0;
194 }
195
196 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,
197         "RESTful API module - {{{description}}}",
198         .load = load_module,
199         .unload = unload_module,
200         .nonoptreq = "res_stasis_http",
201         );
202 {{/api_declaration}}