Commits

Viorel Stirbu  committed 9ddd730

replaced urllib2 with requests library

  • Participants
  • Parent commits 4f73842

Comments (0)

Files changed (2)

File atexpc/ancora_api/api.py

 import operator
 from urlparse import urlparse, urlunparse, parse_qsl
 from urllib import urlencode
-from urllib2 import urlopen, URLError, HTTPError # TODO: try urllib3 with connection pooling
 from django.core.cache import cache as django_cache
 
+import requests
+from requests.exceptions import ConnectionError, Timeout
+
 import logging
 logger = logging.getLogger(__name__)
 
 
     def _read_backend(self, uri):
         try:
-            return urlopen(self.normalize_uri(uri)).read()
-        except HTTPError as e:
-            raise APIError("The backend couldn't fulfill the request. Error code: %s"
-                           % e.code)
-        except URLError as e:
-            raise APIError("We failed to reach backend. Reason: %s"
-                           % e.reason)
+            response = requests.get(self.normalize_uri(uri), timeout=10)
+            return response.text
+        except (ConnectionError, Timeout) as e:
+            raise APIError("Failed to reach backend (%s)" % type(e).__name__)
 
     def read(self, uri, post_process=None, use_backend=None, cache_timeout=TIMEOUT_SHORT):
         """ Read data from backend and cache processed data

File requirements/common.txt

 django-pylibmc-sasl==0.2.4
 psycopg2==2.4.5
 south==0.7.6
+requests==0.14.2
 sorl-thumbnail==11.12
 PIL==1.1.7
 dropbox==1.4.0