2013-04-22 14:58:53 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 - 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* David M. Lee, II <dlee@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
* !!!!! DO NOT EDIT !!!!!
|
|
|
|
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
* This file is generated by a mustache template. Please see the original
|
2013-07-27 23:11:02 +00:00
|
|
|
* template in rest-api-templates/res_ari_resource.c.mustache
|
2013-04-22 14:58:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Asterisk resources
|
|
|
|
*
|
|
|
|
* \author David M. Lee, II <dlee@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
2013-07-27 23:11:02 +00:00
|
|
|
<depend type="module">res_ari</depend>
|
2013-05-10 17:12:57 +00:00
|
|
|
<depend type="module">res_stasis</depend>
|
2013-04-22 14:58:53 +00:00
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2013-08-02 14:36:32 +00:00
|
|
|
#include "asterisk/app.h"
|
2013-04-22 14:58:53 +00:00
|
|
|
#include "asterisk/module.h"
|
2013-05-10 17:12:57 +00:00
|
|
|
#include "asterisk/stasis_app.h"
|
2013-07-27 23:11:02 +00:00
|
|
|
#include "ari/resource_asterisk.h"
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
2013-07-27 23:11:02 +00:00
|
|
|
#include "ari/ari_model_validators.h"
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
#endif
|
2013-04-22 14:58:53 +00:00
|
|
|
|
2013-08-02 14:36:32 +00:00
|
|
|
#define MAX_VALS 128
|
|
|
|
|
2013-04-22 14:58:53 +00:00
|
|
|
/*!
|
|
|
|
* \brief Parameter parsing callback for /asterisk/info.
|
|
|
|
* \param get_params GET parameters in the HTTP request.
|
|
|
|
* \param path_vars Path variables extracted from the request.
|
|
|
|
* \param headers HTTP headers.
|
|
|
|
* \param[out] response Response to the HTTP request.
|
|
|
|
*/
|
2013-07-27 23:11:02 +00:00
|
|
|
static void ast_ari_get_asterisk_info_cb(
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
struct ast_variable *get_params, struct ast_variable *path_vars,
|
2013-07-27 23:11:02 +00:00
|
|
|
struct ast_variable *headers, struct ast_ari_response *response)
|
2013-04-22 14:58:53 +00:00
|
|
|
{
|
2013-08-02 14:36:32 +00:00
|
|
|
struct ast_get_asterisk_info_args args = {};
|
|
|
|
struct ast_variable *i;
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
int is_valid;
|
|
|
|
int code;
|
|
|
|
#endif /* AST_DEVMODE */
|
|
|
|
|
2013-04-22 14:58:53 +00:00
|
|
|
for (i = get_params; i; i = i->next) {
|
|
|
|
if (strcmp(i->name, "only") == 0) {
|
2013-08-02 14:36:32 +00:00
|
|
|
/* Parse comma separated list */
|
|
|
|
char *vals[MAX_VALS];
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
args.only_parse = ast_strdup(i->value);
|
|
|
|
if (!args.only_parse) {
|
|
|
|
ast_ari_response_alloc_failed(response);
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
|
ARI: WebSocket event cleanup
Stasis events (which get distributed over the ARI WebSocket) are created
by subscribing to the channel_all_cached and bridge_all_cached topics,
filtering out events for channels/bridges currently subscribed to.
There are two issues with that. First was a race condition, where
messages in-flight to the master subscribe-to-all-things topic would get
sent out, even though the events happened before the channel was put
into Stasis. Secondly, as the number of channels and bridges grow in the
system, the work spent filtering messages becomes excessive.
Since r395954, individual channels and bridges have caching topics, and
can be subscribed to individually. This patch takes advantage, so that
channels and bridges are subscribed to on demand, instead of filtering
the global topics.
The one case where filtering is still required is handling BridgeMerge
messages, which are published directly to the bridge_all topic.
Other than the change to how subscriptions work, this patch mostly just
moves code around. Most of the work generating JSON objects from
messages was moved to .to_json handlers on the message types. The
callback functions handling app subscriptions were moved from res_stasis
(b/c they were global to the model) to stasis/app.c (b/c they are local
to the app now).
(closes issue ASTERISK-21969)
Reported by: Matt Jordan
Review: https://reviewboard.asterisk.org/r/2754/
........
Merged revisions 397816 from http://svn.asterisk.org/svn/asterisk/branches/12
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@397820 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-08-27 19:19:36 +00:00
|
|
|
if (strlen(args.only_parse) == 0) {
|
|
|
|
/* ast_app_separate_args can't handle "" */
|
|
|
|
args.only_count = 1;
|
|
|
|
vals[0] = args.only_parse;
|
|
|
|
} else {
|
|
|
|
args.only_count = ast_app_separate_args(
|
|
|
|
args.only_parse, ',', vals,
|
|
|
|
ARRAY_LEN(vals));
|
|
|
|
}
|
|
|
|
|
2013-08-02 14:36:32 +00:00
|
|
|
if (args.only_count == 0) {
|
|
|
|
ast_ari_response_alloc_failed(response);
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.only_count >= MAX_VALS) {
|
|
|
|
ast_ari_response_error(response, 400,
|
|
|
|
"Bad Request",
|
|
|
|
"Too many values for only");
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.only = ast_malloc(sizeof(*args.only) * args.only_count);
|
|
|
|
if (!args.only) {
|
|
|
|
ast_ari_response_alloc_failed(response);
|
|
|
|
goto fin;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < args.only_count; ++j) {
|
|
|
|
args.only[j] = (vals[j]);
|
|
|
|
}
|
2013-04-22 14:58:53 +00:00
|
|
|
} else
|
|
|
|
{}
|
|
|
|
}
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_get_asterisk_info(headers, &args, response);
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
code = response->response_code;
|
|
|
|
|
|
|
|
switch (code) {
|
2013-07-23 14:57:03 +00:00
|
|
|
case 0: /* Implementation is still a stub, or the code wasn't set */
|
|
|
|
is_valid = response->message == NULL;
|
|
|
|
break;
|
|
|
|
case 500: /* Internal Server Error */
|
|
|
|
case 501: /* Not Implemented */
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
is_valid = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (200 <= code && code <= 299) {
|
2013-07-27 23:11:02 +00:00
|
|
|
is_valid = ast_ari_validate_asterisk_info(
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
response->message);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/info\n", code);
|
|
|
|
is_valid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_valid) {
|
|
|
|
ast_log(LOG_ERROR, "Response validation failed for /asterisk/info\n");
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_response_error(response, 500,
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
"Internal Server Error", "Response validation failed");
|
|
|
|
}
|
|
|
|
#endif /* AST_DEVMODE */
|
2013-08-02 14:36:32 +00:00
|
|
|
|
|
|
|
fin: __attribute__((unused))
|
|
|
|
ast_free(args.only_parse);
|
|
|
|
ast_free(args.only);
|
|
|
|
return;
|
2013-04-22 14:58:53 +00:00
|
|
|
}
|
2013-07-08 14:46:20 +00:00
|
|
|
/*!
|
|
|
|
* \brief Parameter parsing callback for /asterisk/variable.
|
|
|
|
* \param get_params GET parameters in the HTTP request.
|
|
|
|
* \param path_vars Path variables extracted from the request.
|
|
|
|
* \param headers HTTP headers.
|
|
|
|
* \param[out] response Response to the HTTP request.
|
|
|
|
*/
|
2013-07-27 23:11:02 +00:00
|
|
|
static void ast_ari_get_global_var_cb(
|
2013-07-10 13:50:48 +00:00
|
|
|
struct ast_variable *get_params, struct ast_variable *path_vars,
|
2013-07-27 23:11:02 +00:00
|
|
|
struct ast_variable *headers, struct ast_ari_response *response)
|
2013-07-08 14:46:20 +00:00
|
|
|
{
|
2013-08-02 14:36:32 +00:00
|
|
|
struct ast_get_global_var_args args = {};
|
|
|
|
struct ast_variable *i;
|
2013-07-10 13:50:48 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
int is_valid;
|
|
|
|
int code;
|
|
|
|
#endif /* AST_DEVMODE */
|
|
|
|
|
2013-07-08 14:46:20 +00:00
|
|
|
for (i = get_params; i; i = i->next) {
|
|
|
|
if (strcmp(i->name, "variable") == 0) {
|
|
|
|
args.variable = (i->value);
|
|
|
|
} else
|
|
|
|
{}
|
|
|
|
}
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_get_global_var(headers, &args, response);
|
2013-07-10 13:50:48 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
code = response->response_code;
|
|
|
|
|
|
|
|
switch (code) {
|
2013-07-23 14:57:03 +00:00
|
|
|
case 0: /* Implementation is still a stub, or the code wasn't set */
|
|
|
|
is_valid = response->message == NULL;
|
|
|
|
break;
|
|
|
|
case 500: /* Internal Server Error */
|
|
|
|
case 501: /* Not Implemented */
|
2013-08-21 16:23:59 +00:00
|
|
|
case 400: /* Missing variable parameter. */
|
2013-07-10 13:50:48 +00:00
|
|
|
is_valid = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (200 <= code && code <= 299) {
|
2013-07-27 23:11:02 +00:00
|
|
|
is_valid = ast_ari_validate_variable(
|
2013-07-10 13:50:48 +00:00
|
|
|
response->message);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/variable\n", code);
|
|
|
|
is_valid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_valid) {
|
|
|
|
ast_log(LOG_ERROR, "Response validation failed for /asterisk/variable\n");
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_response_error(response, 500,
|
2013-07-10 13:50:48 +00:00
|
|
|
"Internal Server Error", "Response validation failed");
|
|
|
|
}
|
|
|
|
#endif /* AST_DEVMODE */
|
2013-08-02 14:36:32 +00:00
|
|
|
|
|
|
|
fin: __attribute__((unused))
|
|
|
|
return;
|
2013-07-08 14:46:20 +00:00
|
|
|
}
|
|
|
|
/*!
|
|
|
|
* \brief Parameter parsing callback for /asterisk/variable.
|
|
|
|
* \param get_params GET parameters in the HTTP request.
|
|
|
|
* \param path_vars Path variables extracted from the request.
|
|
|
|
* \param headers HTTP headers.
|
|
|
|
* \param[out] response Response to the HTTP request.
|
|
|
|
*/
|
2013-07-27 23:11:02 +00:00
|
|
|
static void ast_ari_set_global_var_cb(
|
2013-07-10 13:50:48 +00:00
|
|
|
struct ast_variable *get_params, struct ast_variable *path_vars,
|
2013-07-27 23:11:02 +00:00
|
|
|
struct ast_variable *headers, struct ast_ari_response *response)
|
2013-07-08 14:46:20 +00:00
|
|
|
{
|
2013-08-02 14:36:32 +00:00
|
|
|
struct ast_set_global_var_args args = {};
|
|
|
|
struct ast_variable *i;
|
2013-07-10 13:50:48 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
int is_valid;
|
|
|
|
int code;
|
|
|
|
#endif /* AST_DEVMODE */
|
|
|
|
|
2013-07-08 14:46:20 +00:00
|
|
|
for (i = get_params; i; i = i->next) {
|
|
|
|
if (strcmp(i->name, "variable") == 0) {
|
|
|
|
args.variable = (i->value);
|
|
|
|
} else
|
|
|
|
if (strcmp(i->name, "value") == 0) {
|
|
|
|
args.value = (i->value);
|
|
|
|
} else
|
|
|
|
{}
|
|
|
|
}
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_set_global_var(headers, &args, response);
|
2013-07-10 13:50:48 +00:00
|
|
|
#if defined(AST_DEVMODE)
|
|
|
|
code = response->response_code;
|
|
|
|
|
|
|
|
switch (code) {
|
2013-07-23 14:57:03 +00:00
|
|
|
case 0: /* Implementation is still a stub, or the code wasn't set */
|
|
|
|
is_valid = response->message == NULL;
|
|
|
|
break;
|
|
|
|
case 500: /* Internal Server Error */
|
|
|
|
case 501: /* Not Implemented */
|
2013-08-21 16:23:59 +00:00
|
|
|
case 400: /* Missing variable parameter. */
|
2013-07-10 13:50:48 +00:00
|
|
|
is_valid = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (200 <= code && code <= 299) {
|
2013-07-27 23:11:02 +00:00
|
|
|
is_valid = ast_ari_validate_void(
|
2013-07-10 13:50:48 +00:00
|
|
|
response->message);
|
|
|
|
} else {
|
|
|
|
ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/variable\n", code);
|
|
|
|
is_valid = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_valid) {
|
|
|
|
ast_log(LOG_ERROR, "Response validation failed for /asterisk/variable\n");
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_response_error(response, 500,
|
2013-07-10 13:50:48 +00:00
|
|
|
"Internal Server Error", "Response validation failed");
|
|
|
|
}
|
|
|
|
#endif /* AST_DEVMODE */
|
2013-08-02 14:36:32 +00:00
|
|
|
|
|
|
|
fin: __attribute__((unused))
|
|
|
|
return;
|
2013-07-08 14:46:20 +00:00
|
|
|
}
|
2013-04-22 14:58:53 +00:00
|
|
|
|
|
|
|
/*! \brief REST handler for /api-docs/asterisk.{format} */
|
|
|
|
static struct stasis_rest_handlers asterisk_info = {
|
|
|
|
.path_segment = "info",
|
|
|
|
.callbacks = {
|
2013-07-27 23:11:02 +00:00
|
|
|
[AST_HTTP_GET] = ast_ari_get_asterisk_info_cb,
|
2013-04-22 14:58:53 +00:00
|
|
|
},
|
|
|
|
.num_children = 0,
|
|
|
|
.children = { }
|
|
|
|
};
|
|
|
|
/*! \brief REST handler for /api-docs/asterisk.{format} */
|
2013-07-08 14:46:20 +00:00
|
|
|
static struct stasis_rest_handlers asterisk_variable = {
|
|
|
|
.path_segment = "variable",
|
|
|
|
.callbacks = {
|
2013-07-27 23:11:02 +00:00
|
|
|
[AST_HTTP_GET] = ast_ari_get_global_var_cb,
|
|
|
|
[AST_HTTP_POST] = ast_ari_set_global_var_cb,
|
2013-07-08 14:46:20 +00:00
|
|
|
},
|
|
|
|
.num_children = 0,
|
|
|
|
.children = { }
|
|
|
|
};
|
|
|
|
/*! \brief REST handler for /api-docs/asterisk.{format} */
|
2013-04-22 14:58:53 +00:00
|
|
|
static struct stasis_rest_handlers asterisk = {
|
|
|
|
.path_segment = "asterisk",
|
|
|
|
.callbacks = {
|
|
|
|
},
|
2013-07-08 14:46:20 +00:00
|
|
|
.num_children = 2,
|
|
|
|
.children = { &asterisk_info,&asterisk_variable, }
|
2013-04-22 14:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
2013-07-03 16:32:00 +00:00
|
|
|
int res = 0;
|
2013-05-10 17:12:57 +00:00
|
|
|
stasis_app_ref();
|
2013-07-27 23:11:02 +00:00
|
|
|
res |= ast_ari_add_handler(&asterisk);
|
2013-07-03 16:32:00 +00:00
|
|
|
return res;
|
2013-04-22 14:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
2013-07-27 23:11:02 +00:00
|
|
|
ast_ari_remove_handler(&asterisk);
|
2013-05-10 17:12:57 +00:00
|
|
|
stasis_app_unref();
|
2013-04-22 14:58:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-24 21:40:52 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Asterisk resources",
|
2013-04-22 14:58:53 +00:00
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
2013-07-27 23:11:02 +00:00
|
|
|
.nonoptreq = "res_ari,res_stasis",
|
2013-04-22 14:58:53 +00:00
|
|
|
);
|