11from collections .abc import Callable , Iterable
22from typing import Any
3+ from uuid import UUID , uuid4
34
45from fastapi import (
56 APIRouter ,
@@ -45,12 +46,13 @@ async def create_user(
4546 update_subscriber ,
4647 wait_timeout ,
4748 path = "/users" ,
48- entries_callback = lambda r , body : [
49+ entries_callback = lambda r , body , update_id : [
4950 create_data_source_entry (
5051 obj_type = "users" ,
5152 obj_id = body ["id" ],
5253 obj_key = body ["key" ],
5354 authorization_header = r .headers .get ("Authorization" ),
55+ update_id = update_id ,
5456 )
5557 ],
5658 timeout_policy = timeout_policy ,
@@ -71,12 +73,13 @@ async def create_tenant(
7173 update_subscriber ,
7274 wait_timeout ,
7375 path = "/tenants" ,
74- entries_callback = lambda r , body : [
76+ entries_callback = lambda r , body , update_id : [
7577 create_data_source_entry (
7678 obj_type = "tenants" ,
7779 obj_id = body ["id" ],
7880 obj_key = body ["key" ],
7981 authorization_header = r .headers .get ("Authorization" ),
82+ update_id = update_id ,
8083 )
8184 ],
8285 timeout_policy = timeout_policy ,
@@ -98,12 +101,13 @@ async def sync_user(
98101 update_subscriber ,
99102 wait_timeout ,
100103 path = f"/users/{ user_id } " ,
101- entries_callback = lambda r , body : [
104+ entries_callback = lambda r , body , update_id : [
102105 create_data_source_entry (
103106 obj_type = "users" ,
104107 obj_id = body ["id" ],
105108 obj_key = body ["key" ],
106109 authorization_header = r .headers .get ("Authorization" ),
110+ update_id = update_id ,
107111 )
108112 ],
109113 timeout_policy = timeout_policy ,
@@ -125,31 +129,36 @@ async def update_user(
125129 update_subscriber ,
126130 wait_timeout ,
127131 path = f"/users/{ user_id } " ,
128- entries_callback = lambda r , body : [
132+ entries_callback = lambda r , body , update_id : [
129133 create_data_source_entry (
130134 obj_type = "users" ,
131135 obj_id = body ["id" ],
132136 obj_key = body ["key" ],
133137 authorization_header = r .headers .get ("Authorization" ),
138+ update_id = update_id ,
134139 )
135140 ],
136141 timeout_policy = timeout_policy ,
137142 )
138143
139144
140- def create_role_assignment_data_entries (request : FastApiRequest , body : dict [str , Any ]) -> Iterable [DataSourceEntry ]:
145+ def create_role_assignment_data_entries (
146+ request : FastApiRequest , body : dict [str , Any ], update_id : UUID | None
147+ ) -> Iterable [DataSourceEntry ]:
141148 if not body .get ("resource_instance" ):
142149 yield create_data_source_entry (
143150 obj_type = "role_assignments" ,
144151 obj_id = body ["user_id" ],
145152 obj_key = f"user:{ body ['user' ]} " ,
146153 authorization_header = request .headers .get ("Authorization" ),
154+ update_id = update_id ,
147155 )
148156 yield create_data_source_entry (
149157 obj_type = "users" ,
150158 obj_id = body ["user_id" ],
151159 obj_key = body ["user" ],
152160 authorization_header = request .headers .get ("Authorization" ),
161+ update_id = update_id ,
153162 )
154163 else :
155164 # note that user_id == subject_id,
@@ -159,6 +168,7 @@ def create_role_assignment_data_entries(request: FastApiRequest, body: dict[str,
159168 obj_id = body ["user_id" ],
160169 obj_key = f"user:{ body ['user' ]} " ,
161170 authorization_header = request .headers .get ("Authorization" ),
171+ update_id = update_id ,
162172 )
163173
164174
@@ -215,12 +225,13 @@ async def create_resource_instance(
215225 update_subscriber ,
216226 wait_timeout ,
217227 path = "/resource_instances" ,
218- entries_callback = lambda r , body : [
228+ entries_callback = lambda r , body , update_id : [
219229 create_data_source_entry (
220230 obj_type = "resource_instances" ,
221231 obj_id = body ["id" ],
222232 obj_key = f"{ body ['resource' ]} :{ body ['key' ]} " ,
223233 authorization_header = r .headers .get ("Authorization" ),
234+ update_id = update_id ,
224235 ),
225236 ],
226237 timeout_policy = timeout_policy ,
@@ -242,12 +253,13 @@ async def update_resource_instance(
242253 update_subscriber ,
243254 wait_timeout ,
244255 path = f"/resource_instances/{ instance_id } " ,
245- entries_callback = lambda r , body : [
256+ entries_callback = lambda r , body , update_id : [
246257 create_data_source_entry (
247258 obj_type = "resource_instances" ,
248259 obj_id = body ["id" ],
249260 obj_key = f"{ body ['resource' ]} :{ body ['key' ]} " ,
250261 authorization_header = r .headers .get ("Authorization" ),
262+ update_id = update_id ,
251263 ),
252264 ],
253265 timeout_policy = timeout_policy ,
@@ -268,12 +280,13 @@ async def create_relationship_tuple(
268280 update_subscriber ,
269281 wait_timeout ,
270282 path = "/relationship_tuples" ,
271- entries_callback = lambda r , body : [
283+ entries_callback = lambda r , body , update_id : [
272284 create_data_source_entry (
273285 obj_type = "relationships" ,
274286 obj_id = body ["object_id" ],
275287 obj_key = body ["object" ],
276288 authorization_header = r .headers .get ("Authorization" ),
289+ update_id = update_id ,
277290 ),
278291 ],
279292 timeout_policy = timeout_policy ,
@@ -287,16 +300,20 @@ async def forward_request_then_wait_for_update(
287300 wait_timeout : float | None ,
288301 * ,
289302 path : str ,
290- entries_callback : Callable [[FastApiRequest , dict [str , Any ]], Iterable [DataSourceEntry ]],
303+ update_id : UUID | None = None ,
304+ entries_callback : Callable [[FastApiRequest , dict [str , Any ], UUID | None ], Iterable [DataSourceEntry ]],
291305 timeout_policy : TimeoutPolicy = TimeoutPolicy .IGNORE ,
292306) -> Response :
307+ _update_id = update_id or uuid4 ()
293308 response = await client .send_forward_request (request , path )
294309 body = client .extract_body (response )
295310 if body is None :
296311 return client .convert_response (response )
297312
298313 try :
299- data_update_entry = create_data_update_entry (list (entries_callback (request , body )))
314+ data_update_entry = create_data_update_entry (
315+ list (entries_callback (request , body , _update_id )), update_id = _update_id
316+ )
300317 except KeyError as e :
301318 logger .warning (f"Missing required field { e .args [0 ]} in the response body, skipping wait for update." )
302319 return client .convert_response (response )
@@ -315,6 +332,7 @@ async def forward_request_then_wait_for_update(
315332 )
316333 else :
317334 logger .warning ("Timeout waiting for update and policy is set to ignore" )
335+ return client .convert_response (response )
318336
319337
320338@facts_router .api_route (
0 commit comments