param_parsing.mustache 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {{!
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 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. * Snippet for decoding parameters into an _args struct.
  20. }}
  21. {{#has_query_parameters}}
  22. for (i = get_params; i; i = i->next) {
  23. {{#query_parameters}}
  24. if (strcmp(i->name, "{{name}}") == 0) {
  25. {{^allow_multiple}}
  26. args.{{c_name}} = {{c_convert}}(i->value);
  27. {{/allow_multiple}}
  28. {{#allow_multiple}}
  29. /* Parse comma separated list */
  30. char *vals[MAX_VALS];
  31. size_t j;
  32. args.{{c_name}}_parse = ast_strdup(i->value);
  33. if (!args.{{c_name}}_parse) {
  34. ast_ari_response_alloc_failed(response);
  35. goto fin;
  36. }
  37. if (strlen(args.{{c_name}}_parse) == 0) {
  38. /* ast_app_separate_args can't handle "" */
  39. args.{{c_name}}_count = 1;
  40. vals[0] = args.{{c_name}}_parse;
  41. } else {
  42. args.{{c_name}}_count = ast_app_separate_args(
  43. args.{{c_name}}_parse, ',', vals,
  44. ARRAY_LEN(vals));
  45. }
  46. if (args.{{c_name}}_count == 0) {
  47. ast_ari_response_alloc_failed(response);
  48. goto fin;
  49. }
  50. if (args.{{c_name}}_count >= MAX_VALS) {
  51. ast_ari_response_error(response, 400,
  52. "Bad Request",
  53. "Too many values for {{c_name}}");
  54. goto fin;
  55. }
  56. args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count);
  57. if (!args.{{c_name}}) {
  58. ast_ari_response_alloc_failed(response);
  59. goto fin;
  60. }
  61. for (j = 0; j < args.{{c_name}}_count; ++j) {
  62. args.{{c_name}}[j] = {{c_convert}}(vals[j]);
  63. }
  64. {{/allow_multiple}}
  65. } else
  66. {{/query_parameters}}
  67. {}
  68. }
  69. {{/has_query_parameters}}
  70. {{#has_path_parameters}}
  71. for (i = path_vars; i; i = i->next) {
  72. {{#path_parameters}}
  73. if (strcmp(i->name, "{{name}}") == 0) {
  74. args.{{c_name}} = {{c_convert}}(i->value);
  75. } else
  76. {{/path_parameters}}
  77. {}
  78. }
  79. {{/has_path_parameters}}
  80. {{^is_websocket}}
  81. {{#parse_body}}
  82. {{#body_parameter}}
  83. args.{{c_name}} = body;
  84. {{/body_parameter}}
  85. {{^body_parameter}}
  86. if (ast_ari_{{c_name}}_{{c_nickname}}_parse_body(body, &args)) {
  87. ast_ari_response_alloc_failed(response);
  88. goto fin;
  89. }
  90. {{/body_parameter}}
  91. {{/parse_body}}
  92. {{/is_websocket}}