Skip to content

Commit 0f981e0

Browse files
committed
Revert "feat: remove EventWithAuthContext and add additional fields to Event"
This reverts commit 3799007.
1 parent 3799007 commit 0f981e0

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/firebase_functions/firestore_fn.py

+34-28
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
_event_type_updated_with_auth_context = "google.cloud.firestore.document.v1.updated.withAuthContext"
4444
_event_type_deleted_with_auth_context = "google.cloud.firestore.document.v1.deleted.withAuthContext"
4545

46-
AuthType = _typing.Literal["service_account", "api_key", "system",
47-
"unauthenticated", "unknown"]
48-
4946

5047
@_dataclass.dataclass(frozen=True)
5148
class Event(_core.CloudEvent[_core.T]):
@@ -78,12 +75,6 @@ class Event(_core.CloudEvent[_core.T]):
7875
The document path.
7976
"""
8077

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-
8778
params: dict[str, str]
8879
"""
8980
An dict containing the values of the path patterns.
@@ -96,9 +87,26 @@ class Event(_core.CloudEvent[_core.T]):
9687
_C1 = _typing.Callable[[_E1], None]
9788
_C2 = _typing.Callable[[_E2], None]
9889

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+
99107

100108
def _firestore_endpoint_handler(
101-
func: _C1 | _C2,
109+
func: _C1 | _C2 | _C3 | _C4,
102110
event_type: str,
103111
document_pattern: _path_pattern.PathPattern,
104112
raw: _ce.CloudEvent,
@@ -195,19 +203,17 @@ def _firestore_endpoint_handler(
195203
data=firestore_event_data,
196204
subject=event_attributes["subject"],
197205
params=params,
198-
auth_type=None,
199-
auth_id=None,
200206
)
201207

202208
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)
211217

212218

213219
@_util.copy_func_kwargs(FirestoreOptions)
@@ -271,13 +277,13 @@ def on_document_written_with_auth_context(**kwargs
271277
.. code-block:: python
272278
273279
@on_document_written_with_auth_context(document="*")
274-
def example(event: Event[Change[DocumentSnapshot]]) -> None:
280+
def example(event: EventWithAuthContext[Change[DocumentSnapshot]]) -> None:
275281
pass
276282
277283
:param \\*\\*kwargs: Firestore options.
278284
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
279285
:rtype: :exc:`typing.Callable`
280-
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
286+
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
281287
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
282288
A function that takes a Firestore event and returns ``None``.
283289
"""
@@ -370,13 +376,13 @@ def on_document_updated_with_auth_context(**kwargs
370376
.. code-block:: python
371377
372378
@on_document_updated_with_auth_context(document="*")
373-
def example(event: Event[Change[DocumentSnapshot]]) -> None:
379+
def example(event: EventWithAuthContext[Change[DocumentSnapshot]]) -> None:
374380
pass
375381
376382
:param \\*\\*kwargs: Firestore options.
377383
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
378384
:rtype: :exc:`typing.Callable`
379-
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
385+
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
380386
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
381387
A function that takes a Firestore event and returns ``None``.
382388
"""
@@ -469,13 +475,13 @@ def on_document_created_with_auth_context(**kwargs
469475
.. code-block:: python
470476
471477
@on_document_created_with_auth_context(document="*")
472-
def example(event: Event[DocumentSnapshot]):
478+
def example(event: EventWithAuthContext[DocumentSnapshot]):
473479
pass
474480
475481
:param \\*\\*kwargs: Firestore options.
476482
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
477483
:rtype: :exc:`typing.Callable`
478-
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
484+
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
479485
:exc:`object` \\] \\], `None` \\]
480486
A function that takes a Firestore event and returns ``None``.
481487
"""
@@ -568,13 +574,13 @@ def on_document_deleted_with_auth_context(**kwargs
568574
.. code-block:: python
569575
570576
@on_document_deleted_with_auth_context(document="*")
571-
def example(event: Event[DocumentSnapshot]) -> None:
577+
def example(event: EventWithAuthContext[DocumentSnapshot]) -> None:
572578
pass
573579
574580
:param \\*\\*kwargs: Firestore options.
575581
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
576582
:rtype: :exc:`typing.Callable`
577-
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
583+
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
578584
:exc:`object` \\] \\], `None` \\]
579585
A function that takes a Firestore event and returns ``None``.
580586
"""

tests/test_firestore_fn.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_firestore_endpoint_handler_calls_function_with_correct_args(self):
2222
with patch.dict("sys.modules", mocked_modules):
2323
from cloudevents.http import CloudEvent
2424
from firebase_functions.firestore_fn import _event_type_created_with_auth_context as event_type, \
25-
_firestore_endpoint_handler as firestore_endpoint_handler, Event
25+
_firestore_endpoint_handler as firestore_endpoint_handler, EventWithAuthContext
2626
from firebase_functions.private import path_pattern
2727

2828
func = Mock(__name__="example_func")
@@ -67,6 +67,6 @@ def test_firestore_endpoint_handler_calls_function_with_correct_args(self):
6767

6868
event = func.call_args.args[0]
6969
self.assertIsNotNone(event)
70-
self.assertIsInstance(event, Event)
70+
self.assertIsInstance(event, EventWithAuthContext)
7171
self.assertEqual(event.auth_type, "unauthenticated")
7272
self.assertEqual(event.auth_id, "foo")

0 commit comments

Comments
 (0)