stinger-python-utils 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.
- stinger_python_utils/__init__.py +1 -0
- stinger_python_utils/message_creator +187 -0
- stinger_python_utils/return_codes.py +151 -0
- stinger_python_utils-0.1.0.dist-info/METADATA +13 -0
- stinger_python_utils-0.1.0.dist-info/RECORD +7 -0
- stinger_python_utils-0.1.0.dist-info/WHEEL +4 -0
- stinger_python_utils-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
""""""
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
|
|
2
|
+
from pyqttier.message import Message
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from typing import Union, Optional
|
|
5
|
+
import uuid
|
|
6
|
+
|
|
7
|
+
class MessageCreator:
|
|
8
|
+
|
|
9
|
+
@classmethod
|
|
10
|
+
def signal_message(
|
|
11
|
+
cls, topic: str, payload: BaseModel
|
|
12
|
+
) -> Message:
|
|
13
|
+
return Message(
|
|
14
|
+
topic=topic,
|
|
15
|
+
payload=payload.model_dump_json(by_alias=True).encode("utf-8"),
|
|
16
|
+
qos=1,
|
|
17
|
+
retain=False,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def status_message(
|
|
22
|
+
cls, topic, status_message: BaseModel, expiry_seconds: int
|
|
23
|
+
) -> Message:
|
|
24
|
+
return Message(
|
|
25
|
+
topic=topic,
|
|
26
|
+
payload=status_message.model_dump_json(by_alias=True).encode("utf-8"),
|
|
27
|
+
qos=1,
|
|
28
|
+
retain=True,
|
|
29
|
+
message_expiry_interval=expiry_seconds,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def error_response_message(
|
|
34
|
+
cls,
|
|
35
|
+
topic: str,
|
|
36
|
+
return_code: int,
|
|
37
|
+
correlation_id: Union[str, bytes],
|
|
38
|
+
debug_info: Optional[str] = None,
|
|
39
|
+
) -> Message:
|
|
40
|
+
"""
|
|
41
|
+
This could be used for a response to a request, but where there was an error fulfilling the request.
|
|
42
|
+
"""
|
|
43
|
+
msg_obj = Message(
|
|
44
|
+
topic=topic,
|
|
45
|
+
payload=b"{}",
|
|
46
|
+
qos=1,
|
|
47
|
+
retain=False,
|
|
48
|
+
correlation_data=(
|
|
49
|
+
correlation_id.encode("utf-8")
|
|
50
|
+
if isinstance(correlation_id, str)
|
|
51
|
+
else correlation_id
|
|
52
|
+
),
|
|
53
|
+
user_properties={"ReturnCode": str(return_code)},
|
|
54
|
+
)
|
|
55
|
+
if (
|
|
56
|
+
debug_info is not None and msg_obj.user_properties is not None
|
|
57
|
+
): # user_properties should never be None here, but checking to satisfy type checker
|
|
58
|
+
msg_obj.user_properties["DebugInfo"] = debug_info
|
|
59
|
+
return msg_obj
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def response_message(
|
|
63
|
+
cls,
|
|
64
|
+
response_topic: str,
|
|
65
|
+
response_obj: BaseModel|str|bytes,
|
|
66
|
+
return_code: int,
|
|
67
|
+
correlation_id: Union[str, bytes],
|
|
68
|
+
) -> Message:
|
|
69
|
+
"""
|
|
70
|
+
This could be used for a successful response to a request.
|
|
71
|
+
"""
|
|
72
|
+
payload = response_obj.model_dump_json(by_alias=True).encode("utf-8") if isinstance(response_obj, BaseModel) else response_obj
|
|
73
|
+
msg_obj = Message(
|
|
74
|
+
topic=response_topic,
|
|
75
|
+
payload=payload,
|
|
76
|
+
qos=1,
|
|
77
|
+
retain=False,
|
|
78
|
+
correlation_data=(
|
|
79
|
+
correlation_id.encode("utf-8")
|
|
80
|
+
if isinstance(correlation_id, str)
|
|
81
|
+
else correlation_id
|
|
82
|
+
),
|
|
83
|
+
user_properties={"ReturnCode": str(return_code)},
|
|
84
|
+
)
|
|
85
|
+
return msg_obj
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def property_state_message(
|
|
89
|
+
cls, topic: str, state_obj: BaseModel, state_version: Optional[int] = None
|
|
90
|
+
) -> Message:
|
|
91
|
+
"""
|
|
92
|
+
Creates a retained message representing the state/value of a property.
|
|
93
|
+
"""
|
|
94
|
+
msg_obj = Message(
|
|
95
|
+
topic=topic,
|
|
96
|
+
payload=state_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
97
|
+
qos=1,
|
|
98
|
+
retain=True,
|
|
99
|
+
)
|
|
100
|
+
if state_version is not None:
|
|
101
|
+
msg_obj.user_properties = {"PropertyVersion": str(state_version)}
|
|
102
|
+
return msg_obj
|
|
103
|
+
|
|
104
|
+
@classmethod
|
|
105
|
+
def property_update_request_message(
|
|
106
|
+
cls,
|
|
107
|
+
topic: str,
|
|
108
|
+
property_obj: BaseModel,
|
|
109
|
+
version: str,
|
|
110
|
+
response_topic: str,
|
|
111
|
+
correlation_id: Union[str, bytes, None] = None,
|
|
112
|
+
) -> Message:
|
|
113
|
+
"""
|
|
114
|
+
Creates a message representing a request to update a property.
|
|
115
|
+
"""
|
|
116
|
+
msg_obj = Message(
|
|
117
|
+
topic=topic,
|
|
118
|
+
payload=property_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
119
|
+
qos=1,
|
|
120
|
+
retain=False,
|
|
121
|
+
response_topic=response_topic,
|
|
122
|
+
correlation_data=(
|
|
123
|
+
correlation_id.encode("utf-8")
|
|
124
|
+
if isinstance(correlation_id, str)
|
|
125
|
+
else correlation_id
|
|
126
|
+
),
|
|
127
|
+
user_properties={"PropertyVersion": str(version)},
|
|
128
|
+
)
|
|
129
|
+
return msg_obj
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def property_response_message(
|
|
133
|
+
cls,
|
|
134
|
+
response_topic: str,
|
|
135
|
+
property_obj: BaseModel,
|
|
136
|
+
version: str,
|
|
137
|
+
return_code: int,
|
|
138
|
+
correlation_id: Union[str, bytes],
|
|
139
|
+
debug_info: Optional[str] = None,
|
|
140
|
+
) -> Message:
|
|
141
|
+
"""
|
|
142
|
+
Creates a message representing a response to a property update request.
|
|
143
|
+
"""
|
|
144
|
+
msg_obj = Message(
|
|
145
|
+
topic=response_topic,
|
|
146
|
+
payload=property_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
147
|
+
qos=1,
|
|
148
|
+
retain=False,
|
|
149
|
+
correlation_data=(
|
|
150
|
+
correlation_id.encode("utf-8")
|
|
151
|
+
if isinstance(correlation_id, str)
|
|
152
|
+
else correlation_id
|
|
153
|
+
),
|
|
154
|
+
user_properties={
|
|
155
|
+
"ReturnCode": str(return_code),
|
|
156
|
+
"PropertyVersion": str(version),
|
|
157
|
+
},
|
|
158
|
+
)
|
|
159
|
+
if (
|
|
160
|
+
debug_info is not None and msg_obj.user_properties is not None
|
|
161
|
+
): # user_properties should never be None here, but checking to satisfy type checker
|
|
162
|
+
msg_obj.user_properties["DebugInfo"] = debug_info
|
|
163
|
+
return msg_obj
|
|
164
|
+
|
|
165
|
+
@classmethod
|
|
166
|
+
def request_message(
|
|
167
|
+
cls,
|
|
168
|
+
topic: str,
|
|
169
|
+
request_obj: BaseModel,
|
|
170
|
+
response_topic: str,
|
|
171
|
+
correlation_id: Union[str, bytes, None] = None,
|
|
172
|
+
) -> Message:
|
|
173
|
+
if correlation_id is None:
|
|
174
|
+
correlation_id = str(uuid.uuid4())
|
|
175
|
+
msg_obj = Message(
|
|
176
|
+
topic=topic,
|
|
177
|
+
payload=request_obj.model_dump_json(by_alias=True).encode("utf-8"),
|
|
178
|
+
qos=1,
|
|
179
|
+
retain=False,
|
|
180
|
+
response_topic=response_topic,
|
|
181
|
+
correlation_data=(
|
|
182
|
+
correlation_id.encode("utf-8")
|
|
183
|
+
if isinstance(correlation_id, str)
|
|
184
|
+
else correlation_id
|
|
185
|
+
),
|
|
186
|
+
)
|
|
187
|
+
return msg_obj
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from enum import IntEnum
|
|
3
|
+
from pyqttier.message import Message
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MethodReturnCode(IntEnum):
|
|
7
|
+
SUCCESS = 0
|
|
8
|
+
CLIENT_ERROR = 1
|
|
9
|
+
SERVER_ERROR = 2
|
|
10
|
+
TRANSPORT_ERROR = 3
|
|
11
|
+
PAYLOAD_ERROR = 4
|
|
12
|
+
CLIENT_SERIALIZATION_ERROR = 5
|
|
13
|
+
CLIENT_DESERIALIZATION_ERROR = 6
|
|
14
|
+
SERVER_SERIALIZATION_ERROR = 7
|
|
15
|
+
SERVER_DESERIALIZATION_ERROR = 8
|
|
16
|
+
METHOD_NOT_FOUND = 9
|
|
17
|
+
UNAUTHORIZED = 10
|
|
18
|
+
TIMEOUT = 11
|
|
19
|
+
OUT_OF_SYNC = 12
|
|
20
|
+
UNKNOWN_ERROR = 13
|
|
21
|
+
NOT_IMPLEMENTED = 14
|
|
22
|
+
SERVICE_UNAVAILABLE = 15
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class StingerMethodException(Exception):
|
|
26
|
+
|
|
27
|
+
def __init__(self, return_code: MethodReturnCode, message: str):
|
|
28
|
+
super().__init__(message)
|
|
29
|
+
self._return_code = return_code
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def return_code(self) -> MethodReturnCode:
|
|
33
|
+
return self._return_code
|
|
34
|
+
|
|
35
|
+
def to_response_message(
|
|
36
|
+
self, response_topic: str, correlation_id: Optional[bytes] = None
|
|
37
|
+
) -> Message:
|
|
38
|
+
return Message(
|
|
39
|
+
topic=response_topic,
|
|
40
|
+
payload=b"{}",
|
|
41
|
+
qos=1,
|
|
42
|
+
retain=False,
|
|
43
|
+
correlation_data=correlation_id,
|
|
44
|
+
user_properties={
|
|
45
|
+
"ReturnCode": str(self._return_code.value),
|
|
46
|
+
"DebugInfo": str(self),
|
|
47
|
+
},
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SuccessStingerMethodException(StingerMethodException):
|
|
52
|
+
def __init__(self, message: str):
|
|
53
|
+
super().__init__(MethodReturnCode.SUCCESS, message)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ClientErrorStingerMethodException(StingerMethodException):
|
|
57
|
+
def __init__(self, message: str):
|
|
58
|
+
super().__init__(MethodReturnCode.CLIENT_ERROR, message)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class ServerErrorStingerMethodException(StingerMethodException):
|
|
62
|
+
def __init__(self, message: str):
|
|
63
|
+
super().__init__(MethodReturnCode.SERVER_ERROR, message)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TransportErrorStingerMethodException(StingerMethodException):
|
|
67
|
+
def __init__(self, message: str):
|
|
68
|
+
super().__init__(MethodReturnCode.TRANSPORT_ERROR, message)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class PayloadErrorStingerMethodException(StingerMethodException):
|
|
72
|
+
def __init__(self, message: str):
|
|
73
|
+
super().__init__(MethodReturnCode.PAYLOAD_ERROR, message)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class ClientSerializationErrorStingerMethodException(StingerMethodException):
|
|
77
|
+
def __init__(self, message: str):
|
|
78
|
+
super().__init__(MethodReturnCode.CLIENT_SERIALIZATION_ERROR, message)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ClientDeserializationErrorStingerMethodException(StingerMethodException):
|
|
82
|
+
def __init__(self, message: str):
|
|
83
|
+
super().__init__(MethodReturnCode.CLIENT_DESERIALIZATION_ERROR, message)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class ServerSerializationErrorStingerMethodException(StingerMethodException):
|
|
87
|
+
def __init__(self, message: str):
|
|
88
|
+
super().__init__(MethodReturnCode.SERVER_SERIALIZATION_ERROR, message)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class ServerDeserializationErrorStingerMethodException(StingerMethodException):
|
|
92
|
+
def __init__(self, message: str):
|
|
93
|
+
super().__init__(MethodReturnCode.SERVER_DESERIALIZATION_ERROR, message)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class MethodNotFoundStingerMethodException(StingerMethodException):
|
|
97
|
+
def __init__(self, message: str):
|
|
98
|
+
super().__init__(MethodReturnCode.METHOD_NOT_FOUND, message)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class UnauthorizedStingerMethodException(StingerMethodException):
|
|
102
|
+
def __init__(self, message: str):
|
|
103
|
+
super().__init__(MethodReturnCode.UNAUTHORIZED, message)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TimeoutStingerMethodException(StingerMethodException):
|
|
107
|
+
def __init__(self, message: str):
|
|
108
|
+
super().__init__(MethodReturnCode.TIMEOUT, message)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class OutOfSyncStingerMethodException(StingerMethodException):
|
|
112
|
+
def __init__(self, message: str):
|
|
113
|
+
super().__init__(MethodReturnCode.OUT_OF_SYNC, message)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class UnknownErrorStingerMethodException(StingerMethodException):
|
|
117
|
+
def __init__(self, message: str):
|
|
118
|
+
super().__init__(MethodReturnCode.UNKNOWN_ERROR, message)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class NotImplementedStingerMethodException(StingerMethodException):
|
|
122
|
+
def __init__(self, message: str):
|
|
123
|
+
super().__init__(MethodReturnCode.NOT_IMPLEMENTED, message)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class ServiceUnavailableStingerMethodException(StingerMethodException):
|
|
127
|
+
def __init__(self, message: str):
|
|
128
|
+
super().__init__(MethodReturnCode.SERVICE_UNAVAILABLE, message)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def stinger_exception_factory(return_code: int, message: Optional[str] = None):
|
|
132
|
+
exc_classes = {
|
|
133
|
+
0: SuccessStingerMethodException,
|
|
134
|
+
1: ClientErrorStingerMethodException,
|
|
135
|
+
2: ServerErrorStingerMethodException,
|
|
136
|
+
3: TransportErrorStingerMethodException,
|
|
137
|
+
4: PayloadErrorStingerMethodException,
|
|
138
|
+
5: ClientSerializationErrorStingerMethodException,
|
|
139
|
+
6: ClientDeserializationErrorStingerMethodException,
|
|
140
|
+
7: ServerSerializationErrorStingerMethodException,
|
|
141
|
+
8: ServerDeserializationErrorStingerMethodException,
|
|
142
|
+
9: MethodNotFoundStingerMethodException,
|
|
143
|
+
10: UnauthorizedStingerMethodException,
|
|
144
|
+
11: TimeoutStingerMethodException,
|
|
145
|
+
12: OutOfSyncStingerMethodException,
|
|
146
|
+
13: UnknownErrorStingerMethodException,
|
|
147
|
+
14: NotImplementedStingerMethodException,
|
|
148
|
+
15: ServiceUnavailableStingerMethodException,
|
|
149
|
+
}
|
|
150
|
+
exc_class = exc_classes[return_code]
|
|
151
|
+
return exc_class(message or "")
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
|
@@ -0,0 +1,7 @@
|
|
|
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,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jacob Brunson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|