|
| 1 | +# Copyright 2022 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Cloud functions to handle Eventarc events.""" |
| 15 | + |
| 16 | +# pylint: disable=protected-access |
| 17 | +import typing as _typing |
| 18 | +import functools as _functools |
| 19 | +import datetime as _dt |
| 20 | +import cloudevents.http as _ce |
| 21 | + |
| 22 | +import firebase_functions.options as _options |
| 23 | +import firebase_functions.private.util as _util |
| 24 | +from firebase_functions.core import CloudEvent |
| 25 | + |
| 26 | + |
| 27 | +@_util.copy_func_kwargs(_options.EventarcTriggerOptions) |
| 28 | +def on_custom_event_published( |
| 29 | + **kwargs |
| 30 | +) -> _typing.Callable[[_typing.Callable[[CloudEvent], None]], _typing.Callable[ |
| 31 | + [CloudEvent], None]]: |
| 32 | + """ |
| 33 | + Creates a handler for events published on the default event eventarc channel. |
| 34 | +
|
| 35 | + Example: |
| 36 | +
|
| 37 | + .. code-block:: python |
| 38 | +
|
| 39 | + from firebase_functions import eventarc_fn |
| 40 | +
|
| 41 | + @eventarc_fn.on_custom_event_published( |
| 42 | + event_type="firebase.extensions.storage-resize-images.v1.complete", |
| 43 | + ) |
| 44 | + def onimageresize(event: eventarc_fn.CloudEvent) -> None: |
| 45 | + pass |
| 46 | +
|
| 47 | + :param \\*\\*kwargs: Options. |
| 48 | + :type \\*\\*kwargs: as :exc:`firebase_functions.options.EventarcTriggerOptions` |
| 49 | + :rtype: :exc:`typing.Callable` |
| 50 | + \\[ \\[ :exc:`firebase_functions.core.CloudEvent` \\], `None` \\] |
| 51 | + A function that takes a CloudEvent and returns None. |
| 52 | + """ |
| 53 | + options = _options.EventarcTriggerOptions(**kwargs) |
| 54 | + |
| 55 | + def on_custom_event_published_decorator(func: _typing.Callable[[CloudEvent], |
| 56 | + None]): |
| 57 | + |
| 58 | + @_functools.wraps(func) |
| 59 | + def on_custom_event_published_wrapped(raw: _ce.CloudEvent): |
| 60 | + event_attributes = raw._get_attributes() |
| 61 | + event_data: _typing.Any = raw.get_data() |
| 62 | + event_dict = {**event_data, **event_attributes} |
| 63 | + event: CloudEvent = CloudEvent( |
| 64 | + data=event_data, |
| 65 | + id=event_dict["id"], |
| 66 | + source=event_dict["source"], |
| 67 | + specversion=event_dict["specversion"], |
| 68 | + subject=event_dict["subject"] |
| 69 | + if "subject" in event_dict else None, |
| 70 | + time=_dt.datetime.strptime( |
| 71 | + event_dict["time"], |
| 72 | + "%Y-%m-%dT%H:%M:%S.%f%z", |
| 73 | + ), |
| 74 | + type=event_dict["type"], |
| 75 | + ) |
| 76 | + func(event) |
| 77 | + |
| 78 | + _util.set_func_endpoint_attr( |
| 79 | + on_custom_event_published_wrapped, |
| 80 | + options._endpoint(func_name=func.__name__), |
| 81 | + ) |
| 82 | + _util.set_required_apis_attr( |
| 83 | + on_custom_event_published_wrapped, |
| 84 | + options._required_apis(), |
| 85 | + ) |
| 86 | + return on_custom_event_published_wrapped |
| 87 | + |
| 88 | + return on_custom_event_published_decorator |
0 commit comments