def dispatch_query(self, request_url, headers, method="GET", data=None):
"""Synchronously dispatch an OAuth-signed request to L{request_url}.
:param request_url: The URL to which the request is to be sent.
:param headers: Headers to include in the request.
:type headers: A dict.
:param method: The HTTP method, e.g. C{GET}, C{POST}, etc.
An AssertionError is raised if trying to pass data for a GET.
:param data: The data to send, if any.
:type data: A byte string.
:return: A open file-like object that contains the response.
"""
headers = dict(headers)
# header keys are case insensitive, so we have to pass over them
set_accept_encoding = False
for key in headers:
if key.lower() == 'accept-encoding':
# The user already supplied a requested encoding, so just pass
# it along.
break
else:
set_accept_encoding = True
headers['Accept-encoding'] = 'gzip'
req = RequestWithMethod(request_url, data, headers, method=method)
res = urllib2.urlopen(req)
# If we set the Accept-encoding header, then we decode the header for
# the caller.
is_gzip = (
set_accept_encoding
and res.info().get('Content-Encoding') == 'gzip')
if is_gzip:
# Workaround python's gzip failure, gzip.GzipFile wants to be able
# to seek the file object.
res_content_io = BytesIO(res.read())
ungz = gzip.GzipFile(mode='rb', fileobj=res_content_io)
res = urllib2.addinfourl(ungz, res.headers, res.url, res.code)
return res
评论列表
文章目录