diff --git a/examples/industrial-ai-apiserver/industrialai/api/controllers/root.py b/examples/industrial-ai-apiserver/industrialai/api/controllers/root.py index 6f596a50f655c1ad932973931ac8d7a9f4aee341..6ea53d1752d720d3d565bb9f379fcc823dc382ff 100644 --- a/examples/industrial-ai-apiserver/industrialai/api/controllers/root.py +++ b/examples/industrial-ai-apiserver/industrialai/api/controllers/root.py @@ -9,6 +9,7 @@ from pecan import expose from pecan import request from pecan_swagger import decorators as swagger from wsme import types as wtypes + logger = logging.getLogger(__name__) @@ -22,6 +23,7 @@ class checkForm: pwd = wtypes.text +@swagger.definitions('object') class registryForm: account = wtypes.text pwd = wtypes.text @@ -29,14 +31,33 @@ class registryForm: mail = wtypes.text name = wtypes.text number = wtypes.text - - + check = checkForm() + + +@swagger.parameter('myself', {'in': "body", + 'name': "body", + 'description': "get user", + 'required': True, + 'schema': 'userInfo'}) +@swagger.parameter('index', {'in': "body", + 'name': "body", + 'description': "get user", + 'required': True, + 'schema': 'userInfo'}) +@swagger.parameter('test', {'in': "body", + 'name': "body", + 'description': "get user", + 'required': True, + 'schema': 'userInfo'}) +@swagger.response('myself', '401', 'fail') +@swagger.response('index', '401', 'fail') +@swagger.response('test', '200', 'test description') +@swagger.method('test', 'index', 'signin', 'pwdcheck', 'myself') @swagger.path("/", "Root") class RootController(object): - _custom_actions = { 'test': ['GET'], - 'signin':['POST'], + 'signin': ['POST'], 'pwdcheck': ['POST'], 'myself': ['POST'] } @@ -78,6 +99,7 @@ class RootController(object): "debuginfo": null } """ + @wsexpose(wtypes.text, body=logForm) def signin(self, logForm): account = logForm.account @@ -106,6 +128,7 @@ class RootController(object): "result": True True:校验通过/False:校验失败 } """ + @wsexpose(wtypes.text, body=checkForm) def pwdcheck(self, checkForm): try: @@ -132,6 +155,7 @@ class RootController(object): "phone": "12345", 手机号 } """ + @expose('json') def myself(self): token = request.cookies.get("token") diff --git a/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/role.py b/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/role.py index 5c3ebe50b3979dcb62499d752380fc55d3c3afc1..2f1a9d6db735e0849bd3ed07679815dd94de3e2d 100644 --- a/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/role.py +++ b/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/role.py @@ -12,6 +12,18 @@ class Roletest(wstypes.Base): roleId = int +@swagger.definitions('object') +class RoleItem: + title = wstypes.text + id = int + +@swagger.parameter('get_all', {'in': "body", + 'name': "body", + 'description': "get all roles", + 'required': True, + 'schema': 'RoleItem'}) +@swagger.response('get_all', '401', 'fail') +@swagger.method('test', 'get_all') @swagger.path("roles", "Role", "Root") class RoleController(RestController): _custom_actions = { diff --git a/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/user.py b/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/user.py index cdf98c36e83a00c12e7c3bb4cce0c4aa3a221b03..dd764b8f648f96da5f7471807e9881874eb2b8ff 100644 --- a/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/user.py +++ b/examples/industrial-ai-apiserver/industrialai/api/controllers/v1/user.py @@ -5,9 +5,80 @@ from pecan import expose from pecan import request from pecan.rest import RestController from pecan_swagger import decorators as swagger - +from wsme import types as wtypes + + +@swagger.definitions('object') +class userInfo: + id = int + number = int + roleId = int + status = wtypes.text + create_at = wtypes.text + name = wtypes.text + account = wtypes.text + phone = wtypes.text + email = wtypes.text + + +@swagger.definitions('object') +class userInfoUpdate: + id = wtypes.text + enterpriseId = wtypes.text + enterpriseGroupId = wtypes.text + name = wtypes.text + + +@swagger.response('put', '401', 'fail') +@swagger.response('post', '401', 'fail') +@swagger.response('get_one', '401', 'fail') +@swagger.response('get', '401', 'fail') @swagger.response('get', '200', 'test description') -@swagger.method('get','get_one','post','put') +@swagger.parameter('get', {'name': "status", + 'in': "query", + 'description': 'Status values that need to be considered for filter', + 'required': True, + 'type': 'array', + 'items': {'type': 'string', + 'enum': ["available", "pending", "sold"], + 'default': "available"}, + 'collectionFormat': "multi"}) +@swagger.parameter('get_one', {'in': "body", + 'name': "body", + 'description': "get one user", + 'required': True, + 'schema': 'userInfo'}) +@swagger.parameter('put', {'in': "body", + 'name': "body", + 'description': "get one user", + 'required': True, + 'schema': 'userInfo'}) +@swagger.parameter('post', {'name': "userId", + 'in': "formData", + 'description': "ID of user that needs to be created", + 'required': True, + 'type': "integer", + 'format': "int64"}, + {'name': "name", + 'in': "formData", + 'description': "created name of the user", + 'required': False, + 'type': "string"}, + {'name': "status", + 'in': "formData", + 'description': "created status of the user", + 'required': False, + 'type': "string"}) +@swagger.consumes('post', 'multipart/form-data') +@swagger.produces('post', 'application/json') +@swagger.operationId('get', 'get users') +@swagger.description('get', 'get users information') +@swagger.summary('get', 'get users information') +@swagger.tags('put', 'user') +@swagger.tags('post', 'user') +@swagger.tags('get', 'user') +@swagger.tags('get_one', 'user') +@swagger.method('get', 'get_one', 'post', 'put') @swagger.path("users", "User", "Root") class UserController(RestController): """@api {get} /v1/users/?name= get_all_users diff --git a/examples/industrial-ai-apiserver/pecan_swagger/decorators.py b/examples/industrial-ai-apiserver/pecan_swagger/decorators.py index 08de1584942739d632cec61f5d2c636b454e92df..8c014866900bff251412917f3076a85eb41a214a 100644 --- a/examples/industrial-ai-apiserver/pecan_swagger/decorators.py +++ b/examples/industrial-ai-apiserver/pecan_swagger/decorators.py @@ -27,7 +27,6 @@ def path(endpoint, name, parent=None): c.__swag = dict(endpoint=endpoint, name=name, parent=parent) g.add_path(c) return c - return decorator @@ -39,7 +38,12 @@ def response(method_name, res_code, des, schema=None): """ def decorator(m): - + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) if not m.__swag['method'][method_name].get('responses'): m.__swag['method'][method_name]['responses'] = {} if not m.__swag['method'][method_name]['responses'].get(res_code): @@ -60,6 +64,12 @@ def tags(method_name, *tags): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) if not m.__swag['method'][method_name].get('tags'): m.__swag['method'][method_name]['tags'] = [] for i in tags: @@ -77,6 +87,12 @@ def summary(method_name, summary): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) m.__swag['method'][method_name]['summary'] = summary return m @@ -91,6 +107,12 @@ def description(method_name, des): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) m.__swag['method'][method_name]['description'] = des return m @@ -105,6 +127,12 @@ def operationId(method_name, oi): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) m.__swag['method'][method_name]['operationId'] = oi return m @@ -119,6 +147,8 @@ def method(*args): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) d = {} for i in args: if hasattr(m, i): @@ -140,6 +170,49 @@ def definitions(t, *args): return decorator +def consumes(method_name, *consume): + """ + 获取方法的consumes + 生成字典{consumes:consumes} + 并加入controller.__swag[method][method_name] + """ + + def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) + if not m.__swag['method'][method_name].get('consumes'): + m.__swag['method'][method_name]['consumes'] = [] + for i in consume: + m.__swag['method'][method_name]['consumes'].append(i) + return m + + return decorator + +def produces(method_name, *pro): + """ + 获取方法的produces + 生成字典{produces:pro} + 并加入controller.__swag[method][method_name] + """ + + def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) + if not m.__swag['method'][method_name].get('produces'): + m.__swag['method'][method_name]['produces'] = [] + for i in pro: + m.__swag['method'][method_name]['produces'].append(i) + return m + + return decorator def parameter(method_name, *args): """ @@ -149,6 +222,12 @@ def parameter(method_name, *args): """ def decorator(m): + if not hasattr(m, '__swag'): + raise Exception('You need to write a path in {} first'.format(m.__name__)) + if not 'method' in m.__swag: + raise Exception('You need to write the methods in {}'.format(m.__name__)) + if not method_name in m.__swag['method']: + raise Exception('There is no method called {} in {}'.format(method_name, m.__name__)) if not m.__swag['method'][method_name].get('parameters'): m.__swag['method'][method_name]['parameters'] = [] for i in args: @@ -158,4 +237,3 @@ def parameter(method_name, *args): return m return decorator -