2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 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/endpoints.{format} implementation- Endpoint resources
23 * \author David M. Lee, II <dlee@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "resource_endpoints.h"
32 #include "asterisk/astobj2.h"
33 #include "asterisk/stasis.h"
34 #include "asterisk/stasis_endpoints.h"
35 #include "asterisk/channel.h"
37 void ast_ari_endpoints_list(struct ast_variable *headers,
38 struct ast_ari_endpoints_list_args *args,
39 struct ast_ari_response *response)
41 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
42 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
43 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
44 struct ao2_iterator i;
47 cache = ast_endpoint_cache();
49 ast_ari_response_error(
50 response, 500, "Internal Server Error",
51 "Message bus not initialized");
56 snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type());
58 ast_ari_response_alloc_failed(response);
62 json = ast_json_array_create();
64 ast_ari_response_alloc_failed(response);
68 i = ao2_iterator_init(snapshots, 0);
69 while ((obj = ao2_iterator_next(&i))) {
70 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
71 struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
72 int r = ast_json_array_append(
73 json, ast_endpoint_snapshot_to_json(snapshot));
75 ast_ari_response_alloc_failed(response);
79 ao2_iterator_destroy(&i);
81 ast_ari_response_ok(response, ast_json_ref(json));
83 void ast_ari_endpoints_list_by_tech(struct ast_variable *headers,
84 struct ast_ari_endpoints_list_by_tech_args *args,
85 struct ast_ari_response *response)
87 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
88 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
89 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
90 struct ao2_iterator i;
93 if (!ast_get_channel_tech(args->tech)) {
94 ast_ari_response_error(response, 404, "Not Found",
95 "No Endpoints found - invalid tech %s", args->tech);
99 cache = ast_endpoint_cache();
101 ast_ari_response_error(
102 response, 500, "Internal Server Error",
103 "Message bus not initialized");
108 snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type());
110 ast_ari_response_alloc_failed(response);
114 json = ast_json_array_create();
116 ast_ari_response_alloc_failed(response);
120 i = ao2_iterator_init(snapshots, 0);
121 while ((obj = ao2_iterator_next(&i))) {
122 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
123 struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
126 if (strcasecmp(args->tech, snapshot->tech) != 0) {
130 r = ast_json_array_append(
131 json, ast_endpoint_snapshot_to_json(snapshot));
133 ast_ari_response_alloc_failed(response);
137 ao2_iterator_destroy(&i);
138 ast_ari_response_ok(response, ast_json_ref(json));
140 void ast_ari_endpoints_get(struct ast_variable *headers,
141 struct ast_ari_endpoints_get_args *args,
142 struct ast_ari_response *response)
144 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
145 RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
147 snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
149 ast_ari_response_error(response, 404, "Not Found",
150 "Endpoint not found");
154 json = ast_endpoint_snapshot_to_json(snapshot);
156 ast_ari_response_alloc_failed(response);
160 ast_ari_response_ok(response, ast_json_ref(json));