X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f22dda84271714c924c322bafba5e9f184e4fb33..bac1772ab074713e3c50632a4cad3cc1ce50d0ec:/sdk/python/arvados/safeapi.py diff --git a/sdk/python/arvados/safeapi.py b/sdk/python/arvados/safeapi.py index 9737da1031..c6e17cae0b 100644 --- a/sdk/python/arvados/safeapi.py +++ b/sdk/python/arvados/safeapi.py @@ -1,32 +1,47 @@ -import threading -import api -import keep -import config +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + +from __future__ import absolute_import + +from builtins import object import copy +import threading + +import arvados +import arvados.keep as keep +import arvados.config as config class ThreadSafeApiCache(object): - """Threadsafe wrapper for API objects. This stores and returns a different api - object per thread, because httplib2 which underlies apiclient is not - threadsafe. + """Threadsafe wrapper for API objects. + + This stores and returns a different api object per thread, because httplib2 + which underlies apiclient is not threadsafe. + """ - def __init__(self, apiconfig=None, keep_params={}): + def __init__(self, apiconfig=None, keep_params={}, api_params={}): if apiconfig is None: apiconfig = config.settings() self.apiconfig = copy.copy(apiconfig) + self.api_params = api_params self.local = threading.local() + + # Initialize an API object for this thread before creating + # KeepClient, this will report if ARVADOS_API_HOST or + # ARVADOS_API_TOKEN are missing. + self.localapi() + self.keep = keep.KeepClient(api_client=self, **keep_params) def localapi(self): if 'api' not in self.local.__dict__: - self.local.api = api.api('v1', False, apiconfig=self.apiconfig) + self.local.api = arvados.api_from_config('v1', apiconfig=self.apiconfig, + **self.api_params) return self.local.api def __getattr__(self, name): # Proxy nonexistent attributes to the thread-local API client. if name == "api_token": return self.apiconfig['ARVADOS_API_TOKEN'] - try: - return getattr(self.localapi(), name) - except AttributeError: - return super(ThreadSafeApiCache, self).__getattr__(name) + return getattr(self.localapi(), name)