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

Commit d6b8569

Browse files
feat: add context manager support in client (#83)
- [ ] 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 9162f27 commit d6b8569

21 files changed

Lines changed: 294 additions & 15 deletions

File tree

google/cloud/metastore_v1/services/dataproc_metastore/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,12 @@ async def delete_backup(
15471547
# Done; return the response.
15481548
return response
15491549

1550+
async def __aenter__(self):
1551+
return self
1552+
1553+
async def __aexit__(self, exc_type, exc, tb):
1554+
await self.transport.close()
1555+
15501556

15511557
try:
15521558
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/metastore_v1/services/dataproc_metastore/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,7 @@ def __init__(
425425
client_cert_source_for_mtls=client_cert_source_func,
426426
quota_project_id=client_options.quota_project_id,
427427
client_info=client_info,
428-
always_use_jwt_access=(
429-
Transport == type(self).get_transport_class("grpc")
430-
or Transport == type(self).get_transport_class("grpc_asyncio")
431-
),
428+
always_use_jwt_access=True,
432429
)
433430

434431
def list_services(
@@ -1783,6 +1780,19 @@ def delete_backup(
17831780
# Done; return the response.
17841781
return response
17851782

1783+
def __enter__(self):
1784+
return self
1785+
1786+
def __exit__(self, type, value, traceback):
1787+
"""Releases underlying transport's resources.
1788+
1789+
.. warning::
1790+
ONLY use as a context manager if the transport is NOT shared
1791+
with other clients! Exiting the with block will CLOSE the transport
1792+
and may cause errors in other clients!
1793+
"""
1794+
self.transport.close()
1795+
17861796

17871797
try:
17881798
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/metastore_v1/services/dataproc_metastore/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ def _prep_wrapped_messages(self, client_info):
207207
),
208208
}
209209

210+
def close(self):
211+
"""Closes resources associated with the transport.
212+
213+
.. warning::
214+
Only call this method if the transport is NOT shared
215+
with other clients - this may cause errors in other clients!
216+
"""
217+
raise NotImplementedError()
218+
210219
@property
211220
def operations_client(self) -> operations_v1.OperationsClient:
212221
"""Return the client designed to process long-running operations."""

google/cloud/metastore_v1/services/dataproc_metastore/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,8 @@ def delete_backup(
655655
)
656656
return self._stubs["delete_backup"]
657657

658+
def close(self):
659+
self.grpc_channel.close()
660+
658661

659662
__all__ = ("DataprocMetastoreGrpcTransport",)

google/cloud/metastore_v1/services/dataproc_metastore/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,5 +685,8 @@ def delete_backup(
685685
)
686686
return self._stubs["delete_backup"]
687687

688+
def close(self):
689+
return self.grpc_channel.close()
690+
688691

689692
__all__ = ("DataprocMetastoreGrpcAsyncIOTransport",)

google/cloud/metastore_v1/types/metastore.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
class Service(proto.Message):
6363
r"""A managed metastore service that serves metadata queries.
64+
6465
Attributes:
6566
hive_metastore_config (google.cloud.metastore_v1.types.HiveMetastoreConfig):
6667
Configuration information specific to running
@@ -219,6 +220,7 @@ class HiveMetastoreConfig(proto.Message):
219220

220221
class KerberosConfig(proto.Message):
221222
r"""Configuration information for a Kerberos principal.
223+
222224
Attributes:
223225
keytab (google.cloud.metastore_v1.types.Secret):
224226
A Kerberos keytab file that can be used to
@@ -242,6 +244,7 @@ class KerberosConfig(proto.Message):
242244

243245
class Secret(proto.Message):
244246
r"""A securely stored value.
247+
245248
Attributes:
246249
cloud_secret (str):
247250
The relative resource name of a Secret Manager secret
@@ -255,6 +258,7 @@ class Secret(proto.Message):
255258

256259
class MetadataManagementActivity(proto.Message):
257260
r"""The metadata management activities of the metastore service.
261+
258262
Attributes:
259263
metadata_exports (Sequence[google.cloud.metastore_v1.types.MetadataExport]):
260264
Output only. The latest metadata exports of
@@ -272,6 +276,7 @@ class MetadataManagementActivity(proto.Message):
272276

273277
class MetadataImport(proto.Message):
274278
r"""A metastore resource that imports metadata.
279+
275280
Attributes:
276281
database_dump (google.cloud.metastore_v1.types.MetadataImport.DatabaseDump):
277282
Immutable. A database dump from a pre-
@@ -345,6 +350,7 @@ class DatabaseType(proto.Enum):
345350

346351
class MetadataExport(proto.Message):
347352
r"""The details of a metadata export operation.
353+
348354
Attributes:
349355
destination_gcs_uri (str):
350356
Output only. A Cloud Storage URI of a folder that metadata
@@ -381,6 +387,7 @@ class State(proto.Enum):
381387

382388
class Backup(proto.Message):
383389
r"""The details of a backup resource.
390+
384391
Attributes:
385392
name (str):
386393
Immutable. The relative resource name of the backup, in the
@@ -425,6 +432,7 @@ class State(proto.Enum):
425432

426433
class Restore(proto.Message):
427434
r"""The details of a metadata restore operation.
435+
428436
Attributes:
429437
start_time (google.protobuf.timestamp_pb2.Timestamp):
430438
Output only. The time when the restore
@@ -1054,6 +1062,7 @@ class ExportMetadataRequest(proto.Message):
10541062

10551063
class RestoreServiceRequest(proto.Message):
10561064
r"""Request message for [DataprocMetastore.Restore][].
1065+
10571066
Attributes:
10581067
service (str):
10591068
Required. The relative resource name of the metastore
@@ -1094,6 +1103,7 @@ class RestoreServiceRequest(proto.Message):
10941103

10951104
class OperationMetadata(proto.Message):
10961105
r"""Represents the metadata of a long-running operation.
1106+
10971107
Attributes:
10981108
create_time (google.protobuf.timestamp_pb2.Timestamp):
10991109
Output only. The time the operation was
@@ -1132,6 +1142,7 @@ class OperationMetadata(proto.Message):
11321142

11331143
class LocationMetadata(proto.Message):
11341144
r"""Metadata about the service in a location.
1145+
11351146
Attributes:
11361147
supported_hive_metastore_versions (Sequence[google.cloud.metastore_v1.types.LocationMetadata.HiveMetastoreVersion]):
11371148
The versions of Hive Metastore that can be used when
@@ -1165,7 +1176,8 @@ class HiveMetastoreVersion(proto.Message):
11651176
class DatabaseDumpSpec(proto.Message):
11661177
r"""The specification of database dump to import from or export
11671178
to.
1168-
"""
1179+
1180+
"""
11691181

11701182
class Type(proto.Enum):
11711183
r"""The type of the database dump."""

google/cloud/metastore_v1alpha/services/dataproc_metastore/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,12 @@ async def delete_backup(
15481548
# Done; return the response.
15491549
return response
15501550

1551+
async def __aenter__(self):
1552+
return self
1553+
1554+
async def __aexit__(self, exc_type, exc, tb):
1555+
await self.transport.close()
1556+
15511557

15521558
try:
15531559
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/metastore_v1alpha/services/dataproc_metastore/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,7 @@ def __init__(
426426
client_cert_source_for_mtls=client_cert_source_func,
427427
quota_project_id=client_options.quota_project_id,
428428
client_info=client_info,
429-
always_use_jwt_access=(
430-
Transport == type(self).get_transport_class("grpc")
431-
or Transport == type(self).get_transport_class("grpc_asyncio")
432-
),
429+
always_use_jwt_access=True,
433430
)
434431

435432
def list_services(
@@ -1784,6 +1781,19 @@ def delete_backup(
17841781
# Done; return the response.
17851782
return response
17861783

1784+
def __enter__(self):
1785+
return self
1786+
1787+
def __exit__(self, type, value, traceback):
1788+
"""Releases underlying transport's resources.
1789+
1790+
.. warning::
1791+
ONLY use as a context manager if the transport is NOT shared
1792+
with other clients! Exiting the with block will CLOSE the transport
1793+
and may cause errors in other clients!
1794+
"""
1795+
self.transport.close()
1796+
17871797

17881798
try:
17891799
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/metastore_v1alpha/services/dataproc_metastore/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ def _prep_wrapped_messages(self, client_info):
207207
),
208208
}
209209

210+
def close(self):
211+
"""Closes resources associated with the transport.
212+
213+
.. warning::
214+
Only call this method if the transport is NOT shared
215+
with other clients - this may cause errors in other clients!
216+
"""
217+
raise NotImplementedError()
218+
210219
@property
211220
def operations_client(self) -> operations_v1.OperationsClient:
212221
"""Return the client designed to process long-running operations."""

google/cloud/metastore_v1alpha/services/dataproc_metastore/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,5 +656,8 @@ def delete_backup(
656656
)
657657
return self._stubs["delete_backup"]
658658

659+
def close(self):
660+
self.grpc_channel.close()
661+
659662

660663
__all__ = ("DataprocMetastoreGrpcTransport",)

0 commit comments

Comments
 (0)