Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions github_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,18 @@ async def query(self, generated_query: str) -> Dict:
headers = {
"Authorization": f"Bearer {self.access_token}",
}
url = "https://api.github.com/graphql"
try:
async with self.semaphore:
r_async = await self.session.post(
"https://api.github.com/graphql",
headers=headers,
json={"query": generated_query},
)
r_async = await self.session.post(url, headers=headers, json={"query": generated_query})
result = await r_async.json()
if result is not None:
return result
except:
print("aiohttp failed for GraphQL query")
except Exception as e:
print(f"Async failed for GraphQL query: {str(e)}")
# Fall back on non-async requests
async with self.semaphore:
r_requests = requests.post(
"https://api.github.com/graphql",
headers=headers,
json={"query": generated_query},
)
r_requests = requests.post(url, headers=headers, json={"query": generated_query},)
result = r_requests.json()
if result is not None:
return result
Expand All @@ -81,39 +74,29 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
params = dict()
if path.startswith("/"):
path = path[1:]
url = f"https://api.github.com/{path}"
try:
async with self.semaphore:
r_async = await self.session.get(
f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()),
)
if r_async.status == 202:
# print(f"{path} returned 202. Retrying...")
print(f"A path returned 202. Retrying...")
await asyncio.sleep(2)
continue

result = await r_async.json()
if result is not None:
return result
except:
print("aiohttp failed for rest query")
r = await self.session.get(url, headers=headers, params=tuple(params.items()))
if r.status == 200:
result = await r.json()
if result is not None:
return result
print(f"Async get {url} returned static code {r.status}. After 2 seconds retrying...")
await asyncio.sleep(2)
continue
except Exception as e:
print(f"Async failed for rest query: {str(e)}")
# Fall back on non-async requests
async with self.semaphore:
r_requests = requests.get(
f"https://api.github.com/{path}",
headers=headers,
params=tuple(params.items()),
)
if r_requests.status_code == 202:
print(f"A path returned 202. Retrying...")
await asyncio.sleep(2)
continue
elif r_requests.status_code == 200:
r_requests = requests.get(url, headers=headers, params=tuple(params.items()))
if r_requests.status_code == 200:
return r_requests.json()
print(f"Sync get {url} returned static code {r_requests.status_code}. After 2 seconds retrying...")
await asyncio.sleep(2)
continue
# 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.")
print("TThere are too many access failures. Data for this repository will be incomplete.")
return dict()

@staticmethod
Expand Down