summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tornado_swagger_ui/README.md
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-11 11:48:06 +0800
committerSerena Feng <feng.xiaowei@zte.com.cn>2016-05-11 09:28:03 +0000
commit5993262403014cfd053783cca1ac646089101cc0 (patch)
tree1d83460b35c8b431a364eb464b5cf4dd953906bb /utils/test/result_collection_api/tornado_swagger_ui/README.md
parentaccf6fb545a8c406cc5cb3f6c6f502d7419fb7b9 (diff)
integration of tornado and swagger-ui
this code will be applied to testApi later, as regards how to use it, please refer utils/test/result_collection_api/tornado_swagger_ui/README.md JIRA: FUNCTEST-247 Change-Id: Ie40e125846add7ceda15f05a38232dc8813b8f29 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/result_collection_api/tornado_swagger_ui/README.md')
-rw-r--r--utils/test/result_collection_api/tornado_swagger_ui/README.md182
1 files changed, 182 insertions, 0 deletions
diff --git a/utils/test/result_collection_api/tornado_swagger_ui/README.md b/utils/test/result_collection_api/tornado_swagger_ui/README.md
new file mode 100644
index 000000000..1ae383461
--- /dev/null
+++ b/utils/test/result_collection_api/tornado_swagger_ui/README.md
@@ -0,0 +1,182 @@
+# tornado-swagger
+
+## What is tornado-swagger?
+tornado is a wrapper for tornado which enables swagger-ui support.
+
+In essense, you just need to wrap the Api instance and add a few python decorators to get full swagger support.
+
+## How to:
+Install:
+
+```
+python setup.py install
+```
+(This installs tornado and epydoc as well)
+
+
+And in your program, where you'd usually just use tornado, add just a little bit of sauce and get a swagger spec out.
+
+
+```python
+from tornado.web import RequestHandler, HTTPError
+from tornado_swagger import swagger
+
+swagger.docs()
+
+# You may decorate your operation with @swagger.operation and use docs to inform information
+class ItemNoParamHandler(GenericApiHandler):
+ @swagger.operation(nickname='create')
+ def post(self):
+ """
+ @param body: create test results for a pod.
+ @type body: L{Item}
+ @return 200: pod is created.
+ @raise 400: invalid input
+ """
+
+# Operations not decorated with @swagger.operation do not get added to the swagger docs
+
+class ItemNoParamHandler(GenericApiHandler):
+ def options(self):
+ """
+ I'm not visible in the swagger docs
+ """
+ pass
+
+
+# Then you use swagger.Application instead of tornado.web.Application
+# and do other operations as usual
+
+def make_app():
+ return swagger.Application([
+ (r"/pods", ItemNoParamHandler),
+ (r"/pods/([^/]+)", ItemHandler),
+ (r"/projects/([^/]+)/cases/([^/]+)", ItemOptionParamHandler),
+ ])
+
+# You define models like this:
+@swagger.model
+class Item:
+ """
+ @descriptin:
+ This is an example of a model class that has parameters in its constructor
+ and the fields in the swagger spec are derived from the parameters to __init__.
+ @notes:
+ In this case we would have property1, property2 as required parameters and property3 as optional parameter.
+ @property property3: Item decription
+ @ptype property3: L{PropertySubclass}
+ """
+ def __init__(self, property1, property2=None):
+ self.property1 = property1
+ self.property2 = property2
+
+# Swagger json:
+ "models": {
+ "Item": {
+ "description": "A description...",
+ "id": "Item",
+ "required": [
+ "property1",
+ ],
+ "properties": [
+ "property1": {
+ "type": "string"
+ },
+ "property2": {
+ "type": "string"
+ "default": null
+ }
+ ]
+ }
+ }
+
+# If you declare an __init__ method with meaningful arguments
+# then those args could be used to deduce the swagger model fields.
+# just as shown above
+
+# if you declare an @property in docs, this property property2 will also be used to deduce the swagger model fields
+class Item:
+ """
+ @property property3: Item description
+ """
+ def __init__(self, property1, property2):
+ self.property1 = property1
+ self.property2 = property2
+
+# Swagger json:
+ "models": {
+ "Item": {
+ "description": "A description...",
+ "id": "Item",
+ "required": [
+ "property1",
+ ],
+ "properties": [
+ "property1": {
+ "type": "string"
+ },
+ "property2": {
+ "type": "string"
+ }
+ "property3": {
+ "type": "string"
+ }
+ ]
+ }
+ }
+
+# if you declare an argument with @ptype, the type of this argument will be specified rather than the default 'string'
+class Item:
+ """
+ @ptype property3: L{PropertySubclass}
+ """
+ def __init__(self, property1, property2, property3=None):
+ self.property1 = property1
+ self.property2 = property2
+ self.property3 = property3
+
+# Swagger json:
+ "models": {
+ "Item": {
+ "description": "A description...",
+ "id": "Item",
+ "required": [
+ "property1",
+ ],
+ "properties": [
+ "property1": {
+ "type": "string"
+ },
+ "property2": {
+ "type": "string"
+ },
+ "property3": {
+ "type": "PropertySubclass"
+ "default": null
+ }
+ ]
+ }
+ }
+```
+
+# Running and testing
+
+Now run your tornado app
+
+```
+python basic.py
+```
+
+And visit:
+
+```
+curl http://localhost:711/swagger/spec
+```
+
+access to web
+```
+http://localhost:711/swagger/spec.html
+```
+
+# Passing more metadata to swagger
+customized arguments used in creating the 'swagger.docs' object will be supported later