def _method_call(self, method, *args, **kwargs): api_call = getattr(self, method) try: api_call(*args, **kwargs) except ResponseMsg as e: resp = self.init_resp(e.code, e.msg) except tornado.web.HTTPError as e: raise e except Exception as e: app_log.error('Uncaught Exception in %s %s call'%(getattr(getattr(self, '__class__'), '__name__'), method), exc_info=True) resp = self.init_resp(1) else: resp = self.init_resp() self.wo_resp(resp)
def _method_call(self, method, *args, **kwargs): api_call = getattr(self, method) try: api_call(*args, **kwargs) except ResponseMsg as e: resp = self.init_resp(e.code, e.msg) except tornado.web.HTTPError as e: raise e except Exception as e: app_log.error('Uncaught Exception in %s %s call' % ( getattr(getattr(self, '__class__'), '__name__'), method), exc_info=True) resp = self.init_resp(1, 'Unknown Error') else: resp = self.init_resp() self.wo_resp(resp)
def parameter(self): ''' according to request method config to filter all request paremter if value is invalid then set None ''' method = self.request.method.lower() arguments = self.request.arguments files = self.request.files rpd = {} # request parameter dict def filter_parameter(key, tp, default=None): if tp not in self._types: raise ValueError( '%s parameter expected types %s' % (key, self._types)) if tp != file: if key not in arguments: rpd[key] = default return if tp in [ObjectId, int, float, bool]: rpd[key] = getattr(self, 'to_%s' % getattr( tp, '__name__').lower())(self.get_argument(key)) return if tp == basestring or tp == str: rpd[key] = self.get_argument(key, strip=False) return if tp == list: rpd[key] = self.get_arguments(key) return if tp == file: if key not in files: rpd[key] = [] return rpd[key] = self.request.files[key] required_params = getattr(self, '_required_params', None) if isinstance(required_params, list): for key, tp, default in required_params: filter_parameter(key, tp, default) # extract method required params method_required_params = getattr( self, '_%s_required_params' % method, None) if isinstance(method_required_params, list): for key, tp, default in method_required_params: filter_parameter(key, tp, default) params = getattr(self, '_%s_params' % method, None) if params is None: return rpd # need arguments try: for key, tp in params.get('need', []): if tp == list: filter_parameter(key, tp, []) else: filter_parameter(key, tp) except ValueError as e: app_log.error( '%s request need arguments parse error: %s' % (method, e)) raise ValueError(e) except Exception as e: app_log.error( '%s request need arguments parse error: %s' % (method, e)) raise e # option arguments for key, tp, default in params.get('option', []): filter_parameter(key, tp, default) return rpd
def parameter(self): ''' according to request method config to filter all request paremter if value is invalid then set None ''' method = self.request.method.lower() arguments = self.request.arguments files = self.request.files rpd = {} # request parameter dict def filter_parameter(key, tp, default=None): if tp not in self._types: raise ValueError("%s parameter expected types %s" % (key, self._types)) if tp != file: if key not in arguments: rpd[key] = default return if tp in [ObjectId, int, float, bool]: rpd[key] = getattr(self, 'to_%s' % getattr(tp, '__name__').lower())(self.get_argument(key)) return if tp == basestring or tp == str: rpd[key] = self.get_argument(key, strip=False) return if tp == list: rpd[key] = self.get_arguments(key) return if tp == file: if key not in files: rpd[key] = [] return rpd[key] = self.request.files[key] required_params = getattr(self, '_required_params', None) if isinstance(required_params, list): for key, tp, default in required_params: filter_parameter(key, tp, default) #extract method required params method_required_params = getattr(self, '_%s_required_params' % method, None) if isinstance(method_required_params, list): for key, tp, default in method_required_params: filter_parameter(key, tp, default) params = getattr(self, '_%s_params' % method, None) if params is None: return rpd #need arguments try: for key, tp in params.get('need', []): if tp == list: filter_parameter(key, tp, []) else: filter_parameter(key, tp) except ValueError as e: app_log.error('%s request need arguments parse error: %s' % (method, e)) raise ValueError(e) except Exception as e: app_log.error('%s request need arguments parse error: %s' % (method, e)) raise e #option arguments for key, tp, default in params.get('option', []): filter_parameter(key, tp, default) return rpd