__copyright__ = 'Copyright 2011 Kenneth Reitz'
__all__ = [
- 'Request', 'Response', 'request', 'get', 'head', 'post', 'put', 'delete',
- 'auth_manager', 'AuthObject','RequestException', 'AuthenticationError',
+ 'Request', 'Response', 'request', 'get', 'head', 'post', 'put', 'delete',
+ 'auth_manager', 'AuthObject','RequestException', 'AuthenticationError',
'URLRequired', 'InvalidMethod', 'HTTPError'
]
def __init__(self, url=None, headers=dict(), files=None, method=None,
data=dict(), auth=None, cookiejar=None):
-
+
self.url = url
self.headers = headers
self.files = files
def _build_response(self, resp):
"""Build internal Response object from given response."""
-
+
self.response.status_code = getattr(resp, 'code', None)
self.response.headers = getattr(resp.info(), 'dict', None)
self.response.url = getattr(resp, 'url', None)
@staticmethod
def _build_url(url, data):
"""Build URLs."""
-
+
if urlparse(url).query:
return '%s&%s' % (url, data)
else:
if self.data:
self.files.update(self.data)
-
+
datagen, headers = multipart_encode(self.files)
req = _Request(self.url, data=datagen, headers=headers, method=self.method)
-
+
else:
req = _Request(self.url, data=self._enc_data, method=self.method)
if self.cookiejar is not None:
self.cookiejar.extract_cookies(resp, req)
-
+
except urllib2.HTTPError, why:
self._build_response(why)
self.response.error = why
return self.sent
- def read(self):
+ def read(self, *args):
return self.response.read()
-
+
class Response(object):
"""The :class:`Request` object. All :class:`Request` objects contain a
:class:`Request.response <response>` attribute, which is an instance of
if self.error:
raise self.error
- def read(self):
+ def read(self, *args):
return self.content
class AuthManager(object):
"""Authentication Manager."""
-
+
def __new__(cls):
singleton = cls.__dict__.get('__singleton__')
if singleton is not None:
def add_auth(self, uri, auth):
"""Registers AuthObject to AuthManager."""
-
+
uri = self.reduce_uri(uri, False)
self._auth[uri] = auth
# uri could be a single URI or a sequence
if isinstance(uri, basestring):
uri = [uri]
-
+
reduced_uri = tuple([self.reduce_uri(u, False) for u in uri])
-
+
if reduced_uri not in self.passwd:
self.passwd[reduced_uri] = {}
self.passwd[reduced_uri] = (user, passwd)
authority = "%s:%d" % (host, dport)
return authority, path
-
+
def is_suburi(self, base, test):
"""Check if test is below base in a URI tree
:param cookies: (optional) CookieJar object to send with the :class:`Request`.
:param auth: (optional) AuthObject to enable Basic HTTP Auth.
"""
-
+
return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)