diff --git a/LICENSE.txt b/LICENSE similarity index 96% rename from LICENSE.txt rename to LICENSE index fc557c2..4bb16c5 100644 --- a/LICENSE.txt +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013 Ryn Daniels +Copyright (c) 2013 Katherine Daniels Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/pysensu/pysensu.py b/pysensu/pysensu.py old mode 100644 new mode 100755 index 4fc7614..93e5a29 --- a/pysensu/pysensu.py +++ b/pysensu/pysensu.py @@ -5,36 +5,38 @@ class Pysensu(): - def __init__(self, host, user=None, password=None, port=4567, ssl=False): + def __init__(self, host, user=None, password=None, port=4567, ssl=False, timeout=None): self.host = host self.user = user self.password = password self.port = port self.ssl = ssl + + self.session = requests.Session() + + if (user and not password) or (password and not user): + raise ValueError("Must specify both user and password, or neither") + + self.session.auth = (user, password) self.api_url = self._build_api_url(host, user, password, port, ssl) + self.timeout = timeout def _build_api_url(self, host, user, password, port, ssl): if ssl == True: protocol = 'https' else: protocol = 'http' - if user and password: - credentials = "{}:{}@".format(user, password) - elif (user and not password) or (password and not user): - raise ValueError("Must specify both user and password, or neither") - else: - credentials = "" - return "{}://{}{}:{}".format(protocol, credentials, host, port) + return "{}://{}:{}".format(protocol, host, port) def _api_call(self, url, method, data=None): if method == "post": - return requests.post(url, data=data) + return self.session.post(url, data=data, timeout=self.timeout) elif method == "get": - return requests.get(url, data=data) + return self.session.get(url, data=data, timeout=self.timeout) elif method == "put": - return requests.put(url, data=data) + return self.session.put(url, data=data, timeout=self.timeout) elif method == "delete": - return requests.delete(url, data=data) + return self.session.delete(url, data=data, timeout=self.timeout) else: raise ValueError("Invalid method")