generated from Luis/nextjs-python-web-template
clan_cli: Added automated database population. Fixed incorrect error handling in backend
This commit is contained in:
@@ -22,6 +22,12 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--host", type=str, default="localhost", help="Host to listen on"
|
"--host", type=str, default="localhost", help="Host to listen on"
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--no-populate",
|
||||||
|
action="store_true",
|
||||||
|
help="Don't populate the database with dummy data",
|
||||||
|
default=False,
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--no-open", action="store_true", help="Don't open the browser", default=False
|
"--no-open", action="store_true", help="Don't open the browser", default=False
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -35,10 +35,11 @@ def setup_app() -> FastAPI:
|
|||||||
# bind sql engine
|
# bind sql engine
|
||||||
# TODO comment aut and add flag to run with pupulated data rm *.sql run pytest with marked then start clan webui
|
# TODO comment aut and add flag to run with pupulated data rm *.sql run pytest with marked then start clan webui
|
||||||
# https://docs.pytest.org/en/7.1.x/example/markers.html
|
# https://docs.pytest.org/en/7.1.x/example/markers.html
|
||||||
sql_models.Base.metadata.drop_all(engine)
|
# sql_models.Base.metadata.drop_all(engine)
|
||||||
|
|
||||||
sql_models.Base.metadata.create_all(bind=engine)
|
sql_models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan, swagger_ui_parameters={"tryItOutEnabled": True})
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=origins,
|
allow_origins=origins,
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def sql_error_handler(request: Request, exc: SQLAlchemyError) -> JSONResponse:
|
|||||||
|
|
||||||
|
|
||||||
def clan_error_handler(request: Request, exc: ClanError) -> JSONResponse:
|
def clan_error_handler(request: Request, exc: ClanError) -> JSONResponse:
|
||||||
log.error("ClanError: %s", exc)
|
log.exception(exc)
|
||||||
detail = [
|
detail = [
|
||||||
{
|
{
|
||||||
"loc": [],
|
"loc": [],
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
@@ -5,6 +6,7 @@ import httpx
|
|||||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ...errors import ClanError
|
||||||
from .. import sql_crud, sql_db, sql_models
|
from .. import sql_crud, sql_db, sql_models
|
||||||
from ..schemas import (
|
from ..schemas import (
|
||||||
Consumer,
|
Consumer,
|
||||||
@@ -20,6 +22,8 @@ from ..tags import Tags
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
# #
|
# #
|
||||||
@@ -141,11 +145,7 @@ def get_repository(
|
|||||||
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
|
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
|
||||||
def create_entity(
|
def create_entity(
|
||||||
entity: EntityCreate, db: Session = Depends(sql_db.get_db)
|
entity: EntityCreate, db: Session = Depends(sql_db.get_db)
|
||||||
) -> EntityCreate | str:
|
) -> EntityCreate:
|
||||||
# todo checken ob schon da ...
|
|
||||||
if sql_crud.get_entity_by_did(db, did=entity.did):
|
|
||||||
print("did already exsists")
|
|
||||||
return "Error did already exsists in db"
|
|
||||||
return sql_crud.create_entity(db, entity)
|
return sql_crud.create_entity(db, entity)
|
||||||
|
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ def get_attached_entities(
|
|||||||
return entities
|
return entities
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/v1/detach")
|
@router.post("/api/v1/detach", response_model=Entity, tags=[Tags.entities])
|
||||||
async def detach(
|
async def detach(
|
||||||
background_tasks: BackgroundTasks,
|
background_tasks: BackgroundTasks,
|
||||||
entity_did: str = "did:sov:test:1234",
|
entity_did: str = "did:sov:test:1234",
|
||||||
@@ -186,13 +186,11 @@ async def detach(
|
|||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
db: Session = Depends(sql_db.get_db),
|
db: Session = Depends(sql_db.get_db),
|
||||||
) -> dict[str, str]:
|
) -> dict[str, str]:
|
||||||
background_tasks.add_task(
|
entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
|
||||||
sql_crud.set_attached_by_entity_did, db, entity_did, False
|
return entity
|
||||||
)
|
|
||||||
return {"message": "Detaching in the background"}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/api/v1/attach")
|
@router.post("/api/v1/attach", tags=[Tags.entities])
|
||||||
async def attach(
|
async def attach(
|
||||||
background_tasks: BackgroundTasks,
|
background_tasks: BackgroundTasks,
|
||||||
entity_did: str = "did:sov:test:1234",
|
entity_did: str = "did:sov:test:1234",
|
||||||
@@ -200,15 +198,16 @@ async def attach(
|
|||||||
limit: int = 100,
|
limit: int = 100,
|
||||||
db: Session = Depends(sql_db.get_db),
|
db: Session = Depends(sql_db.get_db),
|
||||||
) -> dict[str, str]:
|
) -> dict[str, str]:
|
||||||
|
if sql_crud.get_entity_by_did(db, entity_did) is None:
|
||||||
|
raise ClanError(f"Entity with did '{entity_did}' not found")
|
||||||
|
|
||||||
background_tasks.add_task(attach_entity, db, entity_did)
|
background_tasks.add_task(attach_entity, db, entity_did)
|
||||||
return {"message": "Attaching in the background"}
|
return {"message": "Attaching in the background"}
|
||||||
|
|
||||||
|
|
||||||
# TODO
|
|
||||||
def attach_entity(db: Session, entity_did: str) -> None:
|
def attach_entity(db: Session, entity_did: str) -> None:
|
||||||
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
|
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
|
||||||
try:
|
try:
|
||||||
if db_entity is not None:
|
|
||||||
while db_entity.attached:
|
while db_entity.attached:
|
||||||
# query status endpoint
|
# query status endpoint
|
||||||
# https://www.python-httpx.org/
|
# https://www.python-httpx.org/
|
||||||
@@ -220,7 +219,7 @@ def attach_entity(db: Session, entity_did: str) -> None:
|
|||||||
# httpx http://localhost:5555
|
# httpx http://localhost:5555
|
||||||
# except not reached set false
|
# except not reached set false
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
print(e)
|
log.warning(f"Entity {entity_did} not reachable. Setting attached to false")
|
||||||
if db_entity is not None:
|
|
||||||
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
|
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ class ProducerBase(BaseModel):
|
|||||||
service_type: str = "3D Printing"
|
service_type: str = "3D Printing"
|
||||||
endpoint_url: str = "http://127.0.0.1:8000"
|
endpoint_url: str = "http://127.0.0.1:8000"
|
||||||
status: str = "unknown"
|
status: str = "unknown"
|
||||||
other: dict = {"test": "test"}
|
other: dict = {"action": ["register", "deregister", "delete", "create"]}
|
||||||
|
|
||||||
|
|
||||||
class ProducerCreate(ProducerBase):
|
class ProducerCreate(ProducerBase):
|
||||||
@@ -91,7 +91,11 @@ class EntityBase(BaseModel):
|
|||||||
name: str = "C1"
|
name: str = "C1"
|
||||||
ip: str = "127.0.0.1"
|
ip: str = "127.0.0.1"
|
||||||
attached: bool = False
|
attached: bool = False
|
||||||
other: dict = {"test": "test"}
|
visible: bool = True
|
||||||
|
other: dict = {
|
||||||
|
"network": "Carlo's Home Network",
|
||||||
|
"roles": ["service repository", "service prosumer"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class EntityCreate(EntityBase):
|
class EntityCreate(EntityBase):
|
||||||
|
|||||||
@@ -105,6 +105,27 @@ def start_server(args: argparse.Namespace) -> None:
|
|||||||
if not args.no_open:
|
if not args.no_open:
|
||||||
Thread(target=open_browser, args=(base_url, args.sub_url)).start()
|
Thread(target=open_browser, args=(base_url, args.sub_url)).start()
|
||||||
|
|
||||||
|
# DELETE all data from the database
|
||||||
|
from . import sql_models
|
||||||
|
from .sql_db import engine
|
||||||
|
|
||||||
|
sql_models.Base.metadata.drop_all(engine)
|
||||||
|
|
||||||
|
if args.no_populate is False:
|
||||||
|
test_dir = Path(__file__).parent.parent.parent / "tests"
|
||||||
|
|
||||||
|
if not test_dir.is_dir():
|
||||||
|
raise ClanError(f"Could not find test dir: {test_dir}")
|
||||||
|
|
||||||
|
test_db_api = test_dir / "test_db_api.py"
|
||||||
|
if not test_db_api.is_file():
|
||||||
|
raise ClanError(f"Could not find test db api: {test_db_api}")
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
cmd = ["pytest", "-s", "-n0", str(test_db_api)]
|
||||||
|
subprocess.run(cmd, check=True)
|
||||||
|
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"clan_cli.webui.app:app",
|
"clan_cli.webui.app:app",
|
||||||
host=args.host,
|
host=args.host,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from typing import List, Optional
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from sqlalchemy.sql.expression import true
|
from sqlalchemy.sql.expression import true
|
||||||
|
|
||||||
|
from ..errors import ClanError
|
||||||
from . import schemas, sql_models
|
from . import schemas, sql_models
|
||||||
|
|
||||||
#########################
|
#########################
|
||||||
@@ -153,21 +154,18 @@ def get_attached_entities(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# set attached
|
|
||||||
# None if did not found
|
|
||||||
# Returns same entity if setting didnt changed something
|
# Returns same entity if setting didnt changed something
|
||||||
def set_attached_by_entity_did(
|
def set_attached_by_entity_did(
|
||||||
db: Session, entity_did: str, value: bool
|
db: Session, entity_did: str, value: bool
|
||||||
) -> Optional[sql_models.Entity]:
|
) -> sql_models.Entity:
|
||||||
# ste attached to true
|
|
||||||
db_entity = get_entity_by_did(db, entity_did)
|
db_entity = get_entity_by_did(db, entity_did)
|
||||||
if db_entity is not None:
|
if db_entity is None:
|
||||||
# db_entity.attached = Column(True)
|
raise ClanError(f"Entity with did '{entity_did}' not found")
|
||||||
|
|
||||||
setattr(db_entity, "attached", value)
|
setattr(db_entity, "attached", value)
|
||||||
|
|
||||||
# save changes in db
|
# save changes in db
|
||||||
db.add(db_entity)
|
db.add(db_entity)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_entity)
|
db.refresh(db_entity)
|
||||||
return db_entity
|
return db_entity
|
||||||
else:
|
|
||||||
return db_entity
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class Entity(Base):
|
|||||||
name = Column(String, index=True)
|
name = Column(String, index=True)
|
||||||
ip = Column(String, index=True)
|
ip = Column(String, index=True)
|
||||||
attached = Column(Boolean, index=True)
|
attached = Column(Boolean, index=True)
|
||||||
|
visible = Column(Boolean, index=True)
|
||||||
|
|
||||||
## Non queryable body ##
|
## Non queryable body ##
|
||||||
# In here we deposit: Network, Roles, Visible, etc.
|
# In here we deposit: Network, Roles, Visible, etc.
|
||||||
|
|||||||
@@ -77,7 +77,9 @@ def test_producer(api: TestClient) -> None:
|
|||||||
"service_type": "3D Printing",
|
"service_type": "3D Printing",
|
||||||
"endpoint_url": "http://127.0.0.1:8000",
|
"endpoint_url": "http://127.0.0.1:8000",
|
||||||
"status": "unknown",
|
"status": "unknown",
|
||||||
"other": {"test": "test"},
|
"other": {
|
||||||
|
"action": ["register", "deregister", "delete", "create"]
|
||||||
|
},
|
||||||
"entity_did": default_entity_did,
|
"entity_did": default_entity_did,
|
||||||
}
|
}
|
||||||
paramter = "producer"
|
paramter = "producer"
|
||||||
@@ -92,7 +94,9 @@ def test_producer2(api: TestClient) -> None:
|
|||||||
"service_type": "Fax",
|
"service_type": "Fax",
|
||||||
"endpoint_url": "http://127.0.0.1:8001",
|
"endpoint_url": "http://127.0.0.1:8001",
|
||||||
"status": "unknown",
|
"status": "unknown",
|
||||||
"other": {"faxen": "dicke"},
|
"other": {
|
||||||
|
"action": ["register", "deregister", "delete", "create"]
|
||||||
|
},
|
||||||
"entity_did": default_entity_did2,
|
"entity_did": default_entity_did2,
|
||||||
}
|
}
|
||||||
paramter = "producer"
|
paramter = "producer"
|
||||||
@@ -107,7 +111,9 @@ def test_producer3(api: TestClient) -> None:
|
|||||||
"service_type": "VR-Stream",
|
"service_type": "VR-Stream",
|
||||||
"endpoint_url": "http://127.0.0.1:8002",
|
"endpoint_url": "http://127.0.0.1:8002",
|
||||||
"status": "unknown",
|
"status": "unknown",
|
||||||
"other": {"oculos": "rift"},
|
"other": {
|
||||||
|
"action": ["register", "deregister", "delete", "create"]
|
||||||
|
},
|
||||||
"entity_did": default_entity_did3,
|
"entity_did": default_entity_did3,
|
||||||
}
|
}
|
||||||
paramter = "producer"
|
paramter = "producer"
|
||||||
@@ -122,7 +128,9 @@ def test_producer4(api: TestClient) -> None:
|
|||||||
"service_type": "gallary",
|
"service_type": "gallary",
|
||||||
"endpoint_url": "http://127.0.0.1:8003",
|
"endpoint_url": "http://127.0.0.1:8003",
|
||||||
"status": "unknown",
|
"status": "unknown",
|
||||||
"other": {"nice": "pics"},
|
"other": {
|
||||||
|
"action": ["register", "deregister", "delete", "create"]
|
||||||
|
},
|
||||||
"entity_did": default_entity_did4,
|
"entity_did": default_entity_did4,
|
||||||
}
|
}
|
||||||
paramter = "producer"
|
paramter = "producer"
|
||||||
@@ -137,7 +145,9 @@ def test_producer5(api: TestClient) -> None:
|
|||||||
"service_type": "Game-Shop",
|
"service_type": "Game-Shop",
|
||||||
"endpoint_url": "http://127.0.0.1:8004",
|
"endpoint_url": "http://127.0.0.1:8004",
|
||||||
"status": "unknown",
|
"status": "unknown",
|
||||||
"other": {"war": "games"},
|
"other": {
|
||||||
|
"action": ["register", "deregister", "delete", "create"]
|
||||||
|
},
|
||||||
"entity_did": default_entity_did5,
|
"entity_did": default_entity_did5,
|
||||||
}
|
}
|
||||||
paramter = "producer"
|
paramter = "producer"
|
||||||
@@ -263,7 +273,11 @@ def test_entity(api: TestClient) -> None:
|
|||||||
"name": "C1",
|
"name": "C1",
|
||||||
"ip": "127.0.0.1",
|
"ip": "127.0.0.1",
|
||||||
"attached": False,
|
"attached": False,
|
||||||
"other": {"test": "test"},
|
"visible": True,
|
||||||
|
"other": {
|
||||||
|
"network": "Carlo1's Home Network",
|
||||||
|
"roles": ["service repository", "service consumer"],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
paramter = "entity"
|
paramter = "entity"
|
||||||
# get_request = "entity_did=did%3Asov%3Atest%3A1234"
|
# get_request = "entity_did=did%3Asov%3Atest%3A1234"
|
||||||
@@ -276,7 +290,11 @@ def test_entity2(api: TestClient) -> None:
|
|||||||
"name": "C2",
|
"name": "C2",
|
||||||
"ip": "127.0.0.2",
|
"ip": "127.0.0.2",
|
||||||
"attached": False,
|
"attached": False,
|
||||||
"other": {"test": "test"},
|
"visible": True,
|
||||||
|
"other": {
|
||||||
|
"network": "Carlo2's Home Network",
|
||||||
|
"roles": ["service repository", "service prosumer"],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
paramter = "entity"
|
paramter = "entity"
|
||||||
get_request = "entity_did=" + url.quote(default_entity_did2)
|
get_request = "entity_did=" + url.quote(default_entity_did2)
|
||||||
|
|||||||
Reference in New Issue
Block a user