This commit is contained in:
envolution 2024-11-19 23:34:00 -05:00
parent 15da543c55
commit 200d7843c5

View file

@ -21,13 +21,19 @@ GITHUB_GRAPHQL_URL = 'https://api.%s/graphql'
async def get_http_client(): async def get_http_client():
"""Initialize and return the HTTP client.""" """Initialize and return the HTTP client."""
global http_client global _http_client
if http_client is None: if _http_client is None:
if asyncio.iscoroutine(session): if asyncio.iscoroutine(session):
http_client = await session # Properly await the session coroutine
client = await session
# Ensure the client supports async context management
if hasattr(client, '__aenter__'):
_http_client = client
else: else:
http_client = session raise RuntimeError("HTTP client must support async context management")
return http_client else:
_http_client = session
return _http_client
async def execute_github_query(host: str, owner: str, reponame: str, token: str) -> dict: async def execute_github_query(host: str, owner: str, reponame: str, token: str) -> dict:
""" """
@ -43,15 +49,27 @@ async def execute_github_query(host: str, owner: str, reponame: str, token: str)
query_vars = QUERY_GITHUB.replace("$owner", owner).replace("$name", reponame) query_vars = QUERY_GITHUB.replace("$owner", owner).replace("$name", reponame)
async with client.post( try:
# Create the request without using async with
response = await client.post(
GITHUB_GRAPHQL_URL % host, GITHUB_GRAPHQL_URL % host,
headers=headers, headers=headers,
json={'query': query_vars} json={'query': query_vars}
) as res: )
j = await res.json()
if 'errors' in j: # Handle the response manually
raise GetVersionError(f"GitHub API error: {j['errors']}") try:
return j['data']['repository'] data = await response.json()
if 'errors' in data:
raise GetVersionError(f"GitHub API error: {data['errors']}")
return data['data']['repository']
finally:
# Ensure we clean up the response
if hasattr(response, 'close'):
await response.close()
except Exception as e:
logger.error("GitHub API request failed", error=str(e))
raise
def get_github_token(conf: Entry, host: str, keymanager: KeyManager) -> Optional[str]: def get_github_token(conf: Entry, host: str, keymanager: KeyManager) -> Optional[str]:
"""Get GitHub token from config, keymanager, or environment.""" """Get GitHub token from config, keymanager, or environment."""