Skip to content

Commit

Permalink
feat: [Many APIs] Add REST Interceptors which support reading metadata (
Browse files Browse the repository at this point in the history
#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
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joicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWFwcGVuZ2luZS1hZG1pbi8uT3dsQm90LnlhbWwiLCJoIjoiZTI5MWM0ZGQxZDY3MGVkYTE5OTk4ZGU3NmY5NjdlMTYwM2E0ODk5MyJ9

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: ohmayr <[email protected]>
  • Loading branch information
3 people authored Feb 11, 2025
1 parent c6c63e1 commit a961bc0
Show file tree
Hide file tree
Showing 304 changed files with 25,987 additions and 2,083 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "0.2.3" # {x-release-please-version}
__version__ = "0.0.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
__version__ = "0.2.3" # {x-release-please-version}
__version__ = "0.0.0" # {x-release-please-version}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#
from collections import OrderedDict
from http import HTTPStatus
import json
import logging as std_logging
import os
import re
Expand Down Expand Up @@ -531,6 +533,33 @@ def _validate_universe_domain(self):
# NOTE (b/349488459): universe validation is disabled until further notice.
return True

def _add_cred_info_for_auth_errors(
self, error: core_exceptions.GoogleAPICallError
) -> None:
"""Adds credential info string to error details for 401/403/404 errors.
Args:
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
"""
if error.code not in [
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
HTTPStatus.NOT_FOUND,
]:
return

cred = self._transport._credentials

# get_cred_info is only available in google-auth>=2.35.0
if not hasattr(cred, "get_cred_info"):
return

# ignore the type check since pypy test fails when get_cred_info
# is not available
cred_info = cred.get_cred_info() # type: ignore
if cred_info and hasattr(error._details, "append"):
error._details.append(json.dumps(cred_info))

@property
def api_endpoint(self):
"""Return the API endpoint used by the client instance.
Expand Down Expand Up @@ -1126,16 +1155,20 @@ def get_operation(
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)
try:
# Send the request.
response = rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
self._add_cred_info_for_auth_errors(e)
raise e


DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,35 @@ def post_get_ad_unit(
) -> ad_unit_messages.AdUnit:
"""Post-rpc interceptor for get_ad_unit
Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_get_ad_unit_with_metadata`
interceptor instead.
Override in a subclass to read or manipulate the response
after it is returned by the AdUnitService server but before
it is returned to user code.
it is returned to user code. This `post_get_ad_unit` interceptor runs
before the `post_get_ad_unit_with_metadata` interceptor.
"""
return response

def post_get_ad_unit_with_metadata(
self,
response: ad_unit_messages.AdUnit,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[ad_unit_messages.AdUnit, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for get_ad_unit
Override in a subclass to read or manipulate the response or metadata after it
is returned by the AdUnitService server but before it is returned to user code.
We recommend only using this `post_get_ad_unit_with_metadata`
interceptor in new development instead of the `post_get_ad_unit` interceptor.
When both interceptors are used, this `post_get_ad_unit_with_metadata` interceptor runs after the
`post_get_ad_unit` interceptor. The (possibly modified) response returned by
`post_get_ad_unit` will be passed to
`post_get_ad_unit_with_metadata`.
"""
return response, metadata

def pre_list_ad_units(
self,
request: ad_unit_service.ListAdUnitsRequest,
Expand All @@ -143,12 +166,37 @@ def post_list_ad_units(
) -> ad_unit_service.ListAdUnitsResponse:
"""Post-rpc interceptor for list_ad_units
Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_list_ad_units_with_metadata`
interceptor instead.
Override in a subclass to read or manipulate the response
after it is returned by the AdUnitService server but before
it is returned to user code.
it is returned to user code. This `post_list_ad_units` interceptor runs
before the `post_list_ad_units_with_metadata` interceptor.
"""
return response

def post_list_ad_units_with_metadata(
self,
response: ad_unit_service.ListAdUnitsResponse,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[
ad_unit_service.ListAdUnitsResponse, Sequence[Tuple[str, Union[str, bytes]]]
]:
"""Post-rpc interceptor for list_ad_units
Override in a subclass to read or manipulate the response or metadata after it
is returned by the AdUnitService server but before it is returned to user code.
We recommend only using this `post_list_ad_units_with_metadata`
interceptor in new development instead of the `post_list_ad_units` interceptor.
When both interceptors are used, this `post_list_ad_units_with_metadata` interceptor runs after the
`post_list_ad_units` interceptor. The (possibly modified) response returned by
`post_list_ad_units` will be passed to
`post_list_ad_units_with_metadata`.
"""
return response, metadata

def pre_list_ad_unit_sizes(
self,
request: ad_unit_service.ListAdUnitSizesRequest,
Expand All @@ -168,12 +216,37 @@ def post_list_ad_unit_sizes(
) -> ad_unit_service.ListAdUnitSizesResponse:
"""Post-rpc interceptor for list_ad_unit_sizes
Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_list_ad_unit_sizes_with_metadata`
interceptor instead.
Override in a subclass to read or manipulate the response
after it is returned by the AdUnitService server but before
it is returned to user code.
it is returned to user code. This `post_list_ad_unit_sizes` interceptor runs
before the `post_list_ad_unit_sizes_with_metadata` interceptor.
"""
return response

def post_list_ad_unit_sizes_with_metadata(
self,
response: ad_unit_service.ListAdUnitSizesResponse,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[
ad_unit_service.ListAdUnitSizesResponse, Sequence[Tuple[str, Union[str, bytes]]]
]:
"""Post-rpc interceptor for list_ad_unit_sizes
Override in a subclass to read or manipulate the response or metadata after it
is returned by the AdUnitService server but before it is returned to user code.
We recommend only using this `post_list_ad_unit_sizes_with_metadata`
interceptor in new development instead of the `post_list_ad_unit_sizes` interceptor.
When both interceptors are used, this `post_list_ad_unit_sizes_with_metadata` interceptor runs after the
`post_list_ad_unit_sizes` interceptor. The (possibly modified) response returned by
`post_list_ad_unit_sizes` will be passed to
`post_list_ad_unit_sizes_with_metadata`.
"""
return response, metadata

def pre_get_operation(
self,
request: operations_pb2.GetOperationRequest,
Expand Down Expand Up @@ -407,6 +480,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_get_ad_unit(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_get_ad_unit_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down Expand Up @@ -553,6 +630,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_list_ad_units(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_list_ad_units_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down Expand Up @@ -701,6 +782,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

resp = self._interceptor.post_list_ad_unit_sizes(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_list_ad_unit_sizes_with_metadata(
resp, response_metadata
)
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
logging.DEBUG
): # pragma: NO COVER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
#
from collections import OrderedDict
from http import HTTPStatus
import json
import logging as std_logging
import os
import re
Expand Down Expand Up @@ -554,6 +556,33 @@ def _validate_universe_domain(self):
# NOTE (b/349488459): universe validation is disabled until further notice.
return True

def _add_cred_info_for_auth_errors(
self, error: core_exceptions.GoogleAPICallError
) -> None:
"""Adds credential info string to error details for 401/403/404 errors.
Args:
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
"""
if error.code not in [
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
HTTPStatus.NOT_FOUND,
]:
return

cred = self._transport._credentials

# get_cred_info is only available in google-auth>=2.35.0
if not hasattr(cred, "get_cred_info"):
return

# ignore the type check since pypy test fails when get_cred_info
# is not available
cred_info = cred.get_cred_info() # type: ignore
if cred_info and hasattr(error._details, "append"):
error._details.append(json.dumps(cred_info))

@property
def api_endpoint(self):
"""Return the API endpoint used by the client instance.
Expand Down Expand Up @@ -1028,16 +1057,20 @@ def get_operation(
# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)
try:
# Send the request.
response = rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Done; return the response.
return response
# Done; return the response.
return response
except core_exceptions.GoogleAPICallError as e:
self._add_cred_info_for_auth_errors(e)
raise e


DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Loading

0 comments on commit a961bc0

Please sign in to comment.