How to use the fair.response.ResponseRaise function in fair

To help you get started, we’ve selected a few fair examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github remyzane / fair-api / fair / ui / __init__.py View on Github external
params = get_request_params(request)
        params_proto = params.copy()

        # plugin
        for plugin in view_func.meta.plugins:
            plugin.before_request(view_func)
            for parameter in plugin.parameters:
                del params[parameter[0]]

        # structure parameters
        params = structure_params(view_func, params_proto, params)
        request.meta = view_func.meta
        response_content = view_func(**params)
        if isinstance(response_content, ResponseRaise):
            response_content = response_content.response()
    except ResponseRaise as response_raise:
        response_content = response_raise.response()
    except Exception as e:
        response_content = view_func.meta.response('exception').response()
    return response_content
github remyzane / fair-api / fair / app.py View on Github external
# get request parameters
            params = get_request_params()
            params_proto = params.copy()

            # plugin
            for plugin in view_func.meta.plugins:
                plugin.before_request(view_func.meta, params)
                for parameter in plugin.parameters:
                    del params[parameter[0]]

            # structure parameters
            params = structure_params(view_func, params_proto, params)
            response_content = view_func(**params)
            if isinstance(response_content, ResponseRaise):
                response_content = response_content.response()
        except ResponseRaise as response_raise:
            response_content = response_raise.response()
        except Exception as e:
            response_content = view_func.meta.response('exception').response()
        return response_content
github remyzane / fair-api / fair / view.py View on Github external
for plugin in self.element.plugins:
                plugin.before_request(self)
                for parameter in plugin.parameters:
                    del self.params[parameter[0]]

            # structure parameters
            self.__structure_params()

            response_content = self.method(self, **self.params)

            if type(response_content) == tuple:
                code, content, response_content = response_content
                self.log(code, content)
            else:
                self.log('success', response_content)
        except ResponseRaise as response_raise:
            code, content, response_content = response_raise.response()
            self.log(code, content)
        except Exception:
            code, content, response_content = self.r('exception')
            self.log(code, content, True)
        return response_content
github remyzane / fair-api / fair / response.py View on Github external
JSON_P = 'application/javascript; charset=utf-8'


class ResponseRaise(Exception):

    def __init__(self, code, data=None, status=None):
        self.code = code
        self.info = request.meta.code_dict[code]
        self.data = data
        self.status = status

    def response(self):
        raise NotImplementedError()


class JsonRaise(ResponseRaise):
    """Json format:{ "code": "", "info": "",  "data": ... } """   # 请勿修改该 doc str,doc_ui 界面要使用

    def response(self):
        ret = {'code': self.code, 'info': self.info, 'data': self.data}
        if self.code == 'exception':
            log.exception('%s %s', request.path, ret)
        return Response(json.dumps(ret), content_type=JSON, status=self.status)
github remyzane / fair-api / fair / app.py View on Github external
try:
            request.meta = view_func.meta
            # get request parameters
            params = get_request_params()
            params_proto = params.copy()

            # plugin
            for plugin in view_func.meta.plugins:
                plugin.before_request(view_func.meta, params)
                for parameter in plugin.parameters:
                    del params[parameter[0]]

            # structure parameters
            params = structure_params(view_func, params_proto, params)
            response_content = view_func(**params)
            if isinstance(response_content, ResponseRaise):
                response_content = response_content.response()
        except ResponseRaise as response_raise:
            response_content = response_raise.response()
        except Exception as e:
            response_content = view_func.meta.response('exception').response()
        return response_content
github remyzane / fair-api / fair / ui / __init__.py View on Github external
try:
        # get request parameters
        params = get_request_params(request)
        params_proto = params.copy()

        # plugin
        for plugin in view_func.meta.plugins:
            plugin.before_request(view_func)
            for parameter in plugin.parameters:
                del params[parameter[0]]

        # structure parameters
        params = structure_params(view_func, params_proto, params)
        request.meta = view_func.meta
        response_content = view_func(**params)
        if isinstance(response_content, ResponseRaise):
            response_content = response_content.response()
    except ResponseRaise as response_raise:
        response_content = response_raise.response()
    except Exception as e:
        response_content = view_func.meta.response('exception').response()
    return response_content
github remyzane / fair-api / fair / view_old.py View on Github external
for plugin in self.meta.plugins:
                plugin.before_request(self)
                for parameter in plugin.parameters:
                    del self.params[parameter[0]]

            # structure parameters
            self.__structure_params()

            response_content = self.method(self, **self.params)

            if type(response_content) == tuple:
                code, content, response_content = response_content
                self.log(code, content)
            else:
                self.log('success', response_content)
        except ResponseRaise as response_raise:
            code, content, response_content = response_raise.response()
            self.log(code, content)
        except Exception:
            code, content, response_content = self.r('exception')
            self.log(code, content, True)
        return response_content
github remyzane / fair-api / fair / plugin / jsonp.py View on Github external
import json
from flask import Response, request
from ..api_setts import Setts
from ..api_meta import Meta
from ..plugin import Plugin
from ..response import ResponseRaise, JSON_P


class JsonPRaise(ResponseRaise):
    """Json format:{ "code": "", "info": "",  "data": ... } """   # 请勿修改该 doc str,doc_ui 界面要使用

    def response(self):
        ret = {'code': self.code, 'info': request.meta.code_dict[self.code], 'data': self.data}
        content = request.meta.json_p_callback_name + '(' + json.dumps(ret) + ')'
        return self.code, ret, Response(content, content_type=JSON_P, status=self.status)


class JsonP(Plugin):
    """ JsonP response Plugin

    if defined in view's return will using jsonp (accustomed to using 'callback')
    """
    error_codes = {}

    def __init__(self, callback_field_name):