-
Notifications
You must be signed in to change notification settings - Fork 9
/
trust_registry.py
153 lines (120 loc) · 4.64 KB
/
trust_registry.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from typing import List, Optional
from fastapi import APIRouter, HTTPException
import app.services.trust_registry.actors as registry_actors
import app.services.trust_registry.schemas as registry_schemas
from shared.log_config import get_logger
from shared.models.trustregistry import Actor, Schema
logger = get_logger(__name__)
router = APIRouter(prefix="/v1/trust-registry", tags=["trust-registry"])
@router.get("/schemas", response_model=List[Schema])
async def get_schemas() -> List[Schema]:
"""
Fetch the schemas from the trust registry.
Returns:
---------
Only the schemas from the trust registry
"""
logger.debug("GET request received: Fetch schemas from the trust registry")
schemas = await registry_schemas.fetch_schemas()
logger.debug("Successfully retrieved schemas.")
return schemas
@router.get("/schemas/{schema_id}", response_model=Schema)
async def get_schema_by_id(schema_id: str) -> Schema:
"""
Retrieve schema by id.
Parameters:
-----------
schema_id: str
Returns:
-----------
A schema from the trust registry
"""
bound_logger = logger.bind(body={"schema_id": schema_id})
bound_logger.debug("GET request received: Fetch schema by id")
schema = await registry_schemas.get_schema_by_id(schema_id)
if schema:
bound_logger.debug("Successfully fetched schema by id.")
return schema
else:
bound_logger.info("Bad request: schema not found.")
raise HTTPException(404, f"Schema with id: {schema_id} not found")
@router.get("/actors", response_model=List[Actor])
async def get_actors(
actor_did: Optional[str] = None,
actor_id: Optional[str] = None,
actor_name: Optional[str] = None,
) -> List[Actor]:
"""
Fetch all actors from the trust registry.
Alternatively, provide one of: did, id, or name, to fetch corresponding actor.
Parameters:
-----------
actor_did: str (Optional) - DID of the Actor
actor_id: str (Optional) - Wallet ID of the Actor
actor_name: str (Optional) - Alias of the Actor
Returns:
---------
All actors from the trust registry, or one actor if a query parameter is passed
"""
param_count = sum(1 for var in [actor_did, actor_name, actor_id] if var)
if param_count == 0:
logger.debug("GET request received: Fetch all actors from the trust registry")
actors = await registry_actors.fetch_all_actors()
logger.debug("Successfully retrieved actors.")
return actors
bound_logger = logger.bind(
body={"actor_did": actor_did, "actor_id": actor_id, "actor_name": actor_name}
)
bound_logger.debug("GET request received: Fetch actor by query param")
if param_count > 1:
bound_logger.info("Bad request, more than one query param provided.")
raise HTTPException(
400,
"Bad request: More than one query parameter provided when max 1 expected",
)
# One query param provided:
if actor_did:
bound_logger.debug(
"GET request received: Fetch actor by did from the trust registry"
)
actor = await registry_actors.fetch_actor_by_did(actor_did)
elif actor_id:
bound_logger.debug(
"GET request received: Fetch actor by id from the trust registry"
)
actor = await registry_actors.fetch_actor_by_id(actor_id)
else: # actor_name
bound_logger.debug(
"GET request received: Fetch actor by name from the trust registry"
)
actor = await registry_actors.fetch_actor_by_name(actor_name)
if actor:
bound_logger.debug("Successfully retrieved actor.")
return [actor]
else:
bound_logger.info("Bad request: actor not found.")
raise HTTPException(404, "Actor not found")
@router.get("/actors/issuers", response_model=List[Actor])
async def get_issuers() -> List[Actor]:
"""
Fetch the issuers from the trust registry.
Returns:
---------
List of issuer actors
"""
logger.debug("GET request received: Fetch the issuers from the trust registry")
issuers = await registry_actors.fetch_actors_with_role("issuer")
logger.debug("Successfully retrieved issuers.")
return issuers
@router.get("/actors/verifiers", response_model=List[Actor])
async def get_verifiers() -> List[Actor]:
"""
Fetch the verifiers from the trust registry.
Returns:
---------
List of verifier actors
"""
logger.debug("GET request received: Fetch the verifiers from the trust registry")
verifiers = await registry_actors.fetch_actors_with_role("verifier")
logger.debug("Successfully retrieved verifiers.")
return verifiers