Changes log:

feat: add user URI API endpoint
feat: integrate show_user_uri_api in users page
refactor: remove URI generation from viewmodel
This commit is contained in:
Whispering Wind
2025-04-19 18:18:01 +03:30
committed by GitHub
parent 325c130fd0
commit fb221d350e
4 changed files with 133 additions and 153 deletions

View File

@ -1,4 +1,4 @@
from pydantic import BaseModel
from typing import Optional
from pydantic import BaseModel, RootModel
@ -37,3 +37,9 @@ class EditUserInputBody(BaseModel):
renew_password: bool = False
renew_creation_date: bool = False
blocked: bool = False
class UserUriResponse(BaseModel):
username: str
ipv4: str | None = None
ipv6: str | None = None
normal_sub: str | None = None

View File

@ -150,11 +150,33 @@ async def reset_user_api(username: str):
except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
# TODO implement show user uri endpoint
# @router.get('/{username}/uri', response_model=TODO)
# async def show_user_uri(username: str):
# try:
# res = cli_api.show_user_uri(username)
# return res
# except Exception as e:
# raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
@router.get('/{username}/uri', response_model=UserUriResponse)
async def show_user_uri_api(username: str):
"""
Get the URI information for a user in JSON format.
Args:
username: The username of the user.
Returns:
UserUriResponse: An object containing URI information for the user.
Raises:
HTTPException: 404 if the user is not found, 400 if another error occurs.
"""
try:
uri_data_list = cli_api.show_user_uri_json([username])
if not uri_data_list:
raise HTTPException(status_code=404, detail=f'URI for user {username} not found.')
uri_data = uri_data_list[0]
if uri_data.get('error'):
raise HTTPException(status_code=404, detail=f"{uri_data['error']}")
return uri_data
except cli_api.ScriptNotFoundError as e:
raise HTTPException(status_code=500, detail=f'Server script error: {str(e)}')
except cli_api.CommandExecutionError as e:
raise HTTPException(status_code=400, detail=f'Error executing script: {str(e)}')
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=400, detail=f'Unexpected error: {str(e)}')