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 #285

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -621,6 +621,12 @@ async def delete_autoscaling_policy(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
Expand Up @@ -350,10 +350,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_autoscaling_policy(
Expand Down Expand Up @@ -791,6 +788,19 @@ def delete_autoscaling_policy(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
Expand Up @@ -210,6 +210,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_autoscaling_policy(
self,
Expand Down
Expand Up @@ -376,5 +376,8 @@ def delete_autoscaling_policy(
)
return self._stubs["delete_autoscaling_policy"]

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


__all__ = ("AutoscalingPolicyServiceGrpcTransport",)
Expand Up @@ -380,5 +380,8 @@ def delete_autoscaling_policy(
)
return self._stubs["delete_autoscaling_policy"]

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


__all__ = ("AutoscalingPolicyServiceGrpcAsyncIOTransport",)
Expand Up @@ -1015,6 +1015,12 @@ async def diagnose_cluster(
# 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
18 changes: 14 additions & 4 deletions google/cloud/dataproc_v1/services/cluster_controller/client.py
Expand Up @@ -369,10 +369,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_cluster(
Expand Down Expand Up @@ -1164,6 +1161,19 @@ def diagnose_cluster(
# 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
Expand Up @@ -250,6 +250,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Expand Up @@ -467,5 +467,8 @@ def diagnose_cluster(
)
return self._stubs["diagnose_cluster"]

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


__all__ = ("ClusterControllerGrpcTransport",)
Expand Up @@ -478,5 +478,8 @@ def diagnose_cluster(
)
return self._stubs["diagnose_cluster"]

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


__all__ = ("ClusterControllerGrpcAsyncIOTransport",)
Expand Up @@ -798,6 +798,12 @@ async def delete_job(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
18 changes: 14 additions & 4 deletions google/cloud/dataproc_v1/services/job_controller/client.py
Expand Up @@ -328,10 +328,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 submit_job(
Expand Down Expand Up @@ -903,6 +900,19 @@ def delete_job(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
Expand Up @@ -261,6 +261,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Expand Up @@ -419,5 +419,8 @@ def delete_job(self) -> Callable[[jobs.DeleteJobRequest], empty_pb2.Empty]:
)
return self._stubs["delete_job"]

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


__all__ = ("JobControllerGrpcTransport",)
Expand Up @@ -428,5 +428,8 @@ def delete_job(
)
return self._stubs["delete_job"]

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


__all__ = ("JobControllerGrpcAsyncIOTransport",)
Expand Up @@ -948,6 +948,12 @@ async def delete_workflow_template(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
Expand Up @@ -387,10 +387,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_workflow_template(
Expand Down Expand Up @@ -1106,6 +1103,19 @@ def delete_workflow_template(
request, retry=retry, timeout=timeout, metadata=metadata,
)

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
Expand Up @@ -259,6 +259,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 operations_client(self) -> operations_v1.OperationsClient:
"""Return the client designed to process long-running operations."""
Expand Down
Expand Up @@ -500,5 +500,8 @@ def delete_workflow_template(
)
return self._stubs["delete_workflow_template"]

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


__all__ = ("WorkflowTemplateServiceGrpcTransport",)
Expand Up @@ -507,5 +507,8 @@ def delete_workflow_template(
)
return self._stubs["delete_workflow_template"]

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


__all__ = ("WorkflowTemplateServiceGrpcAsyncIOTransport",)
6 changes: 6 additions & 0 deletions google/cloud/dataproc_v1/types/autoscaling_policies.py
Expand Up @@ -84,6 +84,7 @@ class AutoscalingPolicy(proto.Message):

class BasicAutoscalingAlgorithm(proto.Message):
r"""Basic algorithm for autoscaling.

Attributes:
yarn_config (google.cloud.dataproc_v1.types.BasicYarnAutoscalingConfig):
Required. YARN autoscaling configuration.
Expand All @@ -105,6 +106,7 @@ class BasicAutoscalingAlgorithm(proto.Message):

class BasicYarnAutoscalingConfig(proto.Message):
r"""Basic autoscaling configurations for YARN.

Attributes:
graceful_decommission_timeout (google.protobuf.duration_pb2.Duration):
Required. Timeout for YARN graceful decommissioning of Node
Expand Down Expand Up @@ -216,6 +218,7 @@ class InstanceGroupAutoscalingPolicyConfig(proto.Message):

class CreateAutoscalingPolicyRequest(proto.Message):
r"""A request to create an autoscaling policy.

Attributes:
parent (str):
Required. The "resource name" of the region or location, as
Expand All @@ -239,6 +242,7 @@ class CreateAutoscalingPolicyRequest(proto.Message):

class GetAutoscalingPolicyRequest(proto.Message):
r"""A request to fetch an autoscaling policy.

Attributes:
name (str):
Required. The "resource name" of the autoscaling policy, as
Expand All @@ -259,6 +263,7 @@ class GetAutoscalingPolicyRequest(proto.Message):

class UpdateAutoscalingPolicyRequest(proto.Message):
r"""A request to update an autoscaling policy.

Attributes:
policy (google.cloud.dataproc_v1.types.AutoscalingPolicy):
Required. The updated autoscaling policy.
Expand Down Expand Up @@ -292,6 +297,7 @@ class DeleteAutoscalingPolicyRequest(proto.Message):

class ListAutoscalingPoliciesRequest(proto.Message):
r"""A request to list autoscaling policies in a project.

Attributes:
parent (str):
Required. The "resource name" of the region or location, as
Expand Down