Skip to content

Commit

Permalink
Fall back on requests if aiohttp fails
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrieb committed Aug 31, 2020
1 parent 9f77953 commit d1d8b76
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
58 changes: 40 additions & 18 deletions github_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Dict, List, Optional, Set, Tuple

import aiohttp
import requests


###############################################################################
Expand Down Expand Up @@ -34,11 +35,19 @@ async def query(self, generated_query: str) -> Dict:
headers = {
"Authorization": f"Bearer {self.access_token}",
}
async with self.semaphore:
r = await self.session.post("https://api.github.com/graphql",
headers=headers,
json={"query": generated_query})
return await r.json()
try:
async with self.semaphore:
r = await self.session.post("https://api.github.com/graphql",
headers=headers,
json={"query": generated_query})
return await r.json()
except:
# Fall back on non-async requests
async with self.semaphore:
r = requests.post("https://api.github.com/graphql",
headers=headers,
json={"query": generated_query})
return r.json()

async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
"""
Expand All @@ -56,19 +65,32 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
params = dict()
if path.startswith("/"):
path = path[1:]
async with self.semaphore:
r = await self.session.get(f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()))
if r.status == 202:
# print(f"{path} returned 202. Retrying...")
print(f"A path returned 202. Retrying...")
await asyncio.sleep(1)
continue

result = await r.json()
if result is not None:
return result
try:
async with self.semaphore:
r = await self.session.get(f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()))
if r.status == 202:
# print(f"{path} returned 202. Retrying...")
print(f"A path returned 202. Retrying...")
await asyncio.sleep(1)
continue

result = await r.json()
if result is not None:
return result
except:
# Fall back on non-async requests
async with self.semaphore:
r = requests.get(f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()))
if r.status_code == 202:
print(f"A path returned 202. Retrying...")
await asyncio.sleep(1)
continue

return r.json()

@staticmethod
def repos_overview(contrib_cursor: Optional[str] = None,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests
aiohttp

0 comments on commit d1d8b76

Please sign in to comment.