1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
16 'resources': '/api/resources/',
17 'servers': '/api/servers/',
18 'bookings': '/api/bookings',
19 'resource_status': '/api/resource_status/',
22 class DashboardAPI(object):
23 def __init__(self, dashboard_url, api_token='', verbose=False):
24 self._api_token = api_token
25 self._verbose = verbose
26 self._resources_url = dashboard_url + URLS['resources']
27 self._servers_url = dashboard_url + URLS['servers']
28 self._bookings_url = dashboard_url + URLS['bookings']
29 self._resources_status_url = dashboard_url + URLS['resource_status']
30 self._logger = logging.getLogger(__name__)
32 def get_all_resources(self):
33 return self._get_json(self._resources_url)
35 def get_resource(self, id='', name='', url=''):
37 return self._get_json(url)[0]
38 url = self._resources_url + self._url_parameter(id=id, name=name)
39 return self._get_json(url)[0]
41 def get_all_bookings(self):
42 return self._get_json(self._bookings_url)
44 def get_resource_bookings(self, resource_id):
45 url = self._bookings_url + self._url_parameter(resource_id=resource_id)
46 return self._get_json(url)
48 def get_booking(self, id):
49 url = self._bookings_url + self._url_parameter(id=id)
50 return self._get_json(url)[0]
52 def post_resource_status(self, resource_id, type, title, content):
54 'resource': resource_id,
59 return self._post_json(self._resources_status_url, data)
61 def get_url(self, url):
62 return self._get_json(url)
64 def _url_parameter(self, **kwargs):
67 for key, val in kwargs.items():
68 res += prefix + key + '=' + str(val)
72 def _get_json(self, url):
74 response = requests.get(url)
76 print('Get JSON: ' + url)
77 print(response.status_code, response.content)
78 return response.json()
79 except requests.exceptions.RequestException as e:
80 self._logger.exception(e)
81 except ValueError as e:
82 self._logger.exception(e)
84 def _post_json(self, url, json):
85 if self._api_token == '':
86 raise Exception('Need api token to POST data.')
87 response = requests.post(url, json, headers={'Authorization': 'Token ' + self._api_token})
89 print('Post JSON: ' + url)
90 print(response.status_code, response.content)
91 return response.status_code