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 fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy.orm import Session
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)
async def create_service(service: schemas.ServiceCreate, db: Session = Depends(get_db)):
"""Service types: icmp = 0, http = 1, ssh=2"""
if service.service_type not in [0, 1, 2]:
"""Service types: icmp = 0, http = 1"""
if service.service_type not in [0, 1]:
raise HTTPException(status_code=422, detail="Invalid service type provided")
owner = get_usr(service.owner_id, db)
if owner is None:

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