2022-12-17 12:44:03 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2025-01-01 18:18:38 +00:00
|
|
|
from redis.asyncio import Redis
|
2022-12-17 12:44:03 +00:00
|
|
|
|
2025-01-01 19:01:10 +00:00
|
|
|
from pssecret_server.models import Secret
|
2022-12-17 12:44:03 +00:00
|
|
|
|
2025-01-01 18:18:38 +00:00
|
|
|
|
|
|
|
async def get_new_key(redis: Redis) -> str:
|
|
|
|
"""Returns free Redis key"""
|
2022-12-17 12:44:03 +00:00
|
|
|
while True:
|
|
|
|
new_key = str(uuid4())
|
|
|
|
|
|
|
|
if not await redis.exists(new_key):
|
|
|
|
return new_key
|
2025-01-01 18:18:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def save_secret(data: Secret, redis: Redis) -> str:
|
|
|
|
"""Save passed data, returns retrieval key"""
|
|
|
|
new_key = await get_new_key(redis)
|
|
|
|
await redis.setex(new_key, 60 * 60 * 24, data.data)
|
|
|
|
|
|
|
|
return new_key
|