-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathregistry_actors.py
129 lines (104 loc) · 4.65 KB
/
registry_actors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from typing import List
from fastapi import APIRouter, Depends
from fastapi.exceptions import HTTPException
from sqlalchemy.orm import Session
from shared.log_config import get_logger
from shared.models.trustregistry import Actor
from trustregistry import crud
from trustregistry.db import get_db
logger = get_logger(__name__)
router = APIRouter(prefix="/registry/actors", tags=["actor"])
@router.get("", response_model=List[Actor])
async def get_actors(db_session: Session = Depends(get_db)) -> List[Actor]:
logger.info("GET request received: Fetch all actors")
db_actors = crud.get_actors(db_session)
return db_actors
@router.post("", response_model=Actor)
async def register_actor(actor: Actor, db_session: Session = Depends(get_db)) -> Actor:
bound_logger = logger.bind(body={"actor": actor})
bound_logger.info("POST request received: Register actor")
try:
created_actor = crud.create_actor(db_session, actor=actor)
except crud.ActorAlreadyExistsException as e:
bound_logger.info("Bad request: Actor already exists.")
raise HTTPException(status_code=409, detail=str(e)) from e
except Exception as e:
bound_logger.error("Something went wrong during actor creation.")
raise HTTPException(status_code=500, detail=str(e)) from e
return created_actor
@router.put("/{actor_id}", response_model=Actor)
async def update_actor(
actor_id: str, actor: Actor, db_session: Session = Depends(get_db)
) -> Actor:
bound_logger = logger.bind(body={"actor_id": actor_id, "actor": actor})
bound_logger.info("PUT request received: Update actor")
if actor.id and actor.id != actor_id:
bound_logger.info("Bad request: Actor ID in request doesn't match ID in URL.")
raise HTTPException(
status_code=400,
detail=f"The provided actor ID '{actor.id}' in the request body "
f"does not match the actor ID '{actor_id}' in the URL.",
)
if not actor.id:
actor.id = actor_id
try:
update_actor_result = crud.update_actor(db_session, actor=actor)
except crud.ActorDoesNotExistException as e:
bound_logger.info("Bad request: Actor not found.")
raise HTTPException(
status_code=404, detail=f"Actor with id {actor_id} not found."
) from e
return update_actor_result
@router.get("/did/{actor_did}", response_model=Actor)
async def get_actor_by_did(
actor_did: str, db_session: Session = Depends(get_db)
) -> Actor:
bound_logger = logger.bind(body={"actor_did": actor_did})
bound_logger.info("GET request received: Get actor by DID")
try:
actor = crud.get_actor_by_did(db_session, actor_did=actor_did)
except crud.ActorDoesNotExistException as e:
bound_logger.info("Bad request: Actor not found.")
raise HTTPException(
status_code=404, detail=f"Actor with did {actor_did} not found."
) from e
return actor
@router.get("/{actor_id}", response_model=Actor)
async def get_actor_by_id(
actor_id: str, db_session: Session = Depends(get_db)
) -> Actor:
bound_logger = logger.bind(body={"actor_id": actor_id})
bound_logger.info("GET request received: Get actor by ID")
try:
actor = crud.get_actor_by_id(db_session, actor_id=actor_id)
except crud.ActorDoesNotExistException as e:
bound_logger.info("Bad request: Actor not found.")
raise HTTPException(
status_code=404, detail=f"Actor with id {actor_id} not found."
) from e
return actor
@router.get("/name/{actor_name}", response_model=Actor)
async def get_actor_by_name(
actor_name: str, db_session: Session = Depends(get_db)
) -> Actor:
bound_logger = logger.bind(body={"actor_name": actor_name})
bound_logger.info("GET request received: Get actor by name")
try:
actor = crud.get_actor_by_name(db_session, actor_name=actor_name)
except crud.ActorDoesNotExistException:
bound_logger.info("Bad request: Actor with name {} not found", actor_name)
raise HTTPException(
status_code=404, detail=f"Actor with name {actor_name} not found"
)
return actor
@router.delete("/{actor_id}", status_code=204)
async def remove_actor(actor_id: str, db_session: Session = Depends(get_db)) -> None:
bound_logger = logger.bind(body={"actor_id": actor_id})
bound_logger.info("DELETE request received: Delete actor by ID")
try:
crud.delete_actor(db_session, actor_id=actor_id)
except crud.ActorDoesNotExistException as e:
bound_logger.info("Bad request: Actor not found.")
raise HTTPException(
status_code=404, detail=f"Actor with id {actor_id} not found."
) from e