| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 238d9a9 commit f108dab
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,14 +186,8 @@ def patched_init(self, *args, **kwargs): | |||
| 186 | 186 | if specified_credentials is None: | |
| 187 | 187 | Log.info("No credentials specified, using KaggleKernelCredentials.") | |
| 188 | 188 | kwargs['credentials'] = kaggle_kernel_credentials | |
| 189 | - | ||
| 190 | - # TODO(vimota): Remove the exclusion of TablesClient once | ||
| 191 | - # the client has fixed the error: | ||
| 192 | - # `multiple values for keyword argument 'client_info'`` | ||
| 193 | - from google.cloud import automl_v1beta1 | ||
| 194 | - if (client_klass != automl_v1beta1.TablesClient): | ||
| 195 | - kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info')) | ||
| 196 | 189 | ||
| 190 | + kwargs['client_info'] = set_kaggle_user_agent(kwargs.get('client_info')) | ||
| 197 | 191 | ||
| 198 | 192 | return client_init(self, *args, **kwargs) | |
| 199 | 193 | ||
@@ -227,30 +221,35 @@ def init_gcs(): | |||
| 227 | 221 | ||
| 228 | 222 | def init_automl(): | |
| 229 | 223 | is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os.environ | |
| 230 | - from google.cloud import automl_v1beta1 as automl | ||
| 224 | + from google.cloud import automl, automl_v1beta1 | ||
| 231 | 225 | if not is_user_secrets_token_set: | |
| 232 | - return automl | ||
| 226 | + return | ||
| 233 | 227 | ||
| 234 | 228 | from kaggle_gcp import get_integrations | |
| 235 | 229 | if not get_integrations().has_automl(): | |
| 236 | - return automl | ||
| 230 | + return | ||
| 237 | 231 | ||
| 238 | 232 | from kaggle_secrets import GcpTarget | |
| 239 | 233 | from kaggle_gcp import KaggleKernelCredentials | |
| 240 | 234 | kaggle_kernel_credentials = KaggleKernelCredentials(target=GcpTarget.AUTOML) | |
| 241 | 235 | ||
| 242 | - # The AutoML client library exposes 4 different client classes (AutoMlClient, | ||
| 243 | - # TablesClient, PredictionServiceClient and GcsClient), so patch each of them. | ||
| 244 | - # The same KaggleKernelCredentials are passed to all of them. | ||
| 236 | + # Patch the 2 GA clients: AutoMlClient and PreditionServiceClient | ||
| 245 | 237 | monkeypatch_client(automl.AutoMlClient, kaggle_kernel_credentials) | |
| 246 | - monkeypatch_client(automl.TablesClient, kaggle_kernel_credentials) | ||
| 247 | 238 | monkeypatch_client(automl.PredictionServiceClient, kaggle_kernel_credentials) | |
| 248 | - # TODO(markcollins): The GcsClient in the AutoML client library version | ||
| 249 | - # 0.5.0 doesn't handle credentials properly. I wrote PR: | ||
| 250 | - # https://github.com/googleapis/google-cloud-python/pull/9299 | ||
| 251 | - # to address this issue. Add patching for GcsClient when we get a version of | ||
| 252 | - # the library that includes the fixes. | ||
| 253 | - return automl | ||
| 239 | + | ||
| 240 | + # The AutoML client library exposes 3 different client classes (AutoMlClient, | ||
| 241 | + # TablesClient, PredictionServiceClient), so patch each of them. | ||
| 242 | + # The same KaggleKernelCredentials are passed to all of them. | ||
| 243 | + # The GcsClient class is only used internally by TablesClient. | ||
| 244 | + | ||
| 245 | + # The beta version of the clients that are now GA are included here for now. | ||
| 246 | + # They are deprecated and will be removed by 1 May 2020. | ||
| 247 | + monkeypatch_client(automl_v1beta1.AutoMlClient, kaggle_kernel_credentials) | ||
| 248 | + monkeypatch_client(automl_v1beta1.PredictionServiceClient, kaggle_kernel_credentials) | ||
| 249 | + | ||
| 250 | + # The TablesClient is still in beta, so this will not be deprecated until | ||
| 251 | + # the TablesClient is GA. | ||
| 252 | + monkeypatch_client(automl_v1beta1.TablesClient, kaggle_kernel_credentials) | ||
| 254 | 253 | ||
| 255 | 254 | def init(): | |
| 256 | 255 | init_bigquery() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,19 +4,14 @@ | |||
| 4 | 4 | ||
| 5 | 5 | from kaggle_gcp import KaggleKernelCredentials, init_automl | |
| 6 | 6 | from test.support import EnvironmentVarGuard | |
| 7 | - from google.cloud import storage, automl_v1beta1 as automl | ||
| 8 | - from packaging.version import parse | ||
| 7 | + from google.cloud import storage, automl_v1beta1, automl | ||
| 9 | 8 | ||
| 10 | 9 | def _make_credentials(): | |
| 11 | 10 | import google.auth.credentials | |
| 12 | 11 | return Mock(spec=google.auth.credentials.Credentials) | |
| 13 | 12 | ||
| 14 | 13 | class TestAutoMl(unittest.TestCase): | |
| 15 | 14 | ||
| 16 | - def test_version(self): | ||
| 17 | - self.assertIsNotNone(automl.auto_ml_client._GAPIC_LIBRARY_VERSION) | ||
| 18 | - self.assertGreaterEqual(parse(automl.auto_ml_client._GAPIC_LIBRARY_VERSION), parse("0.5.0")) | ||
| 19 | - | ||
| 20 | 15 | class FakeClient: | |
| 21 | 16 | def __init__(self, credentials=None, client_info=None, **kwargs): | |
| 22 | 17 | self.credentials = credentials | |
@@ -27,7 +22,7 @@ def __init__(self, user_agent): | |||
| 27 | 22 | if (client_info is not None): | |
| 28 | 23 | self._connection = FakeConnection(client_info.user_agent) | |
| 29 | 24 | ||
| 30 | - @patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient) | ||
| 25 | + @patch("google.cloud.automl.AutoMlClient", new=FakeClient) | ||
| 31 | 26 | def test_user_provided_credentials(self): | |
| 32 | 27 | credentials = _make_credentials() | |
| 33 | 28 | env = EnvironmentVarGuard() | |
@@ -42,10 +37,10 @@ def test_user_provided_credentials(self): | |||
| 42 | 37 | def test_tables_gcs_client(self): | |
| 43 | 38 | # The GcsClient can't currently be monkeypatched for default | |
| 44 | 39 | # credentials because it requires a project which can't be set. | |
| 45 | - # Verify that creating an automl.GcsClient given an actual | ||
| 40 | + # Verify that creating an automl_v1beta1.GcsClient given an actual | ||
| 46 | 41 | # storage.Client sets the client properly. | |
| 47 | 42 | gcs_client = storage.Client(project="xyz", credentials=_make_credentials()) | |
| 48 | - tables_gcs_client = automl.GcsClient(client=gcs_client) | ||
| 43 | + tables_gcs_client = automl_v1beta1.GcsClient(client=gcs_client) | ||
| 49 | 44 | self.assertIs(tables_gcs_client.client, gcs_client) | |
| 50 | 45 | ||
| 51 | 46 | @patch("google.cloud.automl_v1beta1.gapic.auto_ml_client.AutoMlClient", new=FakeClient) | |
@@ -56,10 +51,10 @@ def test_tables_client_credentials(self): | |||
| 56 | 51 | env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | |
| 57 | 52 | with env: | |
| 58 | 53 | init_automl() | |
| 59 | - tables_client = automl.TablesClient(credentials=credentials) | ||
| 54 | + tables_client = automl_v1beta1.TablesClient(credentials=credentials) | ||
| 60 | 55 | self.assertEqual(tables_client.auto_ml_client.credentials, credentials) | |
| 61 | 56 | ||
| 62 | - @patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient) | ||
| 57 | + @patch("google.cloud.automl.AutoMlClient", new=FakeClient) | ||
| 63 | 58 | def test_default_credentials_automl_client(self): | |
| 64 | 59 | env = EnvironmentVarGuard() | |
| 65 | 60 | env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | |
@@ -71,18 +66,31 @@ def test_default_credentials_automl_client(self): | |||
| 71 | 66 | self.assertIsInstance(automl_client.credentials, KaggleKernelCredentials) | |
| 72 | 67 | self.assertTrue(automl_client._connection.user_agent.startswith("kaggle-gcp-client/1.0")) | |
| 73 | 68 | ||
| 69 | + @patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient) | ||
| 70 | + def test_default_credentials_automl_v1beta1_client(self): | ||
| 71 | + env = EnvironmentVarGuard() | ||
| 72 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 73 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | ||
| 74 | + with env: | ||
| 75 | + init_automl() | ||
| 76 | + automl_client = automl_v1beta1.AutoMlClient() | ||
| 77 | + self.assertIsNotNone(automl_client.credentials) | ||
| 78 | + self.assertIsInstance(automl_client.credentials, KaggleKernelCredentials) | ||
| 79 | + self.assertTrue(automl_client._connection.user_agent.startswith("kaggle-gcp-client/1.0")) | ||
| 80 | + | ||
| 74 | 81 | @patch("google.cloud.automl_v1beta1.TablesClient", new=FakeClient) | |
| 75 | 82 | def test_default_credentials_tables_client(self): | |
| 76 | 83 | env = EnvironmentVarGuard() | |
| 77 | 84 | env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | |
| 78 | 85 | env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | |
| 79 | 86 | with env: | |
| 80 | 87 | init_automl() | |
| 81 | - tables_client = automl.TablesClient() | ||
| 88 | + tables_client = automl_v1beta1.TablesClient() | ||
| 82 | 89 | self.assertIsNotNone(tables_client.credentials) | |
| 83 | 90 | self.assertIsInstance(tables_client.credentials, KaggleKernelCredentials) | |
| 91 | + self.assertTrue(tables_client._connection.user_agent.startswith("kaggle-gcp-client/1.0")) | ||
| 84 | 92 | ||
| 85 | - @patch("google.cloud.automl_v1beta1.PredictionServiceClient", new=FakeClient) | ||
| 93 | + @patch("google.cloud.automl.PredictionServiceClient", new=FakeClient) | ||
| 86 | 94 | def test_default_credentials_prediction_client(self): | |
| 87 | 95 | env = EnvironmentVarGuard() | |
| 88 | 96 | env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | |
@@ -91,6 +99,18 @@ def test_default_credentials_prediction_client(self): | |||
| 91 | 99 | prediction_client = automl.PredictionServiceClient() | |
| 92 | 100 | self.assertIsNotNone(prediction_client.credentials) | |
| 93 | 101 | self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials) | |
| 102 | + self.assertTrue(prediction_client._connection.user_agent.startswith("kaggle-gcp-client/1.0")) | ||
| 103 | + | ||
| 104 | + @patch("google.cloud.automl_v1beta1.PredictionServiceClient", new=FakeClient) | ||
| 105 | + def test_default_credentials_prediction_v1beta1_client(self): | ||
| 106 | + env = EnvironmentVarGuard() | ||
| 107 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 108 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'AUTOML') | ||
| 109 | + with env: | ||
| 110 | + prediction_client = automl_v1beta1.PredictionServiceClient() | ||
| 111 | + self.assertIsNotNone(prediction_client.credentials) | ||
| 112 | + self.assertIsInstance(prediction_client.credentials, KaggleKernelCredentials) | ||
| 113 | + self.assertTrue(prediction_client._connection.user_agent.startswith("kaggle-gcp-client/1.0")) | ||
| 94 | 114 | ||
| 95 | 115 | def test_monkeypatching_idempotent(self): | |
| 96 | 116 | env = EnvironmentVarGuard() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments