summaryrefslogtreecommitdiff
path: root/src/meetingtools/ac
diff options
context:
space:
mode:
authorLeif Johansson <leifj@sunet.se>2011-02-09 11:06:55 +0100
committerLeif Johansson <leifj@sunet.se>2011-02-09 11:06:55 +0100
commitc33af4c971f26fcf3e7eb61a6c3dd8e8af8418a6 (patch)
tree8cbdeaeb48e3bfb5f4dd7d3ab190f8b8be634479 /src/meetingtools/ac
parent5f6b0000b9f611c56f42ff37f32003325c410947 (diff)
restructure
Diffstat (limited to 'src/meetingtools/ac')
-rw-r--r--src/meetingtools/ac/__init__.py12
-rw-r--r--src/meetingtools/ac/api.py87
2 files changed, 99 insertions, 0 deletions
diff --git a/src/meetingtools/ac/__init__.py b/src/meetingtools/ac/__init__.py
new file mode 100644
index 0000000..1250a5f
--- /dev/null
+++ b/src/meetingtools/ac/__init__.py
@@ -0,0 +1,12 @@
+from meetingtools.ac.api import ACPClient
+
+def ac_api_client_cached(request,acc):
+ tag = 'ac_api_client_%s' % acc.name
+ if not request.session.has_key(tag):
+ request.session[tag] = ACPClient(acc.api_url,acc.user,acc.password)
+
+ return request.session[tag]
+
+def ac_api_client(request,acc):
+ return ACPClient(acc.api_url,acc.user,acc.password)
+ \ No newline at end of file
diff --git a/src/meetingtools/ac/api.py b/src/meetingtools/ac/api.py
new file mode 100644
index 0000000..1ed974b
--- /dev/null
+++ b/src/meetingtools/ac/api.py
@@ -0,0 +1,87 @@
+'''
+Created on Jan 31, 2011
+
+@author: leifj
+'''
+
+from lxml import etree
+import httplib2
+from urllib import quote
+import logging
+from pprint import pformat
+
+class ACPException(Exception):
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return etree.tostring(self.value)
+
+class ACPResult():
+
+ def __init__(self,content):
+ self.et = etree.fromstring(content)
+ self.status = self.et.find('status')
+
+ def is_error(self):
+ return self.status.get('code') != 'ok'
+
+ def exception(self):
+ raise ACPException,self.status
+
+ def get_principal(self):
+ return self.et.find('principal')
+
+def _enc(v):
+ ev = v
+ if isinstance(ev,str) or isinstance(ev,unicode):
+ ev = ev.encode('iso-8859-1')
+ return ev
+
+class ACPClient():
+
+ def __init__(self,url,username=None,password=None):
+ self.url = url
+ self.session = None
+ if username and password:
+ self.login(username,password)
+
+ def request(self,method,p={},raise_error=False):
+ url = self.url+"?"+"action=%s" % method
+ if self.session:
+ url = url + "&session=%s" % self.session
+
+ u = []
+ for (k,v) in p.items():
+ if v:
+ kv = "%s=%s" % (k,quote(str(v)))
+ u.append(kv)
+ url = url + "&" + "&".join(u)
+
+ h = httplib2.Http(".cache");
+ logging.debug(url)
+ resp, content = h.request(url, "GET")
+ logging.debug(pformat(resp))
+ logging.debug(pformat(content))
+ if resp.status != 200:
+ raise ACPException,resp.reason
+
+ if resp.has_key('set-cookie'):
+ cookie = resp['set-cookie']
+ if cookie:
+ avp = cookie.split(";")
+ if len(avp) > 0:
+ av = avp[0].split('=')
+ self.session = av[1]
+
+ r = ACPResult(content)
+ if r.is_error() and raise_error:
+ raise r.exception()
+
+ return r;
+
+ def login(self,username,password):
+ result = self.request('login',{'login':username,'password':password})
+ if result.is_error():
+ raise result.exception()
+ \ No newline at end of file