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,579 @@
1
+ """Composition Root and high-level HosteDay SDK client."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping
6
+ from types import TracebackType
7
+ from typing import Any
8
+
9
+ import httpx
10
+
11
+ from hosteday.auth.hosteday_auth import HostedayAuth
12
+ from hosteday.auth.hosteday_auth_storage import (
13
+ HostedayAuthStorage,
14
+ HostedayAuthStorageKeys,
15
+ MemoryHostedayAuthStorage,
16
+ )
17
+ from hosteday.auth.hosteday_token_provider import (
18
+ HostedayAuthTokenProvider,
19
+ HostedayCombinedTokenProvider,
20
+ HostedayTokenProvider,
21
+ )
22
+ from hosteday.config.hosteday_config import HostedayConfig
23
+ from hosteday.config.hosteday_url_resolver import HostedayStaticPages
24
+ from hosteday.connectivity.hosteday_connectivity import HostedayConnectivity
25
+ from hosteday.exceptions import HostedayException
26
+ from hosteday.features.hosteday_features import HostedayFeatures
27
+ from hosteday.http.hosteday_http_client import HostedayHttpClient
28
+ from hosteday.http.hosteday_response_reader import HostedayPage, HostedayResponseReader
29
+ from hosteday.jobs.hosteday_jobs_client import HostedayJobsClient
30
+ from hosteday.notifications.hosteday_notification_client import (
31
+ HostedayNotificationClient,
32
+ )
33
+ from hosteday.realtime.hosteday_realtime_client import (
34
+ HostedayRealtimeClient,
35
+ WebSocketConnector,
36
+ )
37
+
38
+
39
+ class HostedayClient:
40
+ """High-level client owning all HosteDay sub-services and lifecycle hooks."""
41
+
42
+ def __init__(
43
+ self,
44
+ *,
45
+ config: HostedayConfig,
46
+ token_provider: HostedayTokenProvider | None = None,
47
+ http_client: httpx.AsyncClient | None = None,
48
+ auth_storage: HostedayAuthStorage | None = None,
49
+ features: HostedayFeatures | None = None,
50
+ connectivity: HostedayConnectivity | None = None,
51
+ enable_active_probe: bool = True,
52
+ ws_connector: WebSocketConnector | None = None,
53
+ timeout: float = 30.0,
54
+ ) -> None:
55
+ self.config: HostedayConfig = config
56
+ self.token_provider: HostedayTokenProvider | None = token_provider
57
+ self.auth_storage: HostedayAuthStorage = auth_storage or MemoryHostedayAuthStorage()
58
+ self.features: HostedayFeatures = features or config.features
59
+
60
+ auth_token_provider = HostedayAuthTokenProvider(self.auth_storage)
61
+ self.effective_token_provider: HostedayTokenProvider = HostedayCombinedTokenProvider(
62
+ primary=auth_token_provider,
63
+ fallback=token_provider,
64
+ )
65
+
66
+ self.connectivity: HostedayConnectivity = connectivity or HostedayConnectivity(
67
+ config=config,
68
+ enable_active_probe=enable_active_probe,
69
+ )
70
+
71
+ def _on_network_result(is_online: bool) -> None:
72
+ if is_online:
73
+ self.connectivity.report_success()
74
+ else:
75
+ self.connectivity.report_failure()
76
+
77
+ self.http: HostedayHttpClient = HostedayHttpClient(
78
+ config=config,
79
+ token_provider=self.effective_token_provider,
80
+ client=http_client,
81
+ timeout=timeout,
82
+ on_network_result=_on_network_result,
83
+ )
84
+
85
+ async def _realtime_headers() -> dict[str, str]:
86
+ session_token = (
87
+ self.auth.current_session.access_token.strip()
88
+ if self.auth.current_session is not None
89
+ else ""
90
+ )
91
+ current_token = session_token or (
92
+ await self.effective_token_provider.get_token()
93
+ or await self.auth_storage.read(HostedayAuthStorageKeys.ACCESS_TOKEN)
94
+ or ""
95
+ )
96
+ clean_token = current_token.strip()
97
+ headers: dict[str, str] = {
98
+ "Accept": "application/json",
99
+ **self.config.default_headers,
100
+ }
101
+ api_token = self.config.api_token.strip() if self.config.api_token else ""
102
+ if api_token:
103
+ headers["X-Api-Token"] = api_token
104
+ if self.config.api_token_header:
105
+ headers[self.config.api_token_header] = api_token
106
+ if clean_token:
107
+ headers["Authorization"] = f"Bearer {clean_token}"
108
+ return headers
109
+
110
+ self.realtime: HostedayRealtimeClient = HostedayRealtimeClient(
111
+ config=config,
112
+ token_provider=self.effective_token_provider,
113
+ auth_storage=self.auth_storage,
114
+ headers_callback=_realtime_headers,
115
+ http_client=http_client,
116
+ ws_connector=ws_connector,
117
+ )
118
+
119
+ self.notifications: HostedayNotificationClient = HostedayNotificationClient(
120
+ config=config,
121
+ http=self.http,
122
+ token_provider=self.effective_token_provider,
123
+ )
124
+
125
+ self.jobs: HostedayJobsClient = HostedayJobsClient(
126
+ config=config,
127
+ http=self.http,
128
+ token_provider=self.effective_token_provider,
129
+ )
130
+
131
+ async def _on_session_activated(_user: Any) -> None:
132
+ current_device_token = self.notifications.current_token
133
+ if current_device_token:
134
+ try:
135
+ access_token = await self.auth.get_access_token()
136
+ await self.notifications.sync_device_token(
137
+ token=current_device_token,
138
+ access_token=access_token,
139
+ device_id=self.notifications.current_device_id,
140
+ device_type=self.notifications.current_device_type,
141
+ device_name=self.notifications.current_device_name,
142
+ )
143
+ except Exception:
144
+ pass
145
+ if (
146
+ self.config.has_realtime
147
+ and self.features.is_realtime_enabled
148
+ and not self.realtime.is_connected
149
+ ):
150
+ try:
151
+ await self.realtime.connect()
152
+ except Exception:
153
+ pass
154
+
155
+ async def _on_before_sign_out() -> None:
156
+ current_device_token = self.notifications.current_token
157
+ if current_device_token:
158
+ try:
159
+ access_token = await self.auth.get_access_token()
160
+ await self.notifications.unregister_device_token(
161
+ token=current_device_token,
162
+ device_id=self.notifications.current_device_id,
163
+ action="unlink",
164
+ logout_only=True,
165
+ access_token=access_token,
166
+ )
167
+ except Exception:
168
+ pass
169
+
170
+ self.auth: HostedayAuth = HostedayAuth(
171
+ config=config,
172
+ http=self.http,
173
+ storage=self.auth_storage,
174
+ on_session_cleared=self.realtime.disconnect,
175
+ on_session_activated=_on_session_activated,
176
+ on_before_sign_out=_on_before_sign_out,
177
+ )
178
+
179
+ @property
180
+ def host(self) -> str:
181
+ """Root URL of the project, e.g. ``https://max.hosteday.com``."""
182
+ return self.config.host
183
+
184
+ @property
185
+ def api_base_url(self) -> str:
186
+ """Effective REST base URL, normally ``{host}/api``."""
187
+ return self.config.api_base_url
188
+
189
+ @property
190
+ def static_pages(self) -> HostedayStaticPages:
191
+ """Public static page accessor, e.g. ``client.static_pages['info']``."""
192
+ return HostedayStaticPages(self.config.urls)
193
+
194
+ def storage_url(self, path: str) -> str:
195
+ """Resolves a relative public file path under ``/storage``; preserves external HTTP(S) URLs."""
196
+ return self.config.urls.storage(path)
197
+
198
+ async def initialize(self) -> None:
199
+ """Restores persisted auth session and starts periodic connectivity probing."""
200
+ await self.auth.initialize()
201
+ self.connectivity.start()
202
+
203
+ async def __aenter__(self) -> HostedayClient:
204
+ await self.initialize()
205
+ return self
206
+
207
+ async def __aexit__(
208
+ self,
209
+ exc_type: type[BaseException] | None,
210
+ exc_val: BaseException | None,
211
+ exc_tb: TracebackType | None,
212
+ ) -> None:
213
+ await self.dispose()
214
+
215
+ # ------------------------------------------------------------------
216
+ # Standard High-Level CRUD Interface
217
+ # ------------------------------------------------------------------
218
+
219
+ async def index(
220
+ self,
221
+ path: str,
222
+ *,
223
+ page: int | None = None,
224
+ per_page: int | None = None,
225
+ search: str | None = None,
226
+ filters: Mapping[str, Any] | None = None,
227
+ relation_field: str | None = None,
228
+ relation_value: Any = None,
229
+ query_parameters: Mapping[str, Any] | None = None,
230
+ with_auth: bool = False,
231
+ headers: Mapping[str, str] | None = None,
232
+ timeout: float | None = None,
233
+ ) -> HostedayPage[dict[str, Any]]:
234
+ """Fetches one paginated page, unwrapping the HosteDay pagination or flat-list envelope."""
235
+ if page is not None and (type(page) is not int or page < 1):
236
+ raise ValueError("page must be a positive integer.")
237
+ if per_page is not None and (type(per_page) is not int or per_page < 1):
238
+ raise ValueError("per_page must be a positive integer.")
239
+
240
+ merged_query: dict[str, Any] = dict(query_parameters) if query_parameters else {}
241
+ if page is not None:
242
+ merged_query["page"] = page
243
+ if per_page is not None:
244
+ merged_query["per_page"] = per_page
245
+
246
+ response = await self.get(
247
+ path,
248
+ search=search,
249
+ filters=filters,
250
+ relation_field=relation_field,
251
+ relation_value=relation_value,
252
+ query_parameters=merged_query or None,
253
+ with_auth=with_auth,
254
+ headers=headers,
255
+ timeout=timeout,
256
+ )
257
+ return HostedayResponseReader.index(response)
258
+
259
+ async def show(
260
+ self,
261
+ path: str,
262
+ *,
263
+ id: Any,
264
+ search: str | None = None,
265
+ relation_field: str | None = None,
266
+ relation_value: Any = None,
267
+ query_parameters: Mapping[str, Any] | None = None,
268
+ with_auth: bool = False,
269
+ headers: Mapping[str, str] | None = None,
270
+ timeout: float | None = None,
271
+ ) -> dict[str, Any]:
272
+ """Fetches a single record by ``id``, unwrapping the HosteDay resource envelope."""
273
+ response = await self.get(
274
+ path,
275
+ id=id,
276
+ search=search,
277
+ relation_field=relation_field,
278
+ relation_value=relation_value,
279
+ query_parameters=query_parameters,
280
+ with_auth=with_auth,
281
+ headers=headers,
282
+ timeout=timeout,
283
+ )
284
+ return HostedayResponseReader.show(response)
285
+
286
+ async def create(
287
+ self,
288
+ path: str,
289
+ *,
290
+ body: Mapping[str, Any],
291
+ query_parameters: Mapping[str, Any] | None = None,
292
+ with_auth: bool = True,
293
+ headers: Mapping[str, str] | None = None,
294
+ timeout: float | None = None,
295
+ ) -> dict[str, Any]:
296
+ """Creates a new resource at ``path`` via authenticated ``POST`` and unwraps the record."""
297
+ response = await self.post(
298
+ path,
299
+ body=body,
300
+ query_parameters=query_parameters,
301
+ with_auth=with_auth,
302
+ headers=headers,
303
+ timeout=timeout,
304
+ )
305
+ return HostedayResponseReader.unwrap_record(response)
306
+
307
+ async def update(
308
+ self,
309
+ path: str,
310
+ *,
311
+ id: Any,
312
+ body: Mapping[str, Any],
313
+ relation_field: str | None = None,
314
+ relation_value: Any = None,
315
+ query_parameters: Mapping[str, Any] | None = None,
316
+ with_auth: bool = True,
317
+ headers: Mapping[str, str] | None = None,
318
+ timeout: float | None = None,
319
+ partial: bool = True,
320
+ ) -> dict[str, Any]:
321
+ """Updates a resource by ``id`` (auto-falling back from ``PATCH`` to ``PUT`` on 404/405) and unwraps the record."""
322
+ if partial:
323
+ try:
324
+ response = await self.patch(
325
+ path,
326
+ id=id,
327
+ body=body,
328
+ relation_field=relation_field,
329
+ relation_value=relation_value,
330
+ query_parameters=query_parameters,
331
+ with_auth=with_auth,
332
+ headers=headers,
333
+ timeout=timeout,
334
+ )
335
+ return HostedayResponseReader.unwrap_record(response)
336
+ except HostedayException as exc:
337
+ if exc.status_code not in {404, 405}:
338
+ raise
339
+
340
+ response = await self.put(
341
+ path,
342
+ id=id,
343
+ body=body,
344
+ relation_field=relation_field,
345
+ relation_value=relation_value,
346
+ query_parameters=query_parameters,
347
+ with_auth=with_auth,
348
+ headers=headers,
349
+ timeout=timeout,
350
+ )
351
+ return HostedayResponseReader.unwrap_record(response)
352
+
353
+ async def delete(
354
+ self,
355
+ path: str,
356
+ *,
357
+ id: Any = None,
358
+ body: Mapping[str, Any] | None = None,
359
+ relation_field: str | None = None,
360
+ relation_value: Any = None,
361
+ query_parameters: Mapping[str, Any] | None = None,
362
+ with_auth: bool = True,
363
+ headers: Mapping[str, str] | None = None,
364
+ timeout: float | None = None,
365
+ ) -> dict[str, Any]:
366
+ """Deletes a resource at ``path`` (with optional ``id``) and returns a normalized result."""
367
+ response = await self.http.delete(
368
+ path,
369
+ id=id,
370
+ body=body,
371
+ relation_field=relation_field,
372
+ relation_value=relation_value,
373
+ query_parameters=query_parameters,
374
+ with_auth=with_auth,
375
+ headers=headers,
376
+ timeout=timeout,
377
+ )
378
+ return HostedayResponseReader.unwrap_delete(response)
379
+
380
+ # ------------------------------------------------------------------
381
+ # Low-Level HTTP Methods
382
+ # ------------------------------------------------------------------
383
+
384
+ async def request(
385
+ self,
386
+ *,
387
+ method: str,
388
+ path: str,
389
+ id: Any = None,
390
+ body: Mapping[str, Any] | None = None,
391
+ query_parameters: Mapping[str, Any] | None = None,
392
+ with_auth: bool = False,
393
+ headers: Mapping[str, str] | None = None,
394
+ timeout: float | None = None,
395
+ ) -> dict[str, Any]:
396
+ return await self.http.request(
397
+ method=method,
398
+ path=path,
399
+ id=id,
400
+ body=body,
401
+ query_parameters=query_parameters,
402
+ with_auth=with_auth,
403
+ headers=headers,
404
+ timeout=timeout,
405
+ )
406
+
407
+ async def get(
408
+ self,
409
+ path: str,
410
+ *,
411
+ id: Any = None,
412
+ search: str | None = None,
413
+ filters: Mapping[str, Any] | None = None,
414
+ relation_field: str | None = None,
415
+ relation_value: Any = None,
416
+ query_parameters: Mapping[str, Any] | None = None,
417
+ with_auth: bool = False,
418
+ headers: Mapping[str, str] | None = None,
419
+ timeout: float | None = None,
420
+ ) -> dict[str, Any]:
421
+ return await self.http.get(
422
+ path,
423
+ id=id,
424
+ search=search,
425
+ filters=filters,
426
+ relation_field=relation_field,
427
+ relation_value=relation_value,
428
+ query_parameters=query_parameters,
429
+ with_auth=with_auth,
430
+ headers=headers,
431
+ timeout=timeout,
432
+ )
433
+
434
+ async def post(
435
+ self,
436
+ path: str,
437
+ *,
438
+ body: Mapping[str, Any] | None = None,
439
+ query_parameters: Mapping[str, Any] | None = None,
440
+ with_auth: bool = True,
441
+ headers: Mapping[str, str] | None = None,
442
+ timeout: float | None = None,
443
+ ) -> dict[str, Any]:
444
+ return await self.http.post(
445
+ path,
446
+ body=body,
447
+ query_parameters=query_parameters,
448
+ with_auth=with_auth,
449
+ headers=headers,
450
+ timeout=timeout,
451
+ )
452
+
453
+ async def put(
454
+ self,
455
+ path: str,
456
+ *,
457
+ id: Any = None,
458
+ body: Mapping[str, Any] | None = None,
459
+ relation_field: str | None = None,
460
+ relation_value: Any = None,
461
+ query_parameters: Mapping[str, Any] | None = None,
462
+ with_auth: bool = True,
463
+ headers: Mapping[str, str] | None = None,
464
+ timeout: float | None = None,
465
+ ) -> dict[str, Any]:
466
+ return await self.http.put(
467
+ path,
468
+ id=id,
469
+ body=body,
470
+ relation_field=relation_field,
471
+ relation_value=relation_value,
472
+ query_parameters=query_parameters,
473
+ with_auth=with_auth,
474
+ headers=headers,
475
+ timeout=timeout,
476
+ )
477
+
478
+ async def patch(
479
+ self,
480
+ path: str,
481
+ *,
482
+ id: Any = None,
483
+ body: Mapping[str, Any] | None = None,
484
+ relation_field: str | None = None,
485
+ relation_value: Any = None,
486
+ query_parameters: Mapping[str, Any] | None = None,
487
+ with_auth: bool = True,
488
+ headers: Mapping[str, str] | None = None,
489
+ timeout: float | None = None,
490
+ ) -> dict[str, Any]:
491
+ return await self.http.patch(
492
+ path,
493
+ id=id,
494
+ body=body,
495
+ relation_field=relation_field,
496
+ relation_value=relation_value,
497
+ query_parameters=query_parameters,
498
+ with_auth=with_auth,
499
+ headers=headers,
500
+ timeout=timeout,
501
+ )
502
+
503
+ # ------------------------------------------------------------------
504
+ # Realtime REST Event Publishing Helpers
505
+ # ------------------------------------------------------------------
506
+
507
+ async def publish_public_event(
508
+ self,
509
+ *,
510
+ channel: str,
511
+ event: str,
512
+ payload: Mapping[str, Any],
513
+ ) -> dict[str, Any]:
514
+ payload_dict = dict(payload)
515
+ return await self.post(
516
+ self.config.public_events_path,
517
+ with_auth=False,
518
+ body={
519
+ "channel": channel.strip(),
520
+ "event": event,
521
+ "data": payload_dict,
522
+ "payload": payload_dict,
523
+ },
524
+ )
525
+
526
+ async def publish_private_event(
527
+ self,
528
+ *,
529
+ channel: str,
530
+ event: str,
531
+ payload: Mapping[str, Any],
532
+ ) -> dict[str, Any]:
533
+ cleaned = channel.strip()
534
+ normalized = cleaned if cleaned.startswith("private-") else f"private-{cleaned}"
535
+ payload_dict = dict(payload)
536
+ return await self.post(
537
+ self.config.private_events_path,
538
+ with_auth=True,
539
+ body={
540
+ "channel": normalized,
541
+ "event": event,
542
+ "data": payload_dict,
543
+ "payload": payload_dict,
544
+ },
545
+ )
546
+
547
+ async def publish_presence_event(
548
+ self,
549
+ *,
550
+ channel: str,
551
+ event: str,
552
+ payload: Mapping[str, Any],
553
+ ) -> dict[str, Any]:
554
+ cleaned = channel.strip()
555
+ normalized = cleaned if cleaned.startswith("presence-") else f"presence-{cleaned}"
556
+ payload_dict = dict(payload)
557
+ return await self.post(
558
+ self.config.presence_events_path,
559
+ with_auth=True,
560
+ body={
561
+ "channel": normalized,
562
+ "event": event,
563
+ "data": payload_dict,
564
+ "payload": payload_dict,
565
+ },
566
+ )
567
+
568
+ # ------------------------------------------------------------------
569
+ # Deterministic Disposal Sequence
570
+ # ------------------------------------------------------------------
571
+
572
+ async def dispose(self) -> None:
573
+ """Releases resources in strict order: connectivity -> notifications -> jobs -> realtime -> auth -> http."""
574
+ await self.connectivity.dispose()
575
+ await self.notifications.dispose()
576
+ await self.jobs.dispose()
577
+ await self.realtime.disconnect()
578
+ await self.auth.dispose()
579
+ await self.http.close()
@@ -0,0 +1,18 @@
1
+ """HTTP transport, query builder, and strict response envelope reader."""
2
+
3
+ from hosteday.http.hosteday_http_client import HostedayHttpClient
4
+ from hosteday.http.hosteday_response_reader import HostedayPage, HostedayResponseReader
5
+ from hosteday.http.query_builder import (
6
+ MAX_FILTER_VALUES,
7
+ MAX_FILTERS,
8
+ QueryBuilder,
9
+ )
10
+
11
+ __all__ = [
12
+ "HostedayHttpClient",
13
+ "HostedayPage",
14
+ "HostedayResponseReader",
15
+ "MAX_FILTERS",
16
+ "MAX_FILTER_VALUES",
17
+ "QueryBuilder",
18
+ ]