backend: Fully working roles field. Added get_entity_by_roles #50

Merged
merge-bot merged 7 commits from Qubasa-main into main 2024-01-14 12:57:44 +00:00
2 changed files with 38 additions and 17 deletions
Showing only changes of commit 0a20931ba6 - Show all commits

View File

@@ -1,6 +1,6 @@
import logging import logging
import time import time
from typing import List, Optional from typing import Any, List, Optional
import httpx import httpx
from fastapi import APIRouter, BackgroundTasks, Depends, Query from fastapi import APIRouter, BackgroundTasks, Depends, Query
@@ -237,6 +237,26 @@ def delete_entity(
return {"message": "Entity deleted and all relations to that entity"} return {"message": "Entity deleted and all relations to that entity"}
def get_rpc_by_role(db: Session, role: Role, path: str) -> Any:
matching_entities = sql_crud.get_entity_by_role(db, roles=[role])
if matching_entities is None:
raise ClanError(f"No {role} found")
if len(matching_entities) > 1:
raise ClanError(f"More than one {role} found")
dlg = matching_entities[0]
url = f"http://{dlg.ip}/{path}"
try:
response = httpx.get(url, timeout=2)
except httpx.HTTPError as e:
raise ClanError(f"{role} not reachable at {url}") from e
if response.status_code != 200:
raise ClanError(f"{role} returned {response.status_code}")
return response.json()
######################### #########################
# # # #
# Resolution # # Resolution #
@@ -248,24 +268,21 @@ def delete_entity(
def get_all_resolutions( def get_all_resolutions(
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[Resolution]: ) -> List[Resolution]:
matching_entities = sql_crud.get_entity_by_role(db, roles=[Role("DLG")]) return get_rpc_by_role(db, Role("DLG"), "dlg_list_of_did_resolutions")
if matching_entities is None:
raise ClanError("No DLG found")
if len(matching_entities) > 1:
raise ClanError("More than one DLG found")
dlg = matching_entities[0]
url = f"http://{dlg.ip}/dlg_list_of_did_resolutions"
try:
response = httpx.get(url, timeout=2)
except httpx.HTTPError as e:
raise ClanError(f"DLG not reachable at {url}") from e
if response.status_code != 200: #########################
raise ClanError(f"DLG returned {response.status_code}") # #
# Repository #
resolutions = response.json() # #
return resolutions #########################
@router.get(
"/api/v1/repositories", tags=[Tags.repositories], response_model=List[Service]
)
def get_all_repositories(
skip: int = 0, limit: int = 100, db: Session = Depends(sql_db.get_db)
) -> List[Service]:
return get_rpc_by_role(db, Role("AP"), "ap_list_of_services")
######################### #########################

View File

@@ -18,6 +18,10 @@ tags_metadata: List[Dict[str, Any]] = [
"name": str(Tags.services), "name": str(Tags.services),
"description": "Operations on a service.", "description": "Operations on a service.",
}, },
{
"name": str(Tags.repositories),
"description": "Operations on a repository.",
},
{ {
"name": str(Tags.entities), "name": str(Tags.entities),
"description": "Operations on an entity.", "description": "Operations on an entity.",