summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/dashboard_api
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2017-01-05 12:34:37 +0100
committermaxbr <maxbr@mi.fu-berlin.de>2017-01-05 12:34:37 +0100
commit55c3b53ba08b2c6cf3c392b10a065a3076525951 (patch)
tree43ae585550a312c4c91bb002626cec5770556e98 /tools/pharos-dashboard/dashboard_api
parent688948508e373829cdcd7bb1e4ebb7f55e43cdac (diff)
Add library for dashboard API
JIRA: PHAROS-265 The DashboardAPI class can be used to query and update dashboard data. Change-Id: I584a3cc500ff1d67011c18af4e73e504126310a2 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'tools/pharos-dashboard/dashboard_api')
-rw-r--r--tools/pharos-dashboard/dashboard_api/__init__.py8
-rw-r--r--tools/pharos-dashboard/dashboard_api/api.py91
2 files changed, 99 insertions, 0 deletions
diff --git a/tools/pharos-dashboard/dashboard_api/__init__.py b/tools/pharos-dashboard/dashboard_api/__init__.py
new file mode 100644
index 00000000..ce1acf36
--- /dev/null
+++ b/tools/pharos-dashboard/dashboard_api/__init__.py
@@ -0,0 +1,8 @@
+##############################################################################
+# Copyright (c) 2016 Max Breitenfeldt and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+############################################################################## \ No newline at end of file
diff --git a/tools/pharos-dashboard/dashboard_api/api.py b/tools/pharos-dashboard/dashboard_api/api.py
new file mode 100644
index 00000000..d40e0aa4
--- /dev/null
+++ b/tools/pharos-dashboard/dashboard_api/api.py
@@ -0,0 +1,91 @@
+##############################################################################
+# Copyright (c) 2016 Max Breitenfeldt and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+import logging
+
+import requests
+
+URLS = {
+ 'resources': '/api/resources/',
+ 'servers': '/api/servers/',
+ 'bookings': '/api/bookings',
+ 'resource_status': '/api/resource_status/',
+}
+
+class DashboardAPI(object):
+ def __init__(self, dashboard_url, api_token='', verbose=False):
+ self._api_token = api_token
+ self._verbose = verbose
+ self._resources_url = dashboard_url + URLS['resources']
+ self._servers_url = dashboard_url + URLS['servers']
+ self._bookings_url = dashboard_url + URLS['bookings']
+ self._resources_status_url = dashboard_url + URLS['resource_status']
+ self._logger = logging.getLogger(__name__)
+
+ def get_all_resources(self):
+ return self._get_json(self._resources_url)
+
+ def get_resource(self, id='', name='', url=''):
+ if url != '':
+ return self._get_json(url)[0]
+ url = self._resources_url + self._url_parameter(id=id, name=name)
+ return self._get_json(url)[0]
+
+ def get_all_bookings(self):
+ return self._get_json(self._bookings_url)
+
+ def get_resource_bookings(self, resource_id):
+ url = self._bookings_url + self._url_parameter(resource_id=resource_id)
+ return self._get_json(url)
+
+ def get_booking(self, id):
+ url = self._bookings_url + self._url_parameter(id=id)
+ return self._get_json(url)[0]
+
+ def post_resource_status(self, resource_id, type, title, content):
+ data = {
+ 'resource': resource_id,
+ 'type': type,
+ 'title': title,
+ 'content': content
+ }
+ return self._post_json(self._resources_status_url, data)
+
+ def get_url(self, url):
+ return self._get_json(url)
+
+ def _url_parameter(self, **kwargs):
+ res = ''
+ prefix = '?'
+ for key, val in kwargs.items():
+ res += prefix + key + '=' + str(val)
+ prefix = '&'
+ return res
+
+ def _get_json(self, url):
+ try:
+ response = requests.get(url)
+ if self._verbose:
+ print('Get JSON: ' + url)
+ print(response.status_code, response.content)
+ return response.json()
+ except requests.exceptions.RequestException as e:
+ self._logger.exception(e)
+ except ValueError as e:
+ self._logger.exception(e)
+
+ def _post_json(self, url, json):
+ if self._api_token == '':
+ raise Exception('Need api token to POST data.')
+ response = requests.post(url, json, headers={'Authorization': 'Token ' + self._api_token})
+ if self._verbose:
+ print('Post JSON: ' + url)
+ print(response.status_code, response.content)
+ return response.status_code