From 42b7bb69f19e07e8c799838fec37a4b1e2a88814 Mon Sep 17 00:00:00 2001 From: surtur Date: Fri, 7 Aug 2020 22:56:00 +0200 Subject: [PATCH] refactor: service types reduction, url validation * remove ssh from service types for now * validate urls as http/s using pydantic type HttpUrl --- app/main.py | 5 ++--- app/schemas.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index b209d8f..680d8d5 100644 --- a/app/main.py +++ b/app/main.py @@ -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: diff --git a/app/schemas.py b/app/schemas.py index db57946..31a9e40 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -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):