refactor: service types reduction, url validation

* remove ssh from service types for now
* validate urls as http/s using pydantic type HttpUrl
This commit is contained in:
surtur 2020-08-07 22:56:00 +02:00
parent 74ecee3557
commit 42b7bb69f1
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 5 additions and 6 deletions

@ -4,7 +4,6 @@
from typing import List, Optional from typing import List, Optional
from fastapi import FastAPI, Depends, HTTPException from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app import crud, models, schemas from app import crud, models, schemas
@ -51,8 +50,8 @@ 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"""
if service.service_type not in [0, 1, 2]: if service.service_type not in [0, 1]:
raise HTTPException(status_code=422, detail="Invalid service type provided") raise HTTPException(status_code=422, detail="Invalid service type provided")
owner = get_usr(service.owner_id, db) owner = get_usr(service.owner_id, db)
if owner is None: if owner is None:

@ -1,6 +1,6 @@
import time import time
from typing import List, Optional from typing import List, Optional
from pydantic import BaseModel from pydantic import BaseModel, HttpUrl
class ServiceBase(BaseModel): class ServiceBase(BaseModel):
@ -9,7 +9,7 @@ class ServiceBase(BaseModel):
is_private: bool = True is_private: bool = True
description: Optional[str] = None description: Optional[str] = None
service_type: int service_type: int
url: str url: HttpUrl
is_active: bool = True is_active: bool = True
class Service(BaseModel): class Service(BaseModel):
@ -25,7 +25,7 @@ class ServiceUpdate(ServiceBase):
is_private: Optional[bool] is_private: Optional[bool]
description: Optional[str] description: Optional[str]
service_type: Optional[int] service_type: Optional[int]
url: Optional[str] url: Optional[HttpUrl]
is_active: Optional[bool] is_active: Optional[bool]
class Service(ServiceBase): class Service(ServiceBase):