2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
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.
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.
21 * \brief /api-docs/applications.{format} implementation - Stasis application
24 * \author David M. Lee, II <dlee@digium.com>
29 #include "asterisk/stasis_app.h"
30 #include "resource_applications.h"
32 static int append_json(void *obj, void *arg, int flags)
34 const char *app = obj;
35 struct ast_json *array = arg;
37 ast_json_array_append(array, stasis_app_to_json(app));
42 void ast_ari_applications_list(struct ast_variable *headers,
43 struct ast_ari_applications_list_args *args,
44 struct ast_ari_response *response)
46 RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
47 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
50 apps = stasis_app_get_all();
51 json = ast_json_array_create();
53 ast_ari_response_error(response, 500, "Internal Server Error",
59 count = ao2_container_count(apps);
60 ao2_callback(apps, OBJ_NOLOCK | OBJ_NODATA, append_json, json);
63 if (count != ast_json_array_size(json)) {
64 ast_ari_response_error(response, 500, "Internal Server Error",
69 ast_ari_response_ok(response, ast_json_ref(json));
72 void ast_ari_applications_get(struct ast_variable *headers,
73 struct ast_ari_applications_get_args *args,
74 struct ast_ari_response *response)
76 struct ast_json *json;
78 json = stasis_app_to_json(args->application_name);
81 ast_ari_response_error(response, 404, "Not Found",
82 "Application not found");
86 ast_ari_response_ok(response, json);
89 void ast_ari_applications_subscribe(struct ast_variable *headers,
90 struct ast_ari_applications_subscribe_args *args,
91 struct ast_ari_response *response)
93 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
94 enum stasis_app_subscribe_res res;
96 if (args->event_source_count <= 0) {
97 ast_ari_response_error(response, 400, "Bad Request",
98 "Missing parameter eventSource");
102 if (ast_strlen_zero(args->application_name)) {
103 ast_ari_response_error(response, 400, "Bad Request",
104 "Missing parameter applicationName");
108 res = stasis_app_subscribe(args->application_name, args->event_source,
109 args->event_source_count, &json);
113 ast_ari_response_ok(response, ast_json_ref(json));
115 case STASIS_ASR_APP_NOT_FOUND:
116 ast_ari_response_error(response, 404, "Not Found",
117 "Application not found");
119 case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
120 ast_ari_response_error(response, 422, "Unprocessable Entity",
121 "Event source does not exist");
123 case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
124 ast_ari_response_error(response, 400, "Bad Request",
125 "Invalid event source URI scheme");
127 case STASIS_ASR_INTERNAL_ERROR:
128 ast_ari_response_error(response, 500, "Internal Server Error",
129 "Error processing request");
134 void ast_ari_applications_unsubscribe(struct ast_variable *headers,
135 struct ast_ari_applications_unsubscribe_args *args,
136 struct ast_ari_response *response)
138 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
139 enum stasis_app_subscribe_res res;
141 if (args->event_source_count == 0) {
142 ast_ari_response_error(response, 400, "Bad Request",
143 "Missing parameter eventSource");
147 res = stasis_app_unsubscribe(args->application_name, args->event_source,
148 args->event_source_count, &json);
152 ast_ari_response_ok(response, ast_json_ref(json));
154 case STASIS_ASR_APP_NOT_FOUND:
155 ast_ari_response_error(response, 404, "Not Found",
156 "Application not found");
158 case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
159 ast_ari_response_error(response, 422, "Unprocessable Entity",
160 "Event source was not subscribed to");
162 case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
163 ast_ari_response_error(response, 400, "Bad Request",
164 "Invalid event source URI scheme");
166 case STASIS_ASR_INTERNAL_ERROR:
167 ast_ari_response_error(response, 500, "Internal Server Error",
168 "Error processing request");