Accept Messages
This commit is contained in:
@ -15,7 +15,11 @@ from ums.utils.types import (
|
||||
RiddleSolution,
|
||||
RiddleData,
|
||||
RiddleDataType,
|
||||
RiddleStatus
|
||||
RiddleStatus,
|
||||
AgentResponse,
|
||||
MessageDbRow
|
||||
)
|
||||
|
||||
from ums.utils.const import *
|
||||
from ums.utils.const import *
|
||||
|
||||
from ums.utils.request import ManagementRequest
|
45
ums/utils/request.py
Normal file
45
ums/utils/request.py
Normal file
@ -0,0 +1,45 @@
|
||||
# Agenten Plattform
|
||||
#
|
||||
# (c) 2024 Magnus Bender
|
||||
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||
# Universitaet Hamburg
|
||||
# https://www.chai.uni-hamburg.de/~bender
|
||||
#
|
||||
# source code released under the terms of GNU Public License Version 3
|
||||
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
|
||||
import requests
|
||||
|
||||
from ums.utils.types import AgentMessage, AgentResponse, MessageDbRow
|
||||
|
||||
class RequestException(Exception):
|
||||
pass
|
||||
|
||||
class ManagementRequest():
|
||||
|
||||
def __init__(self, hostname:str, port:int=80):
|
||||
self.url = "http://{hostname}:{port}".format(hostname=hostname, port=port)
|
||||
|
||||
def get_status(self, count:int) -> MessageDbRow:
|
||||
r = requests.get(
|
||||
"{}/status".format(self.url),
|
||||
params={"count": count}
|
||||
)
|
||||
|
||||
if r.status_code == 200:
|
||||
return MessageDbRow.model_validate_json(r.text)
|
||||
else:
|
||||
raise RequestException(str(r.text)+str(r.headers))
|
||||
|
||||
|
||||
def send_message(self, message:AgentMessage) -> AgentResponse:
|
||||
r = requests.post(
|
||||
"{}/message".format(self.url),
|
||||
data=message.model_dump_json(),
|
||||
headers={"accept" : "application/json", "content-type" : "application/json"}
|
||||
)
|
||||
|
||||
if r.status_code == 200:
|
||||
return AgentResponse.model_validate_json(r.text)
|
||||
else:
|
||||
return AgentResponse(count=-1, error=True, error_msg=str(r.text)+str(r.headers))
|
@ -289,4 +289,64 @@ class AgentMessage(RiddleInformation):
|
||||
status: RiddleStatus = RiddleStatus()
|
||||
"""
|
||||
The status of the riddle.
|
||||
"""
|
||||
|
||||
class AgentResponse(RiddleInformation):
|
||||
"""
|
||||
Returned by the management when receiving an `AgentMessage`.
|
||||
"""
|
||||
|
||||
count : int
|
||||
"""
|
||||
The count of the message (overall numeric id).
|
||||
"""
|
||||
|
||||
msg: str|None = None
|
||||
"""
|
||||
An additional message.
|
||||
"""
|
||||
|
||||
error: bool = False
|
||||
"""
|
||||
If an error occurred.
|
||||
"""
|
||||
|
||||
error_msg: str|None = None
|
||||
"""
|
||||
Error message (if `error` )
|
||||
"""
|
||||
|
||||
class MessageDbRow(BaseModel):
|
||||
"""
|
||||
Object representing a database row.
|
||||
"""
|
||||
|
||||
count : int
|
||||
"""
|
||||
The count (primary key) of the item.
|
||||
"""
|
||||
|
||||
sender : str
|
||||
"""
|
||||
The sender of the message.
|
||||
"""
|
||||
|
||||
recipient : str
|
||||
"""
|
||||
The recipient of the message
|
||||
"""
|
||||
|
||||
time : int
|
||||
"""
|
||||
The time (unix timestamp) the message was received/ sent.
|
||||
"""
|
||||
|
||||
message : AgentMessage
|
||||
"""
|
||||
The message received/ sent.
|
||||
"""
|
||||
|
||||
processed : bool
|
||||
"""
|
||||
Did the management process the message, i.e., did the tasks necessary for this message (mostly only relevant for received messages).
|
||||
"""
|
Reference in New Issue
Block a user