expose timeout

This commit is contained in:
j 2014-05-25 12:59:19 +02:00
parent 9e53bfe236
commit 797606db01

View file

@ -142,12 +142,15 @@ def resolve_names(objects, key='name'):
class API(object): class API(object):
base = 'https://openlibrary.org/api' base = 'https://openlibrary.org/api'
def _request(self, action, data): def _request(self, action, data, timeout=None):
for key in data: for key in data:
if not isinstance(data[key], basestring): if not isinstance(data[key], basestring):
data[key] = json.dumps(data[key]) data[key] = json.dumps(data[key])
url = self.base + '/' + action + '?' + urlencode(data) url = self.base + '/' + action + '?' + urlencode(data)
if timeout is None:
result = json.loads(read_url(url)) result = json.loads(read_url(url))
else:
result = json.loads(read_url(url, timeout=timeout))
if 'status' in result and result['status'] == 'error' or 'error' in result: if 'status' in result and result['status'] == 'error' or 'error' in result:
logger.info('FAILED %s %s', action, data) logger.info('FAILED %s %s', action, data)
logger.info('URL %s', url) logger.info('URL %s', url)