res_ari_sounds.c 6.2 KB

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