Skip to content

Commit 88f5f3e

Browse files
feat: Add REST Interceptors which support reading metadata (#542)
* 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>
1 parent e8eb848 commit 88f5f3e

File tree

12 files changed

+642
-108
lines changed

12 files changed

+642
-108
lines changed

google/cloud/pubsublite_v1/services/admin_service/client.py

Lines changed: 55 additions & 18 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
@@ -533,6 +535,33 @@ def _validate_universe_domain(self):
533535
# NOTE (b/349488459): universe validation is disabled until further notice.
534536
return True
535537

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

2931-
# Send the request.
2932-
response = rpc(
2933-
request,
2934-
retry=retry,
2935-
timeout=timeout,
2936-
metadata=metadata,
2937-
)
2960+
try:
2961+
# Send the request.
2962+
response = rpc(
2963+
request,
2964+
retry=retry,
2965+
timeout=timeout,
2966+
metadata=metadata,
2967+
)
29382968

2939-
# Done; return the response.
2940-
return response
2969+
# Done; return the response.
2970+
return response
2971+
except core_exceptions.GoogleAPICallError as e:
2972+
self._add_cred_info_for_auth_errors(e)
2973+
raise e
29412974

29422975
def get_operation(
29432976
self,
@@ -2983,16 +3016,20 @@ def get_operation(
29833016
# Validate the universe domain.
29843017
self._validate_universe_domain()
29853018

2986-
# Send the request.
2987-
response = rpc(
2988-
request,
2989-
retry=retry,
2990-
timeout=timeout,
2991-
metadata=metadata,
2992-
)
3019+
try:
3020+
# Send the request.
3021+
response = rpc(
3022+
request,
3023+
retry=retry,
3024+
timeout=timeout,
3025+
metadata=metadata,
3026+
)
29933027

2994-
# Done; return the response.
2995-
return response
3028+
# Done; return the response.
3029+
return response
3030+
except core_exceptions.GoogleAPICallError as e:
3031+
self._add_cred_info_for_auth_errors(e)
3032+
raise e
29963033

29973034
def delete_operation(
29983035
self,

google/cloud/pubsublite_v1/services/cursor_service/client.py

Lines changed: 55 additions & 18 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
@@ -486,6 +488,33 @@ def _validate_universe_domain(self):
486488
# NOTE (b/349488459): universe validation is disabled until further notice.
487489
return True
488490

491+
def _add_cred_info_for_auth_errors(
492+
self, error: core_exceptions.GoogleAPICallError
493+
) -> None:
494+
"""Adds credential info string to error details for 401/403/404 errors.
495+
496+
Args:
497+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
498+
"""
499+
if error.code not in [
500+
HTTPStatus.UNAUTHORIZED,
501+
HTTPStatus.FORBIDDEN,
502+
HTTPStatus.NOT_FOUND,
503+
]:
504+
return
505+
506+
cred = self._transport._credentials
507+
508+
# get_cred_info is only available in google-auth>=2.35.0
509+
if not hasattr(cred, "get_cred_info"):
510+
return
511+
512+
# ignore the type check since pypy test fails when get_cred_info
513+
# is not available
514+
cred_info = cred.get_cred_info() # type: ignore
515+
if cred_info and hasattr(error._details, "append"):
516+
error._details.append(json.dumps(cred_info))
517+
489518
@property
490519
def api_endpoint(self):
491520
"""Return the API endpoint used by the client instance.
@@ -1026,16 +1055,20 @@ def list_operations(
10261055
# Validate the universe domain.
10271056
self._validate_universe_domain()
10281057

1029-
# Send the request.
1030-
response = rpc(
1031-
request,
1032-
retry=retry,
1033-
timeout=timeout,
1034-
metadata=metadata,
1035-
)
1058+
try:
1059+
# Send the request.
1060+
response = rpc(
1061+
request,
1062+
retry=retry,
1063+
timeout=timeout,
1064+
metadata=metadata,
1065+
)
10361066

1037-
# Done; return the response.
1038-
return response
1067+
# Done; return the response.
1068+
return response
1069+
except core_exceptions.GoogleAPICallError as e:
1070+
self._add_cred_info_for_auth_errors(e)
1071+
raise e
10391072

10401073
def get_operation(
10411074
self,
@@ -1081,16 +1114,20 @@ def get_operation(
10811114
# Validate the universe domain.
10821115
self._validate_universe_domain()
10831116

1084-
# Send the request.
1085-
response = rpc(
1086-
request,
1087-
retry=retry,
1088-
timeout=timeout,
1089-
metadata=metadata,
1090-
)
1117+
try:
1118+
# Send the request.
1119+
response = rpc(
1120+
request,
1121+
retry=retry,
1122+
timeout=timeout,
1123+
metadata=metadata,
1124+
)
10911125

1092-
# Done; return the response.
1093-
return response
1126+
# Done; return the response.
1127+
return response
1128+
except core_exceptions.GoogleAPICallError as e:
1129+
self._add_cred_info_for_auth_errors(e)
1130+
raise e
10941131

10951132
def delete_operation(
10961133
self,

google/cloud/pubsublite_v1/services/partition_assignment_service/client.py

Lines changed: 55 additions & 18 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
@@ -465,6 +467,33 @@ def _validate_universe_domain(self):
465467
# NOTE (b/349488459): universe validation is disabled until further notice.
466468
return True
467469

470+
def _add_cred_info_for_auth_errors(
471+
self, error: core_exceptions.GoogleAPICallError
472+
) -> None:
473+
"""Adds credential info string to error details for 401/403/404 errors.
474+
475+
Args:
476+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
477+
"""
478+
if error.code not in [
479+
HTTPStatus.UNAUTHORIZED,
480+
HTTPStatus.FORBIDDEN,
481+
HTTPStatus.NOT_FOUND,
482+
]:
483+
return
484+
485+
cred = self._transport._credentials
486+
487+
# get_cred_info is only available in google-auth>=2.35.0
488+
if not hasattr(cred, "get_cred_info"):
489+
return
490+
491+
# ignore the type check since pypy test fails when get_cred_info
492+
# is not available
493+
cred_info = cred.get_cred_info() # type: ignore
494+
if cred_info and hasattr(error._details, "append"):
495+
error._details.append(json.dumps(cred_info))
496+
468497
@property
469498
def api_endpoint(self):
470499
"""Return the API endpoint used by the client instance.
@@ -819,16 +848,20 @@ def list_operations(
819848
# Validate the universe domain.
820849
self._validate_universe_domain()
821850

822-
# Send the request.
823-
response = rpc(
824-
request,
825-
retry=retry,
826-
timeout=timeout,
827-
metadata=metadata,
828-
)
851+
try:
852+
# Send the request.
853+
response = rpc(
854+
request,
855+
retry=retry,
856+
timeout=timeout,
857+
metadata=metadata,
858+
)
829859

830-
# Done; return the response.
831-
return response
860+
# Done; return the response.
861+
return response
862+
except core_exceptions.GoogleAPICallError as e:
863+
self._add_cred_info_for_auth_errors(e)
864+
raise e
832865

833866
def get_operation(
834867
self,
@@ -874,16 +907,20 @@ def get_operation(
874907
# Validate the universe domain.
875908
self._validate_universe_domain()
876909

877-
# Send the request.
878-
response = rpc(
879-
request,
880-
retry=retry,
881-
timeout=timeout,
882-
metadata=metadata,
883-
)
910+
try:
911+
# Send the request.
912+
response = rpc(
913+
request,
914+
retry=retry,
915+
timeout=timeout,
916+
metadata=metadata,
917+
)
884918

885-
# Done; return the response.
886-
return response
919+
# Done; return the response.
920+
return response
921+
except core_exceptions.GoogleAPICallError as e:
922+
self._add_cred_info_for_auth_errors(e)
923+
raise e
887924

888925
def delete_operation(
889926
self,

0 commit comments

Comments
 (0)