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.
Files changed (50) hide show
  1. hosteday/__init__.py +111 -0
  2. hosteday/_version.py +3 -0
  3. hosteday/auth/__init__.py +34 -0
  4. hosteday/auth/hosteday_auth.py +444 -0
  5. hosteday/auth/hosteday_auth_storage.py +130 -0
  6. hosteday/auth/hosteday_session.py +184 -0
  7. hosteday/auth/hosteday_token_provider.py +82 -0
  8. hosteday/auth/hosteday_user.py +223 -0
  9. hosteday/cli/__init__.py +5 -0
  10. hosteday/cli/setup.py +163 -0
  11. hosteday/config/__init__.py +15 -0
  12. hosteday/config/hosteday_config.py +576 -0
  13. hosteday/config/hosteday_option_keys.py +60 -0
  14. hosteday/config/hosteday_url_resolver.py +255 -0
  15. hosteday/connectivity/__init__.py +5 -0
  16. hosteday/connectivity/hosteday_connectivity.py +147 -0
  17. hosteday/exceptions.py +233 -0
  18. hosteday/features/__init__.py +5 -0
  19. hosteday/features/hosteday_features.py +78 -0
  20. hosteday/hosteday.py +346 -0
  21. hosteday/hosteday_client.py +579 -0
  22. hosteday/http/__init__.py +18 -0
  23. hosteday/http/hosteday_http_client.py +340 -0
  24. hosteday/http/hosteday_response_reader.py +171 -0
  25. hosteday/http/query_builder.py +204 -0
  26. hosteday/jobs/__init__.py +9 -0
  27. hosteday/jobs/hosteday_job_response.py +76 -0
  28. hosteday/jobs/hosteday_jobs_client.py +109 -0
  29. hosteday/notifications/__init__.py +13 -0
  30. hosteday/notifications/hosteday_notification_client.py +290 -0
  31. hosteday/notifications/hosteday_notification_response.py +79 -0
  32. hosteday/py.typed +1 -0
  33. hosteday/reactive/__init__.py +5 -0
  34. hosteday/reactive/observable_state.py +67 -0
  35. hosteday/realtime/__init__.py +31 -0
  36. hosteday/realtime/connection_state.py +14 -0
  37. hosteday/realtime/hosteday_channel.py +258 -0
  38. hosteday/realtime/hosteday_channel_auth.py +163 -0
  39. hosteday/realtime/hosteday_channel_type.py +68 -0
  40. hosteday/realtime/hosteday_realtime_client.py +790 -0
  41. hosteday/realtime/hosteday_realtime_event.py +130 -0
  42. hosteday/web/__init__.py +5 -0
  43. hosteday/web/server.py +637 -0
  44. hosteday_python/__init__.py +6 -0
  45. hosteday_python/py.typed +1 -0
  46. hosteday_python-0.1.0.dist-info/METADATA +205 -0
  47. hosteday_python-0.1.0.dist-info/RECORD +50 -0
  48. hosteday_python-0.1.0.dist-info/WHEEL +4 -0
  49. hosteday_python-0.1.0.dist-info/entry_points.txt +2 -0
  50. hosteday_python-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,78 @@
1
+ """Represents feature flags and plan entitlements for a HosteDay application."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from collections.abc import Mapping
7
+ from pathlib import Path
8
+ from types import MappingProxyType
9
+ from typing import Any
10
+
11
+
12
+ class HostedayFeatures:
13
+ """Plan entitlements and feature flags for a HosteDay project."""
14
+
15
+ DEFAULT_FEATURES: Mapping[str, bool] = MappingProxyType(
16
+ {
17
+ "push_notifications": True,
18
+ "realtime_websockets": True,
19
+ "database_management": True,
20
+ }
21
+ )
22
+
23
+ def __init__(self, features: Mapping[str, bool] | None = None) -> None:
24
+ if features is None:
25
+ self._features: Mapping[str, bool] = self.DEFAULT_FEATURES
26
+ else:
27
+ self._features = MappingProxyType(dict(features))
28
+
29
+ @classmethod
30
+ def from_dict(cls, data: Mapping[str, Any]) -> HostedayFeatures:
31
+ """Parses feature flags flexibly from booleans, numbers, or strings."""
32
+ parsed: dict[str, bool] = {}
33
+ for key, value in data.items():
34
+ if isinstance(value, bool):
35
+ parsed[str(key)] = value
36
+ elif isinstance(value, (int, float)):
37
+ parsed[str(key)] = value != 0
38
+ elif isinstance(value, str):
39
+ lower = value.strip().lower()
40
+ parsed[str(key)] = lower in {"true", "1"}
41
+ return cls(parsed)
42
+
43
+ @classmethod
44
+ def from_file(cls, path: str | Path) -> HostedayFeatures:
45
+ """Loads feature flags from a JSON file."""
46
+ file_path = Path(path)
47
+ raw = json.loads(file_path.read_text(encoding="utf-8"))
48
+ if not isinstance(raw, Mapping):
49
+ raise ValueError(f"Expected a JSON object in features file '{file_path}'.")
50
+ return cls.from_dict(raw)
51
+
52
+ def is_enabled(self, key: str, *, default: bool = True) -> bool:
53
+ """Checks whether a feature key is enabled, falling back to ``default``."""
54
+ return self._features.get(key, default)
55
+
56
+ @property
57
+ def is_notifications_enabled(self) -> bool:
58
+ return self.is_enabled("push_notifications")
59
+
60
+ @property
61
+ def is_realtime_enabled(self) -> bool:
62
+ return self.is_enabled("realtime_websockets")
63
+
64
+ @property
65
+ def is_database_enabled(self) -> bool:
66
+ return self.is_enabled("database_management")
67
+
68
+ def to_dict(self) -> dict[str, bool]:
69
+ """Returns a mutable copy of all registered feature flags."""
70
+ return dict(self._features)
71
+
72
+ def __repr__(self) -> str:
73
+ return f"HostedayFeatures({dict(self._features)!r})"
74
+
75
+ def __eq__(self, other: object) -> bool:
76
+ if not isinstance(other, HostedayFeatures):
77
+ return NotImplemented
78
+ return dict(self._features) == dict(other._features)
hosteday/hosteday.py ADDED
@@ -0,0 +1,346 @@
1
+ """Static Facade and singleton lifecycle manager for the HosteDay Python SDK."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from collections.abc import Mapping
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ import httpx
11
+
12
+ from hosteday.auth.hosteday_auth import HostedayAuth
13
+ from hosteday.auth.hosteday_auth_storage import HostedayAuthStorage
14
+ from hosteday.auth.hosteday_token_provider import HostedayTokenProvider
15
+ from hosteday.config.hosteday_config import HostedayConfig
16
+ from hosteday.connectivity.hosteday_connectivity import HostedayConnectivity
17
+ from hosteday.features.hosteday_features import HostedayFeatures
18
+ from hosteday.hosteday_client import HostedayClient
19
+ from hosteday.http.hosteday_http_client import HostedayHttpClient
20
+ from hosteday.jobs.hosteday_jobs_client import HostedayJobsClient
21
+ from hosteday.notifications.hosteday_notification_client import (
22
+ HostedayNotificationClient,
23
+ )
24
+ from hosteday.realtime.hosteday_realtime_client import (
25
+ HostedayRealtimeClient,
26
+ WebSocketConnector,
27
+ )
28
+ from hosteday.web.server import HostedayWebApp
29
+
30
+
31
+ class _HostedayMeta(type):
32
+ """Metaclass exposing static properties directly on ``Hosteday``."""
33
+
34
+ _client: HostedayClient | None = None
35
+ _config: HostedayConfig | None = None
36
+ _initializing: asyncio.Future[HostedayClient] | None = None
37
+ _init_lock: asyncio.Lock | None = None
38
+
39
+ @property
40
+ def is_initialized(cls) -> bool:
41
+ return cls._client is not None
42
+
43
+ @property
44
+ def is_initializing(cls) -> bool:
45
+ return cls._initializing is not None and not cls._initializing.done()
46
+
47
+ @property
48
+ def instance(cls) -> HostedayClient:
49
+ if cls._client is None:
50
+ raise RuntimeError(
51
+ "Hosteday has not been initialized. Call Hosteday.initialize_app() first."
52
+ )
53
+ return cls._client
54
+
55
+ @property
56
+ def client(cls) -> HostedayClient:
57
+ return cls.instance
58
+
59
+ @property
60
+ def config(cls) -> HostedayConfig:
61
+ if cls._config is None:
62
+ raise RuntimeError(
63
+ "Hosteday has not been initialized. Call Hosteday.initialize_app() first."
64
+ )
65
+ return cls._config
66
+
67
+ @property
68
+ def auth(cls) -> HostedayAuth:
69
+ return cls.client.auth
70
+
71
+ @property
72
+ def http(cls) -> HostedayHttpClient:
73
+ return cls.client.http
74
+
75
+ @property
76
+ def realtime(cls) -> HostedayRealtimeClient:
77
+ return cls.client.realtime
78
+
79
+ @property
80
+ def notifications(cls) -> HostedayNotificationClient:
81
+ return cls.client.notifications
82
+
83
+ @property
84
+ def jobs(cls) -> HostedayJobsClient:
85
+ return cls.client.jobs
86
+
87
+ @property
88
+ def features(cls) -> HostedayFeatures:
89
+ return cls.client.features
90
+
91
+ @property
92
+ def connectivity(cls) -> HostedayConnectivity:
93
+ return cls.client.connectivity
94
+
95
+ @property
96
+ def is_online(cls) -> bool:
97
+ return cls.client.connectivity.is_connected
98
+
99
+
100
+ class Hosteday(metaclass=_HostedayMeta):
101
+ """Global HosteDay application entry point (Static Facade)."""
102
+
103
+ CONFIG_FILE_PATH: str = "hosteday_config.json"
104
+
105
+ def __new__(cls, *args: Any, **kwargs: Any) -> Hosteday:
106
+ raise TypeError("Hosteday is a static facade and cannot be instantiated directly.")
107
+
108
+ @classmethod
109
+ def _get_lock(cls) -> asyncio.Lock:
110
+ if cls._init_lock is None:
111
+ cls._init_lock = asyncio.Lock()
112
+ return cls._init_lock
113
+
114
+ @classmethod
115
+ async def initialize_app(
116
+ cls,
117
+ *,
118
+ config: HostedayConfig | None = None,
119
+ options: Mapping[str, Any] | None = None,
120
+ config_path: str | Path | None = None,
121
+ firebase_config_path: str | Path | None = None,
122
+ token_provider: HostedayTokenProvider | None = None,
123
+ auth_storage: HostedayAuthStorage | None = None,
124
+ connect_realtime: bool = True,
125
+ http_client: httpx.AsyncClient | None = None,
126
+ connectivity: HostedayConnectivity | None = None,
127
+ enable_active_probe: bool = True,
128
+ ws_connector: WebSocketConnector | None = None,
129
+ ) -> HostedayClient:
130
+ """Initializes the global HosteDay singleton with in-flight deduplication and atomic rollback."""
131
+ lock = cls._get_lock()
132
+ is_leader = False
133
+ in_flight: asyncio.Future[HostedayClient]
134
+
135
+ async with lock:
136
+ if cls._client is not None:
137
+ raise RuntimeError(
138
+ "Hosteday is already initialized. "
139
+ "Use Hosteday.client or call Hosteday.dispose() first."
140
+ )
141
+ if cls._initializing is not None and not cls._initializing.done():
142
+ in_flight = cls._initializing
143
+ else:
144
+ loop = asyncio.get_running_loop()
145
+ in_flight = loop.create_future()
146
+ cls._initializing = in_flight
147
+ is_leader = True
148
+
149
+ if not is_leader:
150
+ return await in_flight
151
+
152
+ app_client: HostedayClient | None = None
153
+ try:
154
+ app_config = cls._resolve_config(
155
+ config=config,
156
+ options=options,
157
+ config_path=config_path,
158
+ firebase_config_path=firebase_config_path,
159
+ )
160
+ app_client = HostedayClient(
161
+ config=app_config,
162
+ token_provider=token_provider,
163
+ http_client=http_client,
164
+ auth_storage=auth_storage,
165
+ connectivity=connectivity,
166
+ enable_active_probe=enable_active_probe,
167
+ ws_connector=ws_connector,
168
+ )
169
+
170
+ await app_client.initialize()
171
+
172
+ if (
173
+ connect_realtime
174
+ and app_config.has_realtime
175
+ and app_config.features.is_realtime_enabled
176
+ ):
177
+ try:
178
+ await app_client.realtime.connect()
179
+ except Exception:
180
+ # Realtime errors are surfaced via Hosteday.realtime.on_error
181
+ # without blocking application startup.
182
+ pass
183
+
184
+ async with lock:
185
+ cls._config = app_config
186
+ cls._client = app_client
187
+ if not in_flight.done():
188
+ in_flight.set_result(app_client)
189
+ cls._initializing = None
190
+
191
+ return app_client
192
+ except BaseException as exc:
193
+ if app_client is not None:
194
+ try:
195
+ await app_client.dispose()
196
+ except Exception:
197
+ pass
198
+ async with lock:
199
+ cls._client = None
200
+ cls._config = None
201
+ if not in_flight.done():
202
+ in_flight.set_exception(exc)
203
+ cls._initializing = None
204
+ raise
205
+
206
+ @classmethod
207
+ def _resolve_config(
208
+ cls,
209
+ *,
210
+ config: HostedayConfig | None,
211
+ options: Mapping[str, Any] | None,
212
+ config_path: str | Path | None,
213
+ firebase_config_path: str | Path | None = None,
214
+ ) -> HostedayConfig:
215
+ if config is not None:
216
+ return config
217
+ if options is not None:
218
+ return HostedayConfig.from_options(
219
+ options,
220
+ firebase_config_path=firebase_config_path,
221
+ )
222
+ target_path = config_path if config_path is not None else cls.CONFIG_FILE_PATH
223
+ return HostedayConfig.from_file(
224
+ target_path,
225
+ firebase_config_path=firebase_config_path,
226
+ )
227
+
228
+ @classmethod
229
+ async def connect_realtime(cls) -> None:
230
+ await cls.realtime.connect()
231
+
232
+ @classmethod
233
+ async def disconnect_realtime(cls) -> None:
234
+ await cls.realtime.disconnect()
235
+
236
+ @classmethod
237
+ def create_web_app(
238
+ cls,
239
+ *,
240
+ title: str = "HosteDay Web App",
241
+ config: HostedayConfig | None = None,
242
+ options: Mapping[str, Any] | None = None,
243
+ config_path: str | Path | None = None,
244
+ firebase_config_path: str | Path | None = None,
245
+ auth_storage: HostedayAuthStorage | None = None,
246
+ session_path: str | Path | None = None,
247
+ static_dir: str | Path | None = None,
248
+ connect_realtime: bool = False,
249
+ enable_active_probe: bool = True,
250
+ http_client: httpx.AsyncClient | None = None,
251
+ auto_initialize: bool = True,
252
+ ) -> HostedayWebApp:
253
+ """Creates an embedded ``HostedayWebApp`` with built-in BFF routes and SPA serving."""
254
+ return HostedayWebApp(
255
+ title=title,
256
+ config=config,
257
+ options=options,
258
+ config_path=config_path,
259
+ firebase_config_path=firebase_config_path,
260
+ auth_storage=auth_storage,
261
+ session_path=session_path,
262
+ static_dir=static_dir,
263
+ connect_realtime=connect_realtime,
264
+ enable_active_probe=enable_active_probe,
265
+ http_client=http_client,
266
+ auto_initialize=auto_initialize,
267
+ )
268
+
269
+ @classmethod
270
+ def serve_web(
271
+ cls,
272
+ *,
273
+ host: str = "127.0.0.1",
274
+ port: int = 8000,
275
+ log_level: str = "info",
276
+ title: str = "HosteDay Web App",
277
+ config: HostedayConfig | None = None,
278
+ options: Mapping[str, Any] | None = None,
279
+ config_path: str | Path | None = None,
280
+ firebase_config_path: str | Path | None = None,
281
+ auth_storage: HostedayAuthStorage | None = None,
282
+ session_path: str | Path | None = None,
283
+ static_dir: str | Path | None = None,
284
+ connect_realtime: bool = False,
285
+ enable_active_probe: bool = True,
286
+ ) -> None:
287
+ """Creates and immediately serves a ``HostedayWebApp``."""
288
+ web_app = cls.create_web_app(
289
+ title=title,
290
+ config=config,
291
+ options=options,
292
+ config_path=config_path,
293
+ firebase_config_path=firebase_config_path,
294
+ auth_storage=auth_storage,
295
+ session_path=session_path,
296
+ static_dir=static_dir,
297
+ connect_realtime=connect_realtime,
298
+ enable_active_probe=enable_active_probe,
299
+ )
300
+ web_app.serve(host=host, port=port, log_level=log_level)
301
+
302
+ @classmethod
303
+ def run_web(
304
+ cls,
305
+ *,
306
+ host: str = "127.0.0.1",
307
+ port: int = 8000,
308
+ log_level: str = "info",
309
+ title: str = "HosteDay Web App",
310
+ config: HostedayConfig | None = None,
311
+ options: Mapping[str, Any] | None = None,
312
+ config_path: str | Path | None = None,
313
+ firebase_config_path: str | Path | None = None,
314
+ auth_storage: HostedayAuthStorage | None = None,
315
+ session_path: str | Path | None = None,
316
+ static_dir: str | Path | None = None,
317
+ connect_realtime: bool = False,
318
+ enable_active_probe: bool = True,
319
+ ) -> None:
320
+ """Alias for :meth:`serve_web`."""
321
+ cls.serve_web(
322
+ host=host,
323
+ port=port,
324
+ log_level=log_level,
325
+ title=title,
326
+ config=config,
327
+ options=options,
328
+ config_path=config_path,
329
+ firebase_config_path=firebase_config_path,
330
+ auth_storage=auth_storage,
331
+ session_path=session_path,
332
+ static_dir=static_dir,
333
+ connect_realtime=connect_realtime,
334
+ enable_active_probe=enable_active_probe,
335
+ )
336
+
337
+ @classmethod
338
+ async def dispose(cls) -> None:
339
+ """Releases all SDK resources and resets the singleton so ``initialize_app`` may be called again."""
340
+ current_client = cls._client
341
+ cls._client = None
342
+ cls._config = None
343
+ cls._initializing = None
344
+ cls._init_lock = None
345
+ if current_client is not None:
346
+ await current_client.dispose()