Py Access to Management

This commit is contained in:
2024-10-30 19:27:56 +01:00
parent cfe3dbd5bb
commit f2b9df7611
6 changed files with 393 additions and 292 deletions

View File

@ -59,22 +59,24 @@ class ManagementRequest():
If `allow_lazy` is active, the type checking (by pydantic) is less strict.
E.g. it does not require that all files in the data section of messages must exist on the file system.
"""
self.allow_lazy = allow_lazy
self.pydantic_context = {
"require_file_exists": not self.allow_lazy
self._allow_lazy = allow_lazy
self._pydantic_context = {
"require_file_exists": not self._allow_lazy
}
@validate_call
def get_message(self, count:int) -> MessageDbRow:
"""
Get a message (like a table row) from the management by using the `count`.
May raise `RequestException`.
"""
row = self._get_request(
'list/single',
{"count": count}
)
return MessageDbRow.model_validate(
row, context=self.pydantic_context
row, context=self._pydantic_context
)
@validate_call
@ -87,6 +89,8 @@ class ManagementRequest():
"""
Get the rows in the tables as list of messages.
The arguments are used for filtering.
May raise `RequestException`.
"""
kwargs = locals().copy()
@ -100,7 +104,7 @@ class ManagementRequest():
return [
MessageDbRow.model_validate(
row, context=self.pydantic_context
row, context=self._pydantic_context
) for row in rows
]
@ -112,6 +116,8 @@ class ManagementRequest():
) -> int:
"""
Get the total number of rows in the tables matching the filters.
May raise `RequestException`.
"""
kwargs = locals().copy()
@ -135,20 +141,33 @@ class ManagementRequest():
raise RequestException(str(r.text)+"\n"+str(r.headers))
@validate_call
def send_message(self, ) -> AgentResponse:
# TODO
pass
def send_message(self, message:AgentMessage) -> AgentResponse:
"""
Send the `message` to the management and return the management's agent response.
(On error an agent response with error message).
"""
try:
return AgentResponse.model_validate(
self._post_request(
"message",
message.model_dump_json()
)
)
except RequestException as e:
return AgentResponse(
count=-1,
error=True,
error_msg=str(e)
)
def _post_request(self, message:AgentMessage) -> AgentResponse:
# TODO
def _post_request(self, endpoint:str, data:Dict[str, Any]):
r = requests.post(
"{}/message".format(self.url),
data=message.model_dump_json(),
"{}/{}".format(self.MANAGEMENT_URL, endpoint),
data=data,
headers={"accept" : "application/json", "content-type" : "application/json"}
)
if r.status_code == 200:
return AgentResponse.model_validate_json(r.text)
return r.json()
else:
return AgentResponse(count=-1, error=True, error_msg=str(r.text)+str(r.headers))
return RequestException(str(r.text)+"\n"+str(r.headers))