Asterisk - The Open Source Telephony Project  21.4.1
res_ari_playbacks.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 <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  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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 Playback control resources
30  *
31  * \author David M. Lee, II <dlee@digium.com>
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  <depend type="module">res_stasis_playback</depend>
39  <support_level>core</support_level>
40  ***/
41 
42 #include "asterisk.h"
43 
44 #include "asterisk/app.h"
45 #include "asterisk/module.h"
46 #include "asterisk/stasis_app.h"
47 #include "ari/resource_playbacks.h"
48 #if defined(AST_DEVMODE)
50 #endif
51 
52 #define MAX_VALS 128
53 
54 /*!
55  * \brief Parameter parsing callback for /playbacks/{playbackId}.
56  * \param ser TCP/TLS session object
57  * \param get_params GET parameters in the HTTP request.
58  * \param path_vars Path variables extracted from the request.
59  * \param headers HTTP headers.
60  * \param body
61  * \param[out] response Response to the HTTP request.
62  */
64  struct ast_tcptls_session_instance *ser,
65  struct ast_variable *get_params, struct ast_variable *path_vars,
66  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
67 {
68  struct ast_ari_playbacks_get_args args = {};
69  struct ast_variable *i;
70 #if defined(AST_DEVMODE)
71  int is_valid;
72  int code;
73 #endif /* AST_DEVMODE */
74 
75  for (i = path_vars; i; i = i->next) {
76  if (strcmp(i->name, "playbackId") == 0) {
77  args.playback_id = (i->value);
78  } else
79  {}
80  }
81  ast_ari_playbacks_get(headers, &args, response);
82 #if defined(AST_DEVMODE)
83  code = response->response_code;
84 
85  switch (code) {
86  case 0: /* Implementation is still a stub, or the code wasn't set */
87  is_valid = response->message == NULL;
88  break;
89  case 500: /* Internal Server Error */
90  case 501: /* Not Implemented */
91  case 404: /* The playback cannot be found */
92  is_valid = 1;
93  break;
94  default:
95  if (200 <= code && code <= 299) {
96  is_valid = ast_ari_validate_playback(
97  response->message);
98  } else {
99  ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}\n", code);
100  is_valid = 0;
101  }
102  }
103 
104  if (!is_valid) {
105  ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}\n");
106  ast_ari_response_error(response, 500,
107  "Internal Server Error", "Response validation failed");
108  }
109 #endif /* AST_DEVMODE */
110 
111 fin: __attribute__((unused))
112  return;
113 }
114 /*!
115  * \brief Parameter parsing callback for /playbacks/{playbackId}.
116  * \param ser TCP/TLS session object
117  * \param get_params GET parameters in the HTTP request.
118  * \param path_vars Path variables extracted from the request.
119  * \param headers HTTP headers.
120  * \param body
121  * \param[out] response Response to the HTTP request.
122  */
124  struct ast_tcptls_session_instance *ser,
125  struct ast_variable *get_params, struct ast_variable *path_vars,
126  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
127 {
128  struct ast_ari_playbacks_stop_args args = {};
129  struct ast_variable *i;
130 #if defined(AST_DEVMODE)
131  int is_valid;
132  int code;
133 #endif /* AST_DEVMODE */
134 
135  for (i = path_vars; i; i = i->next) {
136  if (strcmp(i->name, "playbackId") == 0) {
137  args.playback_id = (i->value);
138  } else
139  {}
140  }
141  ast_ari_playbacks_stop(headers, &args, response);
142 #if defined(AST_DEVMODE)
143  code = response->response_code;
144 
145  switch (code) {
146  case 0: /* Implementation is still a stub, or the code wasn't set */
147  is_valid = response->message == NULL;
148  break;
149  case 500: /* Internal Server Error */
150  case 501: /* Not Implemented */
151  case 404: /* The playback cannot be found */
152  is_valid = 1;
153  break;
154  default:
155  if (200 <= code && code <= 299) {
156  is_valid = ast_ari_validate_void(
157  response->message);
158  } else {
159  ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}\n", code);
160  is_valid = 0;
161  }
162  }
163 
164  if (!is_valid) {
165  ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}\n");
166  ast_ari_response_error(response, 500,
167  "Internal Server Error", "Response validation failed");
168  }
169 #endif /* AST_DEVMODE */
170 
171 fin: __attribute__((unused))
172  return;
173 }
175  struct ast_json *body,
176  struct ast_ari_playbacks_control_args *args)
177 {
178  struct ast_json *field;
179  /* Parse query parameters out of it */
180  field = ast_json_object_get(body, "operation");
181  if (field) {
182  args->operation = ast_json_string_get(field);
183  }
184  return 0;
185 }
186 
187 /*!
188  * \brief Parameter parsing callback for /playbacks/{playbackId}/control.
189  * \param ser TCP/TLS session object
190  * \param get_params GET parameters in the HTTP request.
191  * \param path_vars Path variables extracted from the request.
192  * \param headers HTTP headers.
193  * \param body
194  * \param[out] response Response to the HTTP request.
195  */
197  struct ast_tcptls_session_instance *ser,
198  struct ast_variable *get_params, struct ast_variable *path_vars,
199  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
200 {
201  struct ast_ari_playbacks_control_args args = {};
202  struct ast_variable *i;
203 #if defined(AST_DEVMODE)
204  int is_valid;
205  int code;
206 #endif /* AST_DEVMODE */
207 
208  for (i = get_params; i; i = i->next) {
209  if (strcmp(i->name, "operation") == 0) {
210  args.operation = (i->value);
211  } else
212  {}
213  }
214  for (i = path_vars; i; i = i->next) {
215  if (strcmp(i->name, "playbackId") == 0) {
216  args.playback_id = (i->value);
217  } else
218  {}
219  }
220  if (ast_ari_playbacks_control_parse_body(body, &args)) {
222  goto fin;
223  }
224  ast_ari_playbacks_control(headers, &args, response);
225 #if defined(AST_DEVMODE)
226  code = response->response_code;
227 
228  switch (code) {
229  case 0: /* Implementation is still a stub, or the code wasn't set */
230  is_valid = response->message == NULL;
231  break;
232  case 500: /* Internal Server Error */
233  case 501: /* Not Implemented */
234  case 400: /* The provided operation parameter was invalid */
235  case 404: /* The playback cannot be found */
236  case 409: /* The operation cannot be performed in the playback's current state */
237  is_valid = 1;
238  break;
239  default:
240  if (200 <= code && code <= 299) {
241  is_valid = ast_ari_validate_void(
242  response->message);
243  } else {
244  ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}/control\n", code);
245  is_valid = 0;
246  }
247  }
248 
249  if (!is_valid) {
250  ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}/control\n");
251  ast_ari_response_error(response, 500,
252  "Internal Server Error", "Response validation failed");
253  }
254 #endif /* AST_DEVMODE */
255 
256 fin: __attribute__((unused))
257  return;
258 }
259 
260 /*! \brief REST handler for /api-docs/playbacks.json */
262  .path_segment = "control",
263  .callbacks = {
264  [AST_HTTP_POST] = ast_ari_playbacks_control_cb,
265  },
266  .num_children = 0,
267  .children = { }
268 };
269 /*! \brief REST handler for /api-docs/playbacks.json */
271  .path_segment = "playbackId",
272  .is_wildcard = 1,
273  .callbacks = {
274  [AST_HTTP_GET] = ast_ari_playbacks_get_cb,
275  [AST_HTTP_DELETE] = ast_ari_playbacks_stop_cb,
276  },
277  .num_children = 1,
278  .children = { &playbacks_playbackId_control, }
279 };
280 /*! \brief REST handler for /api-docs/playbacks.json */
282  .path_segment = "playbacks",
283  .callbacks = {
284  },
285  .num_children = 1,
286  .children = { &playbacks_playbackId, }
287 };
288 
289 static int unload_module(void)
290 {
291  ast_ari_remove_handler(&playbacks);
292  return 0;
293 }
294 
295 static int load_module(void)
296 {
297  int res = 0;
298 
299 
300  res |= ast_ari_add_handler(&playbacks);
301  if (res) {
302  unload_module();
304  }
305 
307 }
308 
309 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Playback control resources",
310  .support_level = AST_MODULE_SUPPORT_CORE,
311  .load = load_module,
312  .unload = unload_module,
313  .requires = "res_ari,res_ari_model,res_stasis,res_stasis_playback",
314 );
struct ast_variable * next
void ast_ari_playbacks_control(struct ast_variable *headers, struct ast_ari_playbacks_control_args *args, struct ast_ari_response *response)
Control a playback.
Asterisk main include file. File version handling, generic pbx functions.
Generated file - declares stubs to be implemented in res/ari/resource_playbacks.c.
Structure for variables, used for configurations and for channel variables.
Generated file - Build validators for ARI model objects.
static void ast_ari_playbacks_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 /playbacks/{playbackId}.
void ast_ari_playbacks_get(struct ast_variable *headers, struct ast_ari_playbacks_get_args *args, struct ast_ari_response *response)
Get a playback's details.
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 ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
static struct stasis_rest_handlers playbacks_playbackId
REST handler for /api-docs/playbacks.json.
int response_code
Definition: ari.h:99
static struct stasis_rest_handlers playbacks
REST handler for /api-docs/playbacks.json.
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
int ast_ari_validate_playback(struct ast_json *json)
Validator for Playback.
static void ast_ari_playbacks_control_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 /playbacks/{playbackId}/control.
describes a server instance
Definition: tcptls.h:150
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:94
const char * path_segment
Definition: ari.h:71
void ast_ari_playbacks_stop(struct ast_variable *headers, struct ast_ari_playbacks_stop_args *args, struct ast_ari_response *response)
Stop a playback.
int ast_ari_playbacks_control_parse_body(struct ast_json *body, struct ast_ari_playbacks_control_args *args)
Body parsing function for /playbacks/{playbackId}/control.
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
static void ast_ari_playbacks_stop_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 /playbacks/{playbackId}.
static struct stasis_rest_handlers playbacks_playbackId_control
REST handler for /api-docs/playbacks.json.
Abstract JSON element (object, array, string, int, ...).
Stasis Application API. See Stasis Application API for detailed documentation.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
Handler for a single RESTful path segment.
Definition: ari.h:69