Verteiltesystheme/Code/Communication/APIRequests.py

25 lines
895 B
Python

from dataclasses import asdict
import requests as requests
from Code.Communication.Direction import Direction
from Code.Communication.Member import Member
class APIRequests:
def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> Member:
body = asdict(own_process)
response = requests.post(f"http://{ip}:{port}/connect/{direction.name}", json=body,allow_redirects=True)
jsonValue = response.json()
return Member(jsonValue["ip"], jsonValue["port"])
def get_edge(self, target: Member, direction: Direction):
response = requests.get(f"http://{target.ip}:{target.port}/border/{direction.name}")
return response.json()
def toggle_pause(self, neighbour: Member, new_state: bool):
action = "start" if new_state else "stop"
print(f"sending {action} to {neighbour}")
response = requests.post(f"http://{neighbour.ip}:{neighbour.port}/pause/{action}")