Skip to content

Commit bc3e929

Browse files
feat: add context manager support in client (#328)
- [ ] 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
1 parent db08889 commit bc3e929

File tree

34 files changed

+391
-16
lines changed

34 files changed

+391
-16
lines changed

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_read/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ async def split_read_stream(
467467
# Done; return the response.
468468
return response
469469

470+
async def __aenter__(self):
471+
return self
472+
473+
async def __aexit__(self, exc_type, exc, tb):
474+
await self.transport.close()
475+
470476

471477
try:
472478
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_read/client.py

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

390387
def create_read_session(
@@ -660,6 +657,19 @@ def split_read_stream(
660657
# Done; return the response.
661658
return response
662659

660+
def __enter__(self):
661+
return self
662+
663+
def __exit__(self, type, value, traceback):
664+
"""Releases underlying transport's resources.
665+
666+
.. warning::
667+
ONLY use as a context manager if the transport is NOT shared
668+
with other clients! Exiting the with block will CLOSE the transport
669+
and may cause errors in other clients!
670+
"""
671+
self.transport.close()
672+
663673

664674
try:
665675
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_read/transports/base.py

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

208+
def close(self):
209+
"""Closes resources associated with the transport.
210+
211+
.. warning::
212+
Only call this method if the transport is NOT shared
213+
with other clients - this may cause errors in other clients!
214+
"""
215+
raise NotImplementedError()
216+
208217
@property
209218
def create_read_session(
210219
self,

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc.py

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

349+
def close(self):
350+
self.grpc_channel.close()
351+
349352

350353
__all__ = ("BigQueryReadGrpcTransport",)

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,8 @@ def split_read_stream(
351351
)
352352
return self._stubs["split_read_stream"]
353353

354+
def close(self):
355+
return self.grpc_channel.close()
356+
354357

355358
__all__ = ("BigQueryReadGrpcAsyncIOTransport",)

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_write/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ async def flush_rows(
700700
# Done; return the response.
701701
return response
702702

703+
async def __aenter__(self):
704+
return self
705+
706+
async def __aexit__(self, exc_type, exc, tb):
707+
await self.transport.close()
708+
703709

704710
try:
705711
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_write/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,7 @@ def __init__(
365365
client_cert_source_for_mtls=client_cert_source_func,
366366
quota_project_id=client_options.quota_project_id,
367367
client_info=client_info,
368-
always_use_jwt_access=(
369-
Transport == type(self).get_transport_class("grpc")
370-
or Transport == type(self).get_transport_class("grpc_asyncio")
371-
),
368+
always_use_jwt_access=True,
372369
)
373370

374371
def create_write_stream(
@@ -831,6 +828,19 @@ def flush_rows(
831828
# Done; return the response.
832829
return response
833830

831+
def __enter__(self):
832+
return self
833+
834+
def __exit__(self, type, value, traceback):
835+
"""Releases underlying transport's resources.
836+
837+
.. warning::
838+
ONLY use as a context manager if the transport is NOT shared
839+
with other clients! Exiting the with block will CLOSE the transport
840+
and may cause errors in other clients!
841+
"""
842+
self.transport.close()
843+
834844

835845
try:
836846
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_write/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ def _prep_wrapped_messages(self, client_info):
250250
),
251251
}
252252

253+
def close(self):
254+
"""Closes resources associated with the transport.
255+
256+
.. warning::
257+
Only call this method if the transport is NOT shared
258+
with other clients - this may cause errors in other clients!
259+
"""
260+
raise NotImplementedError()
261+
253262
@property
254263
def create_write_stream(
255264
self,

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,5 +444,8 @@ def flush_rows(
444444
)
445445
return self._stubs["flush_rows"]
446446

447+
def close(self):
448+
self.grpc_channel.close()
449+
447450

448451
__all__ = ("BigQueryWriteGrpcTransport",)

packages/google-cloud-bigquery-storage/google/cloud/bigquery_storage_v1/services/big_query_write/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,5 +448,8 @@ def flush_rows(
448448
)
449449
return self._stubs["flush_rows"]
450450

451+
def close(self):
452+
return self.grpc_channel.close()
453+
451454

452455
__all__ = ("BigQueryWriteGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)