Asterisk - The Open Source Telephony Project  18.5.0
res_ari_sounds.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <[email protected]>
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  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21  * !!!!! DO NOT EDIT !!!!!
22  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23  * This file is generated by a mustache template. Please see the original
24  * template in rest-api-templates/res_ari_resource.c.mustache
25  */
26 
27 /*! \file
28  *
29  * \brief Sound resources
30  *
31  * \author David M. Lee, II <[email protected]>
32  */
33 
34 /*** MODULEINFO
35  <depend type="module">res_ari</depend>
36  <depend type="module">res_ari_model</depend>
37  <depend type="module">res_stasis</depend>
38  <support_level>core</support_level>
39  ***/
40 
41 #include "asterisk.h"
42 
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
45 #include "asterisk/stasis_app.h"
46 #include "ari/resource_sounds.h"
47 #if defined(AST_DEVMODE)
49 #endif
50 
51 #define MAX_VALS 128
52 
54  struct ast_json *body,
56 {
57  struct ast_json *field;
58  /* Parse query parameters out of it */
59  field = ast_json_object_get(body, "lang");
60  if (field) {
61  args->lang = ast_json_string_get(field);
62  }
63  field = ast_json_object_get(body, "format");
64  if (field) {
65  args->format = ast_json_string_get(field);
66  }
67  return 0;
68 }
69 
70 /*!
71  * \brief Parameter parsing callback for /sounds.
72  * \param get_params GET parameters in the HTTP request.
73  * \param path_vars Path variables extracted from the request.
74  * \param headers HTTP headers.
75  * \param[out] response Response to the HTTP request.
76  */
78  struct ast_tcptls_session_instance *ser,
79  struct ast_variable *get_params, struct ast_variable *path_vars,
80  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
81 {
82  struct ast_ari_sounds_list_args args = {};
83  struct ast_variable *i;
84 #if defined(AST_DEVMODE)
85  int is_valid;
86  int code;
87 #endif /* AST_DEVMODE */
88 
89  for (i = get_params; i; i = i->next) {
90  if (strcmp(i->name, "lang") == 0) {
91  args.lang = (i->value);
92  } else
93  if (strcmp(i->name, "format") == 0) {
94  args.format = (i->value);
95  } else
96  {}
97  }
98  if (ast_ari_sounds_list_parse_body(body, &args)) {
100  goto fin;
101  }
102  ast_ari_sounds_list(headers, &args, response);
103 #if defined(AST_DEVMODE)
104  code = response->response_code;
105 
106  switch (code) {
107  case 0: /* Implementation is still a stub, or the code wasn't set */
108  is_valid = response->message == NULL;
109  break;
110  case 500: /* Internal Server Error */
111  case 501: /* Not Implemented */
112  is_valid = 1;
113  break;
114  default:
115  if (200 <= code && code <= 299) {
116  is_valid = ast_ari_validate_list(response->message,
118  } else {
119  ast_log(LOG_ERROR, "Invalid error response %d for /sounds\n", code);
120  is_valid = 0;
121  }
122  }
123 
124  if (!is_valid) {
125  ast_log(LOG_ERROR, "Response validation failed for /sounds\n");
126  ast_ari_response_error(response, 500,
127  "Internal Server Error", "Response validation failed");
128  }
129 #endif /* AST_DEVMODE */
130 
131 fin: __attribute__((unused))
132  return;
133 }
134 /*!
135  * \brief Parameter parsing callback for /sounds/{soundId}.
136  * \param get_params GET parameters in the HTTP request.
137  * \param path_vars Path variables extracted from the request.
138  * \param headers HTTP headers.
139  * \param[out] response Response to the HTTP request.
140  */
142  struct ast_tcptls_session_instance *ser,
143  struct ast_variable *get_params, struct ast_variable *path_vars,
144  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
145 {
146  struct ast_ari_sounds_get_args args = {};
147  struct ast_variable *i;
148 #if defined(AST_DEVMODE)
149  int is_valid;
150  int code;
151 #endif /* AST_DEVMODE */
152 
153  for (i = path_vars; i; i = i->next) {
154  if (strcmp(i->name, "soundId") == 0) {
155  args.sound_id = (i->value);
156  } else
157  {}
158  }
159  ast_ari_sounds_get(headers, &args, response);
160 #if defined(AST_DEVMODE)
161  code = response->response_code;
162 
163  switch (code) {
164  case 0: /* Implementation is still a stub, or the code wasn't set */
165  is_valid = response->message == NULL;
166  break;
167  case 500: /* Internal Server Error */
168  case 501: /* Not Implemented */
169  is_valid = 1;
170  break;
171  default:
172  if (200 <= code && code <= 299) {
173  is_valid = ast_ari_validate_sound(
174  response->message);
175  } else {
176  ast_log(LOG_ERROR, "Invalid error response %d for /sounds/{soundId}\n", code);
177  is_valid = 0;
178  }
179  }
180 
181  if (!is_valid) {
182  ast_log(LOG_ERROR, "Response validation failed for /sounds/{soundId}\n");
183  ast_ari_response_error(response, 500,
184  "Internal Server Error", "Response validation failed");
185  }
186 #endif /* AST_DEVMODE */
187 
188 fin: __attribute__((unused))
189  return;
190 }
191 
192 /*! \brief REST handler for /api-docs/sounds.json */
194  .path_segment = "soundId",
195  .is_wildcard = 1,
196  .callbacks = {
198  },
199  .num_children = 0,
200  .children = { }
201 };
202 /*! \brief REST handler for /api-docs/sounds.json */
203 static struct stasis_rest_handlers sounds = {
204  .path_segment = "sounds",
205  .callbacks = {
207  },
208  .num_children = 1,
209  .children = { &sounds_soundId, }
210 };
211 
212 static int unload_module(void)
213 {
214  ast_ari_remove_handler(&sounds);
215  return 0;
216 }
217 
218 static int load_module(void)
219 {
220  int res = 0;
221 
222 
223  res |= ast_ari_add_handler(&sounds);
224  if (res) {
225  unload_module();
227  }
228 
230 }
231 
232 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Sound resources",
233  .support_level = AST_MODULE_SUPPORT_CORE,
234  .load = load_module,
235  .unload = unload_module,
236  .requires = "res_ari,res_ari_model,res_stasis",
237 );
struct ast_variable * next
Asterisk main include file. File version handling, generic pbx functions.
void ast_ari_sounds_get(struct ast_variable *headers, struct ast_ari_sounds_get_args *args, struct ast_ari_response *response)
Get a sound&#39;s details.
void ast_ari_sounds_list(struct ast_variable *headers, struct ast_ari_sounds_list_args *args, struct ast_ari_response *response)
List all sounds.
Structure for variables, used for configurations and for channel variables.
Generated file - declares stubs to be implemented in res/ari/resource_sounds.c.
Generated file - Build validators for ARI model objects.
static struct stasis_rest_handlers sounds_soundId
REST handler for /api-docs/sounds.json.
const char * args
static void ast_ari_sounds_get_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /sounds/{soundId}.
#define NULL
Definition: resample.c:96
static void ast_ari_sounds_list_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /sounds.
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
static int unload_module(void)
int ast_ari_validate_sound(struct ast_json *json)
Validator for Sound.
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
describes a server instance
Definition: tcptls.h:149
static struct stasis_rest_handlers sounds
REST handler for /api-docs/sounds.json.
#define LOG_ERROR
Definition: logger.h:285
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct ast_json * message
Definition: ari.h:93
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
const char * path_segment
Definition: ari.h:70
ari_validator ast_ari_validate_sound_fn(void)
Function pointer to ast_ari_validate_sound().
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).
Stasis Application API. See Stasis Application API for detailed documentation.
static int load_module(void)
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_ari_sounds_list_parse_body(struct ast_json *body, struct ast_ari_sounds_list_args *args)
Body parsing function for /sounds.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
Handler for a single RESTful path segment.
Definition: ari.h:68