Skip to content

Commit 3b3dea8

Browse files
committed
GH-4 # merge APIPrefix and Prefix types
1 parent 7657214 commit 3b3dea8

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/api/router.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
from typing import Annotated
2-
3-
from fastapi import APIRouter, Path
1+
from fastapi import APIRouter
42
from redis import Redis
53

64
from src.api.dependencies import SettingsDep
7-
from src.common import PasswordStorage
5+
from src.common import Password, PasswordStorage, Prefix
86

97
v1_router = APIRouter(
108
prefix="/api/v1",
119
)
1210

13-
PREFIX_REGEX_PATTERN = "^[0-9a-fA-F]{5}$"
14-
APIPrefix = Annotated[
15-
str,
16-
Path(
17-
description="5 first chars of a password hash",
18-
example="123AB",
19-
pattern=PREFIX_REGEX_PATTERN,
20-
),
21-
]
22-
2311

24-
@v1_router.get("/haveibeenrocked/{prefix}") # TODO add response schema to openAPI
25-
def check_password_leak(prefix: APIPrefix, settings: SettingsDep):
12+
@v1_router.get("/haveibeenrocked/{prefix}")
13+
def check_password_leak(prefix: Prefix, settings: SettingsDep) -> dict[Prefix, set[Password]]:
2614
redis_client = Redis.from_url(str(settings.kvrocks_url))
2715
password_storage = PasswordStorage(client=redis_client)
2816

src/common/types.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
# type alias for clarity
2-
Prefix = str # first 5 chars of password's hash
3-
Password = str
1+
from typing import Annotated
2+
3+
from fastapi import Path
4+
5+
PREFIX_REGEX_PATTERN = "^[0-9a-fA-F]{5}$" # first 5 chars of password's hash
6+
Prefix = Annotated[
7+
str,
8+
Path( # /!\ only enforced on FastApi side
9+
description="5 first chars of a password hash",
10+
example="5c283",
11+
pattern=PREFIX_REGEX_PATTERN,
12+
),
13+
]
14+
Password = str # type alias for clarity

0 commit comments

Comments
 (0)