diff options
author | Ian Cordasco <graffatcolmingov@gmail.com> | 2014-10-24 22:23:15 -0500 |
---|---|---|
committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2014-11-12 13:56:28 -0600 |
commit | 2eb7e3c80b345f6c6e13fadf6f1ba98efccdd2e2 (patch) | |
tree | d3f79119d08bd2c815f9d574fa1cc2becf04ec30 | |
parent | f54a4e3de186495a5254d21ae698c677c71b90c3 (diff) | |
download | python-requests-2eb7e3c80b345f6c6e13fadf6f1ba98efccdd2e2.tar.gz |
Add last few changes and add a quick test
-rw-r--r-- | requests/adapters.py | 10 | ||||
-rw-r--r-- | requests/exceptions.py | 5 | ||||
-rwxr-xr-x | test_requests.py | 14 |
3 files changed, 25 insertions, 4 deletions
diff --git a/requests/adapters.py b/requests/adapters.py index df3345fd..723b8179 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -26,14 +26,15 @@ from .packages.urllib3.exceptions import ProxyError as _ProxyError from .packages.urllib3.exceptions import ProtocolError from .packages.urllib3.exceptions import ReadTimeoutError from .packages.urllib3.exceptions import SSLError as _SSLError +from .packages.urllib3.exceptions import ResponseError from .cookies import extract_cookies_to_jar from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError, - ProxyError) + ProxyError, RetryError) from .auth import _basic_auth_str DEFAULT_POOLBLOCK = False DEFAULT_POOLSIZE = 10 -DEFAULT_RETRIES = object() +DEFAULT_RETRIES = 0 class BaseAdapter(object): @@ -79,7 +80,7 @@ class HTTPAdapter(BaseAdapter): def __init__(self, pool_connections=DEFAULT_POOLSIZE, pool_maxsize=DEFAULT_POOLSIZE, max_retries=DEFAULT_RETRIES, pool_block=DEFAULT_POOLBLOCK): - if max_retries is DEFAULT_RETRIES: + if max_retries == DEFAULT_RETRIES: self.max_retries = Retry(0, read=False) else: self.max_retries = Retry.from_int(max_retries) @@ -415,6 +416,9 @@ class HTTPAdapter(BaseAdapter): if isinstance(e.reason, ConnectTimeoutError): raise ConnectTimeout(e, request=request) + if isinstance(e.reason, ResponseError): + raise RetryError(e, request=request) + raise ConnectionError(e, request=request) except _ProxyError as e: diff --git a/requests/exceptions.py b/requests/exceptions.py index 34c7a0db..89135a80 100644 --- a/requests/exceptions.py +++ b/requests/exceptions.py @@ -90,5 +90,10 @@ class ChunkedEncodingError(RequestException): class ContentDecodingError(RequestException, BaseHTTPError): """Failed to decode response content""" + class StreamConsumedError(RequestException, TypeError): """The content for this response was already consumed""" + + +class RetryError(RequestException): + """Custom retries logic failed""" diff --git a/test_requests.py b/test_requests.py index 4a05cb2e..4624f095 100755 --- a/test_requests.py +++ b/test_requests.py @@ -20,7 +20,7 @@ from requests.compat import ( from requests.cookies import cookiejar_from_dict, morsel_to_cookie from requests.exceptions import (ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL, MissingSchema, - ReadTimeout, Timeout) + ReadTimeout, Timeout, RetryError) from requests.models import PreparedRequest from requests.structures import CaseInsensitiveDict from requests.sessions import SessionRedirectMixin @@ -1520,6 +1520,7 @@ def test_prepared_request_complete_copy(): ) assert_copy(p, p.copy()) + def test_prepare_unicode_url(): p = PreparedRequest() p.prepare( @@ -1529,5 +1530,16 @@ def test_prepare_unicode_url(): ) assert_copy(p, p.copy()) + +def test_urllib3_retries(): + from requests.packages.urllib3.util import Retry + s = requests.Session() + s.mount('https://', HTTPAdapter(max_retries=Retry( + total=2, status_forcelist=[500] + ))) + + with pytest.raises(RetryError): + s.get('https://httpbin.org/status/500') + if __name__ == '__main__': unittest.main() |