-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonhub_service.py
48 lines (40 loc) · 1.43 KB
/
buttonhub_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import requests
class ButtonhubService:
def __init__(self, config):
if 'buttonhub' in config:
self.config = config['buttonhub']
self.base_url = config['buttonhub']['base_url']
self.enabled = True
else:
self.config = {}
self.enabled = False
def get_config(self, name):
return self.config.get(name) or {}
def get_state(self):
try:
response = requests.get(f'{self.base_url}/state', timeout=5)
response.raise_for_status()
return response.json()
except requests.HTTPError:
raise ButtonhubError
except requests.exceptions.ConnectionError:
raise ButtonhubError
def run_flow(self, flow_name):
try:
response = requests.post(f'{self.base_url}/flows/{flow_name}', timeout=5)
response.raise_for_status()
except requests.HTTPError:
raise ButtonhubError
except requests.exceptions.ConnectionError:
raise ButtonhubError
def get_flows(self):
try:
response = requests.get(f'{self.base_url}/flows', timeout=5)
response.raise_for_status()
return response.json()['flows']
except requests.HTTPError:
raise ButtonhubError
except requests.exceptions.ConnectionError:
raise ButtonhubError
class ButtonhubError(RuntimeError):
pass