4823: Add Collection.copy tests
[arvados.git] / sdk / python / arvados / safeapi.py
1 import threading
2 import api
3 import keep
4 import config
5
6 class SafeApi(object):
7     """Threadsafe wrapper for API object.  This stores and returns a different api
8     object per thread, because httplib2 which underlies apiclient is not
9     threadsafe.
10     """
11
12     def __init__(self, apiconfig=None, keep_params={}):
13         if not apiconfig:
14             apiconfig = config
15         self.host = apiconfig.get('ARVADOS_API_HOST')
16         self.api_token = apiconfig.get('ARVADOS_API_TOKEN')
17         self.insecure = apiconfig.flag_is_true('ARVADOS_API_HOST_INSECURE')
18         self.local = threading.local()
19         self.keep = keep.KeepClient(api_client=self, **keep_params)
20
21     def localapi(self):
22         if 'api' not in self.local.__dict__:
23             self.local.api = api.api('v1', False, self.host,
24                                          self.api_token, self.insecure)
25         return self.local.api
26
27     def __getattr__(self, name):
28         # Proxy nonexistent attributes to the thread-local API client.
29         try:
30             return getattr(self.localapi(), name)
31         except AttributeError:
32             return super(SafeApi, self).__getattr__(name)