Permalink
Browse files

better error presentation and more bulk utilities

  • Loading branch information...
1 parent f1dacff commit f7080c5658e94562c585294e2889124c0c3c45f9 @prior committed Apr 10, 2012
Showing with 34 additions and 34 deletions.
  1. +14 −10 giftwrap/error.py
  2. +18 −22 giftwrap/exchange.py
  3. +2 −2 setup.py
View
24 giftwrap/error.py
@@ -1,4 +1,5 @@
from utils.exception import Error as BaseError
+from utils.property import is_cached
class Error(BaseError): pass
@@ -8,10 +9,11 @@ def __init__(self, msg=None, err=None, exchange=None):
self.exchange = exchange
def __unicode__(self):
- url = '???'
- try: url = self.exchange.url
+ url = '?'
+ try:
+ url = self.exchange.url
except: pass
- return u'%s(url=%s):%s' % (self.msg, url, self.wrapped_error_str)
+ return u'%s (url=%s ) %s' % (self.msg, url, self.wrapped_error_str)
def __repr__(self):
return '%s(%s, %s, %s)' % (self.__class__.__name__, repr(self.msg), repr(self.err), repr(self.exchange))
@@ -20,23 +22,25 @@ class RequestError(ExchangeError):
class TimeoutError(ExchangeError):
def __unicode__(self):
- url = '???'
- timeout = '???'
+ url = '?'
+ timeout = '?'
try:
url = self.exchange.url
timeout = self.exchange.timeout
except: pass
- return u'%s(url=%s,timeout=%s):%s' % (self.msg, url, timeout, self.wrapped_error_str)
+ return u'%s (url=%s timeout=%s ) %s' % (self.msg, url, timeout, self.wrapped_error_str)
class ResponseError(ExchangeError):
def __unicode__(self):
- url = '???'
- status_code = '???'
+ url = '?'
+ status_code = '?'
+ text = '?'
try:
- url = self.exchange._url
+ url = self.exchange.url
status_code = self.exchange.response.status_code
+ text = self.exchange.response.content
except: pass
- return u'%s(%s url=%s):%s' % (self.msg, status_code, url, self.wrapped_error_str)
+ return u'%s (%s url=%s %s ) %s' % (self.msg, status_code, url, text, self.wrapped_error_str)
class JsonParseError(ResponseError):
def __unicode__(self):
View
40 giftwrap/exchange.py
@@ -56,6 +56,8 @@ def _process_response(self):
except requests.exceptions.Timeout as err:
return self._retry_or_fail(error.TimeoutError(err=err, exchange=self))
except requests.exceptions.RequestException as err:
+ if is_cached(self,'response') and (self.response.status_code < 200 or self.response.status_code >= 300):
+ return self._retry_or_fail(error.ResponseError(err=err, exchange=self))
return self._retry_or_fail(error.RequestError(err=err, exchange=self))
if not self.response.status_code or self.response.status_code < 200 or self.response.status_code >= 300:
return self._retry_or_fail(error.ResponseError(err=err, exchange=self))
@@ -66,32 +68,26 @@ def process_response(self,response): raise NotImplementedError()
# no need to do synchronous batch calls, cuz they happen automaticaly lazily-- only need to be proactive with asynchronous calls
@classmethod
- def async_exchange(kls, exchanges, async=True):
+ def async_exchanges(kls, exchanges):
for exchange,response in zip(exchanges, requests.async.map([e.request for e in exchanges if not e.triggered])):
exchange.response = response
return exchanges
+ @classmethod
+ def bulk_exchanges(kls, exchanges, async=True):
+ if async: return kls.async_exchange(exchanges)
+ [e.response for e in exchanges]
+ return exchanges
+ # for backward compatability
+ async_exchange = async_exchanges
+ bulk_exchange = bulk_exchanges
+ @classmethod
+ def async_results(kls, exchanges):
+ return [e.result for e in kls.async_exchanges(exchanges)]
- ## to enable mockability
-
- #@classmethod
- #def mockify(kls, response=None, result=None):
- #kls._old_init = kls.__init__
- #kls.__init__ = kls._mockify_init
- #kls._mock_response = response
- #kls._mock_result = result
-
- #def unmockify(kls):
- #if kls.__init__ == kls._mockify_init:
- #kls.__init__ = kls._old_init__
-
- #def _mockify_init(self, *args, **kwargs):
- #mock_response = self.kwargs.pop('response',getattr(self.__class__,'_mock_response',None))
- #mock_result = self.kwargs.pop('result',getattr(self.__class__,'_mock_result',None))
- #self._old_init(*args, **kwargs)
- #if self.mock_response: self.response = mock_response
- #if self.mock_result: self.result = mock_result
-
-
+ @classmethod
+ def bulk_results(kls, exchanges, async=True):
+ return [e.result for e in kls.bulk_exchanges(exchanges, async)]
+
View
4 setup.py
@@ -3,12 +3,12 @@
setup(
name='giftwrap',
- version='1.2.2',
+ version='1.3.0',
description='Python Api Wraping Toolset',
author='Michael Prior',
author_email='prior@cracklabs.com',
url='https://github.com/prior/giftwrap',
- download_url='https://github.com/prior/giftwrap/tarball/v1.2.2',
+ download_url='https://github.com/prior/giftwrap/tarball/v1.3.0',
packages=['giftwrap'],
install_requires=[]
)

0 comments on commit f7080c5

Please sign in to comment.