diff options
Diffstat (limited to 'rest-api-templates')
-rw-r--r-- | rest-api-templates/asterisk_processor.py | 26 | ||||
-rw-r--r-- | rest-api-templates/param_parsing.mustache | 63 | ||||
-rw-r--r-- | rest-api-templates/res_ari_resource.c.mustache | 7 | ||||
-rw-r--r-- | rest-api-templates/swagger_model.py | 13 |
4 files changed, 102 insertions, 7 deletions
diff --git a/rest-api-templates/asterisk_processor.py b/rest-api-templates/asterisk_processor.py index 18044f57e..7eb5bff6f 100644 --- a/rest-api-templates/asterisk_processor.py +++ b/rest-api-templates/asterisk_processor.py @@ -146,6 +146,15 @@ class AsteriskProcessor(SwaggerPostProcessor): 'boolean': 'ast_true', } + #: JSON conversion functions + json_convert_mapping = { + 'string': 'ast_json_string_get', + 'int': 'ast_json_integer_get', + 'long': 'ast_json_integer_get', + 'double': 'ast_json_real_get', + 'boolean': 'ast_json_is_true', + } + def __init__(self, wiki_prefix): self.wiki_prefix = wiki_prefix @@ -190,15 +199,22 @@ class AsteriskProcessor(SwaggerPostProcessor): raise SwaggerError("Summary should end with .", context) operation.wiki_summary = wikify(operation.summary or "") operation.wiki_notes = wikify(operation.notes or "") + operation.parse_body = (operation.body_parameter or operation.has_query_parameters) and True 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.json_convert = self.json_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 = '' diff --git a/rest-api-templates/param_parsing.mustache b/rest-api-templates/param_parsing.mustache index aabd728fd..9d2073869 100644 --- a/rest-api-templates/param_parsing.mustache +++ b/rest-api-templates/param_parsing.mustache @@ -83,3 +83,66 @@ {} } {{/has_path_parameters}} +{{^is_websocket}} +{{#parse_body}} + /* Look for a JSON request entity */ + body = ast_http_get_json(ser, headers); + if (!body) { + switch (errno) { + case EFBIG: + ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large"); + goto fin; + case ENOMEM: + ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request"); + goto fin; + case EIO: + ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body"); + goto fin; + } + } +{{#body_parameter}} + args.{{c_name}} = ast_json_ref(body); +{{/body_parameter}} +{{^body_parameter}} + /* Parse query parameters out of it */ +{{#query_parameters}} + field = ast_json_object_get(body, "{{name}}"); + if (field) { +{{^allow_multiple}} + args.{{c_name}} = {{json_convert}}(field); +{{/allow_multiple}} +{{#allow_multiple}} + /* If they were silly enough to both pass in a query param and a + * JSON body, free up the query value. + */ + ast_free(args.{{c_name}}); + if (ast_json_typeof(field) == AST_JSON_ARRAY) { + /* Multiple param passed as array */ + size_t i; + args.{{c_name}}_count = ast_json_array_size(field); + args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); + + if (!args.{{c_name}}) { + ast_ari_response_alloc_failed(response); + goto fin; + } + + for (i = 0; i < args.{{c_name}}_count; ++i) { + args.{{c_name}}[i] = {{json_convert}}(ast_json_array_get(field, i)); + } + } else { + /* Multiple param passed as single value */ + args.{{c_name}}_count = 1; + args.{{c_name}} = ast_malloc(sizeof(*args.{{c_name}}) * args.{{c_name}}_count); + if (!args.{{c_name}}) { + ast_ari_response_alloc_failed(response); + goto fin; + } + args.{{c_name}}[0] = {{json_convert}}(field); + } +{{/allow_multiple}} + } +{{/query_parameters}} +{{/body_parameter}} +{{/parse_body}} +{{/is_websocket}} diff --git a/rest-api-templates/res_ari_resource.c.mustache b/rest-api-templates/res_ari_resource.c.mustache index 580ba1944..d2823a877 100644 --- a/rest-api-templates/res_ari_resource.c.mustache +++ b/rest-api-templates/res_ari_resource.c.mustache @@ -74,6 +74,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") * \param[out] response Response to the HTTP request. */ static void ast_ari_{{c_name}}_{{c_nickname}}_cb( + struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_ari_response *response) { @@ -81,6 +82,12 @@ static void ast_ari_{{c_name}}_{{c_nickname}}_cb( {{#has_parameters}} struct ast_variable *i; {{/has_parameters}} + RAII_VAR(struct ast_json *, body, NULL, ast_json_unref); +{{^body_parameter}} +{{#has_query_parameters}} + struct ast_json *field; +{{/has_query_parameters}} +{{/body_parameter}} #if defined(AST_DEVMODE) int is_valid; int code; diff --git a/rest-api-templates/swagger_model.py b/rest-api-templates/swagger_model.py index 144f22bdb..01659d145 100644 --- a/rest-api-templates/swagger_model.py +++ b/rest-api-templates/swagger_model.py @@ -377,8 +377,9 @@ class Operation(Stringify): if self.is_websocket: self.websocket_protocol = op_json.get('websocketProtocol') if self.http_method != 'GET': - raise ValueError( - "upgrade: websocket is only valid on GET operations") + raise SwaggerError( + "upgrade: websocket is only valid on GET operations", + context) params_json = op_json.get('parameters') or [] self.parameters = [ @@ -394,6 +395,14 @@ class Operation(Stringify): self.has_header_parameters = self.header_parameters and True self.has_parameters = self.has_query_parameters or \ self.has_path_parameters or self.has_header_parameters + + # Body param is different, since there's at most one + self.body_parameter = [ + p for p in self.parameters if p.is_type('body')] + if len(self.body_parameter) > 1: + raise SwaggerError("Cannot have more than one body param", context) + self.body_parameter = self.body_parameter and self.body_parameter[0] + self.summary = op_json.get('summary') self.notes = op_json.get('notes') err_json = op_json.get('errorResponses') or [] |