generated from Luis/nextjs-python-web-template
get attached - done and health check done
This commit is contained in:
@@ -35,7 +35,7 @@ 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)
|
||||||
|
|||||||
@@ -160,15 +160,17 @@ def get_entities(
|
|||||||
@router.get("/api/v1/get_entity", response_model=Optional[Entity], tags=[Tags.entities])
|
@router.get("/api/v1/get_entity", response_model=Optional[Entity], tags=[Tags.entities])
|
||||||
def get_entity(
|
def get_entity(
|
||||||
entity_did: str = "did:sov:test:1234",
|
entity_did: str = "did:sov:test:1234",
|
||||||
skip: int = 0,
|
|
||||||
limit: int = 100,
|
|
||||||
db: Session = Depends(sql_db.get_db),
|
db: Session = Depends(sql_db.get_db),
|
||||||
) -> Optional[sql_models.Entity]:
|
) -> Optional[sql_models.Entity]:
|
||||||
entity = sql_crud.get_entity_by_did(db, did=entity_did)
|
entity = sql_crud.get_entity_by_did(db, did=entity_did)
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/v1/get_attached_entities", response_model=Optional[Entity], tags=[Tags.entities])
|
@router.get(
|
||||||
|
"/api/v1/get_attached_entities",
|
||||||
|
response_model=List[Entity],
|
||||||
|
tags=[Tags.entities],
|
||||||
|
)
|
||||||
def get_attached_entities(
|
def get_attached_entities(
|
||||||
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||||
) -> List[sql_models.Entity]:
|
) -> List[sql_models.Entity]:
|
||||||
@@ -198,15 +200,15 @@ 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]:
|
||||||
background_tasks.add_task(
|
background_tasks.add_task(attach_entity, db, entity_did)
|
||||||
attach_entity(entity_did, db), db, entity_did
|
|
||||||
)
|
|
||||||
return {"message": "Attaching in the background"}
|
return {"message": "Attaching in the background"}
|
||||||
|
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
async def attach_entity(entity_did: str, db: Session) -> 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/
|
||||||
@@ -214,10 +216,11 @@ async def attach_entity(entity_did: str, db: Session) -> None:
|
|||||||
print(response)
|
print(response)
|
||||||
# test with:
|
# test with:
|
||||||
# while true ; do printf 'HTTP/1.1 200 OK\r\n\r\ncool, thanks' | nc -l -N localhost 5555 ; done
|
# 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
|
# except not reached set false
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
print(e)
|
||||||
|
if db_entity is not None:
|
||||||
#async with httpx.AsyncClient() as client:
|
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, False)
|
||||||
# r =await client.get
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from sqlalchemy.sql.expression import true
|
||||||
|
|
||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -142,13 +144,22 @@ def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]:
|
|||||||
def get_attached_entities(
|
def get_attached_entities(
|
||||||
db: Session, skip: int = 0, limit: int = 100
|
db: Session, skip: int = 0, limit: int = 100
|
||||||
) -> List[sql_models.Entity]:
|
) -> List[sql_models.Entity]:
|
||||||
return db.query(sql_models.Entity).offset(skip).limit(limit).filter(sql_models.Entity.attached is True)
|
return (
|
||||||
|
db.query(sql_models.Entity)
|
||||||
|
.filter(sql_models.Entity.attached == true())
|
||||||
|
# https://stackoverflow.com/questions/18998010/flake8-complains-on-boolean-comparison-in-filter-clause
|
||||||
|
.offset(skip)
|
||||||
|
.limit(limit)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# set attached
|
# set attached
|
||||||
# None if did not found
|
# 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(db: Session, entity_did: str, value: bool) -> bool:
|
def set_attached_by_entity_did(
|
||||||
|
db: Session, entity_did: str, value: bool
|
||||||
|
) -> Optional[sql_models.Entity]:
|
||||||
# ste attached to true
|
# 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 not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user