nearly done with attached checker ... todo client.get and right nc -l 5555 call
Some checks failed
checks-impure / test (pull_request) Failing after 25s
checks / test (pull_request) Failing after 1m42s

This commit is contained in:
Georg-Stahn
2023-12-03 22:13:00 +01:00
parent 1142ee1b80
commit 98747bc39b
4 changed files with 44 additions and 27 deletions

View File

@@ -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)

View File

@@ -1,6 +1,7 @@
import time
from typing import List, Optional from typing import List, Optional
from fastapi import APIRouter, BackgroundTasks, Depends from fastapi import APIRouter, BackgroundTasks, Depends, Request
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from .. import sql_crud, sql_db, sql_models from .. import sql_crud, sql_db, sql_models
@@ -111,7 +112,7 @@ def create_repository(
response_model=List[Repository], response_model=List[Repository],
tags=[Tags.repositories], tags=[Tags.repositories],
) )
def get_repositories( def get_repositories(get
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.Repository]: ) -> List[sql_models.Repository]:
repositories = sql_crud.get_repositories(db, skip=skip, limit=limit) repositories = sql_crud.get_repositories(db, skip=skip, limit=limit)
@@ -166,6 +167,28 @@ def get_entity(
return 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]) @router.get("/api/v1/attach", response_model=Optional[Entity], tags=[Tags.entities])
async def attach( async def attach(
background_tasks: BackgroundTasks, background_tasks: BackgroundTasks,
@@ -181,22 +204,16 @@ async def attach(
# TODO # TODO
def attach_entity(entity_did: str, db: Session) -> None: def attach_entity(entity_did: str, db: Session) -> None:
sql_crud.attach_entity_by_did(db, entity_did, True) db_entity = sql_crud.set_attached_by_entity_did(db, entity_did, True)
while db_entity.attached: try:
#query status endpoint while db_entity.attached:
#except not reached set false #query status endpoint
time.sleep(5) 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
@router.get("/api/v1/detach", response_model=Optional[Entity], tags=[Tags.entities]) time.sleep(1)
async def detach( except Exception as e:
background_tasks: BackgroundTasks, raise e
entity_did: str = "did:sov:test:1234",
skip: int = 0, #async with httpx.AsyncClient() as client:
limit: int = 100, # r =await client.get
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"}

View File

@@ -1,4 +1,3 @@
import time
from typing import List, Optional from typing import List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@@ -134,6 +133,11 @@ def get_entities(
) -> List[sql_models.Entity]: ) -> List[sql_models.Entity]:
return db.query(sql_models.Entity).offset(skip).limit(limit).all() 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 # get attached
def get_attached_entities( def get_attached_entities(
db: Session, skip: int = 0, limit: int = 100 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) 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 # 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