Skip to content

Commit 8788ebc

Browse files
committed
feat: Make Event common object, using __dict__ to spread into AuthContext
1 parent c841351 commit 8788ebc

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

src/firebase_functions/firestore_fn.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Event(_core.CloudEvent[_core.T]):
8888
_C2 = _typing.Callable[[_E2], None]
8989

9090
AuthType = _typing.Literal["service_account", "api_key", "system",
91-
"unauthenticated", "unknown"]
91+
"unauthenticated", "unknown"]
9292

9393

9494
@_dataclass.dataclass(frozen=True)
@@ -106,10 +106,10 @@ class EventWithAuthContext(Event[_core.T]):
106106

107107

108108
def _firestore_endpoint_handler(
109-
func: _C1 | _C2 | _C3 | _C4,
110-
event_type: str,
111-
document_pattern: _path_pattern.PathPattern,
112-
raw: _ce.CloudEvent,
109+
func: _C1 | _C2 | _C3 | _C4,
110+
event_type: str,
111+
document_pattern: _path_pattern.PathPattern,
112+
raw: _ce.CloudEvent,
113113
) -> None:
114114
event_attributes = raw._get_attributes()
115115
event_data: _typing.Any = raw.get_data()
@@ -189,30 +189,29 @@ def _firestore_endpoint_handler(
189189
**document_pattern.extract_matches(event_document),
190190
}
191191

192-
common_event_kwargs = {
193-
"project": event_project,
194-
"namespace": event_namespace,
195-
"database": event_database,
196-
"location": event_location,
197-
"document": event_document,
198-
"specversion": event_attributes["specversion"],
199-
"id": event_attributes["id"],
200-
"source": event_attributes["source"],
201-
"type": event_attributes["type"],
202-
"time": event_time,
203-
"data": firestore_event_data,
204-
"subject": event_attributes["subject"],
205-
"params": params,
206-
}
192+
database_event = Event(
193+
project=event_project,
194+
namespace=event_namespace,
195+
database=event_database,
196+
location=event_location,
197+
document=event_document,
198+
specversion=event_attributes["specversion"],
199+
id=event_attributes["id"],
200+
source=event_attributes["source"],
201+
type=event_attributes["type"],
202+
time=event_time,
203+
data=firestore_event_data,
204+
subject=event_attributes["subject"],
205+
params=params,
206+
)
207207

208208
if event_type.endswith(".withAuthContext"):
209209
database_event_with_auth_context = EventWithAuthContext(
210-
**common_event_kwargs,
210+
**vars(database_event),
211211
auth_type=event_auth_type,
212212
auth_id=event_auth_id)
213213
func(database_event_with_auth_context)
214214
else:
215-
database_event = Event(**common_event_kwargs)
216215
# mypy cannot infer that the event type is correct, hence the cast
217216
_typing.cast(_C1 | _C2, func)(database_event)
218217

@@ -267,7 +266,7 @@ def on_document_written_wrapped(raw: _ce.CloudEvent):
267266

268267
@_util.copy_func_kwargs(FirestoreOptions)
269268
def on_document_written_with_auth_context(**kwargs
270-
) -> _typing.Callable[[_C1], _C1]:
269+
) -> _typing.Callable[[_C1], _C1]:
271270
"""
272271
Event handler that triggers when a document is created, updated, or deleted in Firestore.
273272
This trigger will also provide the authentication context of the principal who triggered
@@ -366,7 +365,7 @@ def on_document_updated_wrapped(raw: _ce.CloudEvent):
366365

367366
@_util.copy_func_kwargs(FirestoreOptions)
368367
def on_document_updated_with_auth_context(**kwargs
369-
) -> _typing.Callable[[_C1], _C1]:
368+
) -> _typing.Callable[[_C1], _C1]:
370369
"""
371370
Event handler that triggers when a document is updated in Firestore.
372371
This trigger will also provide the authentication context of the principal who triggered
@@ -465,7 +464,7 @@ def on_document_created_wrapped(raw: _ce.CloudEvent):
465464

466465
@_util.copy_func_kwargs(FirestoreOptions)
467466
def on_document_created_with_auth_context(**kwargs
468-
) -> _typing.Callable[[_C2], _C2]:
467+
) -> _typing.Callable[[_C2], _C2]:
469468
"""
470469
Event handler that triggers when a document is created in Firestore.
471470
This trigger will also provide the authentication context of the principal who triggered
@@ -564,7 +563,7 @@ def on_document_deleted_wrapped(raw: _ce.CloudEvent):
564563

565564
@_util.copy_func_kwargs(FirestoreOptions)
566565
def on_document_deleted_with_auth_context(**kwargs
567-
) -> _typing.Callable[[_C2], _C2]:
566+
) -> _typing.Callable[[_C2], _C2]:
568567
"""
569568
Event handler that triggers when a document is deleted in Firestore.
570569
This trigger will also provide the authentication context of the principal who triggered

0 commit comments

Comments
 (0)