Merge branch 'master' into 7167-keep-rsync-test-setup
[arvados.git] / sdk / python / arvados / safeapi.py
1 import threading
2 import api
3 import keep
4 import config
5 import copy
6
7 class ThreadSafeApiCache(object):
8     """Threadsafe wrapper for API objects.
9
10     This stores and returns a different api object per thread, because httplib2
11     which underlies apiclient is not threadsafe.
12
13     """
14
15     def __init__(self, apiconfig=None, keep_params={}):
16         if apiconfig is None:
17             apiconfig = config.settings()
18         self.apiconfig = copy.copy(apiconfig)
19         self.local = threading.local()
20         self.keep = keep.KeepClient(api_client=self, **keep_params)
21
22     def localapi(self):
23         if 'api' not in self.local.__dict__:
24             self.local.api = api.api_from_config('v1', apiconfig=self.apiconfig)
25         return self.local.api
26
27     def __getattr__(self, name):
28         # Proxy nonexistent attributes to the thread-local API client.
29         if name == "api_token":
30             return self.apiconfig['ARVADOS_API_TOKEN']
31         return getattr(self.localapi(), name)