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

Commit 9ec2297

Browse files
feat: add context manager support in client (#122)
- [ ] 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 84327a9 commit 9ec2297

14 files changed

Lines changed: 186 additions & 8 deletions

File tree

google/cloud/billing_v1/services/cloud_billing/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,12 @@ async def test_iam_permissions(
11721172
# Done; return the response.
11731173
return response
11741174

1175+
async def __aenter__(self):
1176+
return self
1177+
1178+
async def __aexit__(self, exc_type, exc, tb):
1179+
await self.transport.close()
1180+
11751181

11761182
try:
11771183
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/billing_v1/services/cloud_billing/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,7 @@ def __init__(
330330
client_cert_source_for_mtls=client_cert_source_func,
331331
quota_project_id=client_options.quota_project_id,
332332
client_info=client_info,
333-
always_use_jwt_access=(
334-
Transport == type(self).get_transport_class("grpc")
335-
or Transport == type(self).get_transport_class("grpc_asyncio")
336-
),
333+
always_use_jwt_access=True,
337334
)
338335

339336
def get_billing_account(
@@ -1261,6 +1258,19 @@ def test_iam_permissions(
12611258
# Done; return the response.
12621259
return response
12631260

1261+
def __enter__(self):
1262+
return self
1263+
1264+
def __exit__(self, type, value, traceback):
1265+
"""Releases underlying transport's resources.
1266+
1267+
.. warning::
1268+
ONLY use as a context manager if the transport is NOT shared
1269+
with other clients! Exiting the with block will CLOSE the transport
1270+
and may cause errors in other clients!
1271+
"""
1272+
self.transport.close()
1273+
12641274

12651275
try:
12661276
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/billing_v1/services/cloud_billing/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ def _prep_wrapped_messages(self, client_info):
296296
),
297297
}
298298

299+
def close(self):
300+
"""Closes resources associated with the transport.
301+
302+
.. warning::
303+
Only call this method if the transport is NOT shared
304+
with other clients - this may cause errors in other clients!
305+
"""
306+
raise NotImplementedError()
307+
299308
@property
300309
def get_billing_account(
301310
self,

google/cloud/billing_v1/services/cloud_billing/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,5 +578,8 @@ def test_iam_permissions(
578578
)
579579
return self._stubs["test_iam_permissions"]
580580

581+
def close(self):
582+
self.grpc_channel.close()
583+
581584

582585
__all__ = ("CloudBillingGrpcTransport",)

google/cloud/billing_v1/services/cloud_billing/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,5 +585,8 @@ def test_iam_permissions(
585585
)
586586
return self._stubs["test_iam_permissions"]
587587

588+
def close(self):
589+
return self.grpc_channel.close()
590+
588591

589592
__all__ = ("CloudBillingGrpcAsyncIOTransport",)

google/cloud/billing_v1/services/cloud_catalog/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ async def list_skus(
291291
# Done; return the response.
292292
return response
293293

294+
async def __aenter__(self):
295+
return self
296+
297+
async def __aexit__(self, exc_type, exc, tb):
298+
await self.transport.close()
299+
294300

295301
try:
296302
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/billing_v1/services/cloud_catalog/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,7 @@ def __init__(
351351
client_cert_source_for_mtls=client_cert_source_func,
352352
quota_project_id=client_options.quota_project_id,
353353
client_info=client_info,
354-
always_use_jwt_access=(
355-
Transport == type(self).get_transport_class("grpc")
356-
or Transport == type(self).get_transport_class("grpc_asyncio")
357-
),
354+
always_use_jwt_access=True,
358355
)
359356

360357
def list_services(
@@ -487,6 +484,19 @@ def list_skus(
487484
# Done; return the response.
488485
return response
489486

487+
def __enter__(self):
488+
return self
489+
490+
def __exit__(self, type, value, traceback):
491+
"""Releases underlying transport's resources.
492+
493+
.. warning::
494+
ONLY use as a context manager if the transport is NOT shared
495+
with other clients! Exiting the with block will CLOSE the transport
496+
and may cause errors in other clients!
497+
"""
498+
self.transport.close()
499+
490500

491501
try:
492502
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/billing_v1/services/cloud_catalog/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def _prep_wrapped_messages(self, client_info):
160160
),
161161
}
162162

163+
def close(self):
164+
"""Closes resources associated with the transport.
165+
166+
.. warning::
167+
Only call this method if the transport is NOT shared
168+
with other clients - this may cause errors in other clients!
169+
"""
170+
raise NotImplementedError()
171+
163172
@property
164173
def list_services(
165174
self,

google/cloud/billing_v1/services/cloud_catalog/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,8 @@ def list_skus(
282282
)
283283
return self._stubs["list_skus"]
284284

285+
def close(self):
286+
self.grpc_channel.close()
287+
285288

286289
__all__ = ("CloudCatalogGrpcTransport",)

google/cloud/billing_v1/services/cloud_catalog/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,8 @@ def list_skus(
288288
)
289289
return self._stubs["list_skus"]
290290

291+
def close(self):
292+
return self.grpc_channel.close()
293+
291294

292295
__all__ = ("CloudCatalogGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)