Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Many APIs] Add REST Interceptors which support reading metadata #13498

Merged
merged 7 commits into from
Feb 11, 2025
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__ = "1.9.0" # {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__ = "1.9.0" # {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 @@ -486,6 +488,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
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,37 @@ def post_compute_contacts(
) -> service.ComputeContactsResponse:
"""Post-rpc interceptor for compute_contacts

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_compute_contacts_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the EssentialContactsService server but before
it is returned to user code.
it is returned to user code. This `post_compute_contacts` interceptor runs
before the `post_compute_contacts_with_metadata` interceptor.
"""
return response

def post_compute_contacts_with_metadata(
self,
response: service.ComputeContactsResponse,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[
service.ComputeContactsResponse, Sequence[Tuple[str, Union[str, bytes]]]
]:
"""Post-rpc interceptor for compute_contacts

Override in a subclass to read or manipulate the response or metadata after it
is returned by the EssentialContactsService server but before it is returned to user code.

We recommend only using this `post_compute_contacts_with_metadata`
interceptor in new development instead of the `post_compute_contacts` interceptor.
When both interceptors are used, this `post_compute_contacts_with_metadata` interceptor runs after the
`post_compute_contacts` interceptor. The (possibly modified) response returned by
`post_compute_contacts` will be passed to
`post_compute_contacts_with_metadata`.
"""
return response, metadata

def pre_create_contact(
self,
request: service.CreateContactRequest,
Expand All @@ -161,12 +186,35 @@ def pre_create_contact(
def post_create_contact(self, response: service.Contact) -> service.Contact:
"""Post-rpc interceptor for create_contact

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_create_contact_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the EssentialContactsService server but before
it is returned to user code.
it is returned to user code. This `post_create_contact` interceptor runs
before the `post_create_contact_with_metadata` interceptor.
"""
return response

def post_create_contact_with_metadata(
self,
response: service.Contact,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[service.Contact, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for create_contact

Override in a subclass to read or manipulate the response or metadata after it
is returned by the EssentialContactsService server but before it is returned to user code.

We recommend only using this `post_create_contact_with_metadata`
interceptor in new development instead of the `post_create_contact` interceptor.
When both interceptors are used, this `post_create_contact_with_metadata` interceptor runs after the
`post_create_contact` interceptor. The (possibly modified) response returned by
`post_create_contact` will be passed to
`post_create_contact_with_metadata`.
"""
return response, metadata

def pre_delete_contact(
self,
request: service.DeleteContactRequest,
Expand Down Expand Up @@ -194,12 +242,35 @@ def pre_get_contact(
def post_get_contact(self, response: service.Contact) -> service.Contact:
"""Post-rpc interceptor for get_contact

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_get_contact_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the EssentialContactsService server but before
it is returned to user code.
it is returned to user code. This `post_get_contact` interceptor runs
before the `post_get_contact_with_metadata` interceptor.
"""
return response

def post_get_contact_with_metadata(
self,
response: service.Contact,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[service.Contact, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for get_contact

Override in a subclass to read or manipulate the response or metadata after it
is returned by the EssentialContactsService server but before it is returned to user code.

We recommend only using this `post_get_contact_with_metadata`
interceptor in new development instead of the `post_get_contact` interceptor.
When both interceptors are used, this `post_get_contact_with_metadata` interceptor runs after the
`post_get_contact` interceptor. The (possibly modified) response returned by
`post_get_contact` will be passed to
`post_get_contact_with_metadata`.
"""
return response, metadata

def pre_list_contacts(
self,
request: service.ListContactsRequest,
Expand All @@ -217,12 +288,35 @@ def post_list_contacts(
) -> service.ListContactsResponse:
"""Post-rpc interceptor for list_contacts

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_list_contacts_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the EssentialContactsService server but before
it is returned to user code.
it is returned to user code. This `post_list_contacts` interceptor runs
before the `post_list_contacts_with_metadata` interceptor.
"""
return response

def post_list_contacts_with_metadata(
self,
response: service.ListContactsResponse,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[service.ListContactsResponse, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for list_contacts

Override in a subclass to read or manipulate the response or metadata after it
is returned by the EssentialContactsService server but before it is returned to user code.

We recommend only using this `post_list_contacts_with_metadata`
interceptor in new development instead of the `post_list_contacts` interceptor.
When both interceptors are used, this `post_list_contacts_with_metadata` interceptor runs after the
`post_list_contacts` interceptor. The (possibly modified) response returned by
`post_list_contacts` will be passed to
`post_list_contacts_with_metadata`.
"""
return response, metadata

def pre_send_test_message(
self,
request: service.SendTestMessageRequest,
Expand Down Expand Up @@ -250,12 +344,35 @@ def pre_update_contact(
def post_update_contact(self, response: service.Contact) -> service.Contact:
"""Post-rpc interceptor for update_contact

Override in a subclass to manipulate the response
DEPRECATED. Please use the `post_update_contact_with_metadata`
interceptor instead.

Override in a subclass to read or manipulate the response
after it is returned by the EssentialContactsService server but before
it is returned to user code.
it is returned to user code. This `post_update_contact` interceptor runs
before the `post_update_contact_with_metadata` interceptor.
"""
return response

def post_update_contact_with_metadata(
self,
response: service.Contact,
metadata: Sequence[Tuple[str, Union[str, bytes]]],
) -> Tuple[service.Contact, Sequence[Tuple[str, Union[str, bytes]]]]:
"""Post-rpc interceptor for update_contact

Override in a subclass to read or manipulate the response or metadata after it
is returned by the EssentialContactsService server but before it is returned to user code.

We recommend only using this `post_update_contact_with_metadata`
interceptor in new development instead of the `post_update_contact` interceptor.
When both interceptors are used, this `post_update_contact_with_metadata` interceptor runs after the
`post_update_contact` interceptor. The (possibly modified) response returned by
`post_update_contact` will be passed to
`post_update_contact_with_metadata`.
"""
return response, metadata


@dataclasses.dataclass
class EssentialContactsServiceRestStub:
Expand Down Expand Up @@ -468,6 +585,10 @@ def __call__(
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)

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

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

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

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

resp = self._interceptor.post_update_contact(resp)
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
resp, _ = self._interceptor.post_update_contact_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 @@ -8,7 +8,7 @@
],
"language": "PYTHON",
"name": "google-cloud-essential-contacts",
"version": "1.9.0"
"version": "0.1.0"
},
"snippets": [
{
Expand Down
Loading
Loading