# 值可变,每次使用需要重新赋值
ERR_RESP_TEMPLATE = {"state": "FAILED", "error": None}
RESP_TEMPLATE_4_DELETE = {"tenant_id": "", "state": "FAILED", "error": None} def _catch_except_args(err_dict=ERR_RESP_TEMPLATE):
def _catch_except(func):
@gen.coroutine
def wrapped_func(self, *args, **kwargs):
"""wrapped function"""
try:
yield func(self, *args, **kwargs)
except TMException as excep:
_LOG.exception(str(excep))
err_dict["state"] = "FAILED" # RESP_TEMPLATE_4_DELETE["state"]可能为"SUCCEED"
err_dict['error'] = str(excep)
self._write(200, err_dict)
except Exception as excep:
_LOG.exception(str(excep))
self._write_error(503, SYSTEM_ERR_MSG) return wrapped_func
return _catch_except class RequestHandlerWrapper(RequestHandler):
"""RequestHandler wrapper for write response""" def _write_error(self, status_code, err_msg):
"""write_error"""
ERR_RESP_TEMPLATE["error"] = err_msg
self._write(status_code, ERR_RESP_TEMPLATE) def _write(self, status_code, msg):
"""write"""
self.set_status(status_code) resp_msg = json.dumps(msg)
self.write(resp_msg) _LOG.debug("<< Return response %d, %s", status_code, resp_msg) class HealthHandler(RequestHandlerWrapper):
"""health check, test interface""" URL = "/api/v1/health" def initialize(self, service):
"""initialize"""
pass @gen.coroutine
def get(self):
"""response ok"""
self._write(200, {"state": "Tenant manager is ready."}) class TenantHandler(RequestHandlerWrapper):
"""TenantHandler""" URL = [r"/api/v1/tenants/([\w|\-|\.]*)", r"/api/v1/tenants"] def initialize(self, service):
"""initialize"""
self.__tenant_manager = service @_catch_except_args()
@gen.coroutine
def post(self):
"""post"""
_LOG.info(">> Receive request for add tenant: %s", self.request.body) tenant_info_dict = json_2_dict(self.request.body)
tenant_info_dict = yield self.__tenant_manager.add(tenant_info_dict)
self._write(200, tenant_info_dict) @_catch_except_args(err_dict=RESP_TEMPLATE_4_DELETE)
@gen.coroutine
def delete(self, tenant_id):
"""delete"""
_LOG.info(">> Receive request for delete tenant: %s", tenant_id) RESP_TEMPLATE_4_DELETE['tenant_id'] = tenant_id yield self.__tenant_manager.delete(tenant_id) RESP_TEMPLATE_4_DELETE['state'] = 'SUCCEED'
RESP_TEMPLATE_4_DELETE['error'] = None
self._write(200, RESP_TEMPLATE_4_DELETE) @_catch_except_args()
@gen.coroutine
def put(self, tenant_id):
"""put"""
_LOG.info(">> Receive request for update tenant: %s, %s",
tenant_id, self.request.body) tenant_info_dict = json_2_dict(self.request.body)
tenant_info_dict = self.__tenant_manager.update(
tenant_id, tenant_info_dict)
self._write(200, tenant_info_dict) @_catch_except_args()
@gen.coroutine
def get(self, tenant_id=None):
"""get"""
_LOG.debug(">> Receive request for get tenant(s): %s, %s",
tenant_id, self.request.headers) # 当tenant_id字符串中包含非法字符时:tenant_id被u""代替
substr_of_tenant_id = self.get_argument("tenant_id", None)
query_condition = QueryCondition(
tenant_id, substr_of_tenant_id, self.request.headers)
resp_body = self.__tenant_manager.query(query_condition)
self._write(200, resp_body)

装饰器 -- 函数装饰器(tornado异常响应装饰器)的更多相关文章

  1. day11 装饰器---函数的使用方法

    这个是一个难点,以后面试会经常出现的,要搞懂! 装饰器升级版,进阶内容1: def outer(flag): def wrapper(func): def inner(*args,**kwargs): ...

  2. Python学习日记(九) 装饰器函数

    1.import time a.time.time() 获取到当前的时间,返回值为浮点型 import time print(time.time()) #1565422783.6497557 b.ti ...

  3. Python函数07/有参装饰器/多个装饰器装饰一个函数

    Python函数07/有参装饰器/多个装饰器装饰一个函数 目录 Python函数07/有参装饰器/多个装饰器装饰一个函数 内容大纲 1.有参装饰器 2.多个装饰器装饰一个函数 3.今日总结 3.今日练 ...

  4. property内置装饰器函数和@name.setter、@name.deleter

    # property # 内置装饰器函数 只在面向对象中使用 # 装饰后效果:将类的方法伪装成属性 # 被property装饰后的方法,不能带除了self外的任何参数 from math import ...

  5. python基础篇_004_装饰器函数

    python装饰器函数 1.装饰器函数引导 功能:计算函数执行时长 import time """ 方式一: 函数首位添加时间,差值就是函数执行时间 缺点:每个函数都要加 ...

  6. Python_函数的有用信息、带参数的装饰器、多个装饰器装饰一个函数

    函数的有用信息 代码1: def login(username, password): """ 此函数需要用户名,密码两个参数,完成的是登录的功能. :return: T ...

  7. Python之函数的本质、闭包、装饰器

    函数名的本质 函数名本质上就是函数的内存地址. 1.可以赋值给其他变量,被引用 def func(): print('in func') f = func print(f) 2.可以被当作容器类型的元 ...

  8. python基础(8)-装饰器函数&进阶

    从小例子进入装饰器 统计一个函数执行耗时 原始版本 import time # time模块有提供时间相关函数 def do_something(): print("do_something ...

  9. python之装饰器函数

    本章内容 引入 装饰器的形成过程 开放封闭原则 谈装饰器主要功能和装饰器固定结构 带参数的装饰器 多个装饰器装饰一个函数 引入 作为一个会写函数的python开发,我们从今天开始要去公司上班了.写了一 ...

随机推荐

  1. cornerstone 使用-图标含义-分支-合并

    http://www.jianshu.com/p/7f5c019c528b http://www.cnblogs.com/fyongbetter/p/5404697.html

  2. 黑客炼金术士 Seeker:可以攻破 4G 摸到你短信,还要为朝阳群众提供谍战工具

    在北京上地的一家咖啡馆里,我在等待黑客 Seeker 的到来. 我对黑客 Seeker 颇有期待.他曾在黑客大会 KCon 上演讲<伪基站高级利用技术——彻底攻破短信验证码>,介绍利用 L ...

  3. VirtualBox安装CENTOS7.3常见问题

    1 DHCP 问题无法上网解决 :sudo dhclient 2 安装宝塔面板:yum install -y wget && wget -O install.sh http://dow ...

  4. BOM浏览器

    1.window.open(url,ways) url是打开的网页地址 ways 是打开的方式 2.window.close() 3.window.navigator  浏览器用户信息 4.windo ...

  5. 剑指Offer 41. 和为S的连续正数序列 (其他)

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  6. 实验楼 MySQL 基础课程 挑战:搭建一个简易的成绩管理系统的数据库

    传送门:https://www.shiyanlou.com/courses/running 介绍 现需要构建一个简易的成绩管理系统的数据库,来记录几门课程的学生成绩.数据库中有三张表分别用于记录学生信 ...

  7. Cocos2dx利用intersectsRect函数检测碰撞

    if (sp1->boundingBox().intersectsRect(sp2->boundingBox())) { pLabel->setString(“碰撞飞机爆炸”); } ...

  8. CodeForces - 13E

    Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...

  9. 使用Blend设计出符合效果的WPF界面

    之前不会用blend,感觉好难的,但美工给出的效果自己有没办法实现,所以研究了一下blend,感觉没有想象中的那么难 废话不多说,开始界面设计 今天拿到美工给的一个界面效果图 这个界面说实话,还可以吧 ...

  10. 【工具】switchhost

    1.前提 主要功能切换host 2.下载路径 https://oldj.github.io/SwitchHosts/ 3.使用略(太简单)