[asterisk-commits] dlee: branch dlee/ari-event-remodel r391772 - in /team/dlee/ari-event-remodel...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Jun 14 09:46:10 CDT 2013
Author: dlee
Date: Fri Jun 14 09:46:08 2013
New Revision: 391772
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=391772
Log:
Recognize DISCRIMINATOR as a 1.2 feature
Modified:
team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py
team/dlee/ari-event-remodel/rest-api/api-docs/events.json
Modified: team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py?view=diff&rev=391772&r1=391771&r2=391772
==============================================================================
--- team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py (original)
+++ team/dlee/ari-event-remodel/rest-api-templates/swagger_model.py Fri Jun 14 09:46:08 2013
@@ -37,8 +37,62 @@
except ImportError:
from odict import OrderedDict
-
-SWAGGER_VERSION = "1.1"
+SWAGGER_VERSIONS=["1.1", "1.2"]
+
+class Stringify(object):
+ """Simple mix-in to make the repr of the model classes more meaningful.
+ """
+ def __repr__(self):
+ return "%s(%s)" % (self.__class__, pprint.saferepr(self.__dict__))
+
+
+class ParsingContext(object):
+ """Context information for parsing.
+
+ This object is immutable. To change contexts (like adding an item to the
+ stack), use the next() and next_stack() functions to build a new one.
+ """
+
+ def __init__(self, swagger_version, stack):
+ self.__swagger_version = swagger_version
+ self.__stack = stack
+
+ def __repr__(self):
+ return "ParsingContext(swagger_version=%s, stack=%s)" % (
+ self.swagger_version, self.stack)
+
+ def get_swagger_version(self):
+ return self.__swagger_version
+
+ def get_stack(self):
+ return self.__stack
+
+ swagger_version = property(get_swagger_version)
+
+ stack = property(get_stack)
+
+ def is_version(self, ver):
+ return self.swagger_version == ver
+
+ def next_stack(self, json, id_field):
+ """Returns a new item pushed to the stack.
+
+ @param json: Current JSON object.
+ @param id_field: Field identifying this object.
+ @return New context with additional item in the stack.
+ """
+ if not id_field in json:
+ raise SwaggerError("Missing id_field: %s" % id_field, self)
+ new_stack = self.stack + ['%s=%s' % (id_field, str(json[id_field]))]
+ print new_stack
+ return ParsingContext(self.swagger_version, new_stack)
+
+ def next(self, version=None, stack=None):
+ if version is None:
+ version = self.version
+ if stack is None:
+ stack = self.stack
+ return ParsingContext(version, stack)
class SwaggerError(Exception):
@@ -50,7 +104,7 @@
"""Ctor.
@param msg: String message for the error.
- @param context: Array of strings for current context in the API.
+ @param context: ParsingContext object
@param cause: Optional exception that caused this one.
"""
super(Exception, self).__init__(msg, context, cause)
@@ -84,13 +138,6 @@
@param context: Current context in the API.
"""
pass
-
-
-class Stringify(object):
- """Simple mix-in to make the repr of the model classes more meaningful.
- """
- def __repr__(self):
- return "%s(%s)" % (self.__class__, pprint.saferepr(self.__dict__))
class AllowableRange(Stringify):
@@ -158,7 +205,7 @@
self.allow_multiple = None
def load(self, parameter_json, processor, context):
- context = add_context(context, parameter_json, 'name')
+ context = context.next_stack(parameter_json, 'name')
validate_required_fields(parameter_json, self.required_fields, context)
self.name = parameter_json.get('name')
self.param_type = parameter_json.get('paramType')
@@ -188,7 +235,7 @@
self.reason = None
def load(self, err_json, processor, context):
- context = add_context(context, err_json, 'code')
+ context = context.next_stack(err_json, 'code')
validate_required_fields(err_json, self.required_fields, context)
self.code = err_json.get('code')
self.reason = err_json.get('reason')
@@ -213,7 +260,7 @@
self.error_responses = []
def load(self, op_json, processor, context):
- context = add_context(context, op_json, 'nickname')
+ context = context.next_stack(op_json, 'nickname')
validate_required_fields(op_json, self.required_fields, context)
self.http_method = op_json.get('httpMethod')
self.nickname = op_json.get('nickname')
@@ -255,7 +302,7 @@
self.operations = []
def load(self, api_json, processor, context):
- context = add_context(context, api_json, 'path')
+ context = context.next_stack(api_json, 'path')
validate_required_fields(api_json, self.required_fields, context)
self.path = api_json.get('path')
self.description = api_json.get('description')
@@ -284,6 +331,9 @@
self.type = property_json.get('type')
self.description = property_json.get('description') or ''
self.required = property_json.get('required') or False
+ if self.type == 'DISCRIMINATOR' and context.is_version("1.1"):
+ raise SwaggerError("DISCRIMINATOR support added in Swagger 1.2",
+ context)
return self
@@ -300,7 +350,7 @@
self.properties = None
def load(self, id, model_json, processor, context):
- context = add_context(context, model_json, 'id')
+ context = context.next_stack(model_json, 'id')
# This arrangement is required by the Swagger API spec
self.id = model_json.get('id')
if id != self.id:
@@ -333,8 +383,8 @@
self.apis = []
self.models = []
- def load_file(self, api_declaration_file, processor, context=[]):
- context = context + [api_declaration_file]
+ def load_file(self, api_declaration_file, processor):
+ context = ParsingContext(None, [api_declaration_file])
try:
return self.__load_file(api_declaration_file, processor, context)
except SwaggerError:
@@ -363,7 +413,8 @@
"""
# If the version doesn't match, all bets are off.
self.swagger_version = api_decl_json.get('swaggerVersion')
- if self.swagger_version != SWAGGER_VERSION:
+ context = context.next(version = self.swagger_version)
+ if not self.swagger_version in SWAGGER_VERSIONS:
raise SwaggerError(
"Unsupported Swagger version %s" % swagger_version, context)
@@ -396,7 +447,7 @@
self.api_declaration = None
def load(self, api_json, processor, context):
- context = add_context(context, api_json, 'path')
+ context = context.next_stack(api_json, 'path')
validate_required_fields(api_json, self.required_fields, context)
self.path = api_json['path']
self.description = api_json['description']
@@ -425,7 +476,7 @@
self.apis = None
def load_file(self, resource_file, processor):
- context = [resource_file]
+ context = ParsingContext(None, [resource_file])
try:
return self.__load_file(resource_file, processor, context)
except SwaggerError:
@@ -442,7 +493,7 @@
def load(self, resources_json, processor, context):
# If the version doesn't match, all bets are off.
self.swagger_version = resources_json.get('swaggerVersion')
- if self.swagger_version != SWAGGER_VERSION:
+ if not self.swagger_version in SWAGGER_VERSIONS:
raise SwaggerError(
"Unsupported Swagger version %s" % swagger_version, context)
@@ -469,16 +520,3 @@
if missing_fields:
raise SwaggerError(
"Missing fields: %s" % ', '.join(missing_fields), context)
-
-
-def add_context(context, json, id_field):
- """Returns a new context with a new item added to it.
-
- @param context: Old context.
- @param json: Current JSON object.
- @param id_field: Field identifying this object.
- @return New context with additional item.
- """
- if not id_field in json:
- raise SwaggerError("Missing id_field: %s" % id_field, context)
- return context + ['%s=%s' % (id_field, str(json[id_field]))]
Modified: team/dlee/ari-event-remodel/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel/rest-api/api-docs/events.json?view=diff&rev=391772&r1=391771&r2=391772
==============================================================================
--- team/dlee/ari-event-remodel/rest-api/api-docs/events.json (original)
+++ team/dlee/ari-event-remodel/rest-api/api-docs/events.json Fri Jun 14 09:46:08 2013
@@ -3,7 +3,7 @@
"_author": "David M. Lee, II <dlee at digium.com>",
"_svn_revision": "$Revision$",
"apiVersion": "0.0.1",
- "swaggerVersion": "1.1",
+ "swaggerVersion": "1.2",
"basePath": "http://localhost:8088/stasis",
"resourcePath": "/api-docs/events.{format}",
"apis": [
More information about the asterisk-commits
mailing list