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

Commit b207115

Browse files
feat: add context manager support in client (#129)
- [ ] 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 31f9979 commit b207115

File tree

28 files changed

+303
-14
lines changed

28 files changed

+303
-14
lines changed

google/cloud/osconfig_v1/services/os_config_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,12 @@ async def delete_patch_deployment(
850850
request, retry=retry, timeout=timeout, metadata=metadata,
851851
)
852852

853+
async def __aenter__(self):
854+
return self
855+
856+
async def __aexit__(self, exc_type, exc, tb):
857+
await self.transport.close()
858+
853859

854860
try:
855861
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/osconfig_v1/services/os_config_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,7 @@ def __init__(
380380
client_cert_source_for_mtls=client_cert_source_func,
381381
quota_project_id=client_options.quota_project_id,
382382
client_info=client_info,
383-
always_use_jwt_access=(
384-
Transport == type(self).get_transport_class("grpc")
385-
or Transport == type(self).get_transport_class("grpc_asyncio")
386-
),
383+
always_use_jwt_access=True,
387384
)
388385

389386
def execute_patch_job(
@@ -1066,6 +1063,19 @@ def delete_patch_deployment(
10661063
request, retry=retry, timeout=timeout, metadata=metadata,
10671064
)
10681065

1066+
def __enter__(self):
1067+
return self
1068+
1069+
def __exit__(self, type, value, traceback):
1070+
"""Releases underlying transport's resources.
1071+
1072+
.. warning::
1073+
ONLY use as a context manager if the transport is NOT shared
1074+
with other clients! Exiting the with block will CLOSE the transport
1075+
and may cause errors in other clients!
1076+
"""
1077+
self.transport.close()
1078+
10691079

10701080
try:
10711081
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/osconfig_v1/services/os_config_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ def _prep_wrapped_messages(self, client_info):
193193
),
194194
}
195195

196+
def close(self):
197+
"""Closes resources associated with the transport.
198+
199+
.. warning::
200+
Only call this method if the transport is NOT shared
201+
with other clients - this may cause errors in other clients!
202+
"""
203+
raise NotImplementedError()
204+
196205
@property
197206
def execute_patch_job(
198207
self,

google/cloud/osconfig_v1/services/os_config_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,8 @@ def delete_patch_deployment(
482482
)
483483
return self._stubs["delete_patch_deployment"]
484484

485+
def close(self):
486+
self.grpc_channel.close()
487+
485488

486489
__all__ = ("OsConfigServiceGrpcTransport",)

google/cloud/osconfig_v1/services/os_config_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,5 +490,8 @@ def delete_patch_deployment(
490490
)
491491
return self._stubs["delete_patch_deployment"]
492492

493+
def close(self):
494+
return self.grpc_channel.close()
495+
493496

494497
__all__ = ("OsConfigServiceGrpcAsyncIOTransport",)

google/cloud/osconfig_v1/services/os_config_zonal_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ async def list_vulnerability_reports(
522522
# Done; return the response.
523523
return response
524524

525+
async def __aenter__(self):
526+
return self
527+
528+
async def __aexit__(self, exc_type, exc, tb):
529+
await self.transport.close()
530+
525531

526532
try:
527533
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/osconfig_v1/services/os_config_zonal_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,7 @@ def __init__(
384384
client_cert_source_for_mtls=client_cert_source_func,
385385
quota_project_id=client_options.quota_project_id,
386386
client_info=client_info,
387-
always_use_jwt_access=(
388-
Transport == type(self).get_transport_class("grpc")
389-
or Transport == type(self).get_transport_class("grpc_asyncio")
390-
),
387+
always_use_jwt_access=True,
391388
)
392389

393390
def get_inventory(
@@ -738,6 +735,19 @@ def list_vulnerability_reports(
738735
# Done; return the response.
739736
return response
740737

738+
def __enter__(self):
739+
return self
740+
741+
def __exit__(self, type, value, traceback):
742+
"""Releases underlying transport's resources.
743+
744+
.. warning::
745+
ONLY use as a context manager if the transport is NOT shared
746+
with other clients! Exiting the with block will CLOSE the transport
747+
and may cause errors in other clients!
748+
"""
749+
self.transport.close()
750+
741751

742752
try:
743753
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/osconfig_v1/services/os_config_zonal_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ def _prep_wrapped_messages(self, client_info):
171171
),
172172
}
173173

174+
def close(self):
175+
"""Closes resources associated with the transport.
176+
177+
.. warning::
178+
Only call this method if the transport is NOT shared
179+
with other clients - this may cause errors in other clients!
180+
"""
181+
raise NotImplementedError()
182+
174183
@property
175184
def get_inventory(
176185
self,

google/cloud/osconfig_v1/services/os_config_zonal_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,8 @@ def list_vulnerability_reports(
345345
)
346346
return self._stubs["list_vulnerability_reports"]
347347

348+
def close(self):
349+
self.grpc_channel.close()
350+
348351

349352
__all__ = ("OsConfigZonalServiceGrpcTransport",)

google/cloud/osconfig_v1/services/os_config_zonal_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,5 +349,8 @@ def list_vulnerability_reports(
349349
)
350350
return self._stubs["list_vulnerability_reports"]
351351

352+
def close(self):
353+
return self.grpc_channel.close()
354+
352355

353356
__all__ = ("OsConfigZonalServiceGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)