1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
7 from builtins import object
12 import arvados.keep as keep
13 import arvados.config as config
15 class ThreadSafeApiCache(object):
16 """Threadsafe wrapper for API objects.
18 This stores and returns a different api object per thread, because httplib2
19 which underlies apiclient is not threadsafe.
23 def __init__(self, apiconfig=None, keep_params={}, api_params={}):
25 apiconfig = config.settings()
26 self.apiconfig = copy.copy(apiconfig)
27 self.api_params = api_params
28 self.local = threading.local()
30 # Initialize an API object for this thread before creating
31 # KeepClient, this will report if ARVADOS_API_HOST or
32 # ARVADOS_API_TOKEN are missing.
35 self.keep = keep.KeepClient(api_client=self, **keep_params)
38 if 'api' not in self.local.__dict__:
39 self.local.api = arvados.api_from_config('v1', apiconfig=self.apiconfig,
43 def __getattr__(self, name):
44 # Proxy nonexistent attributes to the thread-local API client.
45 if name == "api_token":
46 return self.apiconfig['ARVADOS_API_TOKEN']
47 return getattr(self.localapi(), name)