ums.utils.request
1# Agenten Plattform 2# 3# (c) 2024 Magnus Bender 4# Institute of Humanities-Centered Artificial Intelligence (CHAI) 5# Universitaet Hamburg 6# https://www.chai.uni-hamburg.de/~bender 7# 8# source code released under the terms of GNU Public License Version 3 9# https://www.gnu.org/licenses/gpl-3.0.txt 10 11import requests 12 13from ums.utils.types import AgentMessage, AgentResponse, MessageDbRow 14 15class RequestException(Exception): 16 pass 17 18class ManagementRequest(): 19 20 def __init__(self, hostname:str, port:int=80): 21 self.url = "http://{hostname}:{port}".format(hostname=hostname, port=port) 22 23 def get_status(self, count:int) -> MessageDbRow: 24 r = requests.get( 25 "{}/status".format(self.url), 26 params={"count": count} 27 ) 28 29 if r.status_code == 200: 30 return MessageDbRow.model_validate_json(r.text) 31 else: 32 raise RequestException(str(r.text)+str(r.headers)) 33 34 35 def send_message(self, message:AgentMessage) -> AgentResponse: 36 r = requests.post( 37 "{}/message".format(self.url), 38 data=message.model_dump_json(), 39 headers={"accept" : "application/json", "content-type" : "application/json"} 40 ) 41 42 if r.status_code == 200: 43 return AgentResponse.model_validate_json(r.text) 44 else: 45 return AgentResponse(count=-1, error=True, error_msg=str(r.text)+str(r.headers))
class
RequestException(builtins.Exception):
Common base class for all non-exit exceptions.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- add_note
- args
class
ManagementRequest:
19class ManagementRequest(): 20 21 def __init__(self, hostname:str, port:int=80): 22 self.url = "http://{hostname}:{port}".format(hostname=hostname, port=port) 23 24 def get_status(self, count:int) -> MessageDbRow: 25 r = requests.get( 26 "{}/status".format(self.url), 27 params={"count": count} 28 ) 29 30 if r.status_code == 200: 31 return MessageDbRow.model_validate_json(r.text) 32 else: 33 raise RequestException(str(r.text)+str(r.headers)) 34 35 36 def send_message(self, message:AgentMessage) -> AgentResponse: 37 r = requests.post( 38 "{}/message".format(self.url), 39 data=message.model_dump_json(), 40 headers={"accept" : "application/json", "content-type" : "application/json"} 41 ) 42 43 if r.status_code == 200: 44 return AgentResponse.model_validate_json(r.text) 45 else: 46 return AgentResponse(count=-1, error=True, error_msg=str(r.text)+str(r.headers))
36 def send_message(self, message:AgentMessage) -> AgentResponse: 37 r = requests.post( 38 "{}/message".format(self.url), 39 data=message.model_dump_json(), 40 headers={"accept" : "application/json", "content-type" : "application/json"} 41 ) 42 43 if r.status_code == 200: 44 return AgentResponse.model_validate_json(r.text) 45 else: 46 return AgentResponse(count=-1, error=True, error_msg=str(r.text)+str(r.headers))