本文整理汇总了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
示例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
示例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
示例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