Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 50a37af

Browse files
feat: Add REST Interceptors which support reading metadata (#884)
* feat: Add REST Interceptors which support reading metadata feat: Add support for reading selective GAPIC generation methods from service YAML chore: Update gapic-generator-python to v1.22.0 PiperOrigin-RevId: 724026024 Source-Link: googleapis/googleapis@ad99638 Source-Link: googleapis/googleapis-gen@e291c4d Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
1 parent fa414ee commit 50a37af

File tree

12 files changed

+402
-2
lines changed

12 files changed

+402
-2
lines changed

google/cloud/bigquery_storage_v1/services/big_query_read/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -531,6 +533,33 @@ def _validate_universe_domain(self):
531533
# NOTE (b/349488459): universe validation is disabled until further notice.
532534
return True
533535

536+
def _add_cred_info_for_auth_errors(
537+
self, error: core_exceptions.GoogleAPICallError
538+
) -> None:
539+
"""Adds credential info string to error details for 401/403/404 errors.
540+
541+
Args:
542+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
543+
"""
544+
if error.code not in [
545+
HTTPStatus.UNAUTHORIZED,
546+
HTTPStatus.FORBIDDEN,
547+
HTTPStatus.NOT_FOUND,
548+
]:
549+
return
550+
551+
cred = self._transport._credentials
552+
553+
# get_cred_info is only available in google-auth>=2.35.0
554+
if not hasattr(cred, "get_cred_info"):
555+
return
556+
557+
# ignore the type check since pypy test fails when get_cred_info
558+
# is not available
559+
cred_info = cred.get_cred_info() # type: ignore
560+
if cred_info and hasattr(error._details, "append"):
561+
error._details.append(json.dumps(cred_info))
562+
534563
@property
535564
def api_endpoint(self):
536565
"""Return the API endpoint used by the client instance.

google/cloud/bigquery_storage_v1/services/big_query_write/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -515,6 +517,33 @@ def _validate_universe_domain(self):
515517
# NOTE (b/349488459): universe validation is disabled until further notice.
516518
return True
517519

520+
def _add_cred_info_for_auth_errors(
521+
self, error: core_exceptions.GoogleAPICallError
522+
) -> None:
523+
"""Adds credential info string to error details for 401/403/404 errors.
524+
525+
Args:
526+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
527+
"""
528+
if error.code not in [
529+
HTTPStatus.UNAUTHORIZED,
530+
HTTPStatus.FORBIDDEN,
531+
HTTPStatus.NOT_FOUND,
532+
]:
533+
return
534+
535+
cred = self._transport._credentials
536+
537+
# get_cred_info is only available in google-auth>=2.35.0
538+
if not hasattr(cred, "get_cred_info"):
539+
return
540+
541+
# ignore the type check since pypy test fails when get_cred_info
542+
# is not available
543+
cred_info = cred.get_cred_info() # type: ignore
544+
if cred_info and hasattr(error._details, "append"):
545+
error._details.append(json.dumps(cred_info))
546+
518547
@property
519548
def api_endpoint(self):
520549
"""Return the API endpoint used by the client instance.

google/cloud/bigquery_storage_v1alpha/services/metastore_partition_service/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -513,6 +515,33 @@ def _validate_universe_domain(self):
513515
# NOTE (b/349488459): universe validation is disabled until further notice.
514516
return True
515517

518+
def _add_cred_info_for_auth_errors(
519+
self, error: core_exceptions.GoogleAPICallError
520+
) -> None:
521+
"""Adds credential info string to error details for 401/403/404 errors.
522+
523+
Args:
524+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
525+
"""
526+
if error.code not in [
527+
HTTPStatus.UNAUTHORIZED,
528+
HTTPStatus.FORBIDDEN,
529+
HTTPStatus.NOT_FOUND,
530+
]:
531+
return
532+
533+
cred = self._transport._credentials
534+
535+
# get_cred_info is only available in google-auth>=2.35.0
536+
if not hasattr(cred, "get_cred_info"):
537+
return
538+
539+
# ignore the type check since pypy test fails when get_cred_info
540+
# is not available
541+
cred_info = cred.get_cred_info() # type: ignore
542+
if cred_info and hasattr(error._details, "append"):
543+
error._details.append(json.dumps(cred_info))
544+
516545
@property
517546
def api_endpoint(self):
518547
"""Return the API endpoint used by the client instance.

google/cloud/bigquery_storage_v1beta2/services/big_query_read/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -534,6 +536,33 @@ def _validate_universe_domain(self):
534536
# NOTE (b/349488459): universe validation is disabled until further notice.
535537
return True
536538

539+
def _add_cred_info_for_auth_errors(
540+
self, error: core_exceptions.GoogleAPICallError
541+
) -> None:
542+
"""Adds credential info string to error details for 401/403/404 errors.
543+
544+
Args:
545+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
546+
"""
547+
if error.code not in [
548+
HTTPStatus.UNAUTHORIZED,
549+
HTTPStatus.FORBIDDEN,
550+
HTTPStatus.NOT_FOUND,
551+
]:
552+
return
553+
554+
cred = self._transport._credentials
555+
556+
# get_cred_info is only available in google-auth>=2.35.0
557+
if not hasattr(cred, "get_cred_info"):
558+
return
559+
560+
# ignore the type check since pypy test fails when get_cred_info
561+
# is not available
562+
cred_info = cred.get_cred_info() # type: ignore
563+
if cred_info and hasattr(error._details, "append"):
564+
error._details.append(json.dumps(cred_info))
565+
537566
@property
538567
def api_endpoint(self):
539568
"""Return the API endpoint used by the client instance.

google/cloud/bigquery_storage_v1beta2/services/big_query_write/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -516,6 +518,33 @@ def _validate_universe_domain(self):
516518
# NOTE (b/349488459): universe validation is disabled until further notice.
517519
return True
518520

521+
def _add_cred_info_for_auth_errors(
522+
self, error: core_exceptions.GoogleAPICallError
523+
) -> None:
524+
"""Adds credential info string to error details for 401/403/404 errors.
525+
526+
Args:
527+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
528+
"""
529+
if error.code not in [
530+
HTTPStatus.UNAUTHORIZED,
531+
HTTPStatus.FORBIDDEN,
532+
HTTPStatus.NOT_FOUND,
533+
]:
534+
return
535+
536+
cred = self._transport._credentials
537+
538+
# get_cred_info is only available in google-auth>=2.35.0
539+
if not hasattr(cred, "get_cred_info"):
540+
return
541+
542+
# ignore the type check since pypy test fails when get_cred_info
543+
# is not available
544+
cred_info = cred.get_cred_info() # type: ignore
545+
if cred_info and hasattr(error._details, "append"):
546+
error._details.append(json.dumps(cred_info))
547+
519548
@property
520549
def api_endpoint(self):
521550
"""Return the API endpoint used by the client instance.

samples/generated_samples/snippet_metadata_google.cloud.bigquery.storage.v1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
],
99
"language": "PYTHON",
1010
"name": "google-cloud-bigquery-storage",
11-
"version": "2.28.0"
11+
"version": "0.1.0"
1212
},
1313
"snippets": [
1414
{

samples/generated_samples/snippet_metadata_google.cloud.bigquery.storage.v1beta2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
],
99
"language": "PYTHON",
1010
"name": "google-cloud-bigquery-storage",
11-
"version": "2.28.0"
11+
"version": "0.1.0"
1212
},
1313
"snippets": [
1414
{

tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
except ImportError: # pragma: NO COVER
2323
import mock
2424

25+
import json
2526
import math
2627

2728
from google.api_core import api_core_version
@@ -55,6 +56,13 @@
5556
)
5657
from google.cloud.bigquery_storage_v1.types import arrow, avro, storage, stream
5758

59+
CRED_INFO_JSON = {
60+
"credential_source": "/path/to/file",
61+
"credential_type": "service account credentials",
62+
"principal": "service-account@example.com",
63+
}
64+
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
65+
5866

5967
async def mock_async_gen(data, chunk_size=1):
6068
for i in range(0, len(data)): # pragma: NO COVER
@@ -298,6 +306,49 @@ def test__get_universe_domain():
298306
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
299307

300308

309+
@pytest.mark.parametrize(
310+
"error_code,cred_info_json,show_cred_info",
311+
[
312+
(401, CRED_INFO_JSON, True),
313+
(403, CRED_INFO_JSON, True),
314+
(404, CRED_INFO_JSON, True),
315+
(500, CRED_INFO_JSON, False),
316+
(401, None, False),
317+
(403, None, False),
318+
(404, None, False),
319+
(500, None, False),
320+
],
321+
)
322+
def test__add_cred_info_for_auth_errors(error_code, cred_info_json, show_cred_info):
323+
cred = mock.Mock(["get_cred_info"])
324+
cred.get_cred_info = mock.Mock(return_value=cred_info_json)
325+
client = BigQueryReadClient(credentials=cred)
326+
client._transport._credentials = cred
327+
328+
error = core_exceptions.GoogleAPICallError("message", details=["foo"])
329+
error.code = error_code
330+
331+
client._add_cred_info_for_auth_errors(error)
332+
if show_cred_info:
333+
assert error.details == ["foo", CRED_INFO_STRING]
334+
else:
335+
assert error.details == ["foo"]
336+
337+
338+
@pytest.mark.parametrize("error_code", [401, 403, 404, 500])
339+
def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
340+
cred = mock.Mock([])
341+
assert not hasattr(cred, "get_cred_info")
342+
client = BigQueryReadClient(credentials=cred)
343+
client._transport._credentials = cred
344+
345+
error = core_exceptions.GoogleAPICallError("message", details=[])
346+
error.code = error_code
347+
348+
client._add_cred_info_for_auth_errors(error)
349+
assert error.details == []
350+
351+
301352
@pytest.mark.parametrize(
302353
"client_class,transport_name",
303354
[

tests/unit/gapic/bigquery_storage_v1/test_big_query_write.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
except ImportError: # pragma: NO COVER
2323
import mock
2424

25+
import json
2526
import math
2627

2728
from google.api_core import api_core_version
@@ -64,6 +65,13 @@
6465
table,
6566
)
6667

68+
CRED_INFO_JSON = {
69+
"credential_source": "/path/to/file",
70+
"credential_type": "service account credentials",
71+
"principal": "service-account@example.com",
72+
}
73+
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON)
74+
6775

6876
async def mock_async_gen(data, chunk_size=1):
6977
for i in range(0, len(data)): # pragma: NO COVER
@@ -318,6 +326,49 @@ def test__get_universe_domain():
318326
assert str(excinfo.value) == "Universe Domain cannot be an empty string."
319327

320328

329+
@pytest.mark.parametrize(
330+
"error_code,cred_info_json,show_cred_info",
331+
[
332+
(401, CRED_INFO_JSON, True),
333+
(403, CRED_INFO_JSON, True),
334+
(404, CRED_INFO_JSON, True),
335+
(500, CRED_INFO_JSON, False),
336+
(401, None, False),
337+
(403, None, False),
338+
(404, None, False),
339+
(500, None, False),
340+
],
341+
)
342+
def test__add_cred_info_for_auth_errors(error_code, cred_info_json, show_cred_info):
343+
cred = mock.Mock(["get_cred_info"])
344+
cred.get_cred_info = mock.Mock(return_value=cred_info_json)
345+
client = BigQueryWriteClient(credentials=cred)
346+
client._transport._credentials = cred
347+
348+
error = core_exceptions.GoogleAPICallError("message", details=["foo"])
349+
error.code = error_code
350+
351+
client._add_cred_info_for_auth_errors(error)
352+
if show_cred_info:
353+
assert error.details == ["foo", CRED_INFO_STRING]
354+
else:
355+
assert error.details == ["foo"]
356+
357+
358+
@pytest.mark.parametrize("error_code", [401, 403, 404, 500])
359+
def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code):
360+
cred = mock.Mock([])
361+
assert not hasattr(cred, "get_cred_info")
362+
client = BigQueryWriteClient(credentials=cred)
363+
client._transport._credentials = cred
364+
365+
error = core_exceptions.GoogleAPICallError("message", details=[])
366+
error.code = error_code
367+
368+
client._add_cred_info_for_auth_errors(error)
369+
assert error.details == []
370+
371+
321372
@pytest.mark.parametrize(
322373
"client_class,transport_name",
323374
[

0 commit comments

Comments
 (0)