43
43
_event_type_updated_with_auth_context = "google.cloud.firestore.document.v1.updated.withAuthContext"
44
44
_event_type_deleted_with_auth_context = "google.cloud.firestore.document.v1.deleted.withAuthContext"
45
45
46
- AuthType = _typing .Literal ["service_account" , "api_key" , "system" ,
47
- "unauthenticated" , "unknown" ]
48
-
49
46
50
47
@_dataclass .dataclass (frozen = True )
51
48
class Event (_core .CloudEvent [_core .T ]):
@@ -78,12 +75,6 @@ class Event(_core.CloudEvent[_core.T]):
78
75
The document path.
79
76
"""
80
77
81
- auth_type : _typing .Optional [AuthType ]
82
- """The type of principal that triggered the event"""
83
-
84
- auth_id : _typing .Optional [str ]
85
- """The unique identifier for the principal"""
86
-
87
78
params : dict [str , str ]
88
79
"""
89
80
An dict containing the values of the path patterns.
@@ -96,9 +87,26 @@ class Event(_core.CloudEvent[_core.T]):
96
87
_C1 = _typing .Callable [[_E1 ], None ]
97
88
_C2 = _typing .Callable [[_E2 ], None ]
98
89
90
+ AuthType = _typing .Literal ["service_account" , "api_key" , "system" ,
91
+ "unauthenticated" , "unknown" ]
92
+
93
+
94
+ @_dataclass .dataclass (frozen = True )
95
+ class EventWithAuthContext (Event [_core .T ]):
96
+ auth_type : AuthType
97
+ """The type of principal that triggered the event"""
98
+ auth_id : str
99
+ """The unique identifier for the principal"""
100
+
101
+
102
+ _E3 = EventWithAuthContext [Change [DocumentSnapshot | None ]]
103
+ _E4 = EventWithAuthContext [DocumentSnapshot | None ]
104
+ _C3 = _typing .Callable [[_E3 ], None ]
105
+ _C4 = _typing .Callable [[_E4 ], None ]
106
+
99
107
100
108
def _firestore_endpoint_handler (
101
- func : _C1 | _C2 ,
109
+ func : _C1 | _C2 | _C3 | _C4 ,
102
110
event_type : str ,
103
111
document_pattern : _path_pattern .PathPattern ,
104
112
raw : _ce .CloudEvent ,
@@ -195,19 +203,17 @@ def _firestore_endpoint_handler(
195
203
data = firestore_event_data ,
196
204
subject = event_attributes ["subject" ],
197
205
params = params ,
198
- auth_type = None ,
199
- auth_id = None ,
200
206
)
201
207
202
208
if event_type .endswith (".withAuthContext" ):
203
- event_attrs = vars ( database_event ). copy ()
204
- event_attrs . update ({
205
- " auth_type" : event_auth_type ,
206
- " auth_id" : event_auth_id ,
207
- } )
208
- database_event = Event ( ** event_attrs )
209
-
210
- func (database_event )
209
+ database_event_with_auth_context = EventWithAuthContext (
210
+ ** vars ( database_event ),
211
+ auth_type = event_auth_type ,
212
+ auth_id = event_auth_id )
213
+ func ( database_event_with_auth_context )
214
+ else :
215
+ # mypy cannot infer that the event type is correct, hence the cast
216
+ _typing . cast ( _C1 | _C2 , func ) (database_event )
211
217
212
218
213
219
@_util .copy_func_kwargs (FirestoreOptions )
@@ -271,13 +277,13 @@ def on_document_written_with_auth_context(**kwargs
271
277
.. code-block:: python
272
278
273
279
@on_document_written_with_auth_context(document="*")
274
- def example(event: Event [Change[DocumentSnapshot]]) -> None:
280
+ def example(event: EventWithAuthContext [Change[DocumentSnapshot]]) -> None:
275
281
pass
276
282
277
283
:param \\ *\\ *kwargs: Firestore options.
278
284
:type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
279
285
:rtype: :exc:`typing.Callable`
280
- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
286
+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
281
287
:exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
282
288
A function that takes a Firestore event and returns ``None``.
283
289
"""
@@ -370,13 +376,13 @@ def on_document_updated_with_auth_context(**kwargs
370
376
.. code-block:: python
371
377
372
378
@on_document_updated_with_auth_context(document="*")
373
- def example(event: Event [Change[DocumentSnapshot]]) -> None:
379
+ def example(event: EventWithAuthContext [Change[DocumentSnapshot]]) -> None:
374
380
pass
375
381
376
382
:param \\ *\\ *kwargs: Firestore options.
377
383
:type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
378
384
:rtype: :exc:`typing.Callable`
379
- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
385
+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
380
386
:exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
381
387
A function that takes a Firestore event and returns ``None``.
382
388
"""
@@ -469,13 +475,13 @@ def on_document_created_with_auth_context(**kwargs
469
475
.. code-block:: python
470
476
471
477
@on_document_created_with_auth_context(document="*")
472
- def example(event: Event [DocumentSnapshot]):
478
+ def example(event: EventWithAuthContext [DocumentSnapshot]):
473
479
pass
474
480
475
481
:param \\ *\\ *kwargs: Firestore options.
476
482
:type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
477
483
:rtype: :exc:`typing.Callable`
478
- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
484
+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
479
485
:exc:`object` \\ ] \\ ], `None` \\ ]
480
486
A function that takes a Firestore event and returns ``None``.
481
487
"""
@@ -568,13 +574,13 @@ def on_document_deleted_with_auth_context(**kwargs
568
574
.. code-block:: python
569
575
570
576
@on_document_deleted_with_auth_context(document="*")
571
- def example(event: Event [DocumentSnapshot]) -> None:
577
+ def example(event: EventWithAuthContext [DocumentSnapshot]) -> None:
572
578
pass
573
579
574
580
:param \\ *\\ *kwargs: Firestore options.
575
581
:type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
576
582
:rtype: :exc:`typing.Callable`
577
- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
583
+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
578
584
:exc:`object` \\ ] \\ ], `None` \\ ]
579
585
A function that takes a Firestore event and returns ``None``.
580
586
"""
0 commit comments