diff options
author | David M. Lee <dlee@digium.com> | 2013-07-03 16:32:41 +0000 |
---|---|---|
committer | David M. Lee <dlee@digium.com> | 2013-07-03 16:32:41 +0000 |
commit | c9a3d4562ddb1ed5b34f7d5530efd6aa695377c2 (patch) | |
tree | dd082285fbb5c7714164e26145acc5c966e663be /res/res_stasis_http_channels.c | |
parent | dcf03554a0b38806bf1fe258acc423b070533d6e (diff) |
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
Diffstat (limited to 'res/res_stasis_http_channels.c')
-rw-r--r-- | res/res_stasis_http_channels.c | 439 |
1 files changed, 413 insertions, 26 deletions
diff --git a/res/res_stasis_http_channels.c b/res/res_stasis_http_channels.c index c865b3931..ebcc9e880 100644 --- a/res/res_stasis_http_channels.c +++ b/res/res_stasis_http_channels.c @@ -44,6 +44,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" #include "asterisk/stasis_app.h" #include "stasis_http/resource_channels.h" +#if defined(AST_DEVMODE) +#include "stasis_http/ari_model_validators.h" +#endif /*! * \brief Parameter parsing callback for /channels. @@ -53,11 +56,39 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") * \param[out] response Response to the HTTP request. */ static void stasis_http_get_channels_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_get_channels_args args = {}; stasis_http_get_channels(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_list(response->message, + ari_validate_channel); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels. @@ -67,9 +98,14 @@ static void stasis_http_get_channels_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_originate_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_originate_args args = {}; struct ast_variable *i; @@ -101,6 +137,29 @@ static void stasis_http_originate_cb( {} } stasis_http_originate(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}. @@ -110,9 +169,14 @@ static void stasis_http_originate_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_get_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_get_channel_args args = {}; struct ast_variable *i; @@ -123,6 +187,30 @@ static void stasis_http_get_channel_cb( {} } stasis_http_get_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_channel( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}. @@ -132,9 +220,14 @@ static void stasis_http_get_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_delete_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_delete_channel_args args = {}; struct ast_variable *i; @@ -145,6 +238,30 @@ static void stasis_http_delete_channel_cb( {} } stasis_http_delete_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/dial. @@ -154,9 +271,14 @@ static void stasis_http_delete_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_dial_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_dial_args args = {}; struct ast_variable *i; @@ -182,6 +304,31 @@ static void stasis_http_dial_cb( {} } stasis_http_dial(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_dialed( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/dial\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/dial\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/continue. @@ -191,9 +338,14 @@ static void stasis_http_dial_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_continue_in_dialplan_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_continue_in_dialplan_args args = {}; struct ast_variable *i; @@ -216,6 +368,31 @@ static void stasis_http_continue_in_dialplan_cb( {} } stasis_http_continue_in_dialplan(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/continue\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/continue\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/answer. @@ -225,9 +402,14 @@ static void stasis_http_continue_in_dialplan_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_answer_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_answer_channel_args args = {}; struct ast_variable *i; @@ -238,6 +420,31 @@ static void stasis_http_answer_channel_cb( {} } stasis_http_answer_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/answer\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/answer\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/mute. @@ -247,9 +454,14 @@ static void stasis_http_answer_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_mute_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_mute_channel_args args = {}; struct ast_variable *i; @@ -266,6 +478,31 @@ static void stasis_http_mute_channel_cb( {} } stasis_http_mute_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/mute\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/mute\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/unmute. @@ -275,9 +512,14 @@ static void stasis_http_mute_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_unmute_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_unmute_channel_args args = {}; struct ast_variable *i; @@ -294,6 +536,31 @@ static void stasis_http_unmute_channel_cb( {} } stasis_http_unmute_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/unmute\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/unmute\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/hold. @@ -303,9 +570,14 @@ static void stasis_http_unmute_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_hold_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_hold_channel_args args = {}; struct ast_variable *i; @@ -316,6 +588,31 @@ static void stasis_http_hold_channel_cb( {} } stasis_http_hold_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/hold\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/hold\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/unhold. @@ -325,9 +622,14 @@ static void stasis_http_hold_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_unhold_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_unhold_channel_args args = {}; struct ast_variable *i; @@ -338,6 +640,31 @@ static void stasis_http_unhold_channel_cb( {} } stasis_http_unhold_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/unhold\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/unhold\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/play. @@ -347,9 +674,14 @@ static void stasis_http_unhold_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_play_on_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_play_on_channel_args args = {}; struct ast_variable *i; @@ -375,6 +707,31 @@ static void stasis_http_play_on_channel_cb( {} } stasis_http_play_on_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel not in a Stasis application */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_playback( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/play\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/play\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! * \brief Parameter parsing callback for /channels/{channelId}/record. @@ -384,9 +741,14 @@ static void stasis_http_play_on_channel_cb( * \param[out] response Response to the HTTP request. */ static void stasis_http_record_channel_cb( - struct ast_variable *get_params, struct ast_variable *path_vars, - struct ast_variable *headers, struct stasis_http_response *response) + struct ast_variable *get_params, struct ast_variable *path_vars, + struct ast_variable *headers, struct stasis_http_response *response) { +#if defined(AST_DEVMODE) + int is_valid; + int code; +#endif /* AST_DEVMODE */ + struct ast_record_channel_args args = {}; struct ast_variable *i; @@ -421,6 +783,31 @@ static void stasis_http_record_channel_cb( {} } stasis_http_record_channel(headers, &args, response); +#if defined(AST_DEVMODE) + code = response->response_code; + + switch (code) { + case 500: /* Internal server error */ + case 404: /* Channel not found */ + case 409: /* Channel is not in a Stasis application, or the channel is currently bridged with other channels. */ + is_valid = 1; + break; + default: + if (200 <= code && code <= 299) { + is_valid = ari_validate_void( + response->message); + } else { + ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/record\n", code); + is_valid = 0; + } + } + + if (!is_valid) { + ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/record\n"); + stasis_http_response_error(response, 500, + "Internal Server Error", "Response validation failed"); + } +#endif /* AST_DEVMODE */ } /*! \brief REST handler for /api-docs/channels.{format} */ |