From 3fac1488c8dc89a27d2cf681098e903b28af2599 Mon Sep 17 00:00:00 2001 From: Amy Unruh Date: Tue, 2 Jun 2020 11:41:26 -0700 Subject: [PATCH 1/3] add example of creating client with non-default endpoint --- tables/automl/automl_tables_set_endpoint.py | 30 +++++++++++++++++++ tables/automl/endpoint_test.py | 32 +++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tables/automl/automl_tables_set_endpoint.py create mode 100644 tables/automl/endpoint_test.py diff --git a/tables/automl/automl_tables_set_endpoint.py b/tables/automl/automl_tables_set_endpoint.py new file mode 100644 index 000000000000..fd865c9cac4d --- /dev/null +++ b/tables/automl/automl_tables_set_endpoint.py @@ -0,0 +1,30 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def create_client_with_endpoint(gcp_project_id): + """Create a Tables client with a non-default endpoint.""" + # [START automl_set_endpoint] + from google.cloud import automl_v1beta1 as automl + from google.api_core.client_options import ClientOptions + + # Set the endpoint you want to use via the ClientOptions. + # gcp_project_id = 'YOUR_PROJECT_ID' + client_options = ClientOptions(api_endpoint="eu-automl.googleapis.com:443") + client = automl.TablesClient( + project=gcp_project_id, region="eu", client_options=client_options + ) + # [END automl_set_endpoint] + + return client diff --git a/tables/automl/endpoint_test.py b/tables/automl/endpoint_test.py new file mode 100644 index 000000000000..7bd350e74f08 --- /dev/null +++ b/tables/automl/endpoint_test.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +from google.api_core import exceptions +import pytest + +import automl_tables_set_endpoint +from google.cloud import automl_v1beta1 as automl +from google.api_core.client_options import ClientOptions + +PROJECT = os.environ["GCLOUD_PROJECT"] + + +def test_client_creation(capsys): + client = automl_tables_set_endpoint.create_client_with_endpoint(PROJECT) + print(client.list_datasets()) + out, _ = capsys.readouterr() + assert "GRPCIterator" in out From 2ff04783a247e6bafe5e53dc635a37a38300e5d0 Mon Sep 17 00:00:00 2001 From: Amy Unruh Date: Tue, 2 Jun 2020 13:46:13 -0700 Subject: [PATCH 2/3] more test file cleanup --- tables/automl/endpoint_test.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tables/automl/endpoint_test.py b/tables/automl/endpoint_test.py index 7bd350e74f08..e00c24d9d692 100644 --- a/tables/automl/endpoint_test.py +++ b/tables/automl/endpoint_test.py @@ -15,12 +15,7 @@ # limitations under the License. import os -from google.api_core import exceptions -import pytest - import automl_tables_set_endpoint -from google.cloud import automl_v1beta1 as automl -from google.api_core.client_options import ClientOptions PROJECT = os.environ["GCLOUD_PROJECT"] From 80d2473fdae823b6d3404a39c105de1fb21a5775 Mon Sep 17 00:00:00 2001 From: Amy Unruh Date: Wed, 3 Jun 2020 08:41:48 -0700 Subject: [PATCH 3/3] move connectivity print stmt out of test fn --- tables/automl/automl_tables_set_endpoint.py | 3 +++ tables/automl/endpoint_test.py | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tables/automl/automl_tables_set_endpoint.py b/tables/automl/automl_tables_set_endpoint.py index fd865c9cac4d..d6ab898b4f5d 100644 --- a/tables/automl/automl_tables_set_endpoint.py +++ b/tables/automl/automl_tables_set_endpoint.py @@ -27,4 +27,7 @@ def create_client_with_endpoint(gcp_project_id): ) # [END automl_set_endpoint] + # do simple test to check client connectivity + print(client.list_datasets()) + return client diff --git a/tables/automl/endpoint_test.py b/tables/automl/endpoint_test.py index e00c24d9d692..b556a4c9827c 100644 --- a/tables/automl/endpoint_test.py +++ b/tables/automl/endpoint_test.py @@ -21,7 +21,6 @@ def test_client_creation(capsys): - client = automl_tables_set_endpoint.create_client_with_endpoint(PROJECT) - print(client.list_datasets()) + automl_tables_set_endpoint.create_client_with_endpoint(PROJECT) out, _ = capsys.readouterr() assert "GRPCIterator" in out