Skip to content

Commit a961bc0

Browse files
feat: [Many APIs] Add REST Interceptors which support reading metadata (#13494)
BEGIN_COMMIT_OVERRIDE 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 END_COMMIT_OVERRIDE - [ ] Regenerate this pull request now. 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: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWFkcy1hZG1hbmFnZXIvLk93bEJvdC55YW1sIiwiaCI6ImUyOTFjNGRkMWQ2NzBlZGExOTk5OGRlNzZmOTY3ZTE2MDNhNDg5OTMifQ== Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWFkcy1tYXJrZXRpbmdwbGF0Zm9ybS1hZG1pbi8uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWFpLWdlbmVyYXRpdmVsYW5ndWFnZS8uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWFuYWx5dGljcy1kYXRhLy5Pd2xCb3QueWFtbCIsImgiOiJlMjkxYzRkZDFkNjcwZWRhMTk5OThkZTc2Zjk2N2UxNjAzYTQ4OTkzIn0= Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWFwcHMtY2FyZC8uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ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uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9 --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: ohmayr <[email protected]>
1 parent c6c63e1 commit a961bc0

File tree

304 files changed

+25987
-2083
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

304 files changed

+25987
-2083
lines changed

packages/google-ads-admanager/google/ads/admanager/gapic_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
__version__ = "0.2.3" # {x-release-please-version}
16+
__version__ = "0.0.0" # {x-release-please-version}

packages/google-ads-admanager/google/ads/admanager_v1/gapic_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
__version__ = "0.2.3" # {x-release-please-version}
16+
__version__ = "0.0.0" # {x-release-please-version}

packages/google-ads-admanager/google/ads/admanager_v1/services/ad_unit_service/client.py

Lines changed: 42 additions & 9 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.
@@ -1126,16 +1155,20 @@ def get_operation(
11261155
# Validate the universe domain.
11271156
self._validate_universe_domain()
11281157

1129-
# Send the request.
1130-
response = rpc(
1131-
request,
1132-
retry=retry,
1133-
timeout=timeout,
1134-
metadata=metadata,
1135-
)
1158+
try:
1159+
# Send the request.
1160+
response = rpc(
1161+
request,
1162+
retry=retry,
1163+
timeout=timeout,
1164+
metadata=metadata,
1165+
)
11361166

1137-
# Done; return the response.
1138-
return response
1167+
# Done; return the response.
1168+
return response
1169+
except core_exceptions.GoogleAPICallError as e:
1170+
self._add_cred_info_for_auth_errors(e)
1171+
raise e
11391172

11401173

11411174
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-ads-admanager/google/ads/admanager_v1/services/ad_unit_service/transports/rest.py

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,35 @@ def post_get_ad_unit(
118118
) -> ad_unit_messages.AdUnit:
119119
"""Post-rpc interceptor for get_ad_unit
120120
121-
Override in a subclass to manipulate the response
121+
DEPRECATED. Please use the `post_get_ad_unit_with_metadata`
122+
interceptor instead.
123+
124+
Override in a subclass to read or manipulate the response
122125
after it is returned by the AdUnitService server but before
123-
it is returned to user code.
126+
it is returned to user code. This `post_get_ad_unit` interceptor runs
127+
before the `post_get_ad_unit_with_metadata` interceptor.
124128
"""
125129
return response
126130

131+
def post_get_ad_unit_with_metadata(
132+
self,
133+
response: ad_unit_messages.AdUnit,
134+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
135+
) -> Tuple[ad_unit_messages.AdUnit, Sequence[Tuple[str, Union[str, bytes]]]]:
136+
"""Post-rpc interceptor for get_ad_unit
137+
138+
Override in a subclass to read or manipulate the response or metadata after it
139+
is returned by the AdUnitService server but before it is returned to user code.
140+
141+
We recommend only using this `post_get_ad_unit_with_metadata`
142+
interceptor in new development instead of the `post_get_ad_unit` interceptor.
143+
When both interceptors are used, this `post_get_ad_unit_with_metadata` interceptor runs after the
144+
`post_get_ad_unit` interceptor. The (possibly modified) response returned by
145+
`post_get_ad_unit` will be passed to
146+
`post_get_ad_unit_with_metadata`.
147+
"""
148+
return response, metadata
149+
127150
def pre_list_ad_units(
128151
self,
129152
request: ad_unit_service.ListAdUnitsRequest,
@@ -143,12 +166,37 @@ def post_list_ad_units(
143166
) -> ad_unit_service.ListAdUnitsResponse:
144167
"""Post-rpc interceptor for list_ad_units
145168
146-
Override in a subclass to manipulate the response
169+
DEPRECATED. Please use the `post_list_ad_units_with_metadata`
170+
interceptor instead.
171+
172+
Override in a subclass to read or manipulate the response
147173
after it is returned by the AdUnitService server but before
148-
it is returned to user code.
174+
it is returned to user code. This `post_list_ad_units` interceptor runs
175+
before the `post_list_ad_units_with_metadata` interceptor.
149176
"""
150177
return response
151178

179+
def post_list_ad_units_with_metadata(
180+
self,
181+
response: ad_unit_service.ListAdUnitsResponse,
182+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
183+
) -> Tuple[
184+
ad_unit_service.ListAdUnitsResponse, Sequence[Tuple[str, Union[str, bytes]]]
185+
]:
186+
"""Post-rpc interceptor for list_ad_units
187+
188+
Override in a subclass to read or manipulate the response or metadata after it
189+
is returned by the AdUnitService server but before it is returned to user code.
190+
191+
We recommend only using this `post_list_ad_units_with_metadata`
192+
interceptor in new development instead of the `post_list_ad_units` interceptor.
193+
When both interceptors are used, this `post_list_ad_units_with_metadata` interceptor runs after the
194+
`post_list_ad_units` interceptor. The (possibly modified) response returned by
195+
`post_list_ad_units` will be passed to
196+
`post_list_ad_units_with_metadata`.
197+
"""
198+
return response, metadata
199+
152200
def pre_list_ad_unit_sizes(
153201
self,
154202
request: ad_unit_service.ListAdUnitSizesRequest,
@@ -168,12 +216,37 @@ def post_list_ad_unit_sizes(
168216
) -> ad_unit_service.ListAdUnitSizesResponse:
169217
"""Post-rpc interceptor for list_ad_unit_sizes
170218
171-
Override in a subclass to manipulate the response
219+
DEPRECATED. Please use the `post_list_ad_unit_sizes_with_metadata`
220+
interceptor instead.
221+
222+
Override in a subclass to read or manipulate the response
172223
after it is returned by the AdUnitService server but before
173-
it is returned to user code.
224+
it is returned to user code. This `post_list_ad_unit_sizes` interceptor runs
225+
before the `post_list_ad_unit_sizes_with_metadata` interceptor.
174226
"""
175227
return response
176228

229+
def post_list_ad_unit_sizes_with_metadata(
230+
self,
231+
response: ad_unit_service.ListAdUnitSizesResponse,
232+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
233+
) -> Tuple[
234+
ad_unit_service.ListAdUnitSizesResponse, Sequence[Tuple[str, Union[str, bytes]]]
235+
]:
236+
"""Post-rpc interceptor for list_ad_unit_sizes
237+
238+
Override in a subclass to read or manipulate the response or metadata after it
239+
is returned by the AdUnitService server but before it is returned to user code.
240+
241+
We recommend only using this `post_list_ad_unit_sizes_with_metadata`
242+
interceptor in new development instead of the `post_list_ad_unit_sizes` interceptor.
243+
When both interceptors are used, this `post_list_ad_unit_sizes_with_metadata` interceptor runs after the
244+
`post_list_ad_unit_sizes` interceptor. The (possibly modified) response returned by
245+
`post_list_ad_unit_sizes` will be passed to
246+
`post_list_ad_unit_sizes_with_metadata`.
247+
"""
248+
return response, metadata
249+
177250
def pre_get_operation(
178251
self,
179252
request: operations_pb2.GetOperationRequest,
@@ -407,6 +480,10 @@ def __call__(
407480
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
408481

409482
resp = self._interceptor.post_get_ad_unit(resp)
483+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
484+
resp, _ = self._interceptor.post_get_ad_unit_with_metadata(
485+
resp, response_metadata
486+
)
410487
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
411488
logging.DEBUG
412489
): # pragma: NO COVER
@@ -553,6 +630,10 @@ def __call__(
553630
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
554631

555632
resp = self._interceptor.post_list_ad_units(resp)
633+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
634+
resp, _ = self._interceptor.post_list_ad_units_with_metadata(
635+
resp, response_metadata
636+
)
556637
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
557638
logging.DEBUG
558639
): # pragma: NO COVER
@@ -701,6 +782,10 @@ def __call__(
701782
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
702783

703784
resp = self._interceptor.post_list_ad_unit_sizes(resp)
785+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
786+
resp, _ = self._interceptor.post_list_ad_unit_sizes_with_metadata(
787+
resp, response_metadata
788+
)
704789
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
705790
logging.DEBUG
706791
): # pragma: NO COVER

packages/google-ads-admanager/google/ads/admanager_v1/services/company_service/client.py

Lines changed: 42 additions & 9 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
@@ -554,6 +556,33 @@ def _validate_universe_domain(self):
554556
# NOTE (b/349488459): universe validation is disabled until further notice.
555557
return True
556558

559+
def _add_cred_info_for_auth_errors(
560+
self, error: core_exceptions.GoogleAPICallError
561+
) -> None:
562+
"""Adds credential info string to error details for 401/403/404 errors.
563+
564+
Args:
565+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
566+
"""
567+
if error.code not in [
568+
HTTPStatus.UNAUTHORIZED,
569+
HTTPStatus.FORBIDDEN,
570+
HTTPStatus.NOT_FOUND,
571+
]:
572+
return
573+
574+
cred = self._transport._credentials
575+
576+
# get_cred_info is only available in google-auth>=2.35.0
577+
if not hasattr(cred, "get_cred_info"):
578+
return
579+
580+
# ignore the type check since pypy test fails when get_cred_info
581+
# is not available
582+
cred_info = cred.get_cred_info() # type: ignore
583+
if cred_info and hasattr(error._details, "append"):
584+
error._details.append(json.dumps(cred_info))
585+
557586
@property
558587
def api_endpoint(self):
559588
"""Return the API endpoint used by the client instance.
@@ -1028,16 +1057,20 @@ def get_operation(
10281057
# Validate the universe domain.
10291058
self._validate_universe_domain()
10301059

1031-
# Send the request.
1032-
response = rpc(
1033-
request,
1034-
retry=retry,
1035-
timeout=timeout,
1036-
metadata=metadata,
1037-
)
1060+
try:
1061+
# Send the request.
1062+
response = rpc(
1063+
request,
1064+
retry=retry,
1065+
timeout=timeout,
1066+
metadata=metadata,
1067+
)
10381068

1039-
# Done; return the response.
1040-
return response
1069+
# Done; return the response.
1070+
return response
1071+
except core_exceptions.GoogleAPICallError as e:
1072+
self._add_cred_info_for_auth_errors(e)
1073+
raise e
10411074

10421075

10431076
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

0 commit comments

Comments
 (0)