2800: Delay API client creation in Python CollectionReader.
authorBrett Smith <brett@curoverse.com>
Wed, 20 Aug 2014 19:12:20 +0000 (15:12 -0400)
committerBrett Smith <brett@curoverse.com>
Wed, 20 Aug 2014 19:12:35 +0000 (15:12 -0400)
This more closely matches prior behavior, and is necessary to make the
Keep tests pass.  See included comments for detailed rationale.
Refs #2800.

sdk/python/arvados/collection.py

index 40b5889c317eef9a59c1d9acea51b93360847baa..400a88c5f24b8da42f8fe2d27e6eb036746f6894 100644 (file)
@@ -116,17 +116,26 @@ class CollectionReader(object):
         if self._streams is not None:
             return
         if not self._manifest_text:
-            if self._api_client is None:
-                self._api_client = arvados.api('v1')
-            if self._keep_client is None:
-                self._keep_client = KeepClient(api_client=self._api_client)
             try:
+                # As in KeepClient itself, we must wait until the last possible
+                # moment to instantiate an API client, in order to avoid
+                # tripping up clients that don't have access to an API server.
+                # If we do build one, make sure our Keep client uses it.
+                # If instantiation fails, we'll fall back to the except clause,
+                # just like any other Collection lookup failure.
+                if self._api_client is None:
+                    self._api_client = arvados.api('v1')
+                    self._keep_client = KeepClient(api_client=self._api_client)
+                if self._keep_client is None:
+                    self._keep_client = KeepClient(api_client=self._api_client)
                 c = self._api_client.collections().get(
                     uuid=self._manifest_locator).execute()
                 self._manifest_text = c['manifest_text']
             except Exception as e:
                 _logger.warning("API lookup failed for collection %s (%s: %s)",
                                 self._manifest_locator, type(e), str(e))
+                if self._keep_client is None:
+                    self._keep_client = KeepClient(api_client=self._api_client)
                 self._manifest_text = self._keep_client.get(self._manifest_locator)
         self._streams = []
         for stream_line in self._manifest_text.split("\n"):