def dispatch_request(self, *args, **kwargs): self.validate_required_keys() test_func = getattr(self, 'test_%s' % request.method.lower(), None) if test_func and not test_func(*args, **kwargs): raise HTTPBadRequest(message=(getattr(self, 'error', ''))) data = super(BaseAPIMethodView, self).dispatch_request(*args, **kwargs) return jsonify(data)
def validate_required_keys(self): method = request.method.lower() keys = getattr(self, 'required_keys_%s' % request.method.lower(), []) for key in keys: is_present = (request.args.get(key) if method == 'get' else request.json.get(key)) if is_present is None: raise HTTPBadRequest(key + ' key is missing') return True
def dispatch_request(self, *args, **kwargs): test_func = getattr(self, 'test_%s' % request.method.lower(), None) if test_func and not test_func(*args, **kwargs): raise HTTPBadRequest(message=(getattr(self, 'error', ''))) data = super(BaseResourcesView, self).dispatch_request(*args, **kwargs) return jsonify({'data': data})
def dispatch_request(self, *args, **kwargs): validate_func = getattr(self, f'test_{request.method.lower()}', None) try: is_validate_failed = validate_func and not validate_func( *args, **kwargs) except BadRequest as e: if e and hasattr(e, 'data'): raise HTTPBadRequest(service_code=e.data.get( 'service_code', ServiceErrorCodes.KEY_PARSING_ERROR), message=e.data.get('message')) raise HTTPBadRequest if is_validate_failed: message = getattr(self, 'error', '') if message: raise HTTPBadRequest(message=message) raise HTTPBadRequest data = super(BaseAPIResource, self).dispatch_request(*args, **kwargs) if data.get("success") is None: data["success"] = True return jsonify(data)