stinger-python-utils 0.1.3__py3-none-any.whl → 0.1.5__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.
- stinger_python_utils/message_creator.py +11 -8
- stinger_python_utils-0.1.5.dist-info/METADATA +84 -0
- stinger_python_utils-0.1.5.dist-info/RECORD +7 -0
- stinger_python_utils-0.1.3.dist-info/METADATA +0 -13
- stinger_python_utils-0.1.3.dist-info/RECORD +0 -7
- {stinger_python_utils-0.1.3.dist-info → stinger_python_utils-0.1.5.dist-info}/WHEEL +0 -0
- {stinger_python_utils-0.1.3.dist-info → stinger_python_utils-0.1.5.dist-info}/licenses/LICENSE +0 -0
|
@@ -2,7 +2,7 @@ from pyqttier.message import Message
|
|
|
2
2
|
from pydantic import BaseModel
|
|
3
3
|
from typing import Union, Optional
|
|
4
4
|
import uuid
|
|
5
|
-
|
|
5
|
+
from .return_codes import MethodReturnCode
|
|
6
6
|
|
|
7
7
|
class MessageCreator:
|
|
8
8
|
|
|
@@ -31,13 +31,14 @@ class MessageCreator:
|
|
|
31
31
|
def error_response_message(
|
|
32
32
|
cls,
|
|
33
33
|
topic: str,
|
|
34
|
-
return_code: int,
|
|
34
|
+
return_code: Union[int, MethodReturnCode],
|
|
35
35
|
correlation_id: Union[str, bytes, None] = None,
|
|
36
36
|
debug_info: Optional[str] = None,
|
|
37
37
|
) -> Message:
|
|
38
38
|
"""
|
|
39
39
|
This could be used for a response to a request, but where there was an error fulfilling the request.
|
|
40
40
|
"""
|
|
41
|
+
rc = return_code.value if isinstance(return_code, MethodReturnCode) else return_code
|
|
41
42
|
msg_obj = Message(
|
|
42
43
|
topic=topic,
|
|
43
44
|
payload=b"{}",
|
|
@@ -48,7 +49,7 @@ class MessageCreator:
|
|
|
48
49
|
if isinstance(correlation_id, str)
|
|
49
50
|
else correlation_id
|
|
50
51
|
),
|
|
51
|
-
user_properties={"ReturnCode": str(
|
|
52
|
+
user_properties={"ReturnCode": str(rc)},
|
|
52
53
|
)
|
|
53
54
|
if (
|
|
54
55
|
debug_info is not None and msg_obj.user_properties is not None
|
|
@@ -60,8 +61,8 @@ class MessageCreator:
|
|
|
60
61
|
def response_message(
|
|
61
62
|
cls,
|
|
62
63
|
response_topic: str,
|
|
63
|
-
response_obj: BaseModel
|
|
64
|
-
return_code: int,
|
|
64
|
+
response_obj: Union[BaseModel, str, bytes],
|
|
65
|
+
return_code: Union[int, MethodReturnCode],
|
|
65
66
|
correlation_id: Union[str, bytes, None] = None,
|
|
66
67
|
) -> Message:
|
|
67
68
|
"""
|
|
@@ -73,6 +74,7 @@ class MessageCreator:
|
|
|
73
74
|
payload = response_obj.encode("utf-8")
|
|
74
75
|
else:
|
|
75
76
|
payload = response_obj
|
|
77
|
+
rc = return_code.value if isinstance(return_code, MethodReturnCode) else return_code
|
|
76
78
|
msg_obj = Message(
|
|
77
79
|
topic=response_topic,
|
|
78
80
|
payload=payload,
|
|
@@ -83,7 +85,7 @@ class MessageCreator:
|
|
|
83
85
|
if isinstance(correlation_id, str)
|
|
84
86
|
else correlation_id
|
|
85
87
|
),
|
|
86
|
-
user_properties={"ReturnCode": str(
|
|
88
|
+
user_properties={"ReturnCode": str(rc)},
|
|
87
89
|
)
|
|
88
90
|
return msg_obj
|
|
89
91
|
|
|
@@ -138,13 +140,14 @@ class MessageCreator:
|
|
|
138
140
|
response_topic: str,
|
|
139
141
|
property_obj: BaseModel,
|
|
140
142
|
version: str,
|
|
141
|
-
return_code: int,
|
|
143
|
+
return_code: Union[int, MethodReturnCode],
|
|
142
144
|
correlation_id: Union[str, bytes, None] = None,
|
|
143
145
|
debug_info: Optional[str] = None,
|
|
144
146
|
) -> Message:
|
|
145
147
|
"""
|
|
146
148
|
Creates a message representing a response to a property update request.
|
|
147
149
|
"""
|
|
150
|
+
rc = return_code.value if isinstance(return_code, MethodReturnCode) else return_code
|
|
148
151
|
msg_obj = Message(
|
|
149
152
|
topic=response_topic,
|
|
150
153
|
payload=property_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
@@ -156,7 +159,7 @@ class MessageCreator:
|
|
|
156
159
|
else correlation_id
|
|
157
160
|
),
|
|
158
161
|
user_properties={
|
|
159
|
-
"ReturnCode": str(
|
|
162
|
+
"ReturnCode": str(rc),
|
|
160
163
|
"PropertyVersion": str(version),
|
|
161
164
|
},
|
|
162
165
|
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stinger-python-utils
|
|
3
|
+
Version: 0.1.5
|
|
4
|
+
Summary: Common utilities for Stinger Python services.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.7
|
|
8
|
+
Requires-Dist: pydantic>=2.5.3
|
|
9
|
+
Requires-Dist: pyqttier>=0.2.0
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# stinger-python-utils
|
|
13
|
+
|
|
14
|
+
Shared utilities for Stinger Python services, providing convenient message creation for MQTT communication.
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
uv add stinger-python-utils
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## MessageCreator
|
|
25
|
+
|
|
26
|
+
`MessageCreator` is a utility class for creating MQTT messages with standardized properties and payloads.
|
|
27
|
+
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from pydantic import BaseModel
|
|
32
|
+
from stinger_python_utils.message_creator import MessageCreator
|
|
33
|
+
|
|
34
|
+
class MyPayload(BaseModel):
|
|
35
|
+
name: str
|
|
36
|
+
value: int
|
|
37
|
+
|
|
38
|
+
payload = MyPayload(name="test", value=42)
|
|
39
|
+
message = MessageCreator.signal_message("my/topic", payload)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Methods
|
|
43
|
+
|
|
44
|
+
| Method | Purpose | Return Code |
|
|
45
|
+
|--------|---------|-------------|
|
|
46
|
+
| `signal_message(topic, payload)` | Send a signal with one-time delivery | QoS 1, no retain |
|
|
47
|
+
| `status_message(topic, payload, expiry_seconds)` | Send status that expires | QoS 1, retained, with expiry |
|
|
48
|
+
| `error_response_message(topic, return_code, correlation_id, debug_info)` | Error response to a request | QoS 1, user properties: `ReturnCode` |
|
|
49
|
+
| `response_message(topic, payload, return_code, correlation_id)` | Successful response to a request | QoS 1, user properties: `ReturnCode` |
|
|
50
|
+
| `property_state_message(topic, payload, state_version)` | Publish property state | QoS 1, retained, JSON content type |
|
|
51
|
+
| `property_update_request_message(topic, payload, version, response_topic, correlation_id)` | Request property update | QoS 1, user property: `PropertyVersion` |
|
|
52
|
+
| `property_response_message(topic, payload, version, return_code, correlation_id, debug_info)` | Respond to property update | QoS 1, user properties: `ReturnCode`, `PropertyVersion` |
|
|
53
|
+
| `request_message(topic, payload, response_topic, correlation_id)` | Send a request (auto-generates UUID if no correlation_id) | QoS 1, auto correlation ID |
|
|
54
|
+
|
|
55
|
+
### Example: Request/Response Pattern
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from pydantic import BaseModel
|
|
59
|
+
from stinger_python_utils.message_creator import MessageCreator
|
|
60
|
+
|
|
61
|
+
class Request(BaseModel):
|
|
62
|
+
action: str
|
|
63
|
+
|
|
64
|
+
request = Request(action="start")
|
|
65
|
+
msg = MessageCreator.request_message(
|
|
66
|
+
"devices/cmd",
|
|
67
|
+
request,
|
|
68
|
+
response_topic="devices/response"
|
|
69
|
+
)
|
|
70
|
+
# Returns a Message with auto-generated correlation ID
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Example: Error Response
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
msg = MessageCreator.error_response_message(
|
|
77
|
+
"devices/response",
|
|
78
|
+
return_code=500,
|
|
79
|
+
correlation_id="req-123",
|
|
80
|
+
debug_info="Device not found"
|
|
81
|
+
)
|
|
82
|
+
# User properties include: ReturnCode=500, DebugInfo=Device not found
|
|
83
|
+
```
|
|
84
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
stinger_python_utils/__init__.py,sha256=IjHRV0k2DNwvFrEHebmsXiBvmITE8nQUnsR07h9tVkU,7
|
|
2
|
+
stinger_python_utils/message_creator.py,sha256=KHiw7Zk2n8QI96Zv4dT2Isj8ZljwSKaVfyoOyIjlRCA,6529
|
|
3
|
+
stinger_python_utils/return_codes.py,sha256=AAwshvHu3SBoctwMX35k3j666J4a7DQ3V8AnR7QBjC8,5114
|
|
4
|
+
stinger_python_utils-0.1.5.dist-info/METADATA,sha256=6qVtX_zsa7TGr3Ny8myBxm0dhY2OSR_IP6xx5SB5Yog,2769
|
|
5
|
+
stinger_python_utils-0.1.5.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
stinger_python_utils-0.1.5.dist-info/licenses/LICENSE,sha256=_T-8ExmblJbhv-1AxC6XDVEHg1JdJA-126NvRiMqS-I,1070
|
|
7
|
+
stinger_python_utils-0.1.5.dist-info/RECORD,,
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: stinger-python-utils
|
|
3
|
-
Version: 0.1.3
|
|
4
|
-
Summary: Common utilities for Stinger Python services.
|
|
5
|
-
License-Expression: MIT
|
|
6
|
-
License-File: LICENSE
|
|
7
|
-
Requires-Python: >=3.7
|
|
8
|
-
Requires-Dist: pydantic>=2.5.3
|
|
9
|
-
Requires-Dist: pyqttier>=0.2.0
|
|
10
|
-
Description-Content-Type: text/markdown
|
|
11
|
-
|
|
12
|
-
# stinger-python-utils
|
|
13
|
-
Common code needed for stinger python generations
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
stinger_python_utils/__init__.py,sha256=IjHRV0k2DNwvFrEHebmsXiBvmITE8nQUnsR07h9tVkU,7
|
|
2
|
-
stinger_python_utils/message_creator.py,sha256=hTGXbVIuvc_7pzP73N0z2VzvRiNYFKNwNKLbvh4Kw14,6155
|
|
3
|
-
stinger_python_utils/return_codes.py,sha256=AAwshvHu3SBoctwMX35k3j666J4a7DQ3V8AnR7QBjC8,5114
|
|
4
|
-
stinger_python_utils-0.1.3.dist-info/METADATA,sha256=TKdZvY9ZQZ0X3j97eLRj_jpYV9L0eSp8a7tH6WaHbh8,364
|
|
5
|
-
stinger_python_utils-0.1.3.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
-
stinger_python_utils-0.1.3.dist-info/licenses/LICENSE,sha256=_T-8ExmblJbhv-1AxC6XDVEHg1JdJA-126NvRiMqS-I,1070
|
|
7
|
-
stinger_python_utils-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
{stinger_python_utils-0.1.3.dist-info → stinger_python_utils-0.1.5.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|