diff --git a/DESCRIPTION.rst b/DESCRIPTION.rst new file mode 100644 index 0000000..0235095 --- /dev/null +++ b/DESCRIPTION.rst @@ -0,0 +1,11 @@ +Python Module for Pingdom REST API +================================== + +Simple Python wrapper for the `Pingdom REST API`_. + +This module does not support the soon-to-be obsolete `Pingdom SOAP API`_. +See `python-pingdom`_ for the SOAP interface. + +.. _`Pingdom REST API`: http://www.pingdom.com/services/api-documentation-rest/ +.. _`Pingdom SOAP API`: http://www.pingdom.com/services/api-documentation/ +.. _`python-pingdom`: https://github.com/danudey/python-pingdom diff --git a/README.rst b/README.rst index 44fe6b5..7880011 100644 --- a/README.rst +++ b/README.rst @@ -27,21 +27,30 @@ Call an arbitrary method as described in the API docs:: Example methods:: + # Create check + parameters = {} + parameters['url'] = "/ping" + parameters['resolution'] = 1 + parameters['sendtoemail'] = "true" + parameters['contactids'] = "123" + parameters['sendnotificationwhendown'] = 1 + p.create_check(hostname, "1.2.3.4", "http", parameters) + # List checks p.method('checks') - + # Modifiy a check, in this case, pause it p.method('checks/CHECK_ID_NUM/', method='PUT', parameters={'paused': True}) - + Some shortcut methods that were useful to me:: # Get checks by name instead of number p.check_by_name('my check name') - + # Pause and unpause a check by name p.pause_check('my check name') p.unpause_check('my check name') - + # Average Response Time p.avg_response(CHEKC_ID_NUM) @@ -50,8 +59,8 @@ Some shortcut methods that were useful to me:: # Average Response Time for the last 15 minutes in the US p.avg_response(CHEKC_ID_NUM, minutes_back=15, country='US') - - + + .. _`Pingdom REST API`: http://www.pingdom.com/services/api-documentation-rest/ .. _`Pingdom SOAP API`: http://www.pingdom.com/services/api-documentation/ diff --git a/pingdom.py b/pingdom.py index c505fbc..bb69094 100644 --- a/pingdom.py +++ b/pingdom.py @@ -8,10 +8,10 @@ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -36,7 +36,7 @@ def __init__(self, url=API_URL, username=None, password=None, appkey=None): password_manager.add_password(None, url, username, password) auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) self.opener = urllib2.build_opener(auth_handler) - + class RequestWithMethod(urllib2.Request): def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False, http_method=None): @@ -48,13 +48,14 @@ def get_method(self): if self.method: return self.method return urllib2.Request.get_method(self) - + def method(self, url, method="GET", parameters=None): if parameters: data = urlencode(parameters) else: data = None method_url = urljoin(self.url, url) + if method == "GET" and data: method_url = method_url+'?'+data req = self.RequestWithMethod(method_url, http_method=method, data=None) @@ -63,12 +64,18 @@ def method(self, url, method="GET", parameters=None): req.add_header('App-Key', self.appkey) response = self.opener.open(req).read() return json.loads(response) - + + def create_check(self, name, host, check_type, parameters={}): + parameters['name'] = name + parameters['host'] = host + parameters['type'] = check_type + print self.method('checks', method='POST', parameters=parameters) + def check_by_name(self, name): resp = self.method('checks') checks = [check for check in resp['checks'] if check['name'] == name] return checks - + def check_status(self, name): checks = self.check_by_name(name) for check in checks: @@ -83,15 +90,15 @@ def modify_check(self, name, parameters={}): id_ = check['id'] response = self.method('checks/%s/' % id_, method='PUT', parameters=parameters) print response['message'] - + def pause_check(self, name): self.modify_check(name, parameters={'paused': True}) self.check_status(name) - + def unpause_check(self, name): self.modify_check(name, parameters={'paused': False}) self.check_status(name) - + def avg_response(self, check_id, minutes_back=None, country=None): parameters = {} if minutes_back: @@ -113,4 +120,3 @@ def avg_response(self, check_id, minutes_back=None, country=None): else: response_time = avgresponse return response_time - diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..79bc678 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +[bdist_wheel] +# This flag says that the code is written to work on both Python 2 and Python +# 3. If at all possible, it is good practice to do this. If you cannot, you +# will need to generate wheels for each Python version that you support. +universal=1 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3f5f6a9 --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from setuptools import setup, find_packages + +setup( + name = "pingdom", + version = "1.0", + scripts=['pingdom.py'], + author = "Dan Craig", + author_email = "drcraig@gmail.com", + description = "Python Module for Pingdom REST API", + license = "MIT", + keywords = "pingdom alerts", + url = "https://github.com/drcraig/python-restful-pingdom/" +)