Better names for pydantic models

This commit is contained in:
Iam54r1n4
2025-01-26 08:10:52 +00:00
parent 33722e3c1f
commit 5a999e54fb
2 changed files with 23 additions and 14 deletions

View File

@ -6,7 +6,7 @@ from pydantic import BaseModel, RootModel
# WHAT I'M SAYING IS THAT OUR CODE IN HERE WILL BE SPAGHETTI CODE IF WE WANT TO HAVE CONSISTENCY IN ALL RESPONSES OF THE APIs # WHAT I'M SAYING IS THAT OUR CODE IN HERE WILL BE SPAGHETTI CODE IF WE WANT TO HAVE CONSISTENCY IN ALL RESPONSES OF THE APIs
# THE MAIN PROBLEM IS IN THE CLI CODE NOT IN THE WEB PANEL CODE (HERE) # THE MAIN PROBLEM IS IN THE CLI CODE NOT IN THE WEB PANEL CODE (HERE)
class UserInfo(BaseModel): class UserInfoResponse(BaseModel):
password: str password: str
max_download_bytes: int max_download_bytes: int
expiration_days: int expiration_days: int
@ -14,8 +14,8 @@ class UserInfo(BaseModel):
blocked: bool blocked: bool
class UserList(RootModel): class UserListResponse(RootModel):
root: dict[str, UserInfo] root: dict[str, UserInfoResponse]
class AddUserInputBody(BaseModel): class AddUserInputBody(BaseModel):

View File

@ -1,13 +1,13 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from .schema.user import UserList, UserInfo, AddUserInputBody, EditUserInputBody from .schema.user import UserListResponse, UserInfoResponse, AddUserInputBody, EditUserInputBody
from .schema.response import DetailResponse from .schema.response import DetailResponse
import cli_api import cli_api
router = APIRouter() router = APIRouter()
@router.get('/', response_model=UserList) @router.get('/', response_model=UserListResponse)
async def list_users(): async def list_users():
try: try:
if res := cli_api.list_users(): if res := cli_api.list_users():
@ -17,7 +17,7 @@ async def list_users():
raise HTTPException(status_code=400, detail=f'Error: {str(e)}') raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@router.get('/{username}', response_model=UserInfo) @router.get('/{username}', response_model=UserInfoResponse)
async def get_user(username: str): async def get_user(username: str):
try: try:
if res := cli_api.get_user(username): if res := cli_api.get_user(username):
@ -46,6 +46,15 @@ async def edit_user(username: str, body: EditUserInputBody):
raise HTTPException(status_code=400, detail=f'Error: {str(e)}') raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@router.delete('/{username}', response_model=DetailResponse)
async def remove_user(username: str):
try:
cli_api.remove_user(username)
return DetailResponse(detail=f'User {username} has been removed.')
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@router.get('/{username}/reset', response_model=DetailResponse) @router.get('/{username}/reset', response_model=DetailResponse)
async def reset_user(username: str): async def reset_user(username: str):
try: try:
@ -54,11 +63,11 @@ async def reset_user(username: str):
except Exception as e: except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}') raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
# TODO implement show user uri endpoint
@router.delete('/{username}', response_model=DetailResponse) # @router.get('/{username}/uri', response_model=TODO)
async def remove_user(username: str): # async def show_user_uri(username: str):
try: # try:
cli_api.remove_user(username) # res = cli_api.show_user_uri(username)
return DetailResponse(detail=f'User {username} has been removed.') # return res
except Exception as e: # except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}') # raise HTTPException(status_code=400, detail=f'Error: {str(e)}')