from typing import Annotated from fastapi import Depends, FastAPI, HTTPException from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer import hsadmin import config app = FastAPI() security = HTTPBearer() def check_token(token: str) -> bool: """Check if the token is valid""" return token == config.token @app.put("/list/{listname}") def create_list(token: Annotated[HTTPAuthorizationCredentials, Depends(security)], listname: str): # Authenticate if not check_token(token.credentials): raise HTTPException(status_code=401, detail="Invalid token") # Create email api = hsadmin.login() if hsadmin.email_exists(api, config.domain, listname): return {"Success": "false", "Message": "list already exists"} result = hsadmin.add_email(api, config.listsuser, config.domain, listname) if result == True: return {"Success": "true"} return {"Success": "false", "Message": result} @app.delete("/list/{listname}") def delete_list(token: Annotated[HTTPAuthorizationCredentials, Depends(security)], listname: str): # Authenticate if not check_token(token.credentials): raise HTTPException(status_code=401, detail="Invalid token") # Delete email api = hsadmin.login() if not hsadmin.email_exists(api, config.domain, listname): return {"Success": "false", "Message": "list does not exist"} result = hsadmin.remove_email(api, config.listsuser, config.domain, listname) if result == True: return {"Success": "true"} return {"Success": "false", "Message": result}