Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Class rework for Pingdom Api Version 3.1 #107

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Contributors
Paolo Sechi (https://github.com/sekipaolo)
Paul Kremer (https://github.com/infothrill)
Kristofer Borgström (https://github.com/kribor)
Lucas Nicolaus (https://github.com/TheLegendaryAdmin)
5 changes: 5 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@
* added changelog
* switched legacy, unsupported maintenance window getter to supported rest
api method

0.3.0
=====
* adjusted API class to work with new Pingdom API 3.1
* introcuded typing to constructors
22 changes: 10 additions & 12 deletions pypingdom/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import requests
from requests.auth import HTTPBasicAuth
from packaging import version


class ApiError(Exception):
Expand All @@ -25,16 +24,14 @@ def __str__(self):

class Api(object):

def __init__(self, username, password, apikey, email=False, apiversion="2.0"):
self.base_url = "https://api.pingdom.com/api/" + apiversion + "/"
if version.parse(apiversion) < version.parse('3.0'):
self.auth = HTTPBasicAuth(username, password)
self.headers = {'App-Key': apikey}
if email:
self.headers['Account-Email'] = email
else:
self.headers = {'Authorization': 'Bearer ' + apikey}
self.auth = None
def __init__(self, apikey: str, email: str =False, version: str="3.1"):
# self.auth = HTTPBasicAuth(username, password)
self.base_url = "https://api.pingdom.com/api/" + str(version) + '/'
self.headers = {'Authorization': 'Bearer ' + str(apikey),
'Host': 'api.pingdom.com',
"Connection": "keep-alive", "Cache-Control": "no-cache"}
if email:
self.headers['Account-Email'] = email

def send(self, method, resource, resource_id=None, data=None, params=None):
if data is None:
Expand All @@ -43,8 +40,9 @@ def send(self, method, resource, resource_id=None, data=None, params=None):
params = {}
if resource_id is not None:
resource = "%s/%s" % (resource, resource_id)
if method != 'get' and data is not None:
self.headers["Content-Type"]: "application/json"
response = requests.request(method, self.base_url + resource,
auth=self.auth,
headers=self.headers,
data=data,
params=params
Expand Down
13 changes: 8 additions & 5 deletions pypingdom/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Client(object):
"""Interact with API and GUI."""

def __init__(self, username, password, apikey, email, api_version='2.0'):
def __init__(self, username:str , password: str, apikey: str, email: str, api_version: str='3.1'):
"""
Initializer.

Expand All @@ -20,7 +20,11 @@ def __init__(self, username, password, apikey, email, api_version='2.0'):
:param email: required for `Multi-User Authentication
<https://www.pingdom.com/resources/api#multi-user+authentication>`_.
"""
self.api = Api(username, password, apikey, email, api_version)
self.username = username
self.password = password
self.apikey = apikey
self.email = email
self.api = Api(apikey, email, api_version)
self.gui = Gui(username, password)
# cache checks
self.checks = {}
Expand All @@ -40,9 +44,8 @@ def get_check(self, name=None, _id=None):
def get_checks(self, filters=None):
if filters is None:
return [c for c in self.checks.values()]

return [c for c in self.checks.values() if len(set(u + filters.get("status", c.status)
for u in filters.get("tags", [])).intersection(set([x['name'] + c.status for x in c.tags])))]
return [c for c in self.checks.values() if not len(set(filters.get("tags", [])).intersection(set([x['name']
for x in c.tags]))) == 0]

def create_check(self, obj):
c = Check(self.api, obj=obj)
Expand Down
2 changes: 1 addition & 1 deletion pypingdom/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __str__(self):

class Gui():

def __init__(self, username, password):
def __init__(self, username: str, password: str):
self.__username = username
self.__password = password
self.session = requests.session()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


setup(name='pypingdom',
version='0.2.2',
version='0.3.0',
description='Client for Pingdom Services',
long_description=README,
author='Paolo Sechi',
Expand Down