Skip to content

Commit

Permalink
feat: add context manager support in client (#8)
Browse files Browse the repository at this point in the history
- [ ] 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
  • Loading branch information
gcf-owl-bot[bot] authored Oct 8, 2021
1 parent 5481c42 commit eb132c0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ async def list_migration_subtasks(
# 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 @@ -367,10 +367,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 create_migration_workflow(
Expand Down Expand Up @@ -902,6 +899,19 @@ def list_migration_subtasks(
# 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 @@ -238,6 +238,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 create_migration_workflow(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,5 +428,8 @@ def list_migration_subtasks(
)
return self._stubs["list_migration_subtasks"]

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


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

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


__all__ = ("MigrationServiceGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

class ResourceErrorDetail(proto.Message):
r"""Provides details for errors and the corresponding resources.
Attributes:
resource_info (google.rpc.error_details_pb2.ResourceInfo):
Required. Information about the resource
Expand Down Expand Up @@ -67,6 +68,7 @@ class ErrorDetail(proto.Message):

class ErrorLocation(proto.Message):
r"""Holds information about where the error is located.
Attributes:
line (int):
Optional. If applicable, denotes the line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

class TimeSeries(proto.Message):
r"""The metrics object for a SubTask.
Attributes:
metric (str):
Required. The name of the metric.
Expand Down Expand Up @@ -67,6 +68,7 @@ class TimeSeries(proto.Message):

class Point(proto.Message):
r"""A single data point in a time series.
Attributes:
interval (google.cloud.bigquery_migration_v2alpha.types.TimeInterval):
The time interval to which the data point applies. For
Expand Down Expand Up @@ -109,6 +111,7 @@ class TimeInterval(proto.Message):

class TypedValue(proto.Message):
r"""A single strongly-typed value.
Attributes:
bool_value (bool):
A Boolean value: ``true`` or ``false``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

class CreateMigrationWorkflowRequest(proto.Message):
r"""Request to create a migration workflow resource.
Attributes:
parent (str):
Required. The name of the project to which this migration
Expand All @@ -53,6 +54,7 @@ class CreateMigrationWorkflowRequest(proto.Message):

class GetMigrationWorkflowRequest(proto.Message):
r"""A request to get a previously created migration workflow.
Attributes:
name (str):
Required. The unique identifier for the migration workflow.
Expand All @@ -67,6 +69,7 @@ class GetMigrationWorkflowRequest(proto.Message):

class ListMigrationWorkflowsRequest(proto.Message):
r"""A request to list previously created migration workflows.
Attributes:
parent (str):
Required. The project and location of the migration
Expand Down Expand Up @@ -95,6 +98,7 @@ class ListMigrationWorkflowsRequest(proto.Message):

class ListMigrationWorkflowsResponse(proto.Message):
r"""Response object for a ``ListMigrationWorkflows`` call.
Attributes:
migration_workflows (Sequence[google.cloud.bigquery_migration_v2alpha.types.MigrationWorkflow]):
The migration workflows for the specified
Expand All @@ -117,6 +121,7 @@ def raw_page(self):

class DeleteMigrationWorkflowRequest(proto.Message):
r"""A request to delete a previously created migration workflow.
Attributes:
name (str):
Required. The unique identifier for the migration workflow.
Expand All @@ -128,6 +133,7 @@ class DeleteMigrationWorkflowRequest(proto.Message):

class StartMigrationWorkflowRequest(proto.Message):
r"""A request to start a previously created migration workflow.
Attributes:
name (str):
Required. The unique identifier for the migration workflow.
Expand All @@ -139,6 +145,7 @@ class StartMigrationWorkflowRequest(proto.Message):

class GetMigrationSubtaskRequest(proto.Message):
r"""A request to get a previously created migration subtasks.
Attributes:
name (str):
Required. The unique identifier for the migration subtask.
Expand All @@ -154,6 +161,7 @@ class GetMigrationSubtaskRequest(proto.Message):

class ListMigrationSubtasksRequest(proto.Message):
r"""A request to list previously created migration subtasks.
Attributes:
parent (str):
Required. The migration task of the subtasks to list.
Expand Down Expand Up @@ -188,6 +196,7 @@ class ListMigrationSubtasksRequest(proto.Message):

class ListMigrationSubtasksResponse(proto.Message):
r"""Response object for a ``ListMigrationSubtasks`` call.
Attributes:
migration_subtasks (Sequence[google.cloud.bigquery_migration_v2alpha.types.MigrationSubtask]):
The migration subtasks for the specified
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_migration_v2alpha.services.migration_service import (
Expand Down Expand Up @@ -2554,6 +2555,9 @@ def test_migration_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_migration_service_base_transport_with_credentials_file():
Expand Down Expand Up @@ -3064,3 +3068,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 = MigrationServiceAsyncClient(
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 = MigrationServiceClient(
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 = MigrationServiceClient(
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()

0 comments on commit eb132c0

Please sign in to comment.