Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
feat: add context manager support in client (#123)
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 7, 2021
1 parent 174f731 commit 4324911
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 10 deletions.
6 changes: 6 additions & 0 deletions google/cloud/redis_v1/services/cloud_redis/async_client.py
Expand Up @@ -1052,6 +1052,12 @@ async def delete_instance(
# 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
18 changes: 14 additions & 4 deletions google/cloud/redis_v1/services/cloud_redis/client.py
Expand Up @@ -368,10 +368,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_instances(
Expand Down Expand Up @@ -1245,6 +1242,19 @@ def delete_instance(
# 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
9 changes: 9 additions & 0 deletions google/cloud/redis_v1/services/cloud_redis/transports/base.py
Expand Up @@ -183,6 +183,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/redis_v1/services/cloud_redis/transports/grpc.py
Expand Up @@ -541,5 +541,8 @@ def delete_instance(
)
return self._stubs["delete_instance"]

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


__all__ = ("CloudRedisGrpcTransport",)
Expand Up @@ -560,5 +560,8 @@ def delete_instance(
)
return self._stubs["delete_instance"]

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


__all__ = ("CloudRedisGrpcAsyncIOTransport",)
9 changes: 8 additions & 1 deletion google/cloud/redis_v1/types/cloud_redis.py
Expand Up @@ -46,6 +46,7 @@

class Instance(proto.Message):
r"""A Google Cloud Redis instance.
Attributes:
name (str):
Required. Unique name of the resource in this scope
Expand Down Expand Up @@ -377,6 +378,7 @@ class DeleteInstanceRequest(proto.Message):

class GcsSource(proto.Message):
r"""The Cloud Storage location for the input content
Attributes:
uri (str):
Required. Source data URI. (e.g.
Expand All @@ -388,6 +390,7 @@ class GcsSource(proto.Message):

class InputConfig(proto.Message):
r"""The input content
Attributes:
gcs_source (google.cloud.redis_v1.types.GcsSource):
Google Cloud Storage location where input
Expand Down Expand Up @@ -418,6 +421,7 @@ class ImportInstanceRequest(proto.Message):

class GcsDestination(proto.Message):
r"""The Cloud Storage location for the output content
Attributes:
uri (str):
Required. Data destination URI (e.g.
Expand All @@ -430,6 +434,7 @@ class GcsDestination(proto.Message):

class OutputConfig(proto.Message):
r"""The output content
Attributes:
gcs_destination (google.cloud.redis_v1.types.GcsDestination):
Google Cloud Storage destination for output
Expand Down Expand Up @@ -487,6 +492,7 @@ class DataProtectionMode(proto.Enum):

class OperationMetadata(proto.Message):
r"""Represents the v1 metadata of the long-running operation.
Attributes:
create_time (google.protobuf.timestamp_pb2.Timestamp):
Creation timestamp.
Expand Down Expand Up @@ -537,7 +543,8 @@ class LocationMetadata(proto.Message):
class ZoneMetadata(proto.Message):
r"""Defines specific information for a particular zone. Currently
empty and reserved for future use only.
"""
"""


__all__ = tuple(sorted(__protobuf__.manifest))
Expand Up @@ -1053,6 +1053,12 @@ async def delete_instance(
# 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
18 changes: 14 additions & 4 deletions google/cloud/redis_v1beta1/services/cloud_redis/client.py
Expand Up @@ -369,10 +369,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_instances(
Expand Down Expand Up @@ -1246,6 +1243,19 @@ def delete_instance(
# 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
Expand Up @@ -183,6 +183,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Expand Up @@ -541,5 +541,8 @@ def delete_instance(
)
return self._stubs["delete_instance"]

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


__all__ = ("CloudRedisGrpcTransport",)
Expand Up @@ -560,5 +560,8 @@ def delete_instance(
)
return self._stubs["delete_instance"]

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


__all__ = ("CloudRedisGrpcAsyncIOTransport",)
8 changes: 7 additions & 1 deletion google/cloud/redis_v1beta1/types/cloud_redis.py
Expand Up @@ -45,6 +45,7 @@

class Instance(proto.Message):
r"""A Google Cloud Redis instance.
Attributes:
name (str):
Required. Unique name of the resource in this scope
Expand Down Expand Up @@ -378,6 +379,7 @@ class DeleteInstanceRequest(proto.Message):

class GcsSource(proto.Message):
r"""The Cloud Storage location for the input content
Attributes:
uri (str):
Required. Source data URI. (e.g.
Expand All @@ -389,6 +391,7 @@ class GcsSource(proto.Message):

class InputConfig(proto.Message):
r"""The input content
Attributes:
gcs_source (google.cloud.redis_v1beta1.types.GcsSource):
Google Cloud Storage location where input
Expand Down Expand Up @@ -419,6 +422,7 @@ class ImportInstanceRequest(proto.Message):

class GcsDestination(proto.Message):
r"""The Cloud Storage location for the output content
Attributes:
uri (str):
Required. Data destination URI (e.g.
Expand All @@ -431,6 +435,7 @@ class GcsDestination(proto.Message):

class OutputConfig(proto.Message):
r"""The output content
Attributes:
gcs_destination (google.cloud.redis_v1beta1.types.GcsDestination):
Google Cloud Storage destination for output
Expand Down Expand Up @@ -509,7 +514,8 @@ class LocationMetadata(proto.Message):
class ZoneMetadata(proto.Message):
r"""Defines specific information for a particular zone. Currently
empty and reserved for future use only.
"""
"""


__all__ = tuple(sorted(__protobuf__.manifest))
50 changes: 50 additions & 0 deletions tests/unit/gapic/redis_v1/test_cloud_redis.py
Expand Up @@ -32,6 +32,7 @@
from google.api_core import grpc_helpers_async
from google.api_core import operation_async # type: ignore
from google.api_core import operations_v1
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.redis_v1.services.cloud_redis import CloudRedisAsyncClient
Expand Down Expand Up @@ -2678,6 +2679,9 @@ def test_cloud_redis_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

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

# Additionally, the LRO client (a property) should
# also raise NotImplementedError
with pytest.raises(NotImplementedError):
Expand Down Expand Up @@ -3174,3 +3178,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 = CloudRedisAsyncClient(
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 = CloudRedisClient(
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 = CloudRedisClient(
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()

0 comments on commit 4324911

Please sign in to comment.