| 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 <[email protected]> |
| 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_ari_resource.c.mustache |
| 30 |
*/ |
| 31 |
|
| 32 |
/*! \file |
| 33 |
* |
| 34 |
* \brief {{{description}}} |
| 35 |
* |
| 36 |
* \author {{{author}}} |
| 37 |
*/ |
| 38 |
|
| 39 |
/*** MODULEINFO |
| 40 |
<depend type="module">res_ari</depend> |
| 41 |
<depend type="module">res_ari_model</depend> |
| 42 |
<depend type="module">res_stasis</depend> |
| 43 |
{{#requires_modules}} |
| 44 |
<depend type="module">{{.}}</depend> |
| 45 |
{{/requires_modules}} |
| 46 |
<support_level>core</support_level> |
| 47 |
***/ |
| 48 |
|
| 49 |
#include "asterisk.h" |
| 50 |
|
| 51 |
#include "asterisk/app.h" |
| 52 |
#include "asterisk/module.h" |
| 53 |
#include "asterisk/stasis_app.h" |
| 54 |
#include "ari/resource_{{c_name}}.h" |
| 55 |
#if defined(AST_DEVMODE) |
| 56 |
#include "ari/ari_model_validators.h" |
| 57 |
#endif |
| 58 |
|
| 59 |
#define MAX_VALS 128 |
| 60 |
|
| 61 |
{{#apis}} |
| 62 |
{{#operations}} |
| 63 |
{{#is_req}} |
| 64 |
{{> body_parsing}} |
| 65 |
/*! |
| 66 |
* \brief Parameter parsing callback for {{path}}. |
| 67 |
* \param ser TCP/TLS session object |
| 68 |
* \param get_params GET parameters in the HTTP request. |
| 69 |
* \param path_vars Path variables extracted from the request. |
| 70 |
* \param headers HTTP headers. |
| 71 |
* \param body |
| 72 |
* \param[out] response Response to the HTTP request. |
| 73 |
*/ |
| 74 |
static void ast_ari_{{c_name}}_{{c_nickname}}_cb( |
| 75 |
struct ast_tcptls_session_instance *ser, |
| 76 |
struct ast_variable *get_params, struct ast_variable *path_vars, |
| 77 |
struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response) |
| 78 |
{ |
| 79 |
struct ast_ari_{{c_name}}_{{c_nickname}}_args args = {}; |
| 80 |
{{#has_parameters}} |
| 81 |
struct ast_variable *i; |
| 82 |
{{/has_parameters}} |
| 83 |
#if defined(AST_DEVMODE) |
| 84 |
int is_valid; |
| 85 |
int code; |
| 86 |
#endif /* AST_DEVMODE */ |
| 87 |
|
| 88 |
{{> param_parsing}} |
| 89 |
{{^is_binary_response}} |
| 90 |
ast_ari_{{c_name}}_{{c_nickname}}(headers, &args, response); |
| 91 |
{{/is_binary_response}} |
| 92 |
{{#is_binary_response}} |
| 93 |
ast_ari_{{c_name}}_{{c_nickname}}(ser, headers, &args, response); |
| 94 |
{{/is_binary_response}} |
| 95 |
#if defined(AST_DEVMODE) |
| 96 |
code = response->response_code; |
| 97 |
|
| 98 |
switch (code) { |
| 99 |
case 0: /* Implementation is still a stub, or the code wasn't set */ |
| 100 |
is_valid = response->message == NULL; |
| 101 |
break; |
| 102 |
case 500: /* Internal Server Error */ |
| 103 |
case 501: /* Not Implemented */ |
| 104 |
{{#error_responses}} |
| 105 |
case {{code}}: /* {{{reason}}} */ |
| 106 |
{{/error_responses}} |
| 107 |
is_valid = 1; |
| 108 |
break; |
| 109 |
default: |
| 110 |
if (200 <= code && code <= 299) { |
| 111 |
{{#response_class}} |
| 112 |
{{#is_list}} |
| 113 |
is_valid = ast_ari_validate_list(response->message, |
| 114 |
ast_ari_validate_{{c_singular_name}}_fn()); |
| 115 |
{{/is_list}} |
| 116 |
{{^is_list}} |
| 117 |
{{^is_binary_response}} |
| 118 |
is_valid = ast_ari_validate_{{c_name}}( |
| 119 |
response->message); |
| 120 |
{{/is_binary_response}} |
| 121 |
{{#is_binary_response}} |
| 122 |
/* No validation on a raw binary response */ |
| 123 |
is_valid = 1; |
| 124 |
{{/is_binary_response}} |
| 125 |
{{/is_list}} |
| 126 |
{{/response_class}} |
| 127 |
} else { |
| 128 |
ast_log(LOG_ERROR, "Invalid error response %d for {{path}}\n", code); |
| 129 |
is_valid = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (!is_valid) { |
| 134 |
ast_log(LOG_ERROR, "Response validation failed for {{path}}\n"); |
| 135 |
ast_ari_response_error(response, 500, |
| 136 |
"Internal Server Error", "Response validation failed"); |
| 137 |
} |
| 138 |
#endif /* AST_DEVMODE */ |
| 139 |
|
| 140 |
fin: __attribute__((unused)) |
| 141 |
{{> param_cleanup}} |
| 142 |
return; |
| 143 |
} |
| 144 |
{{/is_req}} |
| 145 |
{{/operations}} |
| 146 |
{{/apis}} |
| 147 |
|
| 148 |
{{! The rest_handler partial expands to the tree of stasis_rest_handlers }} |
| 149 |
{{#root_path}} |
| 150 |
{{> rest_handler}} |
| 151 |
{{/root_path}} |
| 152 |
|
| 153 |
static int unload_module(void) |
| 154 |
{ |
| 155 |
ast_ari_remove_handler(&{{root_full_name}}); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int load_module(void) |
| 160 |
{ |
| 161 |
int res = 0; |
| 162 |
|
| 163 |
{{#apis}} |
| 164 |
{{#operations}} |
| 165 |
{{#is_websocket}} |
| 166 |
{{full_name}}.is_websocket = 1; |
| 167 |
{{/is_websocket}} |
| 168 |
{{/operations}} |
| 169 |
{{/apis}} |
| 170 |
|
| 171 |
res |= ast_ari_add_handler(&{{root_full_name}}); |
| 172 |
if (res) { |
| 173 |
unload_module(); |
| 174 |
return AST_MODULE_LOAD_DECLINE; |
| 175 |
} |
| 176 |
|
| 177 |
return AST_MODULE_LOAD_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - {{{description}}}", |
| 181 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 182 |
.load = load_module, |
| 183 |
.unload = unload_module, |
| 184 |
.requires = "res_ari,res_ari_model,res_stasis{{#requires_modules}},{{.}}{{/requires_modules}}", |
| 185 |
); |
| 186 |
{{/api_declaration}} |