http.py 文件源码

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

项目:python-valenceclient 作者: openstack 项目源码 文件源码
def _http_request(self, conn_url, method, **kwargs):
        """Send an http request with the specified characteristics

        Wrapper around request.Session.request to handle tasks such as
        setting headers and error handling.
        """
        kwargs['headers'] = kwargs.get('headers', {})
        kwargs['headers'].setdefault('User-agent', USER_AGENT)

        self.log_curl_request(method, conn_url, kwargs)

        body = kwargs.pop('body', None)
        headers = kwargs.pop('headers')

        conn_url = self._make_connection_url(conn_url)
        try:
            resp = self.session.request(method, conn_url, headers=headers,
                                        data=body, json=kwargs)
        except requests.exceptions.RequestException as e:
            msg = (_("Error has occured while handling request for "
                     "%(url)s: %(e)s") % dict(url=conn_url, e=e))
            if isinstance(e, ValueError):
                raise exc.ValidationError(msg)
            raise exc.ConnectionRefuse(msg)

        self.log_http_response(resp, resp.text)
        body_iter = six.StringIO(resp.text)
        if resp.status_code >= http_client.BAD_REQUEST:
            error_json = _extract_error_json(resp.text)
            raise exc.from_response(resp, error_json, method, conn_url)
        elif resp.status_code in (http_client.FOUND,
                                  http_client.USE_PROXY):
            return self._http_request(resp['location'], method, **kwargs)

        return resp, body_iter
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号