Skip to content

Commit 3799007

Browse files
committed
feat: remove EventWithAuthContext and add additional fields to Event
1 parent 7481ddf commit 3799007

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/firebase_functions/firestore_fn.py

+28-34
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
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+
4649

4750
@_dataclass.dataclass(frozen=True)
4851
class Event(_core.CloudEvent[_core.T]):
@@ -75,6 +78,12 @@ class Event(_core.CloudEvent[_core.T]):
7578
The document path.
7679
"""
7780

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+
7887
params: dict[str, str]
7988
"""
8089
An dict containing the values of the path patterns.
@@ -87,26 +96,9 @@ class Event(_core.CloudEvent[_core.T]):
8796
_C1 = _typing.Callable[[_E1], None]
8897
_C2 = _typing.Callable[[_E2], None]
8998

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-
10799

108100
def _firestore_endpoint_handler(
109-
func: _C1 | _C2 | _C3 | _C4,
101+
func: _C1 | _C2,
110102
event_type: str,
111103
document_pattern: _path_pattern.PathPattern,
112104
raw: _ce.CloudEvent,
@@ -203,17 +195,19 @@ def _firestore_endpoint_handler(
203195
data=firestore_event_data,
204196
subject=event_attributes["subject"],
205197
params=params,
198+
auth_type=None,
199+
auth_id=None,
206200
)
207201

208202
if event_type.endswith(".withAuthContext"):
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)
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)
217211

218212

219213
@_util.copy_func_kwargs(FirestoreOptions)
@@ -277,13 +271,13 @@ def on_document_written_with_auth_context(**kwargs
277271
.. code-block:: python
278272
279273
@on_document_written_with_auth_context(document="*")
280-
def example(event: EventWithAuthContext[Change[DocumentSnapshot]]) -> None:
274+
def example(event: Event[Change[DocumentSnapshot]]) -> None:
281275
pass
282276
283277
:param \\*\\*kwargs: Firestore options.
284278
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
285279
:rtype: :exc:`typing.Callable`
286-
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
280+
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
287281
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
288282
A function that takes a Firestore event and returns ``None``.
289283
"""
@@ -376,13 +370,13 @@ def on_document_updated_with_auth_context(**kwargs
376370
.. code-block:: python
377371
378372
@on_document_updated_with_auth_context(document="*")
379-
def example(event: EventWithAuthContext[Change[DocumentSnapshot]]) -> None:
373+
def example(event: Event[Change[DocumentSnapshot]]) -> None:
380374
pass
381375
382376
:param \\*\\*kwargs: Firestore options.
383377
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
384378
:rtype: :exc:`typing.Callable`
385-
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
379+
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
386380
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
387381
A function that takes a Firestore event and returns ``None``.
388382
"""
@@ -475,13 +469,13 @@ def on_document_created_with_auth_context(**kwargs
475469
.. code-block:: python
476470
477471
@on_document_created_with_auth_context(document="*")
478-
def example(event: EventWithAuthContext[DocumentSnapshot]):
472+
def example(event: Event[DocumentSnapshot]):
479473
pass
480474
481475
:param \\*\\*kwargs: Firestore options.
482476
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
483477
:rtype: :exc:`typing.Callable`
484-
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
478+
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
485479
:exc:`object` \\] \\], `None` \\]
486480
A function that takes a Firestore event and returns ``None``.
487481
"""
@@ -574,13 +568,13 @@ def on_document_deleted_with_auth_context(**kwargs
574568
.. code-block:: python
575569
576570
@on_document_deleted_with_auth_context(document="*")
577-
def example(event: EventWithAuthContext[DocumentSnapshot]) -> None:
571+
def example(event: Event[DocumentSnapshot]) -> None:
578572
pass
579573
580574
:param \\*\\*kwargs: Firestore options.
581575
:type \\*\\*kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
582576
:rtype: :exc:`typing.Callable`
583-
\\[ \\[ :exc:`firebase_functions.firestore_fn.EventWithAuthContext` \\[
577+
\\[ \\[ :exc:`firebase_functions.firestore_fn.Event` \\[
584578
:exc:`object` \\] \\], `None` \\]
585579
A function that takes a Firestore event and returns ``None``.
586580
"""

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, EventWithAuthContext
25+
_firestore_endpoint_handler as firestore_endpoint_handler, Event
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, EventWithAuthContext)
70+
self.assertIsInstance(event, Event)
7171
self.assertEqual(event.auth_type, "unauthenticated")
7272
self.assertEqual(event.auth_id, "foo")

0 commit comments

Comments
 (0)