Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4KB

  1. import requests
  2. import abp, json
  3. class ClientError(Exception):
  4. def __init__(self, message):
  5. self.message = message
  6. class Client(object):
  7. def __init__(self, host="localhost", port=5000, clear=False):
  8. self.session = requests.Session()
  9. self.root = "http://{}:{}".format(host, port)
  10. if clear:
  11. self.clear()
  12. def get(self, endpoint):
  13. url =self.root+endpoint
  14. response = self.session.get(url)
  15. if response.status_code == 404:
  16. message = "404. Check that the server is running!".format(self.root, endpoint)
  17. raise ClientError(message)
  18. return response.content
  19. def get_state(self):
  20. response = self.get("/state")
  21. output = abp.GraphState()
  22. output.from_json(json.loads(response))
  23. return output
  24. def set_state(self, state):
  25. response = self.session.post(self.root+"/state", data=state.to_json())
  26. if not response.status_code == 200:
  27. print response.status_code
  28. return response.content
  29. def add_node(self, node):
  30. return self.get("/add_node/{}".format(node))
  31. def act_local_rotation(self, node, operation):
  32. return self.get("/act_local_rotation/{}/{}".format(node, operation))
  33. def act_cz(self, a, b):
  34. return self.get("/act_cz/{}/{}".format(a, b))
  35. def clear(self):
  36. return self.get("/clear")
  37. def kill(self):
  38. self.session.close()