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"
36 void ast_ari_endpoints_list(struct ast_variable *headers,
37 struct ast_ari_endpoints_list_args *args,
38 struct ast_ari_response *response)
40 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
41 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
42 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
43 struct ao2_iterator i;
46 cache = ast_endpoint_cache();
48 ast_ari_response_error(
49 response, 500, "Internal Server Error",
50 "Message bus not initialized");
55 snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type());
57 ast_ari_response_alloc_failed(response);
61 json = ast_json_array_create();
63 ast_ari_response_alloc_failed(response);
67 i = ao2_iterator_init(snapshots, 0);
68 while ((obj = ao2_iterator_next(&i))) {
69 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
70 struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
71 int r = ast_json_array_append(
72 json, ast_endpoint_snapshot_to_json(snapshot));
74 ast_ari_response_alloc_failed(response);
78 ao2_iterator_destroy(&i);
80 ast_ari_response_ok(response, ast_json_ref(json));
82 void ast_ari_endpoints_list_by_tech(struct ast_variable *headers,
83 struct ast_ari_endpoints_list_by_tech_args *args,
84 struct ast_ari_response *response)
86 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
87 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
88 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
89 struct ao2_iterator i;
92 cache = ast_endpoint_cache();
94 ast_ari_response_error(
95 response, 500, "Internal Server Error",
96 "Message bus not initialized");
101 snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type());
103 ast_ari_response_alloc_failed(response);
107 json = ast_json_array_create();
109 ast_ari_response_alloc_failed(response);
113 i = ao2_iterator_init(snapshots, 0);
114 while ((obj = ao2_iterator_next(&i))) {
115 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
116 struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
119 if (strcmp(args->tech, snapshot->tech) != 0) {
123 r = ast_json_array_append(
124 json, ast_endpoint_snapshot_to_json(snapshot));
126 ast_ari_response_alloc_failed(response);
130 ao2_iterator_destroy(&i);
132 if (ast_json_array_size(json)) {
133 ast_ari_response_ok(response, ast_json_ref(json));
135 ast_ari_response_error(response, 404, "Not Found",
136 "No Endpoints found with tech %s", args->tech);
139 void ast_ari_endpoints_get(struct ast_variable *headers,
140 struct ast_ari_endpoints_get_args *args,
141 struct ast_ari_response *response)
143 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
144 RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
146 snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
148 ast_ari_response_error(response, 404, "Not Found",
149 "Endpoint not found");
153 json = ast_endpoint_snapshot_to_json(snapshot);
155 ast_ari_response_alloc_failed(response);
159 ast_ari_response_ok(response, ast_json_ref(json));