Skip to content

Commit

Permalink
feat: add context manager support in client (#187)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

chore: fix docstring for first attribute of protos

committer: @busunkim96
PiperOrigin-RevId: 401271153

Source-Link: googleapis/googleapis@787f8c9

Source-Link: googleapis/googleapis-gen@81decff
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
  • Loading branch information
gcf-owl-bot[bot] committed Oct 8, 2021
1 parent f4f200f commit a4e2cd4
Show file tree
Hide file tree
Showing 22 changed files with 268 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ async def update_group(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def get_group(
Expand Down Expand Up @@ -498,6 +495,19 @@ def update_group(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def get_group(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,8 @@ def update_group(
)
return self._stubs["update_group"]

def close(self):
self.grpc_channel.close()


__all__ = ("ErrorGroupServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,8 @@ def update_group(
)
return self._stubs["update_group"]

def close(self):
return self.grpc_channel.close()


__all__ = ("ErrorGroupServiceGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ async def delete_events(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def list_group_stats(
Expand Down Expand Up @@ -626,6 +623,19 @@ def delete_events(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def list_group_stats(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,8 @@ def delete_events(
)
return self._stubs["delete_events"]

def close(self):
self.grpc_channel.close()


__all__ = ("ErrorStatsServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,8 @@ def delete_events(
)
return self._stubs["delete_events"]

def close(self):
return self.grpc_channel.close()


__all__ = ("ErrorStatsServiceGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ async def report_error_event(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def report_error_event(
Expand Down Expand Up @@ -438,6 +435,19 @@ def report_error_event(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.
.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.
.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def report_error_event(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,8 @@ def report_error_event(
)
return self._stubs["report_error_event"]

def close(self):
self.grpc_channel.close()


__all__ = ("ReportErrorsServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,8 @@ def report_error_event(
)
return self._stubs["report_error_event"]

def close(self):
return self.grpc_channel.close()


__all__ = ("ReportErrorsServiceGrpcAsyncIOTransport",)
1 change: 1 addition & 0 deletions google/cloud/errorreporting_v1beta1/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ResolutionStatus(proto.Enum):

class ErrorGroup(proto.Message):
r"""Description of a group of similar error events.
Attributes:
name (str):
The group resource name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

class GetGroupRequest(proto.Message):
r"""A request to return an individual group.
Attributes:
group_name (str):
Required. The group resource name. Written as
Expand All @@ -41,6 +42,7 @@ class GetGroupRequest(proto.Message):

class UpdateGroupRequest(proto.Message):
r"""A request to replace the existing data for the given group.
Attributes:
group (google.cloud.errorreporting_v1beta1.types.ErrorGroup):
Required. The group which replaces the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ErrorGroupOrder(proto.Enum):

class ListGroupStatsRequest(proto.Message):
r"""Specifies a set of ``ErrorGroupStats`` to return.
Attributes:
project_name (str):
Required. The resource name of the Google Cloud Platform
Expand Down Expand Up @@ -127,6 +128,7 @@ class ListGroupStatsRequest(proto.Message):

class ListGroupStatsResponse(proto.Message):
r"""Contains a set of requested error group stats.
Attributes:
error_group_stats (Sequence[google.cloud.errorreporting_v1beta1.types.ErrorGroupStats]):
The error group stats which match the given
Expand Down Expand Up @@ -255,6 +257,7 @@ class TimedCount(proto.Message):

class ListEventsRequest(proto.Message):
r"""Specifies a set of error events to return.
Attributes:
project_name (str):
Required. The resource name of the Google Cloud Platform
Expand Down Expand Up @@ -296,6 +299,7 @@ class ListEventsRequest(proto.Message):

class ListEventsResponse(proto.Message):
r"""Contains a set of requested error events.
Attributes:
error_events (Sequence[google.cloud.errorreporting_v1beta1.types.ErrorEvent]):
The error events which match the given
Expand Down Expand Up @@ -370,6 +374,7 @@ class ServiceContextFilter(proto.Message):

class DeleteEventsRequest(proto.Message):
r"""Deletes all events in the project.
Attributes:
project_name (str):
Required. The resource name of the Google Cloud Platform
Expand All @@ -384,7 +389,8 @@ class DeleteEventsRequest(proto.Message):


class DeleteEventsResponse(proto.Message):
r"""Response message for deleting error events. """
r"""Response message for deleting error events.
"""


__all__ = tuple(sorted(__protobuf__.manifest))
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

class ReportErrorEventRequest(proto.Message):
r"""A request for reporting an individual error event.
Attributes:
project_name (str):
Required. The resource name of the Google Cloud Platform
Expand All @@ -50,7 +51,8 @@ class ReportErrorEventRequest(proto.Message):
class ReportErrorEventResponse(proto.Message):
r"""Response for reporting an individual error event.
Data may be added to this message in the future.
"""
"""


class ReportedErrorEvent(proto.Message):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from google.api_core import gapic_v1
from google.api_core import grpc_helpers
from google.api_core import grpc_helpers_async
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.errorreporting_v1beta1.services.error_group_service import (
Expand Down Expand Up @@ -1037,6 +1038,9 @@ def test_error_group_service_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

with pytest.raises(NotImplementedError):
transport.close()


@requires_google_auth_gte_1_25_0
def test_error_group_service_base_transport_with_credentials_file():
Expand Down Expand Up @@ -1517,3 +1521,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
)
prep.assert_called_once_with(client_info)


@pytest.mark.asyncio
async def test_transport_close_async():
client = ErrorGroupServiceAsyncClient(
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
)
with mock.patch.object(
type(getattr(client.transport, "grpc_channel")), "close"
) as close:
async with client:
close.assert_not_called()
close.assert_called_once()


def test_transport_close():
transports = {
"grpc": "_grpc_channel",
}

for transport, close_name in transports.items():
client = ErrorGroupServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
with mock.patch.object(
type(getattr(client.transport, close_name)), "close"
) as close:
with client:
close.assert_not_called()
close.assert_called_once()


def test_client_ctx():
transports = [
"grpc",
]
for transport in transports:
client = ErrorGroupServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
# Test client calls underlying transport.
with mock.patch.object(type(client.transport), "close") as close:
close.assert_not_called()
with client:
pass
close.assert_called()
Loading

0 comments on commit a4e2cd4

Please sign in to comment.