From cc41ad15f01a28ac9ff6d934af4e7a8df732f63a Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 16:01:45 +0200 Subject: [PATCH 01/10] Add helper method for creating new checks --- pingdom.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) 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 - From 0b9a397fc732effff24a5da25843515394457dad Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 16:02:14 +0200 Subject: [PATCH 02/10] Add create_check method example and add code samples formatting --- README.rst | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index 44fe6b5..1b896dc 100644 --- a/README.rst +++ b/README.rst @@ -17,31 +17,42 @@ Usage ----- Instantiate:: - +```python import pingdom p = pingdom.Pingdom(username=YourUserName, password=YourPassword, appkey=YourAppKey) +``` Call an arbitrary method as described in the API docs:: - +```python p.method(url='method/url/', method="GET/POST/PUT/etc", parameters={'name':'value', }) - +``` Example methods:: +```python + # 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:: +``` +Some shortcut methods that were useful to me:: +```python # 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 +61,7 @@ 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/ From 39e0a3f0fd9625afb96e42479a39a9192aede796 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 16:03:21 +0200 Subject: [PATCH 03/10] Fix code formatting markup --- README.rst | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 1b896dc..7880011 100644 --- a/README.rst +++ b/README.rst @@ -17,17 +17,16 @@ Usage ----- Instantiate:: -```python + import pingdom p = pingdom.Pingdom(username=YourUserName, password=YourPassword, appkey=YourAppKey) -``` Call an arbitrary method as described in the API docs:: -```python + p.method(url='method/url/', method="GET/POST/PUT/etc", parameters={'name':'value', }) -``` + Example methods:: -```python + # Create check parameters = {} parameters['url'] = "/ping" @@ -42,10 +41,9 @@ Example methods:: # 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:: -```python + # Get checks by name instead of number p.check_by_name('my check name') @@ -61,7 +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/ From 1abdea2a1e372c570726c940b4b1a3c98b9248af Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 17:07:04 +0200 Subject: [PATCH 04/10] Make project pip compatible --- DESCRIPTION.rst | 11 +++++++++++ setup.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 DESCRIPTION.rst create mode 100644 setup.py 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/setup.py b/setup.py new file mode 100644 index 0000000..934ccdf --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +from setuptools import setup, find_packages + +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the relevant file +with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f: + long_description = f.read() + +setup( + name = "pingdom", + version = "1.0", + packages = find_packages(), + 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/" +) From 8e8274861759d82621f826f5855cd87507ed6886 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 17:08:48 +0200 Subject: [PATCH 05/10] Add pip setup configuration --- setup.cfg | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 setup.cfg 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 From 4db2c7c7114ef2ebce5009c11da5d4d49126d015 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 17:09:40 +0200 Subject: [PATCH 06/10] Fix bug with missing path module --- setup.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 934ccdf..3effd52 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,7 @@ from setuptools import setup, find_packages +from os import path here = path.abspath(path.dirname(__file__)) - -# Get the long description from the relevant file with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f: long_description = f.read() From 241037c19ebca16344379e95ff7a58734e7dbf7f Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 17:10:39 +0200 Subject: [PATCH 07/10] Debugging pip setup script --- setup.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/setup.py b/setup.py index 3effd52..aaa1874 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,4 @@ from setuptools import setup, find_packages -from os import path - -here = path.abspath(path.dirname(__file__)) -with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f: - long_description = f.read() setup( name = "pingdom", From 26a863d4d1e9af8d2517d5cba7daeac2248b17a1 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 18:04:29 +0200 Subject: [PATCH 08/10] Fix pip package setup to include reference to lib --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index aaa1874..f18e02e 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,8 @@ setup( name = "pingdom", version = "1.0", + packages=['pingdom'], + scripts=['pingdom.py'], packages = find_packages(), author = "Dan Craig", author_email = "drcraig@gmail.com", From b8b86b6a1624d3366aaf59ecbe9f5081916cea76 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 18:05:29 +0200 Subject: [PATCH 09/10] Fix typo in pip package setup --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index f18e02e..bca10aa 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,6 @@ version = "1.0", packages=['pingdom'], scripts=['pingdom.py'], - packages = find_packages(), author = "Dan Craig", author_email = "drcraig@gmail.com", description = "Python Module for Pingdom REST API", From 55073ddc61b031da4601ff74b9db4df3f2c80ed6 Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Wed, 30 Jul 2014 18:06:30 +0200 Subject: [PATCH 10/10] Remove pip package name from setup script --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index bca10aa..3f5f6a9 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,6 @@ setup( name = "pingdom", version = "1.0", - packages=['pingdom'], scripts=['pingdom.py'], author = "Dan Craig", author_email = "drcraig@gmail.com",