georgs5 #31

Merged
Ghost merged 11 commits from georgs5 into main 2023-12-04 21:22:22 +00:00
4 changed files with 44 additions and 27 deletions
Showing only changes of commit 98747bc39b - Show all commits

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)
try:
while db_entity.attached: while db_entity.attached:
#query status endpoint #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 #except not reached set false
time.sleep(5) time.sleep(1)
except Exception as e:
raise e
#async with httpx.AsyncClient() as client:
@router.get("/api/v1/detach", response_model=Optional[Entity], tags=[Tags.entities]) # r =await client.get
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"}

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