stinger-python-utils 0.1.2__py3-none-any.whl → 0.1.7__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 +34 -10
- stinger_python_utils-0.1.7.dist-info/METADATA +84 -0
- stinger_python_utils-0.1.7.dist-info/RECORD +7 -0
- stinger_python_utils-0.1.2.dist-info/METADATA +0 -13
- stinger_python_utils-0.1.2.dist-info/RECORD +0 -7
- {stinger_python_utils-0.1.2.dist-info → stinger_python_utils-0.1.7.dist-info}/WHEEL +0 -0
- {stinger_python_utils-0.1.2.dist-info → stinger_python_utils-0.1.7.dist-info}/licenses/LICENSE +0 -0
|
@@ -2,6 +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
|
+
from .return_codes import MethodReturnCode
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class MessageCreator:
|
|
@@ -13,6 +14,7 @@ class MessageCreator:
|
|
|
13
14
|
payload=payload.model_dump_json(by_alias=True).encode("utf-8"),
|
|
14
15
|
qos=1,
|
|
15
16
|
retain=False,
|
|
17
|
+
content_type="application/json",
|
|
16
18
|
)
|
|
17
19
|
|
|
18
20
|
@classmethod
|
|
@@ -25,19 +27,25 @@ class MessageCreator:
|
|
|
25
27
|
qos=1,
|
|
26
28
|
retain=True,
|
|
27
29
|
message_expiry_interval=expiry_seconds,
|
|
30
|
+
content_type="application/json",
|
|
28
31
|
)
|
|
29
32
|
|
|
30
33
|
@classmethod
|
|
31
34
|
def error_response_message(
|
|
32
35
|
cls,
|
|
33
36
|
topic: str,
|
|
34
|
-
return_code: int,
|
|
35
|
-
correlation_id: Union[str, bytes],
|
|
37
|
+
return_code: Union[int, MethodReturnCode],
|
|
38
|
+
correlation_id: Union[str, bytes, None] = None,
|
|
36
39
|
debug_info: Optional[str] = None,
|
|
37
40
|
) -> Message:
|
|
38
41
|
"""
|
|
39
42
|
This could be used for a response to a request, but where there was an error fulfilling the request.
|
|
40
43
|
"""
|
|
44
|
+
rc = (
|
|
45
|
+
return_code.value
|
|
46
|
+
if isinstance(return_code, MethodReturnCode)
|
|
47
|
+
else return_code
|
|
48
|
+
)
|
|
41
49
|
msg_obj = Message(
|
|
42
50
|
topic=topic,
|
|
43
51
|
payload=b"{}",
|
|
@@ -48,7 +56,8 @@ class MessageCreator:
|
|
|
48
56
|
if isinstance(correlation_id, str)
|
|
49
57
|
else correlation_id
|
|
50
58
|
),
|
|
51
|
-
user_properties={"ReturnCode": str(
|
|
59
|
+
user_properties={"ReturnCode": str(rc)},
|
|
60
|
+
content_type="application/json",
|
|
52
61
|
)
|
|
53
62
|
if (
|
|
54
63
|
debug_info is not None and msg_obj.user_properties is not None
|
|
@@ -60,9 +69,9 @@ class MessageCreator:
|
|
|
60
69
|
def response_message(
|
|
61
70
|
cls,
|
|
62
71
|
response_topic: str,
|
|
63
|
-
response_obj: BaseModel
|
|
64
|
-
return_code: int,
|
|
65
|
-
correlation_id: Union[str, bytes],
|
|
72
|
+
response_obj: Union[BaseModel, str, bytes],
|
|
73
|
+
return_code: Union[int, MethodReturnCode],
|
|
74
|
+
correlation_id: Union[str, bytes, None] = None,
|
|
66
75
|
) -> Message:
|
|
67
76
|
"""
|
|
68
77
|
This could be used for a successful response to a request.
|
|
@@ -73,6 +82,11 @@ class MessageCreator:
|
|
|
73
82
|
payload = response_obj.encode("utf-8")
|
|
74
83
|
else:
|
|
75
84
|
payload = response_obj
|
|
85
|
+
rc = (
|
|
86
|
+
return_code.value
|
|
87
|
+
if isinstance(return_code, MethodReturnCode)
|
|
88
|
+
else return_code
|
|
89
|
+
)
|
|
76
90
|
msg_obj = Message(
|
|
77
91
|
topic=response_topic,
|
|
78
92
|
payload=payload,
|
|
@@ -83,7 +97,8 @@ class MessageCreator:
|
|
|
83
97
|
if isinstance(correlation_id, str)
|
|
84
98
|
else correlation_id
|
|
85
99
|
),
|
|
86
|
-
user_properties={"ReturnCode": str(
|
|
100
|
+
user_properties={"ReturnCode": str(rc)},
|
|
101
|
+
content_type="application/json",
|
|
87
102
|
)
|
|
88
103
|
return msg_obj
|
|
89
104
|
|
|
@@ -99,6 +114,7 @@ class MessageCreator:
|
|
|
99
114
|
payload=state_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
100
115
|
qos=1,
|
|
101
116
|
retain=True,
|
|
117
|
+
content_type="application/json",
|
|
102
118
|
)
|
|
103
119
|
if state_version is not None:
|
|
104
120
|
msg_obj.user_properties = {"PropertyVersion": str(state_version)}
|
|
@@ -121,6 +137,7 @@ class MessageCreator:
|
|
|
121
137
|
payload=property_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
122
138
|
qos=1,
|
|
123
139
|
retain=False,
|
|
140
|
+
content_type="application/json",
|
|
124
141
|
response_topic=response_topic,
|
|
125
142
|
correlation_data=(
|
|
126
143
|
correlation_id.encode("utf-8")
|
|
@@ -137,25 +154,31 @@ class MessageCreator:
|
|
|
137
154
|
response_topic: str,
|
|
138
155
|
property_obj: BaseModel,
|
|
139
156
|
version: str,
|
|
140
|
-
return_code: int,
|
|
141
|
-
correlation_id: Union[str, bytes],
|
|
157
|
+
return_code: Union[int, MethodReturnCode],
|
|
158
|
+
correlation_id: Union[str, bytes, None] = None,
|
|
142
159
|
debug_info: Optional[str] = None,
|
|
143
160
|
) -> Message:
|
|
144
161
|
"""
|
|
145
162
|
Creates a message representing a response to a property update request.
|
|
146
163
|
"""
|
|
164
|
+
rc = (
|
|
165
|
+
return_code.value
|
|
166
|
+
if isinstance(return_code, MethodReturnCode)
|
|
167
|
+
else return_code
|
|
168
|
+
)
|
|
147
169
|
msg_obj = Message(
|
|
148
170
|
topic=response_topic,
|
|
149
171
|
payload=property_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
150
172
|
qos=1,
|
|
151
173
|
retain=False,
|
|
174
|
+
content_type="application/json",
|
|
152
175
|
correlation_data=(
|
|
153
176
|
correlation_id.encode("utf-8")
|
|
154
177
|
if isinstance(correlation_id, str)
|
|
155
178
|
else correlation_id
|
|
156
179
|
),
|
|
157
180
|
user_properties={
|
|
158
|
-
"ReturnCode": str(
|
|
181
|
+
"ReturnCode": str(rc),
|
|
159
182
|
"PropertyVersion": str(version),
|
|
160
183
|
},
|
|
161
184
|
)
|
|
@@ -181,6 +204,7 @@ class MessageCreator:
|
|
|
181
204
|
qos=1,
|
|
182
205
|
retain=False,
|
|
183
206
|
response_topic=response_topic,
|
|
207
|
+
content_type="application/json",
|
|
184
208
|
correlation_data=(
|
|
185
209
|
correlation_id.encode("utf-8")
|
|
186
210
|
if isinstance(correlation_id, str)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stinger-python-utils
|
|
3
|
+
Version: 0.1.7
|
|
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=M_SRo0gakRdXdiG54O2wrIaQOUw29Nl8kQLy3MhBRaE,6989
|
|
3
|
+
stinger_python_utils/return_codes.py,sha256=AAwshvHu3SBoctwMX35k3j666J4a7DQ3V8AnR7QBjC8,5114
|
|
4
|
+
stinger_python_utils-0.1.7.dist-info/METADATA,sha256=UDzGKsRLcKE0oxFbflKOGdA8cUskWCTuotCtuXMSq-Y,2769
|
|
5
|
+
stinger_python_utils-0.1.7.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
stinger_python_utils-0.1.7.dist-info/licenses/LICENSE,sha256=_T-8ExmblJbhv-1AxC6XDVEHg1JdJA-126NvRiMqS-I,1070
|
|
7
|
+
stinger_python_utils-0.1.7.dist-info/RECORD,,
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: stinger-python-utils
|
|
3
|
-
Version: 0.1.2
|
|
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=ZR4FOhkmJ5R_X5QS-c-QAIpcN-7htIOiHtO40QUS35Y,6071
|
|
3
|
-
stinger_python_utils/return_codes.py,sha256=AAwshvHu3SBoctwMX35k3j666J4a7DQ3V8AnR7QBjC8,5114
|
|
4
|
-
stinger_python_utils-0.1.2.dist-info/METADATA,sha256=LxgDU_LYLjt9F5FMBkIsiK_XR61DL1tOEVOxI0glX78,364
|
|
5
|
-
stinger_python_utils-0.1.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
-
stinger_python_utils-0.1.2.dist-info/licenses/LICENSE,sha256=_T-8ExmblJbhv-1AxC6XDVEHg1JdJA-126NvRiMqS-I,1070
|
|
7
|
-
stinger_python_utils-0.1.2.dist-info/RECORD,,
|
|
File without changes
|
{stinger_python_utils-0.1.2.dist-info → stinger_python_utils-0.1.7.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|