Py Access to Management
This commit is contained in:
@ -18,22 +18,43 @@ if __name__ == "__main__":
|
||||
|
||||
m_request = ManagementRequest()
|
||||
|
||||
# get info from Management
|
||||
# get infos from Management
|
||||
|
||||
print(
|
||||
# message number 12
|
||||
m_request.get_message(count=12)
|
||||
)
|
||||
|
||||
print(
|
||||
# first two messages of id "test"
|
||||
m_request.list_messages(id="test", limit=2)
|
||||
)
|
||||
|
||||
print(
|
||||
# count messages with id "test"
|
||||
m_request.total_messages(id="test")
|
||||
)
|
||||
|
||||
from ums.utils import AgentMessage, RiddleData, RiddleDataType, RiddleSolution
|
||||
from ums.utils import AgentMessage, Riddle, RiddleData, RiddleDataType, RiddleSolution
|
||||
|
||||
# send messages to management
|
||||
|
||||
# TODO
|
||||
# basic message
|
||||
msg = AgentMessage(
|
||||
id="example",
|
||||
riddle=Riddle(context="Today is the 1. January 1970", question="What time is it?"),
|
||||
data=[
|
||||
RiddleData(
|
||||
type=RiddleDataType.TEXT,
|
||||
file_plain="./cv.txt" # make sure this file exists!
|
||||
)
|
||||
]
|
||||
)
|
||||
# disable some steps
|
||||
msg.status.extract.required = False
|
||||
msg.status.validate.required = False
|
||||
|
||||
print(
|
||||
# send the message
|
||||
m_request.send_message(msg)
|
||||
)
|
@ -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))
|
Reference in New Issue
Block a user