Retry logic for Mercury API

This commit is contained in:
Fabio Manganiello 2018-08-19 21:51:59 +00:00
parent d2bd49b364
commit 09d7d98ae3

View file

@ -65,13 +65,30 @@ class RssUpdates(HttpRequest):
def _parse_entry_content(self, link): def _parse_entry_content(self, link):
response = None response = None
err = None
n_tries = 5
try: for _ in range(0, n_tries):
self.logger.info('Parsing content for {}'.format(link)) try:
response = requests.get('https://mercury.postlight.com/parser', self.logger.info('Parsing content for {}'.format(link))
params = {'url': link}, response = requests.get('https://mercury.postlight.com/parser',
headers = {'x-api-key': self.mercury_api_key }) params = {'url': link},
except Exception as e: self.logger.exception(e) headers = {'x-api-key': self.mercury_api_key })
except Exception as e:
err = e
if response.text:
err = None
break
else:
time.sleep(1)
if err:
raise err
if not response.text:
raise RuntimeError("No response from Mercury API for URL {} after {} tries"
.format(link, n_tries))
return response.json()['content'] if response and response.ok else None return response.json()['content'] if response and response.ok else None