From 7266dd3cd58c4400f218bbf5446c90680e85c1e8 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 13:44:03 +0100 Subject: [PATCH 1/4] Basic implementation of creating new secret endpoint --- src/rectes/main.py | 19 +++++++++++++++++++ src/rectes/utils.py | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/rectes/utils.py diff --git a/src/rectes/main.py b/src/rectes/main.py index 0aa38c5..ca2411f 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -1,8 +1,27 @@ from fastapi import FastAPI +from pydantic import BaseModel + +from rectes.redis_db import redis +from rectes.utils import get_new_key app = FastAPI() +class Secret(BaseModel): + data: str + + @app.get("/") def read_root(): return {"Hello": "World"} + + +@app.post("/secret") +async def set_secret(data: Secret): + 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}", + } diff --git a/src/rectes/utils.py b/src/rectes/utils.py new file mode 100644 index 0000000..8fccdf2 --- /dev/null +++ b/src/rectes/utils.py @@ -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 From 3a0ae4503a90773c1eedd3fec8f767728b00d177 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 13:44:28 +0100 Subject: [PATCH 2/4] Removed hello world endpoint --- src/rectes/main.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/rectes/main.py b/src/rectes/main.py index ca2411f..1bc2a2a 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -11,11 +11,6 @@ class Secret(BaseModel): data: str -@app.get("/") -def read_root(): - return {"Hello": "World"} - - @app.post("/secret") async def set_secret(data: Secret): new_key = await get_new_key() From 50e886f4d646a4fad652bebbf4ef0c2c5bf0bf3e Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 14:33:26 +0100 Subject: [PATCH 3/4] Documentation on endpoint return data --- src/rectes/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rectes/main.py b/src/rectes/main.py index 1bc2a2a..0609776 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -11,7 +11,12 @@ class Secret(BaseModel): data: str -@app.post("/secret") +class SecretSaveResult(BaseModel): + status: str + retrieval_url: str + + +@app.post("/secret", response_model=SecretSaveResult) async def set_secret(data: Secret): new_key = await get_new_key() await redis.setex(new_key, 60 * 60 * 24, data.data) From db72c2a6e48c5e5c1acaa38415c7c61934060d50 Mon Sep 17 00:00:00 2001 From: Ivan Golikov Date: Sat, 17 Dec 2022 14:34:40 +0100 Subject: [PATCH 4/4] Moved API models to separate file --- src/rectes/main.py | 11 +---------- src/rectes/models.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 src/rectes/models.py diff --git a/src/rectes/main.py b/src/rectes/main.py index 0609776..5e23523 100644 --- a/src/rectes/main.py +++ b/src/rectes/main.py @@ -1,21 +1,12 @@ from fastapi import FastAPI -from pydantic import BaseModel +from rectes.models import Secret, SecretSaveResult from rectes.redis_db import redis from rectes.utils import get_new_key app = FastAPI() -class Secret(BaseModel): - data: str - - -class SecretSaveResult(BaseModel): - status: str - retrieval_url: str - - @app.post("/secret", response_model=SecretSaveResult) async def set_secret(data: Secret): new_key = await get_new_key() diff --git a/src/rectes/models.py b/src/rectes/models.py new file mode 100644 index 0000000..c51f16d --- /dev/null +++ b/src/rectes/models.py @@ -0,0 +1,10 @@ +from pydantic import BaseModel + + +class Secret(BaseModel): + data: str + + +class SecretSaveResult(BaseModel): + status: str + retrieval_url: str