body_parsing.mustache 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. {{!
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * William Kinsey Moore, III <kmoore@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. {{#parse_body}}
  22. int ast_ari_{{c_name}}_{{c_nickname}}_parse_body(
  23. struct ast_json *body,
  24. struct ast_ari_{{c_name}}_{{c_nickname}}_args *args)
  25. {
  26. {{#has_query_parameters}}
  27. struct ast_json *field;
  28. {{/has_query_parameters}}
  29. /* Parse query parameters out of it */
  30. {{#query_parameters}}
  31. {{^is_body_parameter}}
  32. field = ast_json_object_get(body, "{{name}}");
  33. if (field) {
  34. {{^allow_multiple}}
  35. args->{{c_name}} = {{json_convert}}(field);
  36. {{/allow_multiple}}
  37. {{#allow_multiple}}
  38. /* If they were silly enough to both pass in a query param and a
  39. * JSON body, free up the query value.
  40. */
  41. ast_free(args->{{c_name}});
  42. if (ast_json_typeof(field) == AST_JSON_ARRAY) {
  43. /* Multiple param passed as array */
  44. size_t i;
  45. args->{{c_name}}_count = ast_json_array_size(field);
  46. args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count);
  47. if (!args->{{c_name}}) {
  48. return -1;
  49. }
  50. for (i = 0; i < args->{{c_name}}_count; ++i) {
  51. args->{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i));
  52. }
  53. } else {
  54. /* Multiple param passed as single value */
  55. args->{{c_name}}_count = 1;
  56. args->{{c_name}} = ast_malloc(sizeof(*args->{{c_name}}) * args->{{c_name}}_count);
  57. if (!args->{{c_name}}) {
  58. return -1;
  59. }
  60. args->{{c_name}}[0] = {{json_convert}}(field);
  61. }
  62. {{/allow_multiple}}
  63. }
  64. {{/is_body_parameter}}
  65. {{/query_parameters}}
  66. return 0;
  67. }
  68. {{/parse_body}}