hosteday-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hosteday/__init__.py +111 -0
- hosteday/_version.py +3 -0
- hosteday/auth/__init__.py +34 -0
- hosteday/auth/hosteday_auth.py +444 -0
- hosteday/auth/hosteday_auth_storage.py +130 -0
- hosteday/auth/hosteday_session.py +184 -0
- hosteday/auth/hosteday_token_provider.py +82 -0
- hosteday/auth/hosteday_user.py +223 -0
- hosteday/cli/__init__.py +5 -0
- hosteday/cli/setup.py +163 -0
- hosteday/config/__init__.py +15 -0
- hosteday/config/hosteday_config.py +576 -0
- hosteday/config/hosteday_option_keys.py +60 -0
- hosteday/config/hosteday_url_resolver.py +255 -0
- hosteday/connectivity/__init__.py +5 -0
- hosteday/connectivity/hosteday_connectivity.py +147 -0
- hosteday/exceptions.py +233 -0
- hosteday/features/__init__.py +5 -0
- hosteday/features/hosteday_features.py +78 -0
- hosteday/hosteday.py +346 -0
- hosteday/hosteday_client.py +579 -0
- hosteday/http/__init__.py +18 -0
- hosteday/http/hosteday_http_client.py +340 -0
- hosteday/http/hosteday_response_reader.py +171 -0
- hosteday/http/query_builder.py +204 -0
- hosteday/jobs/__init__.py +9 -0
- hosteday/jobs/hosteday_job_response.py +76 -0
- hosteday/jobs/hosteday_jobs_client.py +109 -0
- hosteday/notifications/__init__.py +13 -0
- hosteday/notifications/hosteday_notification_client.py +290 -0
- hosteday/notifications/hosteday_notification_response.py +79 -0
- hosteday/py.typed +1 -0
- hosteday/reactive/__init__.py +5 -0
- hosteday/reactive/observable_state.py +67 -0
- hosteday/realtime/__init__.py +31 -0
- hosteday/realtime/connection_state.py +14 -0
- hosteday/realtime/hosteday_channel.py +258 -0
- hosteday/realtime/hosteday_channel_auth.py +163 -0
- hosteday/realtime/hosteday_channel_type.py +68 -0
- hosteday/realtime/hosteday_realtime_client.py +790 -0
- hosteday/realtime/hosteday_realtime_event.py +130 -0
- hosteday/web/__init__.py +5 -0
- hosteday/web/server.py +637 -0
- hosteday_python/__init__.py +6 -0
- hosteday_python/py.typed +1 -0
- hosteday_python-0.1.0.dist-info/METADATA +205 -0
- hosteday_python-0.1.0.dist-info/RECORD +50 -0
- hosteday_python-0.1.0.dist-info/WHEEL +4 -0
- hosteday_python-0.1.0.dist-info/entry_points.txt +2 -0
- hosteday_python-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
"""Typed configuration, zero-config parsing, and platform-owned fixed routes for HosteDay."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from collections.abc import Mapping, Sequence
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
from urllib.parse import urlsplit
|
|
12
|
+
|
|
13
|
+
from hosteday.config.hosteday_option_keys import HostedayOptionKeys
|
|
14
|
+
from hosteday.config.hosteday_url_resolver import HostedayUrlResolver
|
|
15
|
+
from hosteday.exceptions import HostedayException
|
|
16
|
+
from hosteday.features.hosteday_features import HostedayFeatures
|
|
17
|
+
|
|
18
|
+
# Platform-owned fixed routes (intentionally not overridable via config options)
|
|
19
|
+
_SIGN_IN_PATH = "/api/auth/login"
|
|
20
|
+
_SIGN_UP_PATH = "/api/auth/register"
|
|
21
|
+
_SEND_PASSWORD_RESET_EMAIL_PATH = "/api/auth/forgot-password"
|
|
22
|
+
_SEND_CURRENT_USER_PASSWORD_RESET_EMAIL_PATH = "/api/user/password-reset-email"
|
|
23
|
+
_CURRENT_USER_PATH = "/api/user"
|
|
24
|
+
_UPDATE_USER_PATH = "/api/user"
|
|
25
|
+
_UPDATE_USER_AVATAR_PATH = "/api/user/avatar"
|
|
26
|
+
_DELETE_USER_PATH = "/api/user"
|
|
27
|
+
_SIGN_OUT_PATH = "/api/logout"
|
|
28
|
+
_SEND_EMAIL_VERIFICATION_NOTIFICATION_PATH = "/api/email/verification-notification"
|
|
29
|
+
|
|
30
|
+
_PUBLIC_EVENTS_PATH = "/api/realtime/events/public"
|
|
31
|
+
_PRIVATE_EVENTS_PATH = "/api/realtime/events/private"
|
|
32
|
+
_PRESENCE_EVENTS_PATH = "/api/realtime/events/presence"
|
|
33
|
+
_BROADCASTING_AUTH_PATH = "/api/broadcasting/auth-manual"
|
|
34
|
+
|
|
35
|
+
_DEFAULT_REGISTER_DEVICE_TOKEN_PATH = "/api/notifications/devices/register"
|
|
36
|
+
_DEFAULT_UNREGISTER_DEVICE_TOKEN_PATH = "/api/notifications/devices/unregister"
|
|
37
|
+
_DEFAULT_SEND_NOTIFICATION_PATH = "/api/notifications/send"
|
|
38
|
+
_DEFAULT_REGISTERED_DEVICES_PATH = "/api/notifications/devices"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class HostedayConfig:
|
|
43
|
+
"""Immutable configuration used by HosteDay clients."""
|
|
44
|
+
|
|
45
|
+
base_url: str = ""
|
|
46
|
+
project_domain: str = ""
|
|
47
|
+
app_name: str | None = None
|
|
48
|
+
slug: str | None = None
|
|
49
|
+
environment_id: str | None = None
|
|
50
|
+
realtime_channel: str | None = None
|
|
51
|
+
api_token: str | None = None
|
|
52
|
+
api_token_header: str = "X-Api-Token"
|
|
53
|
+
pusher_key: str = ""
|
|
54
|
+
realtime_host: str = ""
|
|
55
|
+
realtime_scheme: str = "wss"
|
|
56
|
+
realtime_port: int = 443
|
|
57
|
+
notifications_enabled: bool = True
|
|
58
|
+
default_notification_topic: str | None = None
|
|
59
|
+
firebase_api_key: str | None = None
|
|
60
|
+
firebase_app_id: str | None = None
|
|
61
|
+
firebase_messaging_sender_id: str | None = None
|
|
62
|
+
firebase_project_id: str | None = None
|
|
63
|
+
firebase_vapid_key: str | None = None
|
|
64
|
+
firebase_auth_domain: str | None = None
|
|
65
|
+
firebase_storage_bucket: str | None = None
|
|
66
|
+
firebase_measurement_id: str | None = None
|
|
67
|
+
jobs_enabled: bool = True
|
|
68
|
+
jobs_path: str = "/api/jobs"
|
|
69
|
+
register_device_token_path: str = _DEFAULT_REGISTER_DEVICE_TOKEN_PATH
|
|
70
|
+
unregister_device_token_path: str = _DEFAULT_UNREGISTER_DEVICE_TOKEN_PATH
|
|
71
|
+
send_notification_path: str = _DEFAULT_SEND_NOTIFICATION_PATH
|
|
72
|
+
registered_devices_path: str = _DEFAULT_REGISTERED_DEVICES_PATH
|
|
73
|
+
features: HostedayFeatures = field(default_factory=HostedayFeatures)
|
|
74
|
+
|
|
75
|
+
def __post_init__(self) -> None:
|
|
76
|
+
raw_domain = self.project_domain.strip()
|
|
77
|
+
raw_base = self.base_url.strip()
|
|
78
|
+
|
|
79
|
+
if not raw_base and not raw_domain:
|
|
80
|
+
raise ValueError(
|
|
81
|
+
f'Either "{HostedayOptionKeys.PROJECT_DOMAIN}" or '
|
|
82
|
+
f'"{HostedayOptionKeys.BASE_URL}" is required.'
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
effective_base = raw_base or HostedayUrlResolver.root(raw_domain)
|
|
86
|
+
parsed = urlsplit(effective_base)
|
|
87
|
+
if parsed.scheme.lower() not in {"http", "https"} or not parsed.hostname:
|
|
88
|
+
raise ValueError("A valid HTTP or HTTPS URL is required.")
|
|
89
|
+
|
|
90
|
+
effective_domain = raw_domain or parsed.hostname
|
|
91
|
+
|
|
92
|
+
normalized_scheme = self.realtime_scheme.strip().lower() or "wss"
|
|
93
|
+
if normalized_scheme not in {"ws", "wss"}:
|
|
94
|
+
raise ValueError("Only ws or wss are supported for realtime_scheme.")
|
|
95
|
+
|
|
96
|
+
if self.realtime_port <= 0 or self.realtime_port > 65535:
|
|
97
|
+
raise ValueError("A valid TCP port between 1 and 65535 is required.")
|
|
98
|
+
|
|
99
|
+
# Validate URL policy eagerly
|
|
100
|
+
HostedayUrlResolver(project_domain=effective_domain, api_base_url=effective_base)
|
|
101
|
+
|
|
102
|
+
object.__setattr__(self, "base_url", effective_base)
|
|
103
|
+
object.__setattr__(self, "project_domain", effective_domain)
|
|
104
|
+
object.__setattr__(self, "realtime_scheme", normalized_scheme)
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def from_options(
|
|
108
|
+
cls,
|
|
109
|
+
options: Mapping[str, Any],
|
|
110
|
+
*,
|
|
111
|
+
firebase_config_path: str | Path | None = None,
|
|
112
|
+
) -> HostedayConfig:
|
|
113
|
+
"""Creates a typed ``HostedayConfig`` from a dictionary of canonical or legacy options."""
|
|
114
|
+
effective_options: dict[str, Any] = dict(options)
|
|
115
|
+
fb_path_arg = firebase_config_path or _string_option(
|
|
116
|
+
effective_options, ("firebase_config_path", "firebaseConfigPath")
|
|
117
|
+
)
|
|
118
|
+
fb_overrides = _load_firebase_config_options(
|
|
119
|
+
explicit_path=fb_path_arg,
|
|
120
|
+
search_dir=None if fb_path_arg is not None else Path.cwd(),
|
|
121
|
+
)
|
|
122
|
+
for key, value in fb_overrides.items():
|
|
123
|
+
if not effective_options.get(key):
|
|
124
|
+
effective_options[key] = value
|
|
125
|
+
options = effective_options
|
|
126
|
+
|
|
127
|
+
raw_domain = _string_option(options, (HostedayOptionKeys.PROJECT_DOMAIN, "domain"))
|
|
128
|
+
raw_base = _string_option(
|
|
129
|
+
options,
|
|
130
|
+
(HostedayOptionKeys.API_BASE_URL, HostedayOptionKeys.BASE_URL),
|
|
131
|
+
)
|
|
132
|
+
base_url = raw_base or (
|
|
133
|
+
HostedayUrlResolver.root(raw_domain) if raw_domain is not None else None
|
|
134
|
+
)
|
|
135
|
+
if base_url is None:
|
|
136
|
+
raise ValueError(
|
|
137
|
+
f'Either "{HostedayOptionKeys.PROJECT_DOMAIN}" or '
|
|
138
|
+
f'"{HostedayOptionKeys.BASE_URL}" is required.'
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
parsed_base = urlsplit(base_url)
|
|
142
|
+
if parsed_base.scheme.lower() not in {"http", "https"} or not parsed_base.hostname:
|
|
143
|
+
raise ValueError("A valid HTTP or HTTPS URL is required.")
|
|
144
|
+
|
|
145
|
+
project_domain = raw_domain or parsed_base.hostname
|
|
146
|
+
|
|
147
|
+
realtime_scheme = (
|
|
148
|
+
_string_option(options, (HostedayOptionKeys.REALTIME_SCHEME,)) or "wss"
|
|
149
|
+
).lower()
|
|
150
|
+
if realtime_scheme not in {"ws", "wss"}:
|
|
151
|
+
raise ValueError("Only ws or wss are supported for realtime_scheme.")
|
|
152
|
+
|
|
153
|
+
realtime_port = _int_option(options, (HostedayOptionKeys.REALTIME_PORT,))
|
|
154
|
+
if realtime_port is None:
|
|
155
|
+
realtime_port = 443 if realtime_scheme == "wss" else 80
|
|
156
|
+
if realtime_port <= 0 or realtime_port > 65535:
|
|
157
|
+
raise ValueError("A valid TCP port between 1 and 65535 is required.")
|
|
158
|
+
|
|
159
|
+
raw_features = options.get("features")
|
|
160
|
+
if isinstance(raw_features, HostedayFeatures):
|
|
161
|
+
features = raw_features
|
|
162
|
+
elif isinstance(raw_features, Mapping):
|
|
163
|
+
features = HostedayFeatures.from_dict(raw_features)
|
|
164
|
+
else:
|
|
165
|
+
features = HostedayFeatures()
|
|
166
|
+
|
|
167
|
+
notifications_enabled_opt = _bool_option(
|
|
168
|
+
options, (HostedayOptionKeys.NOTIFICATIONS_ENABLED,)
|
|
169
|
+
)
|
|
170
|
+
jobs_enabled_opt = _bool_option(
|
|
171
|
+
options,
|
|
172
|
+
(HostedayOptionKeys.JOBS_ENABLED, HostedayOptionKeys.BACKGROUND_JOBS_ENABLED),
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return cls(
|
|
176
|
+
project_domain=project_domain,
|
|
177
|
+
base_url=base_url,
|
|
178
|
+
app_name=_string_option(options, ("app_name", "appName", "project_name")),
|
|
179
|
+
slug=_string_option(options, ("slug",)),
|
|
180
|
+
environment_id=_string_option(options, ("environment_id", "environmentId")),
|
|
181
|
+
realtime_channel=_string_option(
|
|
182
|
+
options,
|
|
183
|
+
(HostedayOptionKeys.REALTIME_CHANNEL, "ws_channel", "wsChannel"),
|
|
184
|
+
),
|
|
185
|
+
api_token=_string_option(
|
|
186
|
+
options,
|
|
187
|
+
(HostedayOptionKeys.PROJECT_API_KEY, HostedayOptionKeys.LEGACY_API_TOKEN),
|
|
188
|
+
),
|
|
189
|
+
api_token_header=_string_option(options, (HostedayOptionKeys.API_TOKEN_HEADER,))
|
|
190
|
+
or "X-Api-Token",
|
|
191
|
+
pusher_key=_string_option(options, (HostedayOptionKeys.REALTIME_APP_KEY,)) or "",
|
|
192
|
+
realtime_host=_string_option(options, (HostedayOptionKeys.REALTIME_HOST,)) or "",
|
|
193
|
+
realtime_scheme=realtime_scheme,
|
|
194
|
+
realtime_port=realtime_port,
|
|
195
|
+
notifications_enabled=(
|
|
196
|
+
notifications_enabled_opt if notifications_enabled_opt is not None else True
|
|
197
|
+
),
|
|
198
|
+
default_notification_topic=_string_option(
|
|
199
|
+
options, (HostedayOptionKeys.DEFAULT_NOTIFICATION_TOPIC,)
|
|
200
|
+
),
|
|
201
|
+
firebase_api_key=_string_option(
|
|
202
|
+
options, (HostedayOptionKeys.FIREBASE_API_KEY, HostedayOptionKeys.FCM_API_KEY)
|
|
203
|
+
),
|
|
204
|
+
firebase_app_id=_string_option(
|
|
205
|
+
options, (HostedayOptionKeys.FIREBASE_APP_ID, HostedayOptionKeys.FCM_APP_ID)
|
|
206
|
+
),
|
|
207
|
+
firebase_messaging_sender_id=_string_option(
|
|
208
|
+
options,
|
|
209
|
+
(HostedayOptionKeys.FIREBASE_MESSAGING_SENDER_ID, HostedayOptionKeys.FCM_SENDER_ID),
|
|
210
|
+
),
|
|
211
|
+
firebase_project_id=_string_option(
|
|
212
|
+
options,
|
|
213
|
+
(HostedayOptionKeys.FIREBASE_PROJECT_ID, HostedayOptionKeys.FCM_PROJECT_ID),
|
|
214
|
+
),
|
|
215
|
+
firebase_vapid_key=_string_option(
|
|
216
|
+
options,
|
|
217
|
+
(
|
|
218
|
+
HostedayOptionKeys.FIREBASE_VAPID_KEY,
|
|
219
|
+
HostedayOptionKeys.FCM_VAPID_KEY,
|
|
220
|
+
HostedayOptionKeys.WEB_PUSH_VAPID_KEY,
|
|
221
|
+
),
|
|
222
|
+
),
|
|
223
|
+
firebase_auth_domain=_string_option(
|
|
224
|
+
options, (HostedayOptionKeys.FIREBASE_AUTH_DOMAIN,)
|
|
225
|
+
),
|
|
226
|
+
firebase_storage_bucket=_string_option(
|
|
227
|
+
options, (HostedayOptionKeys.FIREBASE_STORAGE_BUCKET,)
|
|
228
|
+
),
|
|
229
|
+
firebase_measurement_id=_string_option(
|
|
230
|
+
options, (HostedayOptionKeys.FIREBASE_MEASUREMENT_ID,)
|
|
231
|
+
),
|
|
232
|
+
jobs_enabled=jobs_enabled_opt if jobs_enabled_opt is not None else True,
|
|
233
|
+
jobs_path=_string_option(options, (HostedayOptionKeys.JOBS_PATH,)) or "/api/jobs",
|
|
234
|
+
register_device_token_path=_string_option(
|
|
235
|
+
options, (HostedayOptionKeys.REGISTER_DEVICE_TOKEN_PATH,)
|
|
236
|
+
)
|
|
237
|
+
or _DEFAULT_REGISTER_DEVICE_TOKEN_PATH,
|
|
238
|
+
unregister_device_token_path=_string_option(
|
|
239
|
+
options, (HostedayOptionKeys.UNREGISTER_DEVICE_TOKEN_PATH,)
|
|
240
|
+
)
|
|
241
|
+
or _DEFAULT_UNREGISTER_DEVICE_TOKEN_PATH,
|
|
242
|
+
send_notification_path=_string_option(
|
|
243
|
+
options, (HostedayOptionKeys.SEND_NOTIFICATION_PATH,)
|
|
244
|
+
)
|
|
245
|
+
or _DEFAULT_SEND_NOTIFICATION_PATH,
|
|
246
|
+
registered_devices_path=_string_option(
|
|
247
|
+
options, (HostedayOptionKeys.REGISTERED_DEVICES_PATH,)
|
|
248
|
+
)
|
|
249
|
+
or _DEFAULT_REGISTERED_DEVICES_PATH,
|
|
250
|
+
features=features,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
@classmethod
|
|
254
|
+
def from_file(
|
|
255
|
+
cls,
|
|
256
|
+
path: str | Path = "hosteday_config.json",
|
|
257
|
+
*,
|
|
258
|
+
overrides: Mapping[str, Any] | None = None,
|
|
259
|
+
firebase_config_path: str | Path | None = None,
|
|
260
|
+
) -> HostedayConfig:
|
|
261
|
+
"""Loads ``HostedayConfig`` from a JSON configuration file.
|
|
262
|
+
|
|
263
|
+
Automatically discovers and merges a sibling ``firebase-config.json`` (in the same
|
|
264
|
+
directory as ``path`` or in a ``web/`` subdirectory) unless ``firebase_config_path``
|
|
265
|
+
points to a specific file.
|
|
266
|
+
"""
|
|
267
|
+
file_path = Path(path)
|
|
268
|
+
try:
|
|
269
|
+
raw_text = file_path.read_text(encoding="utf-8")
|
|
270
|
+
except OSError as exc:
|
|
271
|
+
raise HostedayException(
|
|
272
|
+
f'Missing configuration file "{file_path}". '
|
|
273
|
+
"Please download hosteday_config.json from your HosteDay dashboard "
|
|
274
|
+
"or run `hosteday-setup`.",
|
|
275
|
+
error=exc,
|
|
276
|
+
) from exc
|
|
277
|
+
|
|
278
|
+
try:
|
|
279
|
+
decoded = json.loads(raw_text)
|
|
280
|
+
except json.JSONDecodeError as exc:
|
|
281
|
+
raise HostedayException(
|
|
282
|
+
f'Invalid JSON in "{file_path}". '
|
|
283
|
+
"Please download a valid hosteday_config.json from your HosteDay dashboard.",
|
|
284
|
+
error=exc,
|
|
285
|
+
) from exc
|
|
286
|
+
|
|
287
|
+
if not isinstance(decoded, Mapping):
|
|
288
|
+
raise HostedayException(
|
|
289
|
+
f'Invalid configuration format in "{file_path}": expected a JSON object.'
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
fb_options = _load_firebase_config_options(
|
|
293
|
+
explicit_path=firebase_config_path,
|
|
294
|
+
search_dir=file_path.parent,
|
|
295
|
+
)
|
|
296
|
+
merged: dict[str, Any] = {
|
|
297
|
+
HostedayOptionKeys.JOBS_PATH: "/api/jobs",
|
|
298
|
+
**fb_options,
|
|
299
|
+
**decoded,
|
|
300
|
+
}
|
|
301
|
+
if overrides:
|
|
302
|
+
merged.update(overrides)
|
|
303
|
+
|
|
304
|
+
try:
|
|
305
|
+
return cls.from_options(merged)
|
|
306
|
+
except ValueError as exc:
|
|
307
|
+
raise HostedayException(
|
|
308
|
+
f'Invalid or incomplete configuration in "{file_path}": {exc}',
|
|
309
|
+
error=exc,
|
|
310
|
+
) from exc
|
|
311
|
+
|
|
312
|
+
@classmethod
|
|
313
|
+
def from_env(
|
|
314
|
+
cls,
|
|
315
|
+
*,
|
|
316
|
+
prefix: str = "HOSTEDAY_",
|
|
317
|
+
environ: Mapping[str, str] | None = None,
|
|
318
|
+
) -> HostedayConfig:
|
|
319
|
+
"""Loads ``HostedayConfig`` from environment variables (e.g. ``HOSTEDAY_PROJECT_DOMAIN``)."""
|
|
320
|
+
env = environ if environ is not None else os.environ
|
|
321
|
+
options: dict[str, Any] = {}
|
|
322
|
+
key_names = (
|
|
323
|
+
HostedayOptionKeys.PROJECT_DOMAIN,
|
|
324
|
+
HostedayOptionKeys.API_BASE_URL,
|
|
325
|
+
HostedayOptionKeys.BASE_URL,
|
|
326
|
+
HostedayOptionKeys.PROJECT_API_KEY,
|
|
327
|
+
HostedayOptionKeys.API_TOKEN_HEADER,
|
|
328
|
+
HostedayOptionKeys.REALTIME_APP_KEY,
|
|
329
|
+
HostedayOptionKeys.REALTIME_HOST,
|
|
330
|
+
HostedayOptionKeys.REALTIME_SCHEME,
|
|
331
|
+
HostedayOptionKeys.REALTIME_PORT,
|
|
332
|
+
HostedayOptionKeys.REALTIME_CHANNEL,
|
|
333
|
+
HostedayOptionKeys.NOTIFICATIONS_ENABLED,
|
|
334
|
+
HostedayOptionKeys.DEFAULT_NOTIFICATION_TOPIC,
|
|
335
|
+
HostedayOptionKeys.REGISTER_DEVICE_TOKEN_PATH,
|
|
336
|
+
HostedayOptionKeys.UNREGISTER_DEVICE_TOKEN_PATH,
|
|
337
|
+
HostedayOptionKeys.SEND_NOTIFICATION_PATH,
|
|
338
|
+
HostedayOptionKeys.REGISTERED_DEVICES_PATH,
|
|
339
|
+
HostedayOptionKeys.JOBS_ENABLED,
|
|
340
|
+
HostedayOptionKeys.JOBS_PATH,
|
|
341
|
+
"app_name",
|
|
342
|
+
"slug",
|
|
343
|
+
"environment_id",
|
|
344
|
+
)
|
|
345
|
+
for key in key_names:
|
|
346
|
+
env_key = f"{prefix}{key.upper()}"
|
|
347
|
+
if env_key in env:
|
|
348
|
+
options[key] = env[env_key]
|
|
349
|
+
|
|
350
|
+
features_raw = env.get(f"{prefix}FEATURES")
|
|
351
|
+
if features_raw:
|
|
352
|
+
try:
|
|
353
|
+
parsed_features = json.loads(features_raw)
|
|
354
|
+
if isinstance(parsed_features, Mapping):
|
|
355
|
+
options["features"] = parsed_features
|
|
356
|
+
except json.JSONDecodeError as exc:
|
|
357
|
+
raise ValueError(f"Invalid JSON in {prefix}FEATURES.") from exc
|
|
358
|
+
|
|
359
|
+
return cls.from_options(options)
|
|
360
|
+
|
|
361
|
+
# ------------------------------------------------------------------
|
|
362
|
+
# Platform-Owned Fixed Routes (read-only properties)
|
|
363
|
+
# ------------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
@property
|
|
366
|
+
def sign_in_path(self) -> str:
|
|
367
|
+
return _SIGN_IN_PATH
|
|
368
|
+
|
|
369
|
+
@property
|
|
370
|
+
def sign_up_path(self) -> str:
|
|
371
|
+
return _SIGN_UP_PATH
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def send_password_reset_email_path(self) -> str:
|
|
375
|
+
return _SEND_PASSWORD_RESET_EMAIL_PATH
|
|
376
|
+
|
|
377
|
+
@property
|
|
378
|
+
def send_current_user_password_reset_email_path(self) -> str:
|
|
379
|
+
return _SEND_CURRENT_USER_PASSWORD_RESET_EMAIL_PATH
|
|
380
|
+
|
|
381
|
+
@property
|
|
382
|
+
def current_user_path(self) -> str:
|
|
383
|
+
return _CURRENT_USER_PATH
|
|
384
|
+
|
|
385
|
+
@property
|
|
386
|
+
def update_user_path(self) -> str:
|
|
387
|
+
return _UPDATE_USER_PATH
|
|
388
|
+
|
|
389
|
+
@property
|
|
390
|
+
def update_user_avatar_path(self) -> str:
|
|
391
|
+
return _UPDATE_USER_AVATAR_PATH
|
|
392
|
+
|
|
393
|
+
@property
|
|
394
|
+
def delete_user_path(self) -> str:
|
|
395
|
+
return _DELETE_USER_PATH
|
|
396
|
+
|
|
397
|
+
@property
|
|
398
|
+
def sign_out_path(self) -> str:
|
|
399
|
+
return _SIGN_OUT_PATH
|
|
400
|
+
|
|
401
|
+
@property
|
|
402
|
+
def send_email_verification_notification_path(self) -> str:
|
|
403
|
+
return _SEND_EMAIL_VERIFICATION_NOTIFICATION_PATH
|
|
404
|
+
|
|
405
|
+
@property
|
|
406
|
+
def public_events_path(self) -> str:
|
|
407
|
+
return _PUBLIC_EVENTS_PATH
|
|
408
|
+
|
|
409
|
+
@property
|
|
410
|
+
def private_events_path(self) -> str:
|
|
411
|
+
return _PRIVATE_EVENTS_PATH
|
|
412
|
+
|
|
413
|
+
@property
|
|
414
|
+
def presence_events_path(self) -> str:
|
|
415
|
+
return _PRESENCE_EVENTS_PATH
|
|
416
|
+
|
|
417
|
+
@property
|
|
418
|
+
def broadcasting_auth_path(self) -> str:
|
|
419
|
+
return _BROADCASTING_AUTH_PATH
|
|
420
|
+
|
|
421
|
+
# ------------------------------------------------------------------
|
|
422
|
+
# Derived URL & Header Properties
|
|
423
|
+
# ------------------------------------------------------------------
|
|
424
|
+
|
|
425
|
+
@property
|
|
426
|
+
def urls(self) -> HostedayUrlResolver:
|
|
427
|
+
return HostedayUrlResolver(
|
|
428
|
+
project_domain=self.project_domain,
|
|
429
|
+
api_base_url=self.base_url,
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
@property
|
|
433
|
+
def host(self) -> str:
|
|
434
|
+
return self.urls.host
|
|
435
|
+
|
|
436
|
+
@property
|
|
437
|
+
def api_base_url(self) -> str:
|
|
438
|
+
return self.urls.api_base_url
|
|
439
|
+
|
|
440
|
+
@property
|
|
441
|
+
def default_headers(self) -> dict[str, str]:
|
|
442
|
+
token = self.api_token.strip() if self.api_token else ""
|
|
443
|
+
if not token:
|
|
444
|
+
return {}
|
|
445
|
+
return {self.api_token_header: token}
|
|
446
|
+
|
|
447
|
+
@property
|
|
448
|
+
def has_realtime(self) -> bool:
|
|
449
|
+
return bool(self.pusher_key.strip() and self.realtime_host.strip())
|
|
450
|
+
|
|
451
|
+
@property
|
|
452
|
+
def has_firebase_config(self) -> bool:
|
|
453
|
+
return bool(
|
|
454
|
+
(self.firebase_api_key or "").strip()
|
|
455
|
+
and (self.firebase_app_id or "").strip()
|
|
456
|
+
and (self.firebase_project_id or "").strip()
|
|
457
|
+
)
|
|
458
|
+
|
|
459
|
+
@property
|
|
460
|
+
def realtime_url(self) -> str:
|
|
461
|
+
resolved_host = HostedayUrlResolver.normalize_realtime_host(self.realtime_host)
|
|
462
|
+
resolved_port = HostedayUrlResolver.resolve_realtime_port(
|
|
463
|
+
scheme=self.realtime_scheme,
|
|
464
|
+
host=resolved_host,
|
|
465
|
+
port=self.realtime_port,
|
|
466
|
+
)
|
|
467
|
+
return f"{self.realtime_scheme}://{resolved_host}:{resolved_port}/app/{self.pusher_key.strip()}"
|
|
468
|
+
|
|
469
|
+
def uri(self, path: str) -> str:
|
|
470
|
+
"""Resolves a REST API path against the configured API base URL."""
|
|
471
|
+
return self.urls.api(path)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
_FIREBASE_JSON_FIELD_MAP: dict[str, str] = {
|
|
475
|
+
"apiKey": HostedayOptionKeys.FIREBASE_API_KEY,
|
|
476
|
+
"appId": HostedayOptionKeys.FIREBASE_APP_ID,
|
|
477
|
+
"messagingSenderId": HostedayOptionKeys.FIREBASE_MESSAGING_SENDER_ID,
|
|
478
|
+
"projectId": HostedayOptionKeys.FIREBASE_PROJECT_ID,
|
|
479
|
+
"authDomain": HostedayOptionKeys.FIREBASE_AUTH_DOMAIN,
|
|
480
|
+
"storageBucket": HostedayOptionKeys.FIREBASE_STORAGE_BUCKET,
|
|
481
|
+
"measurementId": HostedayOptionKeys.FIREBASE_MEASUREMENT_ID,
|
|
482
|
+
"vapidKey": HostedayOptionKeys.FIREBASE_VAPID_KEY,
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def _load_firebase_config_options(
|
|
487
|
+
*,
|
|
488
|
+
explicit_path: str | Path | None,
|
|
489
|
+
search_dir: Path | None,
|
|
490
|
+
) -> dict[str, str]:
|
|
491
|
+
"""Reads an optional Web ``firebase-config.json`` and maps its fields to ``HostedayOptionKeys``."""
|
|
492
|
+
target: Path | None = None
|
|
493
|
+
if explicit_path is not None:
|
|
494
|
+
candidate = Path(explicit_path)
|
|
495
|
+
if not candidate.is_file():
|
|
496
|
+
raise HostedayException(
|
|
497
|
+
f'Missing Firebase configuration file "{candidate}".'
|
|
498
|
+
)
|
|
499
|
+
target = candidate
|
|
500
|
+
elif search_dir is not None:
|
|
501
|
+
for candidate in (
|
|
502
|
+
search_dir / "firebase-config.json",
|
|
503
|
+
search_dir / "web" / "firebase-config.json",
|
|
504
|
+
):
|
|
505
|
+
if candidate.is_file():
|
|
506
|
+
target = candidate
|
|
507
|
+
break
|
|
508
|
+
|
|
509
|
+
if target is None:
|
|
510
|
+
return {}
|
|
511
|
+
|
|
512
|
+
try:
|
|
513
|
+
decoded = json.loads(target.read_text(encoding="utf-8"))
|
|
514
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
515
|
+
if explicit_path is not None:
|
|
516
|
+
raise HostedayException(
|
|
517
|
+
f'Invalid Firebase configuration file "{target}": {exc}',
|
|
518
|
+
error=exc,
|
|
519
|
+
) from exc
|
|
520
|
+
return {}
|
|
521
|
+
|
|
522
|
+
if not isinstance(decoded, Mapping):
|
|
523
|
+
if explicit_path is not None:
|
|
524
|
+
raise HostedayException(
|
|
525
|
+
f'Invalid Firebase configuration format in "{target}": expected a JSON object.'
|
|
526
|
+
)
|
|
527
|
+
return {}
|
|
528
|
+
|
|
529
|
+
mapped: dict[str, str] = {}
|
|
530
|
+
for json_key, option_key in _FIREBASE_JSON_FIELD_MAP.items():
|
|
531
|
+
value = decoded.get(json_key)
|
|
532
|
+
if value is not None:
|
|
533
|
+
text = str(value).strip()
|
|
534
|
+
if text:
|
|
535
|
+
mapped[option_key] = text
|
|
536
|
+
return mapped
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
def _string_option(options: Mapping[str, Any], keys: Sequence[str]) -> str | None:
|
|
540
|
+
for key in keys:
|
|
541
|
+
value = options.get(key)
|
|
542
|
+
if value is None:
|
|
543
|
+
continue
|
|
544
|
+
text = str(value).strip()
|
|
545
|
+
if text:
|
|
546
|
+
return text
|
|
547
|
+
return None
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def _int_option(options: Mapping[str, Any], keys: Sequence[str]) -> int | None:
|
|
551
|
+
for key in keys:
|
|
552
|
+
value = options.get(key)
|
|
553
|
+
if value is None or isinstance(value, bool):
|
|
554
|
+
continue
|
|
555
|
+
if isinstance(value, int):
|
|
556
|
+
return value
|
|
557
|
+
try:
|
|
558
|
+
return int(str(value).strip())
|
|
559
|
+
except ValueError:
|
|
560
|
+
continue
|
|
561
|
+
return None
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def _bool_option(options: Mapping[str, Any], keys: Sequence[str]) -> bool | None:
|
|
565
|
+
for key in keys:
|
|
566
|
+
value = options.get(key)
|
|
567
|
+
if value is None:
|
|
568
|
+
continue
|
|
569
|
+
if isinstance(value, bool):
|
|
570
|
+
return value
|
|
571
|
+
text = str(value).strip().lower()
|
|
572
|
+
if text in {"true", "1"}:
|
|
573
|
+
return True
|
|
574
|
+
if text in {"false", "0"}:
|
|
575
|
+
return False
|
|
576
|
+
return None
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Official and legacy option key constants accepted when initializing HosteDay."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class HostedayOptionKeys:
|
|
7
|
+
"""Canonical option keys for ``HostedayConfig.from_options`` and ``hosteday_config.json``.
|
|
8
|
+
|
|
9
|
+
Authentication and user API paths are owned by HosteDay and are intentionally
|
|
10
|
+
not configurable from initialization options.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
PROJECT_DOMAIN: str = "project_domain"
|
|
14
|
+
API_BASE_URL: str = "api_base_url"
|
|
15
|
+
BASE_URL: str = "base_url"
|
|
16
|
+
|
|
17
|
+
PROJECT_API_KEY: str = "project_api_key"
|
|
18
|
+
API_TOKEN_HEADER: str = "api_token_header"
|
|
19
|
+
LEGACY_API_TOKEN: str = "X-Api-Token"
|
|
20
|
+
|
|
21
|
+
REALTIME_APP_KEY: str = "realtime_app_key"
|
|
22
|
+
REALTIME_HOST: str = "realtime_host"
|
|
23
|
+
REALTIME_SCHEME: str = "realtime_scheme"
|
|
24
|
+
REALTIME_PORT: str = "realtime_port"
|
|
25
|
+
REALTIME_CHANNEL: str = "realtime_channel"
|
|
26
|
+
|
|
27
|
+
BROADCASTING_AUTH_PATH: str = "broadcasting_auth_path"
|
|
28
|
+
PUBLIC_EVENTS_PATH: str = "public_events_path"
|
|
29
|
+
PRIVATE_EVENTS_PATH: str = "private_events_path"
|
|
30
|
+
PRESENCE_EVENTS_PATH: str = "presence_events_path"
|
|
31
|
+
|
|
32
|
+
NOTIFICATIONS_ENABLED: str = "notifications_enabled"
|
|
33
|
+
SEND_NOTIFICATION_PATH: str = "send_notification_path"
|
|
34
|
+
REGISTER_DEVICE_TOKEN_PATH: str = "register_device_token_path"
|
|
35
|
+
UNREGISTER_DEVICE_TOKEN_PATH: str = "unregister_device_token_path"
|
|
36
|
+
REGISTERED_DEVICES_PATH: str = "registered_devices_path"
|
|
37
|
+
DEFAULT_NOTIFICATION_TOPIC: str = "default_notification_topic"
|
|
38
|
+
|
|
39
|
+
FIREBASE_API_KEY: str = "firebase_api_key"
|
|
40
|
+
FCM_API_KEY: str = "fcm_api_key"
|
|
41
|
+
FIREBASE_APP_ID: str = "firebase_app_id"
|
|
42
|
+
FCM_APP_ID: str = "fcm_app_id"
|
|
43
|
+
FIREBASE_MESSAGING_SENDER_ID: str = "firebase_messaging_sender_id"
|
|
44
|
+
FCM_SENDER_ID: str = "fcm_sender_id"
|
|
45
|
+
FIREBASE_PROJECT_ID: str = "firebase_project_id"
|
|
46
|
+
FCM_PROJECT_ID: str = "fcm_project_id"
|
|
47
|
+
FIREBASE_VAPID_KEY: str = "firebase_vapid_key"
|
|
48
|
+
FCM_VAPID_KEY: str = "fcm_vapid_key"
|
|
49
|
+
WEB_PUSH_VAPID_KEY: str = "web_push_vapid_key"
|
|
50
|
+
FIREBASE_AUTH_DOMAIN: str = "firebase_auth_domain"
|
|
51
|
+
FIREBASE_STORAGE_BUCKET: str = "firebase_storage_bucket"
|
|
52
|
+
FIREBASE_MEASUREMENT_ID: str = "firebase_measurement_id"
|
|
53
|
+
|
|
54
|
+
JOBS_ENABLED: str = "jobs_enabled"
|
|
55
|
+
BACKGROUND_JOBS_ENABLED: str = "background_jobs_enabled"
|
|
56
|
+
JOBS_PATH: str = "jobs_path"
|
|
57
|
+
|
|
58
|
+
SEARCH: str = "search"
|
|
59
|
+
RELATION_FIELD: str = "relation_field"
|
|
60
|
+
RELATION_VALUE: str = "relation_value"
|