Handles communication with the puppet API.
| 16 | from viewer_install import ensure_viewer_installed as install_viewer, viewer_path_for_project |
| 17 | |
| 18 | |
| 19 | class PuppetController: |
| 20 | """Handles communication with the puppet API.""" |
| 21 | |
| 22 | def __init__(self, host="localhost", port=42943): |
| 23 | self.base_url = f"http://{host}:{port}" |
| 24 | self._connected = False |
| 25 | |
| 26 | def is_connected(self): |
| 27 | """Check if the puppet API is reachable.""" |
| 28 | try: |
| 29 | response = requests.get(f"{self.base_url}/expressions", timeout=1) |
| 30 | self._connected = response.status_code == 200 |
| 31 | return self._connected |
| 32 | except requests.RequestException: |
| 33 | self._connected = False |
| 34 | return False |
| 35 | |
| 36 | def get_expressions(self): |
| 37 | """Get available expressions from the puppet.""" |
| 38 | try: |
| 39 | response = requests.get(f"{self.base_url}/expressions", timeout=2) |
| 40 | response.raise_for_status() |
| 41 | return response.json() |
| 42 | except requests.RequestException: |
| 43 | return [] |
| 44 | |
| 45 | def set_expression(self, expression): |
| 46 | """Set an expression on the puppet.""" |
| 47 | try: |
| 48 | response = requests.post( |
| 49 | f"{self.base_url}/expression", |
| 50 | json={"expression": expression}, |
| 51 | timeout=2 |
| 52 | ) |
| 53 | response.raise_for_status() |
| 54 | return True |
| 55 | except requests.RequestException: |
| 56 | return False |
| 57 | |
| 58 | def get_motions(self): |
| 59 | """Get available motions from the puppet.""" |
| 60 | try: |
| 61 | response = requests.get(f"{self.base_url}/motions", timeout=2) |
| 62 | response.raise_for_status() |
| 63 | return response.json() |
| 64 | except requests.RequestException: |
| 65 | return {} |
| 66 | |
| 67 | def set_motion(self, motion): |
| 68 | """Set a motion on the puppet.""" |
| 69 | try: |
| 70 | response = requests.post( |
| 71 | f"{self.base_url}/motion", |
| 72 | json={"motion": motion}, |
| 73 | timeout=2 |
| 74 | ) |
| 75 | response.raise_for_status() |