From 3a7da2d58cf4e958f454830e403e607d94ed68fa Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Wed, 10 May 2017 14:03:38 +0800 Subject: leverage tornado framework to simply impl of TestAPI 1) make better use of initialize() method 2) better naming global arguments Change-Id: I89dc72a358bcb2a5688a1ffeb3ed5d735d875a4a Signed-off-by: SerenaFeng --- .../opnfv_testapi/tornado_swagger/handlers.py | 16 ++++----- .../opnfv_testapi/tornado_swagger/settings.py | 14 +++----- .../opnfv_testapi/tornado_swagger/swagger.py | 2 +- .../testapi/opnfv_testapi/tornado_swagger/views.py | 40 +++++++++++++--------- 4 files changed, 37 insertions(+), 35 deletions(-) (limited to 'utils') diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py index 72add2d64..fff6d2137 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/handlers.py @@ -13,7 +13,7 @@ from opnfv_testapi.tornado_swagger import views def swagger_handlers(): - prefix = settings.default_settings.get('swagger_prefix', '/swagger') + prefix = settings.docs_settings.get('swagger_prefix', '/swagger') if prefix[-1] != '/': prefix += '/' @@ -23,20 +23,20 @@ def swagger_handlers(): tornado.web.URLSpec( _path(r'spec.html$'), views.SwaggerUIHandler, - settings.default_settings, - name=settings.SWAGGER_API_DOCS), + settings.docs_settings, + name=settings.API_DOCS_NAME), tornado.web.URLSpec( _path(r'resources.json$'), views.SwaggerResourcesHandler, - settings.default_settings, - name=settings.SWAGGER_RESOURCE_LISTING), + settings.docs_settings, + name=settings.RESOURCE_LISTING_NAME), tornado.web.URLSpec( _path(r'APIs$'), views.SwaggerApiHandler, - settings.default_settings, - name=settings.SWAGGER_API_DECLARATION), + settings.docs_settings, + name=settings.API_DECLARATION_NAME), ( _path(r'(.*\.(css|png|gif|js))'), tornado.web.StaticFileHandler, - {'path': settings.default_settings.get('static_path')}), + {'path': settings.docs_settings.get('static_path')}), ] diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py index 753029709..03e9bbdff 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/settings.py @@ -8,25 +8,21 @@ ############################################################################## import os.path -SWAGGER_VERSION = '1.2' -SWAGGER_API_DOCS = 'swagger-api-docs' -SWAGGER_RESOURCE_LISTING = 'swagger-resource-listing' -SWAGGER_API_DECLARATION = 'swagger-api-declaration' +API_DOCS_NAME = 'swagger-api-docs' +RESOURCE_LISTING_NAME = 'swagger-resource-listing' +API_DECLARATION_NAME = 'swagger-api-declaration' STATIC_PATH = os.path.join(os.path.dirname(os.path.normpath(__file__)), 'static') -default_settings = { +docs_settings = { 'base_url': '', 'static_path': STATIC_PATH, 'swagger_prefix': '/swagger', 'api_version': 'v1.0', + 'swagger_version': '1.2', 'api_key': '', 'enabled_methods': ['get', 'post', 'put', 'patch', 'delete'], 'exclude_namespaces': [], } models = [] - - -def basePath(): - return default_settings.get('base_url') diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py index a0f6ae70c..83f389a6b 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/swagger.py @@ -276,7 +276,7 @@ class operation(DocParser): def docs(**opts): - settings.default_settings.update(opts) + settings.docs_settings.update(opts) class Application(tornado.web.Application): diff --git a/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py index 591cb44e7..ee9cf0487 100644 --- a/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py +++ b/utils/test/testapi/opnfv_testapi/tornado_swagger/views.py @@ -17,39 +17,44 @@ from opnfv_testapi.tornado_swagger import settings def json_dumps(obj, pretty=False): - return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) \ - if pretty else json.dumps(obj) + return json.dumps(obj, + sort_keys=True, + indent=4, + separators=(',', ': ')) if pretty else json.dumps(obj) class SwaggerUIHandler(tornado.web.RequestHandler): - def initialize(self, static_path, **kwds): - self.static_path = static_path + def initialize(self, **kwargs): + self.static_path = kwargs.get('static_path') + self.base_url = kwargs.get('base_url') def get_template_path(self): return self.static_path def get(self): discovery_url = os.path.join( - settings.basePath(), - self.reverse_url(settings.SWAGGER_RESOURCE_LISTING)) + self.base_url, + self.reverse_url(settings.RESOURCE_LISTING_NAME)) self.render('index.html', discovery_url=discovery_url) class SwaggerResourcesHandler(tornado.web.RequestHandler): - def initialize(self, api_version, exclude_namespaces, **kwds): - self.api_version = api_version - self.exclude_namespaces = exclude_namespaces + def initialize(self, **kwargs): + self.api_version = kwargs.get('api_version') + self.swagger_version = kwargs.get('swagger_version') + self.base_url = kwargs.get('base_url') + self.exclude_namespaces = kwargs.get('exclude_namespaces') def get(self): self.set_header('content-type', 'application/json') resources = { 'apiVersion': self.api_version, - 'swaggerVersion': settings.SWAGGER_VERSION, - 'basePath': settings.basePath(), + 'swaggerVersion': self.swagger_version, + 'basePath': self.base_url, 'produces': ["application/json"], 'description': 'Test Api Spec', 'apis': [{ - 'path': self.reverse_url(settings.SWAGGER_API_DECLARATION), + 'path': self.reverse_url(settings.API_DECLARATION_NAME), 'description': 'Test Api Spec' }] } @@ -58,9 +63,10 @@ class SwaggerResourcesHandler(tornado.web.RequestHandler): class SwaggerApiHandler(tornado.web.RequestHandler): - def initialize(self, api_version, base_url, **kwds): - self.api_version = api_version - self.base_url = base_url + def initialize(self, **kwargs): + self.api_version = kwargs.get('api_version') + self.swagger_version = kwargs.get('swagger_version') + self.base_url = kwargs.get('base_url') def get(self): self.set_header('content-type', 'application/json') @@ -70,8 +76,8 @@ class SwaggerApiHandler(tornado.web.RequestHandler): specs = { 'apiVersion': self.api_version, - 'swaggerVersion': settings.SWAGGER_VERSION, - 'basePath': settings.basePath(), + 'swaggerVersion': self.swagger_version, + 'basePath': self.base_url, 'apis': [self.__get_api_spec__(path, spec, operations) for path, spec, operations in apis], 'models': self.__get_models_spec(settings.models) -- cgit 1.2.3-korg