generated from Luis/nextjs-python-web-template
georgs5 (#31)
Reviewed-on: #31
This commit was merged in pull request #31.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import time
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
import httpx
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import sql_crud, sql_db, sql_models
|
||||
@@ -139,8 +141,11 @@ def get_repository(
|
||||
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
|
||||
def create_entity(
|
||||
entity: EntityCreate, db: Session = Depends(sql_db.get_db)
|
||||
) -> EntityCreate:
|
||||
) -> EntityCreate | str:
|
||||
# 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)
|
||||
|
||||
|
||||
@@ -155,9 +160,67 @@ def get_entities(
|
||||
@router.get("/api/v1/get_entity", response_model=Optional[Entity], tags=[Tags.entities])
|
||||
def get_entity(
|
||||
entity_did: str = "did:sov:test:1234",
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: Session = Depends(sql_db.get_db),
|
||||
) -> Optional[sql_models.Entity]:
|
||||
entity = sql_crud.get_entity_by_did(db, did=entity_did)
|
||||
return entity
|
||||
|
||||
|
||||
@router.get(
|
||||
"/api/v1/get_attached_entities",
|
||||
response_model=List[Entity],
|
||||
tags=[Tags.entities],
|
||||
)
|
||||
def get_attached_entities(
|
||||
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||
) -> List[sql_models.Entity]:
|
||||
entities = sql_crud.get_attached_entities(db, skip=skip, limit=limit)
|
||||
return entities
|
||||
|
||||
|
||||
@router.get("/api/v1/detach")
|
||||
async def detach(
|
||||
background_tasks: BackgroundTasks,
|
||||
entity_did: str = "did:sov:test:1234",
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: Session = Depends(sql_db.get_db),
|
||||
) -> dict[str, str]:
|
||||
background_tasks.add_task(
|
||||
sql_crud.set_attached_by_entity_did, db, entity_did, False
|
||||
)
|
||||
return {"message": "Detaching in the background"}
|
||||
|
||||
|
||||
@router.get("/api/v1/attach")
|
||||
async def attach(
|
||||
background_tasks: BackgroundTasks,
|
||||
entity_did: str = "did:sov:test:1234",
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: Session = Depends(sql_db.get_db),
|
||||
) -> dict[str, str]:
|
||||
background_tasks.add_task(attach_entity, db, entity_did)
|
||||
return {"message": "Attaching in the background"}
|
||||
|
||||
|
||||
# TODO
|
||||
def attach_entity(db: Session, entity_did: str) -> None:
|
||||
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
|
||||
try:
|
||||
if db_entity is not None:
|
||||
while db_entity.attached:
|
||||
# query status endpoint
|
||||
# https://www.python-httpx.org/
|
||||
response = httpx.get(f"http://{db_entity.ip}", timeout=2)
|
||||
print(response)
|
||||
# test with:
|
||||
# while true ; do printf 'HTTP/1.1 200 OK\r\n\r\ncool, thanks' | nc -l -N localhost 5555 ; done
|
||||
# client test (apt install python3-httpx):
|
||||
# httpx http://localhost:5555
|
||||
# except not reached set false
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
if db_entity is not None:
|
||||
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
|
||||
|
||||
Reference in New Issue
Block a user