initial commit
This commit is contained in:
commit
9dc0f99cf1
6 changed files with 967 additions and 0 deletions
47
main.py
Normal file
47
main.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
from typing import Annotated
|
||||
from fastapi import Depends, FastAPI
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
|
||||
import hsadmin
|
||||
import config
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {"Hello": "World"};
|
||||
|
||||
@app.post("/token")
|
||||
async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
|
||||
|
||||
if not config.username == form_data.username:
|
||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
||||
if not config.password == form_data.password:
|
||||
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
||||
|
||||
return {"access_token": form_data.username, "token_type": "bearer"}
|
||||
|
||||
|
||||
@app.put("/list/{listname}")
|
||||
def create_list(token: Annotated[str, Depends(oauth2_scheme)], listname: str):
|
||||
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[str, Depends(oauth2_scheme)], listname: str):
|
||||
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}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue