|
| 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 | +# pylint: disable=protected-access |
| 15 | +""" |
| 16 | +Cloud functions to handle Remote Config events. |
| 17 | +""" |
| 18 | +import dataclasses as _dataclasses |
| 19 | +import functools as _functools |
| 20 | +import datetime as _dt |
| 21 | +import typing as _typing |
| 22 | +import cloudevents.http as _ce |
| 23 | +import enum as _enum |
| 24 | + |
| 25 | +import firebase_functions.private.util as _util |
| 26 | + |
| 27 | +from firebase_functions.core import CloudEvent |
| 28 | +from firebase_functions.options import EventHandlerOptions |
| 29 | + |
| 30 | + |
| 31 | +@_dataclasses.dataclass(frozen=True) |
| 32 | +class ConfigUser: |
| 33 | + """ |
| 34 | + All the fields associated with the person/service account that wrote a Remote Config template. |
| 35 | + """ |
| 36 | + |
| 37 | + name: str |
| 38 | + """ |
| 39 | + Display name. |
| 40 | + """ |
| 41 | + |
| 42 | + email: str |
| 43 | + """ |
| 44 | + Email address. |
| 45 | + """ |
| 46 | + |
| 47 | + image_url: str |
| 48 | + """ |
| 49 | + Image URL. |
| 50 | + """ |
| 51 | + |
| 52 | + |
| 53 | +class ConfigUpdateOrigin(str, _enum.Enum): |
| 54 | + """ |
| 55 | + Where the Remote Config update action originated. |
| 56 | + """ |
| 57 | + |
| 58 | + REMOTE_CONFIG_UPDATE_ORIGIN_UNSPECIFIED = "REMOTE_CONFIG_UPDATE_ORIGIN_UNSPECIFIED" |
| 59 | + """ |
| 60 | + Catch-all for unrecognized values. |
| 61 | + """ |
| 62 | + |
| 63 | + CONSOLE = "CONSOLE" |
| 64 | + """ |
| 65 | + The update came from the Firebase UI. |
| 66 | + """ |
| 67 | + |
| 68 | + REST_API = "REST_API" |
| 69 | + """ |
| 70 | + The update came from the Remote Config REST API. |
| 71 | + """ |
| 72 | + |
| 73 | + ADMIN_SDK_NODE = "ADMIN_SDK_NODE" |
| 74 | + """ |
| 75 | + The update came from the Firebase Admin Node SDK. |
| 76 | + """ |
| 77 | + |
| 78 | + |
| 79 | +class ConfigUpdateType(str, _enum.Enum): |
| 80 | + """ |
| 81 | + What type of update was associated with the Remote Config template version. |
| 82 | + """ |
| 83 | + |
| 84 | + REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED = "REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED" |
| 85 | + """ |
| 86 | + Catch-all for unrecognized enum values. |
| 87 | + """ |
| 88 | + |
| 89 | + INCREMENTAL_UPDATE = "INCREMENTAL_UPDATE" |
| 90 | + """ |
| 91 | + A regular incremental update. |
| 92 | + """ |
| 93 | + |
| 94 | + FORCED_UPDATE = "FORCED_UPDATE" |
| 95 | + """ |
| 96 | + A forced update. The ETag was specified as "*" in an UpdateRemoteConfigRequest |
| 97 | + request or the "Force Update" button was pressed on the console. |
| 98 | + """ |
| 99 | + |
| 100 | + ROLLBACK = "ROLLBACK" |
| 101 | + """ |
| 102 | + A rollback to a previous Remote Config template. |
| 103 | + """ |
| 104 | + |
| 105 | + |
| 106 | +@_dataclasses.dataclass(frozen=True) |
| 107 | +class ConfigUpdateData: |
| 108 | + """ |
| 109 | + The data within Firebase Remote Config update events. |
| 110 | + """ |
| 111 | + |
| 112 | + version_number: int |
| 113 | + """ |
| 114 | + The version number of the version's corresponding Remote Config template. |
| 115 | + """ |
| 116 | + |
| 117 | + update_time: _dt.datetime |
| 118 | + """ |
| 119 | + When the Remote Config template was written to the Remote Config server. |
| 120 | + """ |
| 121 | + |
| 122 | + update_user: ConfigUser |
| 123 | + """ |
| 124 | + Aggregation of all metadata fields about the account that performed the update. |
| 125 | + """ |
| 126 | + |
| 127 | + description: str |
| 128 | + """ |
| 129 | + The user-provided description of the corresponding Remote Config template. |
| 130 | + """ |
| 131 | + |
| 132 | + update_origin: ConfigUpdateOrigin |
| 133 | + """ |
| 134 | + Where the update action originated. |
| 135 | + """ |
| 136 | + |
| 137 | + update_type: ConfigUpdateType |
| 138 | + """ |
| 139 | + What type of update was made. |
| 140 | + """ |
| 141 | + |
| 142 | + rollback_source: int | None = None |
| 143 | + """ |
| 144 | + Only present if this version is the result of a rollback, and will be |
| 145 | + the version number of the Remote Config template that was rolled-back to. |
| 146 | + """ |
| 147 | + |
| 148 | + |
| 149 | +_E1 = CloudEvent[ConfigUpdateData] |
| 150 | +_C1 = _typing.Callable[[_E1], None] |
| 151 | + |
| 152 | + |
| 153 | +def _config_handler(func: _C1, raw: _ce.CloudEvent) -> None: |
| 154 | + event_attributes = raw._get_attributes() |
| 155 | + event_data: _typing.Any = raw.get_data() |
| 156 | + event_dict = {**event_data, **event_attributes} |
| 157 | + |
| 158 | + config_data = ConfigUpdateData( |
| 159 | + version_number=event_data["versionNumber"], |
| 160 | + update_time=_dt.datetime.strptime(event_data["updateTime"], |
| 161 | + "%Y-%m-%dT%H:%M:%S.%f%z"), |
| 162 | + update_user=ConfigUser( |
| 163 | + name=event_data["updateUser"]["name"], |
| 164 | + email=event_data["updateUser"]["email"], |
| 165 | + image_url=event_data["updateUser"]["imageUrl"], |
| 166 | + ), |
| 167 | + description=event_data["description"], |
| 168 | + update_origin=ConfigUpdateOrigin(event_data["updateOrigin"]), |
| 169 | + update_type=ConfigUpdateType(event_data["updateType"]), |
| 170 | + rollback_source=event_data.get("rollbackSource", None), |
| 171 | + ) |
| 172 | + |
| 173 | + event: CloudEvent[ConfigUpdateData] = CloudEvent( |
| 174 | + data=config_data, |
| 175 | + id=event_dict["id"], |
| 176 | + source=event_dict["source"], |
| 177 | + specversion=event_dict["specversion"], |
| 178 | + subject=event_dict["subject"] if "subject" in event_dict else None, |
| 179 | + time=_dt.datetime.strptime( |
| 180 | + event_dict["time"], |
| 181 | + "%Y-%m-%dT%H:%M:%S.%f%z", |
| 182 | + ), |
| 183 | + type=event_dict["type"], |
| 184 | + ) |
| 185 | + |
| 186 | + func(event) |
| 187 | + |
| 188 | + |
| 189 | +@_util.copy_func_kwargs(EventHandlerOptions) |
| 190 | +def on_config_updated(**kwargs) -> _typing.Callable[[_C1], _C1]: |
| 191 | + """ |
| 192 | + Event handler which triggers when data is updated in a Remote Config. |
| 193 | +
|
| 194 | + Example: |
| 195 | +
|
| 196 | + .. code-block:: python |
| 197 | +
|
| 198 | + @on_config_updated() |
| 199 | + def example(event: CloudEvent[ConfigUpdateData]) -> None: |
| 200 | + pass |
| 201 | +
|
| 202 | + :param \\*\\*kwargs: Pub/Sub options. |
| 203 | + :type \\*\\*kwargs: as :exc:`firebase_functions.options.EventHandlerOptions` |
| 204 | + :rtype: :exc:`typing.Callable` |
| 205 | + \\[ \\[ :exc:`firebase_functions.core.CloudEvent` \\[ |
| 206 | + :exc:`firebase_functions.remote_config_fn.ConfigUpdateData` \\[ |
| 207 | + :exc:`typing.Any` \\] \\] \\], `None` \\] |
| 208 | + A function that takes a CloudEvent and returns None. |
| 209 | + """ |
| 210 | + options = EventHandlerOptions(**kwargs) |
| 211 | + |
| 212 | + def on_config_updated_inner_decorator(func: _C1): |
| 213 | + |
| 214 | + @_functools.wraps(func) |
| 215 | + def on_config_updated_wrapped(raw: _ce.CloudEvent): |
| 216 | + return _config_handler(func, raw) |
| 217 | + |
| 218 | + _util.set_func_endpoint_attr( |
| 219 | + on_config_updated_wrapped, |
| 220 | + options._endpoint( |
| 221 | + func_name=func.__name__, |
| 222 | + event_filters={}, |
| 223 | + event_type="google.firebase.remoteconfig.remoteConfig.v1.updated" |
| 224 | + ), |
| 225 | + ) |
| 226 | + return on_config_updated_wrapped |
| 227 | + |
| 228 | + return on_config_updated_inner_decorator |
0 commit comments