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,204 @@
|
|
|
1
|
+
"""Laravel-compatible query parameter builder with strict filter validation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections.abc import Iterable, Mapping
|
|
7
|
+
from typing import Any
|
|
8
|
+
from urllib.parse import parse_qsl, quote, urlsplit, urlunsplit
|
|
9
|
+
|
|
10
|
+
from hosteday.config.hosteday_option_keys import HostedayOptionKeys
|
|
11
|
+
|
|
12
|
+
FILTERS_QUERY_KEY = "filters"
|
|
13
|
+
MAX_FILTERS = 20
|
|
14
|
+
MAX_FILTER_VALUES = 100
|
|
15
|
+
_FILTER_FIELD_PATTERN = re.compile(r"^[a-zA-Z0-9_]+$")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class QueryBuilder:
|
|
19
|
+
"""Builds validated HosteDay resource URLs and Laravel bracket-syntax query parameters."""
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def build_resource_query_parameters(
|
|
23
|
+
cls,
|
|
24
|
+
*,
|
|
25
|
+
query_parameters: Mapping[str, Any] | None = None,
|
|
26
|
+
search: str | None = None,
|
|
27
|
+
filters: Mapping[str, Any] | None = None,
|
|
28
|
+
relation_field: str | None = None,
|
|
29
|
+
relation_value: Any = None,
|
|
30
|
+
) -> dict[str, Any] | None:
|
|
31
|
+
clean_search = search.strip() if search is not None else None
|
|
32
|
+
clean_relation_field = relation_field.strip() if relation_field is not None else None
|
|
33
|
+
|
|
34
|
+
has_relation_field = bool(clean_relation_field)
|
|
35
|
+
has_relation_value = relation_value is not None and (
|
|
36
|
+
not isinstance(relation_value, str) or bool(relation_value.strip())
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
if has_relation_field != has_relation_value:
|
|
40
|
+
raise ValueError("relation_field and relation_value must be provided together.")
|
|
41
|
+
|
|
42
|
+
result: dict[str, Any] = dict(query_parameters) if query_parameters else {}
|
|
43
|
+
|
|
44
|
+
if clean_search:
|
|
45
|
+
result[HostedayOptionKeys.SEARCH] = clean_search
|
|
46
|
+
|
|
47
|
+
normalized_filters = cls.normalize_filters(filters)
|
|
48
|
+
if normalized_filters is not None:
|
|
49
|
+
result[FILTERS_QUERY_KEY] = normalized_filters
|
|
50
|
+
|
|
51
|
+
if has_relation_field and clean_relation_field is not None:
|
|
52
|
+
result[HostedayOptionKeys.RELATION_FIELD] = clean_relation_field
|
|
53
|
+
result[HostedayOptionKeys.RELATION_VALUE] = (
|
|
54
|
+
relation_value.strip() if isinstance(relation_value, str) else relation_value
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
return result or None
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def normalize_filters(cls, filters: Mapping[str, Any] | None) -> dict[str, Any] | None:
|
|
61
|
+
if not filters:
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
if len(filters) > MAX_FILTERS:
|
|
65
|
+
raise ValueError(f"A maximum of {MAX_FILTERS} filters is allowed.")
|
|
66
|
+
|
|
67
|
+
normalized: dict[str, Any] = {}
|
|
68
|
+
for raw_field, raw_value in filters.items():
|
|
69
|
+
field = str(raw_field).strip()
|
|
70
|
+
if not field or not _FILTER_FIELD_PATTERN.match(field):
|
|
71
|
+
raise ValueError(
|
|
72
|
+
"Filter field names may contain letters, numbers, and underscores only."
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
value = cls._normalize_filter_value(field, raw_value)
|
|
76
|
+
if value is not None:
|
|
77
|
+
normalized[field] = value
|
|
78
|
+
|
|
79
|
+
return normalized or None
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def _normalize_filter_value(cls, field: str, value: Any) -> Any:
|
|
83
|
+
if value is None:
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
if isinstance(value, (list, tuple, set, frozenset)):
|
|
87
|
+
if len(value) > MAX_FILTER_VALUES:
|
|
88
|
+
raise ValueError(
|
|
89
|
+
f"A maximum of {MAX_FILTER_VALUES} values is allowed per filter ({field})."
|
|
90
|
+
)
|
|
91
|
+
values: list[Any] = []
|
|
92
|
+
for item in value:
|
|
93
|
+
normalized_item = cls._normalize_filter_scalar(field, item)
|
|
94
|
+
if normalized_item is not None and not any(
|
|
95
|
+
type(existing) is type(normalized_item) and existing == normalized_item
|
|
96
|
+
for existing in values
|
|
97
|
+
):
|
|
98
|
+
values.append(normalized_item)
|
|
99
|
+
return values or None
|
|
100
|
+
|
|
101
|
+
return cls._normalize_filter_scalar(field, value)
|
|
102
|
+
|
|
103
|
+
@staticmethod
|
|
104
|
+
def _normalize_filter_scalar(field: str, value: Any) -> str | int | float | bool | None:
|
|
105
|
+
if value is None:
|
|
106
|
+
return None
|
|
107
|
+
|
|
108
|
+
if isinstance(value, str):
|
|
109
|
+
clean = value.strip()
|
|
110
|
+
return clean if clean else None
|
|
111
|
+
|
|
112
|
+
if isinstance(value, (bool, int, float)):
|
|
113
|
+
return value
|
|
114
|
+
|
|
115
|
+
raise ValueError(
|
|
116
|
+
f"Filter values for '{field}' must be strings, numbers, booleans, or lists of them."
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
@classmethod
|
|
120
|
+
def append_id_to_url(cls, base_url: str, resource_id: Any) -> str:
|
|
121
|
+
"""Appends ``resource_id`` as a single percent-encoded final path segment."""
|
|
122
|
+
clean_id = str(resource_id).strip()
|
|
123
|
+
if not clean_id:
|
|
124
|
+
raise ValueError("The resource ID cannot be empty.")
|
|
125
|
+
|
|
126
|
+
parsed = urlsplit(base_url)
|
|
127
|
+
trimmed_path = parsed.path.rstrip("/")
|
|
128
|
+
encoded_id = quote(clean_id, safe="")
|
|
129
|
+
new_path = f"{trimmed_path}/{encoded_id}"
|
|
130
|
+
return urlunsplit((parsed.scheme, parsed.netloc, new_path, parsed.query, ""))
|
|
131
|
+
|
|
132
|
+
@classmethod
|
|
133
|
+
def flatten_query_parameters(
|
|
134
|
+
cls,
|
|
135
|
+
url_with_existing_query: str,
|
|
136
|
+
query_parameters: Mapping[str, Any] | None,
|
|
137
|
+
) -> tuple[str, list[tuple[str, str]]]:
|
|
138
|
+
"""Merges existing URL query parameters with Laravel bracket-encoded parameters."""
|
|
139
|
+
parsed = urlsplit(url_with_existing_query)
|
|
140
|
+
base_without_query = urlunsplit((parsed.scheme, parsed.netloc, parsed.path, "", ""))
|
|
141
|
+
|
|
142
|
+
existing_pairs = parse_qsl(parsed.query, keep_blank_values=False)
|
|
143
|
+
if not query_parameters:
|
|
144
|
+
return base_without_query, existing_pairs
|
|
145
|
+
|
|
146
|
+
new_pairs: list[tuple[str, str]] = []
|
|
147
|
+
overridden_keys: set[str] = set()
|
|
148
|
+
|
|
149
|
+
for key, value in query_parameters.items():
|
|
150
|
+
pairs_for_key: list[tuple[str, str]] = []
|
|
151
|
+
cls._append_query_param(pairs_for_key, str(key), value)
|
|
152
|
+
for k, _ in pairs_for_key:
|
|
153
|
+
overridden_keys.add(k)
|
|
154
|
+
new_pairs.extend(pairs_for_key)
|
|
155
|
+
|
|
156
|
+
merged: list[tuple[str, str]] = [
|
|
157
|
+
(k, v) for (k, v) in existing_pairs if k not in overridden_keys
|
|
158
|
+
]
|
|
159
|
+
merged.extend(new_pairs)
|
|
160
|
+
return base_without_query, merged
|
|
161
|
+
|
|
162
|
+
@classmethod
|
|
163
|
+
def _append_query_param(
|
|
164
|
+
cls,
|
|
165
|
+
result: list[tuple[str, str]],
|
|
166
|
+
key: str,
|
|
167
|
+
value: Any,
|
|
168
|
+
) -> None:
|
|
169
|
+
if value is None:
|
|
170
|
+
return
|
|
171
|
+
|
|
172
|
+
clean_key = key.strip()
|
|
173
|
+
if not clean_key:
|
|
174
|
+
raise ValueError("Query parameter names cannot be empty.")
|
|
175
|
+
|
|
176
|
+
if isinstance(value, Mapping):
|
|
177
|
+
for nested_key_raw, nested_value in value.items():
|
|
178
|
+
nested_key = str(nested_key_raw).strip()
|
|
179
|
+
if not nested_key:
|
|
180
|
+
raise ValueError("Nested query parameter names cannot be empty.")
|
|
181
|
+
cls._append_query_param(result, f"{clean_key}[{nested_key}]", nested_value)
|
|
182
|
+
return
|
|
183
|
+
|
|
184
|
+
if isinstance(value, Iterable) and not isinstance(value, (str, bytes, bytearray)):
|
|
185
|
+
items: list[str] = []
|
|
186
|
+
for item in value:
|
|
187
|
+
if item is None:
|
|
188
|
+
continue
|
|
189
|
+
if isinstance(item, Mapping) or (
|
|
190
|
+
isinstance(item, Iterable) and not isinstance(item, (str, bytes, bytearray))
|
|
191
|
+
):
|
|
192
|
+
raise ValueError("Query parameter lists may contain scalar values only.")
|
|
193
|
+
items.append(cls._stringify_value(item))
|
|
194
|
+
for item_str in items:
|
|
195
|
+
result.append((f"{clean_key}[]", item_str))
|
|
196
|
+
return
|
|
197
|
+
|
|
198
|
+
result.append((clean_key, cls._stringify_value(value)))
|
|
199
|
+
|
|
200
|
+
@staticmethod
|
|
201
|
+
def _stringify_value(value: Any) -> str:
|
|
202
|
+
if isinstance(value, bool):
|
|
203
|
+
return "true" if value else "false"
|
|
204
|
+
return str(value)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Typed + Mapping-compatible response model for HosteDay background jobs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator, Mapping
|
|
6
|
+
from types import MappingProxyType
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class HostedayJobResponse(Mapping[str, Any]):
|
|
11
|
+
"""Represents a background job response with read-only Mapping compatibility."""
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
*,
|
|
16
|
+
raw: Mapping[str, Any],
|
|
17
|
+
job_id: str | None = None,
|
|
18
|
+
status: str | None = None,
|
|
19
|
+
success: bool = True,
|
|
20
|
+
message: str | None = None,
|
|
21
|
+
payload: Mapping[str, Any] | None = None,
|
|
22
|
+
data: Any = None,
|
|
23
|
+
) -> None:
|
|
24
|
+
self._raw: Mapping[str, Any] = MappingProxyType(dict(raw))
|
|
25
|
+
self.job_id: str | None = job_id
|
|
26
|
+
self.status: str | None = status
|
|
27
|
+
self.success: bool = success
|
|
28
|
+
self.message: str | None = message
|
|
29
|
+
self.payload: dict[str, Any] | None = dict(payload) if payload is not None else None
|
|
30
|
+
self.data: Any = data
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def from_json(cls, payload: Mapping[str, Any]) -> HostedayJobResponse:
|
|
34
|
+
raw_status = payload.get("status")
|
|
35
|
+
status_str = str(raw_status) if raw_status is not None else None
|
|
36
|
+
is_success = payload.get("success") is True or (
|
|
37
|
+
status_str is not None and status_str != "failed"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
raw_payload = payload.get("payload")
|
|
41
|
+
payload_dict = dict(raw_payload) if isinstance(raw_payload, Mapping) else None
|
|
42
|
+
|
|
43
|
+
raw_id = payload.get("job_id")
|
|
44
|
+
if raw_id is None:
|
|
45
|
+
raw_id = payload.get("id")
|
|
46
|
+
if raw_id is None:
|
|
47
|
+
raw_id = payload.get("jobId")
|
|
48
|
+
|
|
49
|
+
raw_message = payload.get("message")
|
|
50
|
+
return cls(
|
|
51
|
+
raw=payload,
|
|
52
|
+
job_id=str(raw_id) if raw_id is not None else None,
|
|
53
|
+
status=status_str,
|
|
54
|
+
success=is_success,
|
|
55
|
+
message=str(raw_message) if raw_message is not None else None,
|
|
56
|
+
payload=payload_dict,
|
|
57
|
+
data=payload.get("data"),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def __getitem__(self, key: str) -> Any:
|
|
61
|
+
return self._raw[key]
|
|
62
|
+
|
|
63
|
+
def __iter__(self) -> Iterator[str]:
|
|
64
|
+
return iter(self._raw)
|
|
65
|
+
|
|
66
|
+
def __len__(self) -> int:
|
|
67
|
+
return len(self._raw)
|
|
68
|
+
|
|
69
|
+
def to_json(self) -> dict[str, Any]:
|
|
70
|
+
return dict(self._raw)
|
|
71
|
+
|
|
72
|
+
def __repr__(self) -> str:
|
|
73
|
+
return (
|
|
74
|
+
f"HostedayJobResponse(job_id={self.job_id!r}, status={self.status!r}, "
|
|
75
|
+
f"success={self.success!r}, message={self.message!r})"
|
|
76
|
+
)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Smart background jobs client for HosteDay."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from hosteday.auth.hosteday_token_provider import HostedayTokenProvider
|
|
9
|
+
from hosteday.config.hosteday_config import HostedayConfig
|
|
10
|
+
from hosteday.exceptions import HostedayException
|
|
11
|
+
from hosteday.http.hosteday_http_client import HostedayHttpClient
|
|
12
|
+
from hosteday.jobs.hosteday_job_response import HostedayJobResponse
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class HostedayJobsClient:
|
|
16
|
+
"""Manages HosteDay background jobs: dispatch, status tracking, and listing."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
*,
|
|
21
|
+
config: HostedayConfig,
|
|
22
|
+
http: HostedayHttpClient,
|
|
23
|
+
token_provider: HostedayTokenProvider | None = None,
|
|
24
|
+
) -> None:
|
|
25
|
+
self.config = config
|
|
26
|
+
self.http = http
|
|
27
|
+
self.token_provider = token_provider
|
|
28
|
+
self._disposed: bool = False
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def is_enabled(self) -> bool:
|
|
32
|
+
return self.config.jobs_enabled
|
|
33
|
+
|
|
34
|
+
async def dispatch(
|
|
35
|
+
self,
|
|
36
|
+
*,
|
|
37
|
+
name: str,
|
|
38
|
+
payload: Mapping[str, Any] | None = None,
|
|
39
|
+
delay_seconds: int | None = None,
|
|
40
|
+
with_auth: bool = True,
|
|
41
|
+
) -> HostedayJobResponse:
|
|
42
|
+
"""Dispatches a background job to the HosteDay queue."""
|
|
43
|
+
self._ensure_not_disposed()
|
|
44
|
+
clean_name = name.strip()
|
|
45
|
+
if not clean_name:
|
|
46
|
+
raise HostedayException("A valid job name is required.")
|
|
47
|
+
|
|
48
|
+
body: dict[str, Any] = {"job": clean_name}
|
|
49
|
+
if payload is not None:
|
|
50
|
+
body["payload"] = dict(payload)
|
|
51
|
+
if delay_seconds is not None:
|
|
52
|
+
body["delay_seconds"] = delay_seconds
|
|
53
|
+
|
|
54
|
+
response = await self.http.post(
|
|
55
|
+
self.config.jobs_path,
|
|
56
|
+
body=body,
|
|
57
|
+
with_auth=with_auth,
|
|
58
|
+
)
|
|
59
|
+
return HostedayJobResponse.from_json(response)
|
|
60
|
+
|
|
61
|
+
async def get_status(
|
|
62
|
+
self,
|
|
63
|
+
job_id: str,
|
|
64
|
+
*,
|
|
65
|
+
with_auth: bool = True,
|
|
66
|
+
) -> HostedayJobResponse:
|
|
67
|
+
"""Retrieves execution status and details of a queued job."""
|
|
68
|
+
self._ensure_not_disposed()
|
|
69
|
+
clean_id = job_id.strip()
|
|
70
|
+
if not clean_id:
|
|
71
|
+
raise HostedayException("A valid job ID is required.")
|
|
72
|
+
|
|
73
|
+
response = await self.http.get(
|
|
74
|
+
self.config.jobs_path,
|
|
75
|
+
id=clean_id,
|
|
76
|
+
with_auth=with_auth,
|
|
77
|
+
)
|
|
78
|
+
return HostedayJobResponse.from_json(response)
|
|
79
|
+
|
|
80
|
+
async def list_jobs(
|
|
81
|
+
self,
|
|
82
|
+
*,
|
|
83
|
+
page: int | None = None,
|
|
84
|
+
per_page: int | None = None,
|
|
85
|
+
status: str | None = None,
|
|
86
|
+
with_auth: bool = True,
|
|
87
|
+
) -> dict[str, Any]:
|
|
88
|
+
"""Lists scheduled, running, or completed background jobs."""
|
|
89
|
+
self._ensure_not_disposed()
|
|
90
|
+
query_params: dict[str, Any] = {}
|
|
91
|
+
if page is not None:
|
|
92
|
+
query_params["page"] = page
|
|
93
|
+
if per_page is not None:
|
|
94
|
+
query_params["per_page"] = per_page
|
|
95
|
+
if status and status.strip():
|
|
96
|
+
query_params["status"] = status.strip()
|
|
97
|
+
|
|
98
|
+
return await self.http.get(
|
|
99
|
+
self.config.jobs_path,
|
|
100
|
+
query_parameters=query_params or None,
|
|
101
|
+
with_auth=with_auth,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
async def dispose(self) -> None:
|
|
105
|
+
self._disposed = True
|
|
106
|
+
|
|
107
|
+
def _ensure_not_disposed(self) -> None:
|
|
108
|
+
if self._disposed:
|
|
109
|
+
raise HostedayException("HostedayJobsClient is disposed.")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Push notifications, device token lifecycle, and topics."""
|
|
2
|
+
|
|
3
|
+
from hosteday.notifications.hosteday_notification_client import (
|
|
4
|
+
HostedayNotificationClient,
|
|
5
|
+
)
|
|
6
|
+
from hosteday.notifications.hosteday_notification_response import (
|
|
7
|
+
HostedayNotificationResponse,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"HostedayNotificationClient",
|
|
12
|
+
"HostedayNotificationResponse",
|
|
13
|
+
]
|