prevent oops in future

This commit is contained in:
monosans 2023-10-06 10:53:22 +03:00
parent f87be092a5
commit 8b11b75c11
No known key found for this signature in database

View file

@ -149,8 +149,9 @@ async def run_checks(incoming_request: fastapi.Request):
async def get_crypto_price(cryptocurrency: str) -> float: async def get_crypto_price(cryptocurrency: str) -> float:
"""Gets the price of a cryptocurrency using coinbase's API.""" """Gets the price of a cryptocurrency using coinbase's API."""
cache_path = os.path.join('cache', 'crypto_prices.json')
try: try:
async with aiofiles.open(os.path.join('cache', 'crypto_prices.json')) as f: async with aiofiles.open(cache_path) as f:
content = await f.read() content = await f.read()
except FileNotFoundError: except FileNotFoundError:
cache = {} cache = {}
@ -167,7 +168,7 @@ async def get_crypto_price(cryptocurrency: str) -> float:
cache[cryptocurrency] = usd_price cache[cryptocurrency] = usd_price
cache['_last_updated'] = time.time() cache['_last_updated'] = time.time()
async with aiofiles.open(os.path.join('cache', 'crypto_prices.json'), 'w') as f: async with aiofiles.open(cache_path, 'w') as f:
for chunk in json.JSONEncoder().iterencode(cache): for chunk in json.JSONEncoder().iterencode(cache):
await f.write(chunk) await f.write(chunk)