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

feat: add context manager support in client #234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,12 @@ async def check_valid_creds(
# Done; return the response.
return response

async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc, tb):
await self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,7 @@ def __init__(
client_cert_source_for_mtls=client_cert_source_func,
quota_project_id=client_options.quota_project_id,
client_info=client_info,
always_use_jwt_access=(
Transport == type(self).get_transport_class("grpc")
or Transport == type(self).get_transport_class("grpc_asyncio")
),
always_use_jwt_access=True,
)

def get_data_source(
Expand Down Expand Up @@ -1519,6 +1516,19 @@ def check_valid_creds(
# Done; return the response.
return response

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Releases underlying transport's resources.

.. warning::
ONLY use as a context manager if the transport is NOT shared
with other clients! Exiting the with block will CLOSE the transport
and may cause errors in other clients!
"""
self.transport.close()


try:
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ def _prep_wrapped_messages(self, client_info):
),
}

def close(self):
"""Closes resources associated with the transport.

.. warning::
Only call this method if the transport is NOT shared
with other clients - this may cause errors in other clients!
"""
raise NotImplementedError()

@property
def get_data_source(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,5 +631,8 @@ def check_valid_creds(
)
return self._stubs["check_valid_creds"]

def close(self):
self.grpc_channel.close()


__all__ = ("DataTransferServiceGrpcTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -650,5 +650,8 @@ def check_valid_creds(
)
return self._stubs["check_valid_creds"]

def close(self):
return self.grpc_channel.close()


__all__ = ("DataTransferServiceGrpcAsyncIOTransport",)
12 changes: 12 additions & 0 deletions google/cloud/bigquery_datatransfer_v1/types/datatransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class DataRefreshType(proto.Enum):

class GetDataSourceRequest(proto.Message):
r"""A request to get data source info.

Attributes:
name (str):
Required. The field will contain name of the resource
Expand Down Expand Up @@ -275,6 +276,7 @@ class ListDataSourcesRequest(proto.Message):

class ListDataSourcesResponse(proto.Message):
r"""Returns list of supported data sources and their metadata.

Attributes:
data_sources (Sequence[google.cloud.bigquery_datatransfer_v1.types.DataSource]):
List of supported data sources and their
Expand Down Expand Up @@ -419,6 +421,7 @@ class UpdateTransferConfigRequest(proto.Message):

class GetTransferConfigRequest(proto.Message):
r"""A request to get data transfer information.

Attributes:
name (str):
Required. The field will contain name of the resource
Expand Down Expand Up @@ -447,6 +450,7 @@ class DeleteTransferConfigRequest(proto.Message):

class GetTransferRunRequest(proto.Message):
r"""A request to get data transfer run information.

Attributes:
name (str):
Required. The field will contain name of the resource
Expand All @@ -461,6 +465,7 @@ class GetTransferRunRequest(proto.Message):

class DeleteTransferRunRequest(proto.Message):
r"""A request to delete data transfer run information.

Attributes:
name (str):
Required. The field will contain name of the resource
Expand Down Expand Up @@ -504,6 +509,7 @@ class ListTransferConfigsRequest(proto.Message):

class ListTransferConfigsResponse(proto.Message):
r"""The returned list of pipelines in the project.

Attributes:
transfer_configs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferConfig]):
Output only. The stored pipeline transfer
Expand Down Expand Up @@ -568,6 +574,7 @@ class RunAttempt(proto.Enum):

class ListTransferRunsResponse(proto.Message):
r"""The returned list of pipelines in the project.

Attributes:
transfer_runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]):
Output only. The stored pipeline transfer
Expand Down Expand Up @@ -624,6 +631,7 @@ class ListTransferLogsRequest(proto.Message):

class ListTransferLogsResponse(proto.Message):
r"""The returned list transfer run messages.

Attributes:
transfer_messages (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferMessage]):
Output only. The stored pipeline transfer
Expand Down Expand Up @@ -678,6 +686,7 @@ class CheckValidCredsResponse(proto.Message):

class ScheduleTransferRunsRequest(proto.Message):
r"""A request to schedule transfer runs for a time range.

Attributes:
parent (str):
Required. Transfer configuration name in the form:
Expand All @@ -698,6 +707,7 @@ class ScheduleTransferRunsRequest(proto.Message):

class ScheduleTransferRunsResponse(proto.Message):
r"""A response to schedule transfer runs for a time range.

Attributes:
runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]):
The transfer runs that were scheduled.
Expand All @@ -708,6 +718,7 @@ class ScheduleTransferRunsResponse(proto.Message):

class StartManualTransferRunsRequest(proto.Message):
r"""A request to start manual transfer runs.

Attributes:
parent (str):
Transfer configuration name in the form:
Expand Down Expand Up @@ -759,6 +770,7 @@ class TimeRange(proto.Message):

class StartManualTransferRunsResponse(proto.Message):
r"""A response to start manual transfer runs.

Attributes:
runs (Sequence[google.cloud.bigquery_datatransfer_v1.types.TransferRun]):
The transfer runs that were created.
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/bigquery_datatransfer_v1/types/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class EmailPreferences(proto.Message):

class ScheduleOptions(proto.Message):
r"""Options customizing the data transfer schedule.

Attributes:
disable_auto_scheduling (bool):
If true, automatic scheduling of data
Expand Down Expand Up @@ -205,6 +206,7 @@ class TransferConfig(proto.Message):

class TransferRun(proto.Message):
r"""Represents a data transfer run.

Attributes:
name (str):
The resource name of the transfer run. Transfer run names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from google.api_core import gapic_v1
from google.api_core import grpc_helpers
from google.api_core import grpc_helpers_async
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.bigquery_datatransfer_v1.services.data_transfer_service import (
Expand Down Expand Up @@ -4589,6 +4590,9 @@ def test_data_transfer_service_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

with pytest.raises(NotImplementedError):
transport.close()


@requires_google_auth_gte_1_25_0
def test_data_transfer_service_base_transport_with_credentials_file():
Expand Down Expand Up @@ -5117,3 +5121,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
)
prep.assert_called_once_with(client_info)


@pytest.mark.asyncio
async def test_transport_close_async():
client = DataTransferServiceAsyncClient(
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
)
with mock.patch.object(
type(getattr(client.transport, "grpc_channel")), "close"
) as close:
async with client:
close.assert_not_called()
close.assert_called_once()


def test_transport_close():
transports = {
"grpc": "_grpc_channel",
}

for transport, close_name in transports.items():
client = DataTransferServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
with mock.patch.object(
type(getattr(client.transport, close_name)), "close"
) as close:
with client:
close.assert_not_called()
close.assert_called_once()


def test_client_ctx():
transports = [
"grpc",
]
for transport in transports:
client = DataTransferServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
# Test client calls underlying transport.
with mock.patch.object(type(client.transport), "close") as close:
close.assert_not_called()
with client:
pass
close.assert_called()