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