added attach with background task
All checks were successful
checks-impure / test (pull_request) Successful in 30s
checks / test (pull_request) Successful in 3m18s

This commit is contained in:
Georg-Stahn
2023-12-03 16:56:16 +01:00
parent 09d0c28fd6
commit 9a31fbe010
3 changed files with 57 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
from typing import List, Optional from typing import List, Optional
from fastapi import APIRouter, Depends from fastapi import APIRouter, BackgroundTasks, Depends
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
@@ -139,8 +139,11 @@ def get_repository(
@router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities]) @router.post("/api/v1/create_entity", response_model=Entity, tags=[Tags.entities])
def create_entity( def create_entity(
entity: EntityCreate, db: Session = Depends(sql_db.get_db) entity: EntityCreate, db: Session = Depends(sql_db.get_db)
) -> EntityCreate: ) -> EntityCreate | str:
# todo checken ob schon da ... # 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) return sql_crud.create_entity(db, entity)
@@ -161,3 +164,17 @@ def get_entity(
) -> 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/attach", response_model=Optional[Entity], tags=[Tags.entities])
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(
sql_crud.check_health, db, entity_did, message="Not attched any more"
)
return {"message": "Attaching in the background"}

View File

@@ -1,3 +1,4 @@
import time
from typing import List, Optional from typing import List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@@ -136,3 +137,21 @@ def get_entities(
def get_entity_by_did(db: Session, did: str) -> Optional[sql_models.Entity]: 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() return db.query(sql_models.Entity).filter(sql_models.Entity.did == did).first()
def check_health(db: Session, entity_did: str, message: str) -> str:
# ste attached to true
db_entity = get_entity_by_did(db, entity_did)
if db_entity is not None:
# db_entity.attached = Column(True)
setattr(db_entity, "attached", True)
# save changes in db
db.add(db_entity)
db.commit()
db.refresh(db_entity)
# check every 5 secounds if status has changed
while db_entity.attached:
time.sleep(5)
return f"{entity_did} message"
else:
return f"{entity_did} not found and not attached"

View File

@@ -84,6 +84,7 @@ def test_producer(api: TestClient) -> None:
# get_request = "entity_did=did%3Asov%3Atest%3A1234" # get_request = "entity_did=did%3Asov%3Atest%3A1234"
make_test_post_and_get(api, request_body, paramter) make_test_post_and_get(api, request_body, paramter)
def test_producer2(api: TestClient) -> None: def test_producer2(api: TestClient) -> None:
request_body = { request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1", "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1",
@@ -95,9 +96,10 @@ def test_producer2(api: TestClient) -> None:
"entity_did": default_entity_did2, "entity_did": default_entity_did2,
} }
paramter = "producer" paramter = "producer"
get_request = "entity_did="+url.quote(default_entity_did2) get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
def test_producer3(api: TestClient) -> None: def test_producer3(api: TestClient) -> None:
request_body = { request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2", "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2",
@@ -109,7 +111,7 @@ def test_producer3(api: TestClient) -> None:
"entity_did": default_entity_did3, "entity_did": default_entity_did3,
} }
paramter = "producer" paramter = "producer"
get_request = "entity_did="+url.quote(default_entity_did3) get_request = "entity_did=" + url.quote(default_entity_did3)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
@@ -124,7 +126,7 @@ def test_producer4(api: TestClient) -> None:
"entity_did": default_entity_did4, "entity_did": default_entity_did4,
} }
paramter = "producer" paramter = "producer"
get_request = "entity_did="+url.quote(default_entity_did4) get_request = "entity_did=" + url.quote(default_entity_did4)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
@@ -139,11 +141,10 @@ def test_producer5(api: TestClient) -> None:
"entity_did": default_entity_did5, "entity_did": default_entity_did5,
} }
paramter = "producer" paramter = "producer"
get_request = "entity_did="+url.quote(default_entity_did5) get_request = "entity_did=" + url.quote(default_entity_did5)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# Consumer # # Consumer #
@@ -159,6 +160,7 @@ def test_consumer(api: TestClient) -> None:
# get_request = "entity_did=did%3Asov%3Atest%3A1234" # get_request = "entity_did=did%3Asov%3Atest%3A1234"
make_test_post_and_get(api, request_body, paramter) make_test_post_and_get(api, request_body, paramter)
def test_consumer2(api: TestClient) -> None: def test_consumer2(api: TestClient) -> None:
request_body = { request_body = {
"entity_did": default_entity_did2, "entity_did": default_entity_did2,
@@ -166,9 +168,10 @@ def test_consumer2(api: TestClient) -> None:
"other": {"war": "games"}, "other": {"war": "games"},
} }
paramter = "consumer" paramter = "consumer"
get_request = "entity_did="+url.quote(default_entity_did2) get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# REPOSITORY # # REPOSITORY #
@@ -187,6 +190,8 @@ def test_repository(api: TestClient) -> None:
paramter = "repository" paramter = "repository"
# get_request = "entity_did=did%3Asov%3Atest%3A1234" # get_request = "entity_did=did%3Asov%3Atest%3A1234"
make_test_post_and_get(api, request_body, paramter) make_test_post_and_get(api, request_body, paramter)
def test_repository2(api: TestClient) -> None: def test_repository2(api: TestClient) -> None:
request_body = { request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1", "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d1",
@@ -198,9 +203,10 @@ def test_repository2(api: TestClient) -> None:
"entity_did": default_entity_did2, "entity_did": default_entity_did2,
} }
paramter = "repository" paramter = "repository"
get_request = "entity_did="+url.quote(default_entity_did2) get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
def test_repository3(api: TestClient) -> None: def test_repository3(api: TestClient) -> None:
request_body = { request_body = {
"uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2", "uuid": "8e285c0c-4e40-430a-a477-26b3b81e30d2",
@@ -212,7 +218,7 @@ def test_repository3(api: TestClient) -> None:
"entity_did": default_entity_did3, "entity_did": default_entity_did3,
} }
paramter = "repository" paramter = "repository"
get_request = "entity_did="+url.quote(default_entity_did3) get_request = "entity_did=" + url.quote(default_entity_did3)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
@@ -227,7 +233,7 @@ def test_repository4(api: TestClient) -> None:
"entity_did": default_entity_did4, "entity_did": default_entity_did4,
} }
paramter = "repository" paramter = "repository"
get_request = "entity_did="+url.quote(default_entity_did4) get_request = "entity_did=" + url.quote(default_entity_did4)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
@@ -242,11 +248,10 @@ def test_repository5(api: TestClient) -> None:
"entity_did": default_entity_did5, "entity_did": default_entity_did5,
} }
paramter = "repository" paramter = "repository"
get_request = "entity_did="+url.quote(default_entity_did5) get_request = "entity_did=" + url.quote(default_entity_did5)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)
######################### #########################
# # # #
# Entity # # Entity #
@@ -264,6 +269,7 @@ def test_entity(api: TestClient) -> None:
# get_request = "entity_did=did%3Asov%3Atest%3A1234" # get_request = "entity_did=did%3Asov%3Atest%3A1234"
make_test_post_and_get(api, request_body, paramter) make_test_post_and_get(api, request_body, paramter)
def test_entity2(api: TestClient) -> None: def test_entity2(api: TestClient) -> None:
request_body = { request_body = {
"did": default_entity_did2, "did": default_entity_did2,
@@ -273,5 +279,5 @@ def test_entity2(api: TestClient) -> None:
"other": {"test": "test"}, "other": {"test": "test"},
} }
paramter = "entity" paramter = "entity"
get_request = "entity_did="+url.quote(default_entity_did2) get_request = "entity_did=" + url.quote(default_entity_did2)
make_test_post_and_get(api, request_body, paramter, get_request) make_test_post_and_get(api, request_body, paramter, get_request)