-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsse.py
189 lines (161 loc) · 5.65 KB
/
sse.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from fastapi import APIRouter, Depends, Request
from fastapi.responses import StreamingResponse
from app.dependencies.auth import (
AcaPyAuthVerified,
acapy_auth_verified,
verify_wallet_access,
)
from app.services.sse import (
sse_subscribe_event_with_field_and_state,
sse_subscribe_event_with_state,
sse_subscribe_stream_with_fields,
sse_subscribe_wallet,
sse_subscribe_wallet_topic,
)
from shared.log_config import get_logger
logger = get_logger(__name__)
router = APIRouter(prefix="/sse", tags=["sse"])
@router.get(
"/{wallet_id}", response_class=StreamingResponse, name="Subscribe to Wallet Events"
)
async def get_sse_subscribe_wallet(
request: Request,
wallet_id: str,
auth: AcaPyAuthVerified = Depends(acapy_auth_verified),
):
"""
Subscribe to server-side events for a specific wallet ID.
Args:
wallet_id: The ID of the wallet subscribing to the events.
"""
logger.bind(body={"wallet_id": wallet_id}).info(
"GET request received: Subscribe to wallet events"
)
verify_wallet_access(auth, wallet_id)
return StreamingResponse(
sse_subscribe_wallet(request, wallet_id), media_type="text/event-stream"
)
@router.get(
"/{wallet_id}/{topic}",
response_class=StreamingResponse,
name="Subscribe to Wallet Events by Topic",
)
async def get_sse_subscribe_wallet_topic(
request: Request,
wallet_id: str,
topic: str,
auth: AcaPyAuthVerified = Depends(acapy_auth_verified),
):
"""
Subscribe to server-side events for a specific wallet ID and topic.
Args:
wallet_id: The ID of the wallet subscribing to the events.
topic: The topic to which the wallet is subscribing.
"""
logger.bind(body={"wallet_id": wallet_id, "topic": topic}).info(
"GET request received: Subscribe to wallet events by topic"
)
verify_wallet_access(auth, wallet_id)
return StreamingResponse(
sse_subscribe_wallet_topic(request, wallet_id, topic),
media_type="text/event-stream",
)
@router.get(
"/{wallet_id}/{topic}/{desired_state}",
response_class=StreamingResponse,
name="Subscribe to a Wallet Event by Topic and Desired State",
)
async def get_sse_subscribe_event_with_state(
request: Request,
wallet_id: str,
topic: str,
desired_state: str,
auth: AcaPyAuthVerified = Depends(acapy_auth_verified),
):
"""
Subscribe to server-side events for a specific wallet ID and topic,
and wait for an event that matches the desired state.
Args:
wallet_id: The ID of the wallet subscribing to the events.
topic: The topic to which the wallet is subscribing.
desired_state: The desired state to be reached.
"""
logger.bind(
body={"wallet_id": wallet_id, "topic": topic, "desired_state": desired_state}
).info(
"GET request received: Subscribe to wallet events by topic and desired state"
)
verify_wallet_access(auth, wallet_id)
return StreamingResponse(
sse_subscribe_event_with_state(request, wallet_id, topic, desired_state),
media_type="text/event-stream",
)
@router.get(
"/{wallet_id}/{topic}/{field}/{field_id}",
response_class=StreamingResponse,
name="Subscribe to Wallet Events by Topic and Field",
)
async def get_sse_subscribe_stream_with_fields(
request: Request,
wallet_id: str,
topic: str,
field: str,
field_id: str,
auth: AcaPyAuthVerified = Depends(acapy_auth_verified),
):
"""
Subscribe to server-side events for a specific wallet ID and topic, and
filter the events for payloads containing a specific field and field ID pair.
Args:
wallet_id: The ID of the wallet subscribing to the events.
topic: The topic to which the wallet is subscribing.
field: The field to which the wallet is subscribing.
field_id: The ID of the field subscribing to the events.
"""
logger.bind(body={"wallet_id": wallet_id, "topic": topic, field: field_id}).info(
"GET request received: Subscribe to wallet events by topic and select field"
)
verify_wallet_access(auth, wallet_id)
return StreamingResponse(
sse_subscribe_stream_with_fields(request, wallet_id, topic, field, field_id),
media_type="text/event-stream",
)
@router.get(
"/{wallet_id}/{topic}/{field}/{field_id}/{desired_state}",
response_class=StreamingResponse,
name="Subscribe to a Wallet Event by Topic, Field, and Desired State",
)
async def get_sse_subscribe_event_with_field_and_state(
request: Request,
wallet_id: str,
topic: str,
field: str,
field_id: str,
desired_state: str,
auth: AcaPyAuthVerified = Depends(acapy_auth_verified),
):
"""
Wait for a desired state to be reached for some event for this wallet and topic,
filtering for payloads that contain `field:field_id`.
Args:
wallet_id: The ID of the wallet subscribing to the events.
topic: The topic to which the wallet is subscribing.
field: The field to which the wallet is subscribing.
field_id: The ID of the field subscribing to the events.
desired_state: The desired state to be reached.
"""
logger.bind(
body={
"wallet_id": wallet_id,
"topic": topic,
field: field_id,
"desired_state": desired_state,
}
).info("GET request received: Subscribe to wallet events by topic, field and state")
verify_wallet_access(auth, wallet_id)
return StreamingResponse(
sse_subscribe_event_with_field_and_state(
request, wallet_id, topic, field, field_id, desired_state
),
media_type="text/event-stream",
)