[asterisk-commits] dlee: branch dlee/ASTERISK-22685-json-body r402380 - in /team/dlee/ASTERISK-2...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Nov 1 13:22:12 CDT 2013


Author: dlee
Date: Fri Nov  1 13:22:11 2013
New Revision: 402380

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=402380
Log:
Simple exampleof using a JSON request body

Modified:
    team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.c
    team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.h
    team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c
    team/dlee/ASTERISK-22685-json-body/rest-api-templates/asterisk_processor.py
    team/dlee/ASTERISK-22685-json-body/rest-api/api-docs/asterisk.json

Modified: team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.c?view=diff&rev=402380&r1=402379&r2=402380
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.c (original)
+++ team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.c Fri Nov  1 13:22:11 2013
@@ -36,6 +36,18 @@
 #include "asterisk/paths.h"
 #include "asterisk/pbx.h"
 #include "resource_asterisk.h"
+
+void ast_ari_asterisk_echo(struct ast_variable *headers,
+	struct ast_asterisk_echo_args *args,
+	struct ast_ari_response *response)
+{
+	if (!args->anything) {
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Missing request body");
+		return;
+	}
+	ast_ari_response_ok(response, ast_json_ref(args->anything));
+}
 
 void ast_ari_get_asterisk_info(struct ast_variable *headers,
 	struct ast_get_asterisk_info_args *args,

Modified: team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.h?view=diff&rev=402380&r1=402379&r2=402380
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.h (original)
+++ team/dlee/ASTERISK-22685-json-body/res/ari/resource_asterisk.h Fri Nov  1 13:22:11 2013
@@ -39,6 +39,19 @@
 
 #include "asterisk/ari.h"
 
+/*! \brief Argument struct for ast_ari_asterisk_echo() */
+struct ast_asterisk_echo_args {
+	/*! \brief Content to echo back */
+	struct ast_json *anything;
+};
+/*!
+ * \brief Returns your input.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_asterisk_echo(struct ast_variable *headers, struct ast_asterisk_echo_args *args, struct ast_ari_response *response);
 /*! \brief Argument struct for ast_ari_get_asterisk_info() */
 struct ast_get_asterisk_info_args {
 	/*! \brief Array of Filter information returned */

Modified: team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c?view=diff&rev=402380&r1=402379&r2=402380
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c (original)
+++ team/dlee/ASTERISK-22685-json-body/res/res_ari_asterisk.c Fri Nov  1 13:22:11 2013
@@ -51,6 +51,56 @@
 
 #define MAX_VALS 128
 
+/*!
+ * \brief Parameter parsing callback for /asterisk/echo.
+ * \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.
+ */
+static void ast_ari_asterisk_echo_cb(
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_ari_response *response)
+{
+	struct ast_asterisk_echo_args args = {};
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	ast_ari_asterisk_echo(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	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 */
+	case 400: /* Missing request body */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_object(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/echo\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /asterisk/echo\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
 /*!
  * \brief Parameter parsing callback for /asterisk/info.
  * \param get_params GET parameters in the HTTP request.
@@ -267,6 +317,15 @@
 	return;
 }
 
+/*! \brief REST handler for /api-docs/asterisk.{format} */
+static struct stasis_rest_handlers asterisk_echo = {
+	.path_segment = "echo",
+	.callbacks = {
+		[AST_HTTP_POST] = ast_ari_asterisk_echo_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
 /*! \brief REST handler for /api-docs/asterisk.{format} */
 static struct stasis_rest_handlers asterisk_info = {
 	.path_segment = "info",
@@ -291,8 +350,8 @@
 	.path_segment = "asterisk",
 	.callbacks = {
 	},
-	.num_children = 2,
-	.children = { &asterisk_info,&asterisk_variable, }
+	.num_children = 3,
+	.children = { &asterisk_echo,&asterisk_info,&asterisk_variable, }
 };
 
 static int load_module(void)

Modified: team/dlee/ASTERISK-22685-json-body/rest-api-templates/asterisk_processor.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/rest-api-templates/asterisk_processor.py?view=diff&rev=402380&r1=402379&r2=402380
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/rest-api-templates/asterisk_processor.py (original)
+++ team/dlee/ASTERISK-22685-json-body/rest-api-templates/asterisk_processor.py Fri Nov  1 13:22:11 2013
@@ -191,13 +191,18 @@
         operation.wiki_notes = wikify(operation.notes or "")
 
     def process_parameter(self, parameter, context):
-        if not parameter.data_type in self.type_mapping:
-            raise SwaggerError(
-                "Invalid parameter type %s" % parameter.data_type, context)
+        if parameter.param_type == 'body':
+            parameter.c_data_type = 'struct ast_json *'
+        else:
+            if not parameter.data_type in self.type_mapping:
+                raise SwaggerError(
+                    "Invalid parameter type %s" % parameter.data_type, context)
+            # Type conversions
+            parameter.c_data_type = self.type_mapping[parameter.data_type]
+            parameter.c_convert = self.convert_mapping[parameter.data_type]
+
         # Parameter names are camelcase, Asterisk convention is snake case
         parameter.c_name = snakify(parameter.name)
-        parameter.c_data_type = self.type_mapping[parameter.data_type]
-        parameter.c_convert = self.convert_mapping[parameter.data_type]
         # You shouldn't put a space between 'char *' and the variable
         if parameter.c_data_type.endswith('*'):
             parameter.c_space = ''

Modified: team/dlee/ASTERISK-22685-json-body/rest-api/api-docs/asterisk.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/rest-api/api-docs/asterisk.json?view=diff&rev=402380&r1=402379&r2=402380
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/rest-api/api-docs/asterisk.json (original)
+++ team/dlee/ASTERISK-22685-json-body/rest-api/api-docs/asterisk.json Fri Nov  1 13:22:11 2013
@@ -7,6 +7,33 @@
 	"basePath": "http://localhost:8088/stasis",
 	"resourcePath": "/api-docs/asterisk.{format}",
 	"apis": [
+		{
+			"path": "/asterisk/echo",
+			"description": "Resource to test body parameters",
+			"operations": [
+				{
+					"httpMethod": "POST",
+					"summary": "Returns your input.",
+					"nickname": "asteriskEcho",
+					"responseClass": "object",
+					"parameters": [
+						{
+							"name": "anything",
+							"description": "Content to echo back",
+							"paramType": "body",
+							"required": true,
+							"dataType": "object"
+						}
+					],
+					"errorResponses": [
+						{
+							"code": 400,
+							"reason": "Missing request body"
+						}
+					]
+				}
+			]
+		},
 		{
 			"path": "/asterisk/info",
 			"description": "Asterisk system information (similar to core show settings)",




More information about the asterisk-commits mailing list