-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (52 loc) · 2.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from http import HTTPStatus
from typing import Union, Any
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from domain.exceptions.user import UserNotFoundException, InvalidUserCredentialsException, PasswordLengthException, \
UsernameLengthException
from infra.models.user import UserLogin
from infra.repositories.user import InMemoryUserRepository
from usecases.authentication import AuthenticateUser
app = FastAPI()
class SmokeResponse(BaseModel):
is_ok: bool
@app.get("/smoke")
async def root() -> dict[str, bool]:
return {"is_ok": True}
auth_responses: dict[Union[int, str], dict[str, Any]] = {
HTTPStatus.OK.value: {"description": "User successfully logged in"},
HTTPStatus.NOT_FOUND.value: {
"description": "User Not Found",
"content": {
"application/json": {
"example": {"detail": "User not found"}
}
}
},
HTTPStatus.UNAUTHORIZED.value: {
"description": "Invalid User Credentials",
"content": {
"application/json": {
"example": {"detail": "Invalid user credentials"}
}
}
}
}
@app.post("/auth", responses=auth_responses)
async def auth(user_login: UserLogin) -> bool:
try:
authenticate_user = AuthenticateUser(user_repository=InMemoryUserRepository())
authenticate_user.execute(user_login.to_user())
except UserNotFoundException:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND,
detail="User not found")
except InvalidUserCredentialsException:
raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED,
detail="Invalid user credentials")
except PasswordLengthException:
raise HTTPException(status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail="Passwords must contain more than 9 characters")
except UsernameLengthException:
raise HTTPException(status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail="Username must contain more than 9 characters")
return True