From: Kenneth Reitz Date: Sun, 25 Sep 2011 20:00:44 +0000 (-0400) Subject: Merge branch 'develop' into urllib3 X-Git-Tag: v0.8.0~94^2~75 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d2894b3c58dc40e5666049d6300b38ffb79d9007;p=services%2Fpython-requests.git Merge branch 'develop' into urllib3 Conflicts: requests/api.py requests/exceptions.py requests/models.py --- d2894b3c58dc40e5666049d6300b38ffb79d9007 diff --cc requests/api.py index a8c2f85,48823a9..12381ce --- a/requests/api.py +++ b/requests/api.py @@@ -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. diff --cc requests/exceptions.py index c7aeca6,3a65027..ecedd7f --- a/requests/exceptions.py +++ b/requests/exceptions.py @@@ -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.""" @@@ -16,5 -23,10 +26,13 @@@ 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.""" diff --cc requests/models.py index 4834fe3,f6d84eb..a7a8c81 --- a/requests/models.py +++ b/requests/models.py @@@ -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 ( @@@ -271,16 -244,11 +268,15 @@@ 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. @@@ -317,103 -283,21 +314,105 @@@ 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."""