Merge branch 'develop' of https://github.com/monkeython/requests into develop
authorKenneth Reitz <me@kennethreitz.com>
Tue, 11 Oct 2011 12:36:35 +0000 (08:36 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Tue, 11 Oct 2011 12:36:35 +0000 (08:36 -0400)
Conflicts:
requests/api.py
requests/config.py
requests/models.py

1  2 
requests/api.py
requests/models.py

diff --cc requests/api.py
index dea4c1adc833a2e46dce4a7cd287250cda1b147a,cc64fc41af05eccd1d885563792a6bfc7751d4f6..6b1839a713132ece81579f502a0d75da0e3bab8e
@@@ -11,12 -11,13 +11,12 @@@ This module implements the Requests API
  
  """
  
 -import config
 -from .models import Request, Response, AuthObject
 +from ._config import get_config
 +from .models import Request, Response
  from .status_codes import codes
- from .hooks import dispatch_hook
+ from hooks import setup_hooks, dispatch_hooks
 -from .utils import cookiejar_from_dict
 +from .utils import cookiejar_from_dict, header_expand
  
 -from urlparse import urlparse
  
  __all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')
  
@@@ -44,56 -42,41 +44,59 @@@ def request(method, url
      """
  
      method = str(method).upper()
 +    config = get_config(config)
  
-     if cookies is None:
-         cookies = {}
+     cookies = cookiejar_from_dict(cookies if cookies is not None else dict())
++    cookies = cookiejar_from_dict(cookies)
 +
 +    # cookies = cookiejar_from_dict(cookies)
 +
 +    # Expand header values
 +    if headers:
 +        for k, v in headers.items() or {}:
 +            headers[k] = header_expand(v)
 +
      args = dict(
 -        method = method,
 -        url = url,
 -        data = data,
 -        params = params,
 -        headers = headers,
 -        cookiejar = cookies,
 -        files = files,
 -        auth = auth,
 -        timeout = timeout or config.settings.timeout,
 -        allow_redirects = allow_redirects,
 -        proxies = proxies or config.settings.proxies,
 +        method=method,
 +        url=url,
 +        data=data,
 +        params=params,
 +        headers=headers,
 +        cookies=cookies,
 +        files=files,
 +        auth=auth,
 +        timeout=timeout or config.get('timeout'),
 +        hooks=hooks,
 +        allow_redirects=allow_redirects,
 +        proxies=proxies or config.get('proxies'),
 +        _pools=_pools
      )
+     
+     hooks = setup_hooks(hooks if hooks is not None else dict())
  
      # Arguments manipulation hook.
-     args = dispatch_hook('args', hooks, args)
+     args = dispatch_hooks(hooks['args'], args)
  
 +    # Create Request object.
      r = Request(**args)
  
      # Pre-request hook.
-     r = dispatch_hook('pre_request', hooks, r)
+     r = dispatch_hooks(hooks['pre_request'], r)
  
 +    # Only construct the request (for async)
 +    if _return_request:
 +        return r
 +
      # Send the HTTP Request.
      r.send()
  
 +    # TODO: Add these hooks inline.
      # Post-request hook.
-     r = dispatch_hook('post_request', hooks, r)
+     r = dispatch_hooks(hooks['post_request'], r)
  
      # Response manipulation hook.
-     r.response = dispatch_hook('response', hooks, r.response)
+     r.response = dispatch_hooks(hooks['response'], r.response)
  
      return r.response
  
index 27cae2a5b40b11beccd305176fff811a4943e43e,e199b3ca2d35a79710165d1005ef94e696bc2415..2c863b880ad704fa978a772f5bc1498fe9c08d65
@@@ -452,44 -444,202 +452,64 @@@ class Response(object)
          (if available).
          """
  
-         if self._content is not None:
-             return self._content
+         if self._content is None:
+             # Read the contents.
+             self._content = self.fo.read()
  
-         # Read the contents.
 +        if self._content_consumed:
 +            raise RuntimeError(
 +                'The content for this response was already consumed')
 -        # # Decode unicode content.
 -        # if settings.decode_unicode:
 -        #     self._content = get_unicode_from_response(self)
 -
 -        return self._content
 -
 -
 -    def raise_for_status(self):
 -        """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
 -        if self.error:
 -            raise self.error
 -
+         # # Decode GZip'd content.
+         # if 'gzip' in self.headers.get('content-encoding', ''):
+         #     try:
+         #         self._content = decode_gzip(self._content)
+         #     except zlib.error:
+         #         pass
++        self._content = self.fo.read()
 +        self._content = self.raw.read() or self.raw.data
  
 -
 -class AuthManager(object):
 -    """Requests Authentication Manager."""
 -
 -    def __new__(cls):
 -        singleton = cls.__dict__.get('__singleton__')
 -        if singleton is not None:
 -            return singleton
 -
 -        cls.__singleton__ = singleton = object.__new__(cls)
 -
 -        return singleton
 -
 -
 -    def __init__(self):
 -        self.passwd = {}
 -        self._auth = {}
 -
 -
 -    def __repr__(self):
 -        return '<AuthManager [%s]>' % (self.method)
 -
 -
 -    def add_auth(self, uri, auth):
 -        """Registers AuthObject to AuthManager."""
 -
 -        uri = self.reduce_uri(uri, False)
 -
 -        # try to make it an AuthObject
 -        if not isinstance(auth, AuthObject):
 +        # Decode GZip'd content.
 +        if 'gzip' in self.headers.get('content-encoding', ''):
              try:
 -                auth = AuthObject(*auth)
 -            except TypeError:
 +                self._content = decode_gzip(self._content)
 +            except zlib.error:
                  pass
  
 -        self._auth[uri] = auth
 -
 -
 -    def add_password(self, realm, uri, user, passwd):
 -        """Adds password to AuthManager."""
 -        # 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)
 -
 -
 -    def find_user_password(self, realm, authuri):
 -        for uris, authinfo in self.passwd.iteritems():
 -            reduced_authuri = self.reduce_uri(authuri, False)
 -            for uri in uris:
 -                if self.is_suburi(uri, reduced_authuri):
 -                    return authinfo
 -
 -        return (None, None)
 -
 -
 -    def get_auth(self, uri):
 -        (in_domain, in_path) = self.reduce_uri(uri, False)
 -
 -        for domain, path, authority in (
 -            (i[0][0], i[0][1], i[1]) for i in self._auth.iteritems()
 -        ):
 -            if in_domain == domain:
 -                if path in in_path:
 -                    return authority
 -
 -
 -    def reduce_uri(self, uri, default_port=True):
 -        """Accept authority or URI and extract only the authority and path."""
 -
 -        # note HTTP URLs do not have a userinfo component
 -        parts = urllib2.urlparse.urlsplit(uri)
 -
 -        if parts[1]:
 -            # URI
 -            scheme = parts[0]
 -            authority = parts[1]
 -            path = parts[2] or '/'
 -        else:
 -            # host or host:port
 -            scheme = None
 -            authority = uri
 -            path = '/'
 -
 -        host, port = urllib2.splitport(authority)
 -
 -        if default_port and port is None and scheme is not None:
 -            dport = {"http": 80,
 -                     "https": 443,
 -                     }.get(scheme)
 -            if dport is not None:
 -                authority = "%s:%d" % (host, dport)
++        # Decode unicode content.
++        if settings.decode_unicode:
++            self._content = get_unicode_from_response(self)
++        # Decode GZip'd content.
++        if 'gzip' in self.headers.get('content-encoding', ''):
++            try:
++                self._content = decode_gzip(self._content)
++            except zlib.error:
++                pass
 -        return authority, path
 +        # Decode unicode content.
 +        if self.config.get('decode_unicode'):
 +            self._content = get_unicode_from_response(self)
++        # # Decode unicode content.
++        # if settings.decode_unicode:
++        #     self._content = get_unicode_from_response(self)
  
 +        self._content_consumed = True
 +        return self._content
  
 -    def is_suburi(self, base, test):
 -        """Check if test is below base in a URI tree
  
 -        Both args must be URIs in reduced form.
 +    def raise_for_status(self):
 +        """Raises stored :class:`HTTPError` or :class:`URLError`,
 +        if one occured.
          """
 -        if base == test:
 -            return True
 -        if base[0] != test[0]:
 -            return False
 -        common = urllib2.posixpath.commonprefix((base[1], test[1]))
 -        if len(common) == len(base[1]):
 -            return True
 -        return False
 -
 -
 -    def empty(self):
 -        self.passwd = {}
 -
 -
 -    def remove(self, uri, realm=None):
 -        # uri could be a single URI or a sequence
 -        if isinstance(uri, basestring):
 -            uri = [uri]
 -
 -        for default_port in True, False:
 -            reduced_uri = tuple([self.reduce_uri(u, default_port) for u in uri])
 -            del self.passwd[reduced_uri][realm]
 -
 -
 -    def __contains__(self, uri):
 -        # uri could be a single URI or a sequence
 -        if isinstance(uri, basestring):
 -            uri = [uri]
 -
 -        uri = tuple([self.reduce_uri(u, False) for u in uri])
 -
 -        if uri in self.passwd:
 -            return True
 -
 -        return False
 -
 -auth_manager = AuthManager()
 -
  
 +        if self.error:
 +            raise self.error
  
 -class AuthObject(object):
 -    """The :class:`AuthObject` is a simple HTTP Authentication token. When
 -    given to a Requests function, it enables Basic HTTP Authentication for that
 -    Request. You can also enable Authorization for domain realms with AutoAuth.
 -    See AutoAuth for more details.
 +        if (self.status_code >= 300) and (self.status_code < 400):
 +            raise Exception('300 yo')
  
 -    :param username: Username to authenticate with.
 -    :param password: Password for given username.
 -    :param realm: (optional) the realm this auth applies to
 -    :param handler: (optional) basic || digest || proxy_basic || proxy_digest
 -    """
 +        elif (self.status_code >= 400) and (self.status_code < 500):
 +            raise Exception('400 yo')
  
 -    _handlers = {
 -        'basic': HTTPBasicAuthHandler,
 -        'forced_basic': HTTPForcedBasicAuthHandler,
 -        'digest': HTTPDigestAuthHandler,
 -        'proxy_basic': urllib2.ProxyBasicAuthHandler,
 -        'proxy_digest': urllib2.ProxyDigestAuthHandler
 -    }
 -
 -    def __init__(self, username, password, handler='forced_basic', realm=None):
 -        self.username = username
 -        self.password = password
 -        self.realm = realm
 -
 -        if isinstance(handler, basestring):
 -            self.handler = self._handlers.get(handler.lower(), HTTPForcedBasicAuthHandler)
 -        else:
 -            self.handler = handler
 +        elif (self.status_code >= 500) and (self.status_code < 600):
 +            raise Exception('500 yo')