summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tornado_swagger_ui/example/basic.py
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-13 15:58:04 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-13 16:10:08 +0800
commit83d5946dcedcc60ddca6c7538f692d37bdc873ba (patch)
treefbd3ea7df8a83f84bf985dfe1aa101f723031dba /utils/test/result_collection_api/tornado_swagger_ui/example/basic.py
parent8d2afaee34b976519d94dd003b216fdf62fb97c6 (diff)
tornado_swagger_ui support query operation in "GET" method, and support methods in model
query: GET /item?property1=1&property2=1 methods in model: @swagger.model() class Item: def format_http(self): pass @staticmethod def item_from_dict(item_dict): pass @classmethod def test_classmethod(cls): pass JIRA: FUNCTEST-250 Change-Id: I12f937c4d2f64f93dc1194a8ad982e8b7ff21b7c Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/result_collection_api/tornado_swagger_ui/example/basic.py')
-rw-r--r--utils/test/result_collection_api/tornado_swagger_ui/example/basic.py81
1 files changed, 74 insertions, 7 deletions
diff --git a/utils/test/result_collection_api/tornado_swagger_ui/example/basic.py b/utils/test/result_collection_api/tornado_swagger_ui/example/basic.py
index 20ad9cba1..1731bfd17 100644
--- a/utils/test/result_collection_api/tornado_swagger_ui/example/basic.py
+++ b/utils/test/result_collection_api/tornado_swagger_ui/example/basic.py
@@ -2,7 +2,7 @@ import json
import tornado.ioloop
from tornado.web import RequestHandler, HTTPError
-from tornado_swagger import swagger
+from tornado_swagger_ui.tornado_swagger import swagger
DEFAULT_REPRESENTATION = "application/json"
HTTP_BAD_REQUEST = 400
@@ -12,13 +12,13 @@ HTTP_NOT_FOUND = 404
swagger.docs()
-@swagger.model
+@swagger.model()
class PropertySubclass:
def __init__(self, sub_property=None):
self.sub_property = sub_property
-@swagger.model
+@swagger.model()
class Item:
"""
@description:
@@ -36,6 +36,33 @@ class Item:
self.property3 = property3
self.property4 = property4
+ def format_http(self):
+ return {
+ "property1": self.property1,
+ "property2": self.property2,
+ "property3": self.property3,
+ "property4": self.property4,
+ }
+
+ @staticmethod
+ def item_from_dict(item_dict):
+
+ if item_dict is None:
+ return None
+
+ t = Item(None)
+ t.property1 = item_dict.get('property1')
+ t.property2 = item_dict.get('property2')
+ t.property3 = item_dict.get('property3')
+ t.property4 = item_dict.get('property4')
+
+ return t
+
+ @classmethod
+ def test_classmethod(cls):
+ pass
+
+
items = {}
@@ -75,19 +102,25 @@ class ItemNoParamHandler(GenericApiHandler):
"""
@param body: create a item.
@type body: L{Item}
+ @in body: body
@return 200: item is created.
@raise 400: invalid input
"""
property1 = self.json_args.get('property1')
- items[property1] = self.json_args
- self.finish_request(items[property1])
+ item = Item.item_from_dict(self.json_args)
+ items[property1] = item
+ Item.test_classmethod()
+ self.finish_request(item.format_http())
@swagger.operation(nickname='list')
def get(self):
"""
@rtype: L{Item}
"""
- self.finish_request(items)
+ res = []
+ for key, value in items.iteritems():
+ res.append(value.format_http())
+ self.finish_request(res)
def options(self):
"""
@@ -107,7 +140,7 @@ class ItemHandler(GenericApiHandler):
This will be added to the Implementation Notes.It lets you put very long text in your api.
"""
- self.finish_request(items[arg])
+ self.finish_request(items[arg].format_http())
@swagger.operation(nickname='delete')
def delete(self, arg):
@@ -134,8 +167,42 @@ class ItemOptionParamHandler(GenericApiHandler):
self.write("success")
+class ItemQueryHandler(GenericApiHandler):
+ @swagger.operation(nickname='query')
+ def get(self):
+ """
+ @param property1:
+ @type property1: L{string}
+ @in property1: query
+ @required property1: False
+
+ @param property2:
+ @type property2: L{string}
+ @in property2: query
+ @required property2: True
+ @rtype: L{Item}
+ @notes: GET /item?property1=1&property2=1
+ """
+ property1 = self.get_query_argument("property1", None)
+ property2 = self.get_query_argument("property2", None)
+
+ res = []
+ if property1 is None:
+ for key, value in items.iteritems():
+ if property2 is None:
+ res.append(value.format_http())
+ elif value.property2 == property2:
+ res.append(value.format_http())
+ elif items.has_key(property1):
+ if items.get(property1).property2 == property2:
+ res.append(items.get(property1).format_http())
+
+ self.finish_request(res)
+
+
def make_app():
return swagger.Application([
+ (r"/item", ItemQueryHandler),
(r"/items", ItemNoParamHandler),
(r"/items/([^/]+)", ItemHandler),
(r"/items/([^/]+)/cases/([^/]+)", ItemOptionParamHandler),