Merge pull request 'Basic endpoint implementation for storing new secret' (#3) from set-secret-endpoint into main

This commit is contained in:
ivnglkv 2022-12-17 16:38:01 +03:00
commit 9f46c5cd21
3 changed files with 34 additions and 3 deletions

View file

@ -1,8 +1,18 @@
from fastapi import FastAPI from fastapi import FastAPI
from rectes.models import Secret, SecretSaveResult
from rectes.redis_db import redis
from rectes.utils import get_new_key
app = FastAPI() app = FastAPI()
@app.get("/") @app.post("/secret", response_model=SecretSaveResult)
def read_root(): async def set_secret(data: Secret):
return {"Hello": "World"} new_key = await get_new_key()
await redis.setex(new_key, 60 * 60 * 24, data.data)
return {
"status": "saved",
"retrieval_url": f"/secret/{new_key}",
}

10
src/rectes/models.py Normal file
View file

@ -0,0 +1,10 @@
from pydantic import BaseModel
class Secret(BaseModel):
data: str
class SecretSaveResult(BaseModel):
status: str
retrieval_url: str

11
src/rectes/utils.py Normal file
View file

@ -0,0 +1,11 @@
from uuid import uuid4
from rectes.redis_db import redis
async def get_new_key() -> str:
while True:
new_key = str(uuid4())
if not await redis.exists(new_key):
return new_key