fix: apply timeout parameter to all aiohttp sessions in db_calls.py (#4)
Closes #4 Every async DB function accepted a `timeout` parameter but never passed it to aiohttp, causing scripts to hang indefinitely if the API became unresponsive. Fixed by passing `aiohttp.ClientTimeout(total=timeout)` to each `aiohttp.ClientSession()` constructor across all six functions: db_get, url_get, db_patch, db_post, db_put, db_delete. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f1ca14791d
commit
bd1809261e
32
db_calls.py
32
db_calls.py
@ -25,7 +25,7 @@ def param_char(other_params):
|
||||
def get_req_url(
|
||||
endpoint: str, api_ver: int = 2, object_id: int = None, params: list = None
|
||||
):
|
||||
req_url = f'{DB_URL}/v{api_ver}/{endpoint}{"/" if object_id is not None else ""}{object_id if object_id is not None else ""}'
|
||||
req_url = f"{DB_URL}/v{api_ver}/{endpoint}{'/' if object_id is not None else ''}{object_id if object_id is not None else ''}"
|
||||
|
||||
if params:
|
||||
other_params = False
|
||||
@ -39,11 +39,11 @@ def get_req_url(
|
||||
def log_return_value(log_string: str):
|
||||
if master_debug:
|
||||
logger.info(
|
||||
f'return: {log_string[:1200]}{" [ S N I P P E D ]" if len(log_string) > 1200 else ""}\n'
|
||||
f"return: {log_string[:1200]}{' [ S N I P P E D ]' if len(log_string) > 1200 else ''}\n"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f'return: {log_string[:1200]}{" [ S N I P P E D ]" if len(log_string) > 1200 else ""}\n'
|
||||
f"return: {log_string[:1200]}{' [ S N I P P E D ]' if len(log_string) > 1200 else ''}\n"
|
||||
)
|
||||
|
||||
|
||||
@ -59,7 +59,9 @@ async def db_get(
|
||||
log_string = f"get:\n{endpoint} id: {object_id} params: {params}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession(headers=AUTH_TOKEN) as session:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=AUTH_TOKEN, timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.get(req_url) as r:
|
||||
logger.info(f"session info: {r}")
|
||||
if r.status == 200:
|
||||
@ -80,7 +82,9 @@ async def url_get(url: str, timeout: int = 3):
|
||||
log_string = f"get:\n{url}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with aiohttp.ClientSession(
|
||||
timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.get(url) as r:
|
||||
if r.status == 200:
|
||||
log_string = "200 received"
|
||||
@ -99,7 +103,9 @@ async def db_patch(
|
||||
log_string = f"patch:\n{endpoint} {params}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession(headers=AUTH_TOKEN) as session:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=AUTH_TOKEN, timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.patch(req_url) as r:
|
||||
if r.status == 200:
|
||||
js = await r.json()
|
||||
@ -118,7 +124,9 @@ async def db_post(
|
||||
log_string = f"post:\n{endpoint} payload: {payload}\ntype: {type(payload)}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession(headers=AUTH_TOKEN) as session:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=AUTH_TOKEN, timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.post(req_url, json=payload) as r:
|
||||
if r.status == 200:
|
||||
js = await r.json()
|
||||
@ -137,7 +145,9 @@ async def db_put(
|
||||
log_string = f"put:\n{endpoint} payload: {payload}\ntype: {type(payload)}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession(headers=AUTH_TOKEN) as session:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=AUTH_TOKEN, timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.put(req_url, json=payload) as r:
|
||||
if r.status == 200:
|
||||
js = await r.json()
|
||||
@ -154,7 +164,9 @@ async def db_delete(endpoint: str, object_id: int, api_ver: int = 2, timeout=3):
|
||||
log_string = f"delete:\n{endpoint} {object_id}"
|
||||
logger.info(log_string) if master_debug else logger.debug(log_string)
|
||||
|
||||
async with aiohttp.ClientSession(headers=AUTH_TOKEN) as session:
|
||||
async with aiohttp.ClientSession(
|
||||
headers=AUTH_TOKEN, timeout=aiohttp.ClientTimeout(total=timeout)
|
||||
) as session:
|
||||
async with session.delete(req_url) as r:
|
||||
if r.status == 200:
|
||||
js = await r.json()
|
||||
@ -183,4 +195,4 @@ def get_player_data(
|
||||
def player_desc(this_player) -> str:
|
||||
if this_player["p_name"] in this_player["description"]:
|
||||
return this_player["description"]
|
||||
return f'{this_player["description"]} {this_player["p_name"]}'
|
||||
return f"{this_player['description']} {this_player['p_name']}"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user