feat: changed PUT to PATCH, added sanity checks, fixed typos

* also added nicer error response messages that, I believe, add clarity
This commit is contained in:
surtur 2020-08-07 22:03:42 +02:00
parent 79b77acaef
commit 74ecee3557
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D

@ -52,16 +52,21 @@ async def read_services(skip: int = 0, limit: int = 100, db: Session = Depends(g
@app.post("/api/v1/service", response_model=schemas.Service) @app.post("/api/v1/service", response_model=schemas.Service)
async def create_service(service: schemas.ServiceCreate, db: Session = Depends(get_db)): async def create_service(service: schemas.ServiceCreate, db: Session = Depends(get_db)):
"""Service types: icmp = 0, http = 1, ssh=2""" """Service types: icmp = 0, http = 1, ssh=2"""
if service.service_type not in [0, 1, 2]:
raise HTTPException(status_code=422, detail="Invalid service type provided")
owner = get_usr(service.owner_id, db)
if owner is None:
raise HTTPException(status_code=422, detail="No such owner_id")
return crud.create_service(db=db, service=service) return crud.create_service(db=db, service=service)
@app.get("/api/v1/service/{service_id}", response_model=schemas.Service) @app.get("/api/v1/service/{service_id}", response_model=schemas.Service)
async def read_service(service_id: int, db: Session = Depends(get_db)): async def read_service(service_id: int, db: Session = Depends(get_db)):
db_service = get_srv(service_id, db) db_service = get_srv(service_id, db)
if db_service is None: if db_service is None:
raise HTTPException(status_code=404, detail="User not found") raise HTTPException(status_code=404, detail="Service not found")
return db_service return db_service
@app.put("/api/v1/service/{service_id}") @app.patch("/api/v1/service/{service_id}")
async def update_service(service_id: int, service: schemas.ServiceUpdate, db: Session = Depends(get_db)): async def update_service(service_id: int, service: schemas.ServiceUpdate, db: Session = Depends(get_db)):
if ((service.name is None) and (service.owner_id is None) and (service.is_private is None) and (service.description is None) and (service.service_type is None) and (service.url is None) and (service.is_active is None)): if ((service.name is None) and (service.owner_id is None) and (service.is_private is None) and (service.description is None) and (service.service_type is None) and (service.url is None) and (service.is_active is None)):
raise HTTPException(status_code=400, detail="No data provided") raise HTTPException(status_code=400, detail="No data provided")
@ -85,7 +90,7 @@ async def delete_service(service_id: int, db: Session = Depends(get_db)):
async def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): async def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
db_user = crud.get_user_by_username(db, name=user.name) db_user = crud.get_user_by_username(db, name=user.name)
if db_user: if db_user:
raise HTTPException(status_code=400, detail="username already registered") raise HTTPException(status_code=400, detail="Username already registered")
return crud.create_user(db=db, user=user) return crud.create_user(db=db, user=user)
@app.get("/api/v1/users", response_model=List[schemas.User]) @app.get("/api/v1/users", response_model=List[schemas.User])
@ -100,11 +105,11 @@ async def read_user(user_id: int, db: Session = Depends(get_db)):
raise HTTPException(status_code=404, detail="User not found") raise HTTPException(status_code=404, detail="User not found")
return db_user return db_user
@app.put("/api/v1/users/{user_id}") @app.patch("/api/v1/users/{user_id}")
async def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depends(get_db)): async def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depends(get_db)):
if ((user.name is None) and (user.password is None) and (user.full_name is None) and (user.is_active is None)): if ((user.name is None) and (user.password is None) and (user.full_name is None) and (user.is_active is None)):
raise HTTPException(status_code=400, detail="No data provided") raise HTTPException(status_code=400, detail="No data provided")
db_user = read_usr(user_id, db) db_user = get_usr(user_id, db)
if db_user is None: if db_user is None:
raise HTTPException(status_code=422, detail="Unprocessable entity") raise HTTPException(status_code=422, detail="Unprocessable entity")
crud.update_user(db=db, user_id=user_id, user=user) crud.update_user(db=db, user_id=user_id, user=user)
@ -112,7 +117,7 @@ async def update_user(user_id: int, user: schemas.UserUpdate, db: Session = Depe
@app.delete("/api/v1/users/{user_id}") @app.delete("/api/v1/users/{user_id}")
async def delete_user(user_id: int, db: Session = Depends(get_db)): async def delete_user(user_id: int, db: Session = Depends(get_db)):
db_user = read_usr(user_id, db) db_user = get_usr(user_id, db)
if db_user is None: if db_user is None:
raise HTTPException(status_code=422, detail="Unprocessable entity") raise HTTPException(status_code=422, detail="Unprocessable entity")
crud.del_user(db=db, user_id=user_id) crud.del_user(db=db, user_id=user_id)