diff options
author | Leif Johansson <leifj@sunet.se> | 2011-02-08 20:46:40 +0100 |
---|---|---|
committer | Leif Johansson <leifj@sunet.se> | 2011-02-08 20:46:40 +0100 |
commit | 0fb514f0a73aef806b826348d043c68534af2745 (patch) | |
tree | bd388ab8e3de4c93a6ba0a5e1b83a4087a14b6e2 /meetingtools/ac | |
parent | a20ff434501a099b259da903d88b47bb77e07469 (diff) |
v1
Diffstat (limited to 'meetingtools/ac')
-rw-r--r-- | meetingtools/ac/__init__.py | 12 | ||||
-rw-r--r-- | meetingtools/ac/api.py | 37 |
2 files changed, 41 insertions, 8 deletions
diff --git a/meetingtools/ac/__init__.py b/meetingtools/ac/__init__.py index e69de29..1250a5f 100644 --- a/meetingtools/ac/__init__.py +++ b/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/meetingtools/ac/api.py b/meetingtools/ac/api.py index 2631987..1ed974b 100644 --- a/meetingtools/ac/api.py +++ b/meetingtools/ac/api.py @@ -6,19 +6,21 @@ Created on Jan 31, 2011 from lxml import etree import httplib2 -from urllib import urlencode +from urllib import quote +import logging +from pprint import pformat class ACPException(Exception): def __init__(self, value): self.value = value def __str__(self): - return repr(self.value) + return etree.tostring(self.value) class ACPResult(): def __init__(self,content): - self.et = etree.parse(content) + self.et = etree.fromstring(content) self.status = self.et.find('status') def is_error(self): @@ -30,6 +32,12 @@ class ACPResult(): 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): @@ -37,15 +45,24 @@ class ACPClient(): self.session = None if username and password: self.login(username,password) - - def request(self,method,p={}): + + def request(self,method,p={},raise_error=False): url = self.url+"?"+"action=%s" % method if self.session: url = url + "&session=%s" % self.session - urlencode(dict([k,v.encode("iso-8859-1")] for (k,v) in p.items())) + + 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 @@ -53,11 +70,15 @@ class ACPClient(): cookie = resp['set-cookie'] if cookie: avp = cookie.split(";") - if avp.len > 0: + if len(avp) > 0: av = avp[0].split('=') self.session = av[1] - return ACPResult(content) + 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}) |