当前位置: 首页>>代码示例>>Python>>正文


Python HTTPAdapter.build_response方法代码示例

本文整理汇总了Python中requests.adapters.HTTPAdapter.build_response方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPAdapter.build_response方法的具体用法?Python HTTPAdapter.build_response怎么用?Python HTTPAdapter.build_response使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在requests.adapters.HTTPAdapter的用法示例。


在下文中一共展示了HTTPAdapter.build_response方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _on_request

# 需要导入模块: from requests.adapters import HTTPAdapter [as 别名]
# 或者: from requests.adapters.HTTPAdapter import build_response [as 别名]
    def _on_request(self, request, **kwargs):
        match = self._find_match(request)

        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = 'Connection refused: {0}'.format(request.url)
            response = ConnectionError(error_msg)

            self._calls.add(request, response)
            raise response

        headers = {
            'Content-Type': match['content_type'],
        }
        if match['adding_headers']:
            headers.update(match['adding_headers'])

        response = HTTPResponse(
            status=match['status'],
            body=BufferIO(match['body']),
            headers=headers,
            preload_content=False,
        )

        adapter = HTTPAdapter()

        response = adapter.build_response(request, response)
        if not match['stream']:
            response.content  # NOQA

        self._calls.add(request, response)

        return response
开发者ID:l0kix2,项目名称:responses,代码行数:35,代码来源:responses.py

示例2: _on_request

# 需要导入模块: from requests.adapters import HTTPAdapter [as 别名]
# 或者: from requests.adapters.HTTPAdapter import build_response [as 别名]
    def _on_request(self, request, **kwargs):
        match = self._find_match(request)

        # TODO(dcramer): find the correct class for this
        if match is None:
            raise ConnectionError('Connection refused')

        headers = {
            'Content-Type': match['content_type'],
        }
        if match['adding_headers']:
            headers.update(match['adding_headers'])

        response = HTTPResponse(
            status=match['status'],
            body=StringIO(match['body']),
            headers=headers,
            preload_content=False,
        )

        adapter = HTTPAdapter()

        r = adapter.build_response(request, response)
        if not match['stream']:
            r.content  # NOQA
        return r
开发者ID:jamslevy,项目名称:responses,代码行数:28,代码来源:responses.py

示例3: send

# 需要导入模块: from requests.adapters import HTTPAdapter [as 别名]
# 或者: from requests.adapters.HTTPAdapter import build_response [as 别名]
    def send(self, request, **kwargs):
        if (self._is_cache_disabled
            or request.method not in self._cache_allowable_methods):
            response = super(CachedSession, self).send(request, **kwargs)
            response.from_cache = False
            return response

        cache_key = self.cache.create_key(request)

        def send_request_and_cache_response():
            if self._deny_outbound:
                print(request.url)
                raise Exception(("ERROR: OutBound communication was attempted,"
                                 " but deny_outbound was set to True"))

            cache_response = True
            response = super(CachedSession, self).send(request, **kwargs)
            if response.status_code in self._cache_allowable_codes:

                #
                # Special case for cblr:
                # if we get a status of pending then don't cache
                #
                try:
                    if request.url.find('cblr') != -1 and request.method == 'GET':
                        if isinstance(response.json(), dict) and response.json().get('status', '') == 'pending':
                            cache_response = False
                except:
                    cache_response = True
                if cache_response:
                    self.cache.save_response(cache_key, response)

            response.from_cache = False
            return response

        response = self.cache.get_response(cache_key)
        if response is None:
            return send_request_and_cache_response()

        if 'Content-Encoding' in response.headers:
            del response.headers['Content-Encoding']

        adapter = HTTPAdapter()
        response = adapter.build_response(request, response)


        # dispatch hook here, because we've removed it before pickling
        response.from_cache = True
        response = dispatch_hook('response', request.hooks, response, **kwargs)
        return response
开发者ID:RyPeck,项目名称:cbapi-python,代码行数:52,代码来源:core.py

示例4: _on_request

# 需要导入模块: from requests.adapters import HTTPAdapter [as 别名]
# 或者: from requests.adapters.HTTPAdapter import build_response [as 别名]
    def _on_request(self, request, **kwargs):
        match = self._find_match(request)

        # TODO(dcramer): find the correct class for this
        if match is None:
            error_msg = 'Connection refused: {0}'.format(request.url)
            response = ConnectionError(error_msg)

            self._calls.add(request, response)
            raise response

        if 'body' in match and isinstance(match['body'], Exception):
            self._calls.add(request, match['body'])
            raise match['body']

        headers = {
            'Content-Type': match['content_type'],
        }

        if 'callback' in match:  # use callback
            status, r_headers, body = match['callback'](request)
            body = BufferIO(body.encode('utf-8'))
            headers.update(r_headers)

        elif 'body' in match:
            if match['adding_headers']:
                headers.update(match['adding_headers'])
            status = match['status']
            body = BufferIO(match['body'])

        response = HTTPResponse(
            status=status,
            body=body,
            headers=headers,
            preload_content=False,
        )

        adapter = HTTPAdapter()

        response = adapter.build_response(request, response)
        if not match.get('stream'):
            response.content  # NOQA

        self._calls.add(request, response)

        return response
开发者ID:ryaanwells,项目名称:responses,代码行数:48,代码来源:responses.py


注:本文中的requests.adapters.HTTPAdapter.build_response方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。