summaryrefslogtreecommitdiff
path: root/src/meetingtools/ac/__init__.py
blob: b13823747ef98823c4cc1c575b5b605bd4681897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from meetingtools.ac.api import ACPClient
import time

def ac_api_client_cache(request,acc):
    tag = 'ac_api_client_%s' % acc.name
    if not request.session.has_key(tag):
        request.session[tag] = ACPClientWrapper(acc)
        
    return request.session[tag]

def ac_api_client_nocache(request,acc):
    return ACPClientWrapper(acc)

ac_api_client = ac_api_client_cache

def ac_api(request,acc):
    return ACPClient(acc.api_url,acc.user,acc.password)
    

MAXCALLS = 10    
MAXIDLE = 10

class ACPClientWrapper(object):
    
    def __init__(self,acc):
        self.acc = acc
        self._delegate = None
        self.ncalls = 0
        self.lastcall = time.time()
        
    def invalidate(self):
        self._delegate = None
        
    def client_factory(self):
        now = time.time()
        if self.ncalls > MAXCALLS or now - self.lastcall > MAXIDLE or not self._delegate:
            self._delegate = ACPClient(self.acc.api_url,self.acc.user,self.acc.password)
            self.ncalls = 0
        self.ncalls += 1
        self.lastcall = now
        return self._delegate
        
    def __getattr__(self,name):
        client = self.client_factory()
        return getattr(client,name)