Skip to content

Commit

Permalink
Add timeout to limit indefinite 202s
Browse files Browse the repository at this point in the history
Fix #2
Fix #3
  • Loading branch information
jstrieb committed Oct 5, 2020
1 parent ed9afe4 commit f44d675
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions github_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async def query(self, generated_query: str) -> Dict:
json={"query": generated_query})
return await r.json()
except:
print("aiohttp failed for GraphQL query")
# Fall back on non-async requests
async with self.semaphore:
r = requests.post("https://api.github.com/graphql",
Expand All @@ -57,7 +58,7 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
:return: deserialized REST JSON output
"""

while True:
for _ in range(60):
headers = {
"Authorization": f"token {self.access_token}",
}
Expand All @@ -80,6 +81,7 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
if result is not None:
return result
except:
print("aiohttp failed for rest query")
# Fall back on non-async requests
async with self.semaphore:
r = requests.get(f"https://api.github.com/{path}",
Expand All @@ -89,8 +91,11 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
print(f"A path returned 202. Retrying...")
await asyncio.sleep(1)
continue

return r.json()
elif r.status_code == 200:
return r.json()
# print(f"There were too many 202s. Data for {path} will be incomplete.")
print("There were too many 202s. Data for this repository will be incomplete.")
return dict()

@staticmethod
def repos_overview(contrib_cursor: Optional[str] = None,
Expand Down

0 comments on commit f44d675

Please sign in to comment.