summaryrefslogtreecommitdiffstats
path: root/pykolab/wap_client/__init__.py
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2015-05-20 10:54:29 +0200
committerThomas Bruederli <bruederli@kolabsys.com>2015-05-20 10:54:29 +0200
commit8755e0d1f12e96911859d9057424d1d9045a2e46 (patch)
tree29e2b94bcef43cfa0ad5118bea9eee8610142653 /pykolab/wap_client/__init__.py
parent44db93b6c3bd4f93916d39e8a29e073368d1edc0 (diff)
downloadpykolab-8755e0d1f12e96911859d9057424d1d9045a2e46.tar.gz
Catch webadmin API connection errors (timed out perhaps) and try again with a new connection
Diffstat (limited to 'pykolab/wap_client/__init__.py')
-rw-r--r--pykolab/wap_client/__init__.py34
1 files changed, 29 insertions, 5 deletions
diff --git a/pykolab/wap_client/__init__.py b/pykolab/wap_client/__init__.py
index e74ed59..ed96292 100644
--- a/pykolab/wap_client/__init__.py
+++ b/pykolab/wap_client/__init__.py
@@ -98,10 +98,22 @@ def connect(uri=None):
conn = httplib.HTTPSConnection(API_HOSTNAME, API_PORT)
else:
conn = httplib.HTTPConnection(API_HOSTNAME, API_PORT)
+
conn.connect()
return conn
+def disconnect(quit=False):
+ global conn, session_id
+
+ if quit and session_id:
+ request('GET', 'system.quit')
+ session_id = None
+
+ if conn:
+ conn.close()
+ conn = None
+
def domain_add(domain, aliases=[]):
dna = conf.get('ldap', 'domain_name_attribute')
@@ -380,12 +392,13 @@ def request(method, api_uri, get=None, post=None, headers={}):
else:
return False
-def request_raw(method, api_uri, get=None, post=None, headers={}):
+def request_raw(method, api_uri, get=None, post=None, headers={}, isretry=False):
global session_id
if not session_id == None:
headers["X-Session-Token"] = session_id
+ reconnect = False
conn = connect()
if conf.debuglevel > 8:
@@ -398,13 +411,24 @@ def request_raw(method, api_uri, get=None, post=None, headers={}):
log.debug(_("Requesting %r with params %r") % ("%s/%s" % (API_BASE,api_uri), (get, post)), level=8)
- conn.request(method.upper(), "%s/%s%s" % (API_BASE, api_uri, _get), post, headers)
+ try:
+ conn.request(method.upper(), "%s/%s%s" % (API_BASE, api_uri, _get), post, headers)
+
+ response = conn.getresponse()
+ data = response.read()
- response = conn.getresponse()
+ log.debug(_("Got response: %r") % (data), level=8)
- data = response.read()
+ except (httplib.BadStatusLine, httplib.CannotSendRequest) as e:
+ if isretry:
+ raise e
+ log.info(_("Connection error: %r; re-connecting..."), e)
+ reconnect = True
- log.debug(_("Got response: %r") % (data), level=8)
+ # retry with a new connection
+ if reconnect:
+ disconnect()
+ return request_raw(method, api_uri, get, post, headers, True)
try:
response_data = json.loads(data)