# 值可变,每次使用需要重新赋值 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)