Merge branch 'develop' into urllib3
authorKenneth Reitz <me@kennethreitz.com>
Sun, 25 Sep 2011 20:00:44 +0000 (16:00 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Sun, 25 Sep 2011 20:00:44 +0000 (16:00 -0400)
Conflicts:
requests/api.py
requests/exceptions.py
requests/models.py

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

diff --cc AUTHORS
Simple merge
diff --cc requests/api.py
index a8c2f85d4771161851937e4d707dd59086b93911,48823a9a4c8aa8bfa958de30a8be2c5b3e213be0..12381ce39d31d86ae7210439f22a53bde8fcd2fd
@@@ -56,17 -54,17 +56,39 @@@ def request(method, url
              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,
 +        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,
+         cookiejar=cookies,
+         files=files,
+         auth=auth,
+         timeout=timeout or config.settings.timeout,
+         allow_redirects=allow_redirects,
+         proxies=proxies or config.settings.proxies,
      )
  
      # Arguments manipulation hook.
index c7aeca634dcdf3fb38a8df6ae92282dd65e8a308,3a6502704335d3ec0f719e30d04e6c313f867d36..ecedd7f66fdcd76819bdca9cc8b24afa330a3643
@@@ -10,6 -11,11 +11,14 @@@ class RequestException(Exception)
      """There was an ambiguous exception that occured while handling your
      request."""
  
++class AuthenticationError(RequestException):
++    """The authentication credentials provided were invalid."""
++    
+ class AuthenticationError(RequestException):
+     """The authentication credentials provided were invalid."""
  class Timeout(RequestException):
      """The request timed out."""
  
  class URLRequired(RequestException):
      """A valid URL is required to make a request."""
  
++class InvalidMethod(RequestException):
++    """An inappropriate method was attempted."""
++
+ class InvalidMethod(RequestException):
+     """An inappropriate method was attempted."""
  class TooManyRedirects(RequestException):
      """Too many redirects."""
index 4834fe32f15921628e372e662119da4535e53631,f6d84ebd563bce0b95af5430aa8e1a68bec164b6..a7a8c815163ed1cb8ee1bd7a1172a0d5552181da
@@@ -182,36 -176,30 +176,39 @@@ class Request(object)
          def build(resp):
  
              response = Response()
 -            response.status_code = getattr(resp, 'code', None)
 +            response.status_code = getattr(resp, 'status', None)
  
              try:
 -                response.headers = CaseInsensitiveDict(getattr(resp.info(), 'dict', None))
 -                response.fo = resp
 +                response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))
 +                response.raw = resp._raw
  
 -                if self.cookiejar:
 +                # if self.cookiejar:
 +
++                    response.cookies = dict_from_cookiejar(self.cookiejar)
++
++
 +                    # response.cookies = dict_from_cookiejar(self.cookiejar)
  
+                     response.cookies = dict_from_cookiejar(self.cookiejar)
              except AttributeError:
                  pass
  
              if is_error:
                  response.error = resp
  
 -            response.url = getattr(resp, 'url', None)
 -
              return response
  
 +        # Request collector.
          history = []
  
 +        # Create the lone response object.
          r = build(resp)
  
 +        # Store the HTTP response, just in case.
 +        r._response = resp
 +
 +        # It's a redirect, and we're not already in a redirect loop.
          if r.status_code in REDIRECT_STATI and not self.redirect:
  
              while (
                  request.send()
                  r = request.response
  
 +            # Insert collected history.
              r.history = history
  
 +        # Attach Response to Request.
          self.response = r
 +
 +        # Give Response some context.
          self.response.request = self
  
      @staticmethod
      def _encode_params(data):
          """Encode parameters in a piece of data.
          if isinstance(path, unicode):
              path = path.encode('utf-8')
  
 +        # URL-encode the path.
          path = urllib.quote(path, safe="%/:=&?~#+!$,;'@()*[]")
  
 +        # Turn it back into a bytestring.
 +        self.url = str(urlunparse([scheme, netloc, path, params, query, fragment]))
+         self.url = str(urlunparse(
+           [scheme, netloc, path, params, query, fragment]
+          ))
  
 +        # Query Parameters?
          if self._enc_params:
 +
 +            # If query parameters already exist in the URL, append.
              if urlparse(self.url).query:
                  return '%s&%s' % (self.url, self._enc_params)
 +
 +            # Otherwise, have at it.
              else:
                  return '%s?%s' % (self.url, self._enc_params)
 +
          else:
 +            # Kosher URL.
              return self.url
  
 -    def send(self, anyway=False):
 +    def send(self, connection=None, anyway=False):
 +        """Sends the shit."""
 +
 +        # Safety check.
 +        self._checks()
 +
 +        # Build the final URL.
 +        url = self._build_url()
 +
 +        # Setup Files.
 +        if self.files:
 +            pass
 +
 +        # Setup form data.
 +        elif self.data:
 +            pass
 +
 +        # Setup cookies.
 +        # elif self.cookies:
 +            # pass
 +
 +        # req = _Request(url, data=self._enc_data, method=self.method)
 +
 +        # Only send the Request if new or forced.
 +        if (anyway) or (not self.sent):
 +
 +            try:
 +                # Create a new HTTP connection, since one wasn't passed in.
 +                if not connection:
 +                    connection = urllib3.connection_from_url(url,
 +                        timeout=self.timeout)
 +
 +                    # One-off request. Delay fetching the content until needed.
 +                    do_block = False
 +                else:
 +                    # Part of a connection pool, so no fancy stuff. Sorry!
 +                    do_block = True
 +
 +                # Create the connection.
 +                r = connection.urlopen(
 +                    method=self.method,
 +                    url=url,
 +                    body=self.data,
 +                    headers=self.headers,
 +                    redirect=False,
 +                    assert_same_host=False,
 +                    block=do_block
 +                )
 +
 +                # Extract cookies.
 +                # if self.cookiejar is not None:
 +                    # self.cookiejar.extract_cookies(resp, req)
 +
 +            # except (urllib2.HTTPError, urllib2.URLError), why:
 +            except Exception, why:
 +                # if hasattr(why, 'reason'):
 +                #     if isinstance(why.reason, socket.timeout):
 +                #         why = Timeout(why)
 +
 +                # self._build_response(why, is_error=True)
 +                print 'FUCK'
 +                print why
 +
 +            else:
 +                self._build_response(r)
 +                self.response.ok = True
 +
 +
 +        self.sent = self.response.ok
 +
 +        return self.sent
 +
 +
 +
 +    def old_send(self, anyway=False):
          """Sends the request. Returns True of successful, false if not.
          If there was an HTTPError during transmission,
          self.response.status_code will contain the HTTPError code.
@@@ -607,24 -488,11 +603,24 @@@ class Response(object)
          self._content_consumed = True
          return self._content
  
      def raise_for_status(self):
 -        """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
 +        """Raises stored :class:`HTTPError` or :class:`URLError`,
 +        if one occured.
 +        """
 +
          if self.error:
              raise self.error
  
 +        if (self.status_code >= 300) and (self.status_code < 400):
 +            raise Exception('300 yo')
 +
++
 +        elif (self.status_code >= 400) and (self.status_code < 500):
 +            raise Exception('400 yo')
 +
 +        elif (self.status_code >= 500) and (self.status_code < 600):
 +            raise Exception('500 yo')
 +
  
  class AuthManager(object):
      """Requests Authentication Manager."""