Last active
August 29, 2015 13:57
-
-
Save fridgei/9476718 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import furl | |
import json | |
class DescartesException(Exception): | |
pass | |
class DescartesApi(object): | |
def __init__(self, graphite_host): | |
self.graphite_host = graphite_host | |
@classmethod | |
def _check_response(cls, response): | |
if not response.status_code == 200: | |
raise DescartesException( | |
"{url} returned {status} with payload" | |
"\n{data}\n and params\n{params}".format( | |
url=response.url, | |
status=response.status_code, | |
data=json.dumps( | |
response.request.data, | |
indent=4 | |
), | |
params=json.dumps( | |
response.requst.params, | |
indent=4 | |
) | |
) | |
) | |
return response.json() | |
def _post(self, endpoint, payload): | |
url = furl.urljoin(self.graphite_host, endpoint) | |
response = requests.post(url, data=payload) | |
return DescartesApi._check_response(response) | |
def _get(self, endpoint, payload): | |
url = furl.urljoin(self.graphite_host, endpoint) | |
response = requests.get(url, params=payload) | |
return DescartesApi._check_response(response) | |
def create_descartes_graph(self, url, name, tags=[]): | |
payload = { | |
'name': name, | |
'node': url, | |
'tag': ",".join(tags) | |
} | |
return self._post( | |
"/graphs", | |
payload=payload | |
) | |
def create_descartes_dash(self, name, description='', uuids=[]): | |
payload = { | |
'name': name, | |
'description': description, | |
'uuids': ",".join(uuids) | |
} | |
return self._post( | |
'/dashboards', | |
payload=payload | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment