Skip to content

Commit

Permalink
Merge pull request jgorset#148 from metric-collective/retry-paginated
Browse files Browse the repository at this point in the history
Retries Apply to Paginated Requests
  • Loading branch information
jgorset committed Jun 29, 2016
2 parents 254f6da + 0d9771f commit a58b99b
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions facepy/graph_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
import hashlib
import hmac
import logging

try:
import urllib.parse as urlparse
Expand Down Expand Up @@ -294,9 +295,25 @@ def load(method, url, data):

return result, next_url

def load_with_retry(method, url, data):
remaining_retries = retry
while True:
try:
return load(method, url, data)
except FacepyError as e:
logging.warn("Exception on %s: %s, retries remaining: %s" % (
url,
e,
remaining_retries,
))
if remaining_retries > 0:
remaining_retries -= 1
else:
raise

def paginate(method, url, data):
while url:
result, url = load(method, url, data)
result, url = load_with_retry(method, url, data)

# Reset pagination parameters.
for key in ['offset', 'until', 'since']:
Expand Down Expand Up @@ -325,16 +342,10 @@ def paginate(method, url, data):
if self.appsecret and self.oauth_token:
data['appsecret_proof'] = self._generate_appsecret_proof()

try:
if page:
return paginate(method, url, data)
else:
return load(method, url, data)[0]
except FacepyError:
if retry:
return self._query(method, path, data, page, retry - 1)
else:
raise
if page:
return paginate(method, url, data)
else:
return load_with_retry(method, url, data)[0]

def _get_url(self, path):
# When Facebook returns nested resources (like comments for a post), it
Expand Down

0 comments on commit a58b99b

Please sign in to comment.