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

Commit 4324911

Browse files
feat: add context manager support in client (#123)
- [ ] 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: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent 174f731 commit 4324911

File tree

14 files changed

+185
-10
lines changed

14 files changed

+185
-10
lines changed

google/cloud/redis_v1/services/cloud_redis/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,12 @@ async def delete_instance(
10521052
# Done; return the response.
10531053
return response
10541054

1055+
async def __aenter__(self):
1056+
return self
1057+
1058+
async def __aexit__(self, exc_type, exc, tb):
1059+
await self.transport.close()
1060+
10551061

10561062
try:
10571063
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/redis_v1/services/cloud_redis/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,7 @@ def __init__(
368368
client_cert_source_for_mtls=client_cert_source_func,
369369
quota_project_id=client_options.quota_project_id,
370370
client_info=client_info,
371-
always_use_jwt_access=(
372-
Transport == type(self).get_transport_class("grpc")
373-
or Transport == type(self).get_transport_class("grpc_asyncio")
374-
),
371+
always_use_jwt_access=True,
375372
)
376373

377374
def list_instances(
@@ -1245,6 +1242,19 @@ def delete_instance(
12451242
# Done; return the response.
12461243
return response
12471244

1245+
def __enter__(self):
1246+
return self
1247+
1248+
def __exit__(self, type, value, traceback):
1249+
"""Releases underlying transport's resources.
1250+
1251+
.. warning::
1252+
ONLY use as a context manager if the transport is NOT shared
1253+
with other clients! Exiting the with block will CLOSE the transport
1254+
and may cause errors in other clients!
1255+
"""
1256+
self.transport.close()
1257+
12481258

12491259
try:
12501260
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/redis_v1/services/cloud_redis/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def _prep_wrapped_messages(self, client_info):
183183
),
184184
}
185185

186+
def close(self):
187+
"""Closes resources associated with the transport.
188+
189+
.. warning::
190+
Only call this method if the transport is NOT shared
191+
with other clients - this may cause errors in other clients!
192+
"""
193+
raise NotImplementedError()
194+
186195
@property
187196
def operations_client(self) -> operations_v1.OperationsClient:
188197
"""Return the client designed to process long-running operations."""

google/cloud/redis_v1/services/cloud_redis/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,8 @@ def delete_instance(
541541
)
542542
return self._stubs["delete_instance"]
543543

544+
def close(self):
545+
self.grpc_channel.close()
546+
544547

545548
__all__ = ("CloudRedisGrpcTransport",)

google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,5 +560,8 @@ def delete_instance(
560560
)
561561
return self._stubs["delete_instance"]
562562

563+
def close(self):
564+
return self.grpc_channel.close()
565+
563566

564567
__all__ = ("CloudRedisGrpcAsyncIOTransport",)

google/cloud/redis_v1/types/cloud_redis.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
class Instance(proto.Message):
4848
r"""A Google Cloud Redis instance.
49+
4950
Attributes:
5051
name (str):
5152
Required. Unique name of the resource in this scope
@@ -377,6 +378,7 @@ class DeleteInstanceRequest(proto.Message):
377378

378379
class GcsSource(proto.Message):
379380
r"""The Cloud Storage location for the input content
381+
380382
Attributes:
381383
uri (str):
382384
Required. Source data URI. (e.g.
@@ -388,6 +390,7 @@ class GcsSource(proto.Message):
388390

389391
class InputConfig(proto.Message):
390392
r"""The input content
393+
391394
Attributes:
392395
gcs_source (google.cloud.redis_v1.types.GcsSource):
393396
Google Cloud Storage location where input
@@ -418,6 +421,7 @@ class ImportInstanceRequest(proto.Message):
418421

419422
class GcsDestination(proto.Message):
420423
r"""The Cloud Storage location for the output content
424+
421425
Attributes:
422426
uri (str):
423427
Required. Data destination URI (e.g.
@@ -430,6 +434,7 @@ class GcsDestination(proto.Message):
430434

431435
class OutputConfig(proto.Message):
432436
r"""The output content
437+
433438
Attributes:
434439
gcs_destination (google.cloud.redis_v1.types.GcsDestination):
435440
Google Cloud Storage destination for output
@@ -487,6 +492,7 @@ class DataProtectionMode(proto.Enum):
487492

488493
class OperationMetadata(proto.Message):
489494
r"""Represents the v1 metadata of the long-running operation.
495+
490496
Attributes:
491497
create_time (google.protobuf.timestamp_pb2.Timestamp):
492498
Creation timestamp.
@@ -537,7 +543,8 @@ class LocationMetadata(proto.Message):
537543
class ZoneMetadata(proto.Message):
538544
r"""Defines specific information for a particular zone. Currently
539545
empty and reserved for future use only.
540-
"""
546+
547+
"""
541548

542549

543550
__all__ = tuple(sorted(__protobuf__.manifest))

google/cloud/redis_v1beta1/services/cloud_redis/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ async def delete_instance(
10531053
# Done; return the response.
10541054
return response
10551055

1056+
async def __aenter__(self):
1057+
return self
1058+
1059+
async def __aexit__(self, exc_type, exc, tb):
1060+
await self.transport.close()
1061+
10561062

10571063
try:
10581064
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/redis_v1beta1/services/cloud_redis/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,7 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372-
always_use_jwt_access=(
373-
Transport == type(self).get_transport_class("grpc")
374-
or Transport == type(self).get_transport_class("grpc_asyncio")
375-
),
372+
always_use_jwt_access=True,
376373
)
377374

378375
def list_instances(
@@ -1246,6 +1243,19 @@ def delete_instance(
12461243
# Done; return the response.
12471244
return response
12481245

1246+
def __enter__(self):
1247+
return self
1248+
1249+
def __exit__(self, type, value, traceback):
1250+
"""Releases underlying transport's resources.
1251+
1252+
.. warning::
1253+
ONLY use as a context manager if the transport is NOT shared
1254+
with other clients! Exiting the with block will CLOSE the transport
1255+
and may cause errors in other clients!
1256+
"""
1257+
self.transport.close()
1258+
12491259

12501260
try:
12511261
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/redis_v1beta1/services/cloud_redis/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def _prep_wrapped_messages(self, client_info):
183183
),
184184
}
185185

186+
def close(self):
187+
"""Closes resources associated with the transport.
188+
189+
.. warning::
190+
Only call this method if the transport is NOT shared
191+
with other clients - this may cause errors in other clients!
192+
"""
193+
raise NotImplementedError()
194+
186195
@property
187196
def operations_client(self) -> operations_v1.OperationsClient:
188197
"""Return the client designed to process long-running operations."""

google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,8 @@ def delete_instance(
541541
)
542542
return self._stubs["delete_instance"]
543543

544+
def close(self):
545+
self.grpc_channel.close()
546+
544547

545548
__all__ = ("CloudRedisGrpcTransport",)

0 commit comments

Comments
 (0)