45 lines
1.2 KiB
Python

# 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))