client.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:zeus 作者: getsentry 项目源码 文件源码
def dispatch(
        self,
        path: str,
        method: str,
        data: dict=None,
        files: Mapping[str, BinaryIO]=None,
        json: dict=None,
        request=None,
        tenant=True,
    ) -> Response:
        if request:
            assert not json
            assert not data
            assert not files
            data = request.data
            files = request.files
            json = None

        if tenant is True:
            tenant = auth.get_current_tenant()

        if json:
            assert not data
            data = dumps(json)
            content_type = 'application/json'
        elif files:
            if not data:
                data = {}
            for key, value in request.form.items():
                data[key] = value
            for key, value in files.items():
                data[key] = value
            content_type = 'multipart/form-data'
        else:
            content_type = None

        with current_app.test_client() as client:
            response = client.open(
                path='/api/{}'.format(path.lstrip('/')),
                method=method,
                content_type=content_type,
                data=data,
                environ_overrides={
                    'zeus.tenant': tenant,
                }
            )
        if not (200 <= response.status_code < 300):
            raise ApiError(
                text=response.get_data(as_text=True),
                code=response.status_code,
            )
        if response.headers['Content-Type'] != 'application/json':
            raise ApiError(
                text='Request returned invalid content type: {}'.format(
                    response.headers['Content-Type']),
                code=response.status_code,
            )
        return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号