generated from Luis/nextjs-python-web-template
nearly done with attached checker ... todo client.get and right nc -l 5555 call
This commit is contained in:
Binary file not shown.
@@ -35,7 +35,7 @@ def setup_app() -> FastAPI:
|
||||
# bind sql engine
|
||||
# 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
|
||||
# sql_models.Base.metadata.drop_all(engine)
|
||||
sql_models.Base.metadata.drop_all(engine)
|
||||
sql_models.Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import time
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import sql_crud, sql_db, sql_models
|
||||
@@ -111,7 +112,7 @@ def create_repository(
|
||||
response_model=List[Repository],
|
||||
tags=[Tags.repositories],
|
||||
)
|
||||
def get_repositories(
|
||||
def get_repositories(get
|
||||
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
|
||||
) -> List[sql_models.Repository]:
|
||||
repositories = sql_crud.get_repositories(db, skip=skip, limit=limit)
|
||||
@@ -166,6 +167,28 @@ def get_entity(
|
||||
return entity
|
||||
|
||||
|
||||
@router.get("/api/v1/get_attached_entities", response_model=Optional[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", response_model=Optional[Entity], tags=[Tags.entities])
|
||||
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", response_model=Optional[Entity], tags=[Tags.entities])
|
||||
async def attach(
|
||||
background_tasks: BackgroundTasks,
|
||||
@@ -181,22 +204,16 @@ async def attach(
|
||||
|
||||
# TODO
|
||||
def attach_entity(entity_did: str, db: Session) -> None:
|
||||
sql_crud.attach_entity_by_did(db, entity_did, True)
|
||||
while db_entity.attached:
|
||||
#query status endpoint
|
||||
#except not reached set false
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
@router.get("/api/v1/detach", response_model=Optional[Entity], tags=[Tags.entities])
|
||||
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"}
|
||||
db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
|
||||
try:
|
||||
while db_entity.attached:
|
||||
#query status endpoint
|
||||
subprocess.run(f'curl http://{db_entity.ip}')
|
||||
# test with: while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; echo '<html>test</html>'; } | nc -l 5556; done
|
||||
#except not reached set false
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
#async with httpx.AsyncClient() as client:
|
||||
# r =await client.get
|
||||
@@ -1,4 +1,3 @@
|
||||
import time
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -134,6 +133,11 @@ def get_entities(
|
||||
) -> List[sql_models.Entity]:
|
||||
return db.query(sql_models.Entity).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]:
|
||||
return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first()
|
||||
|
||||
|
||||
# get attached
|
||||
def get_attached_entities(
|
||||
db: Session, skip: int = 0, limit: int = 100
|
||||
@@ -141,10 +145,6 @@ def get_attached_entities(
|
||||
return db.query(sql_models.Entity).offset(skip).limit(limit).filter(sql_models.Entity.attached == True)
|
||||
|
||||
|
||||
def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]:
|
||||
return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first()
|
||||
|
||||
|
||||
# set attached
|
||||
# None if did not found
|
||||
# Returns same entity if setting didnt changed something
|
||||
|
||||
Reference in New Issue
Block a user