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

feat: add context manager support in client #99

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 @@ -938,6 +938,12 @@ async def report_runtime_event(
# 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
Expand Up @@ -353,10 +353,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 list_runtimes(
Expand Down Expand Up @@ -1126,6 +1123,19 @@ def report_runtime_event(
# 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 @@ -186,6 +186,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 @@ -495,5 +495,8 @@ def report_runtime_event(
)
return self._stubs["report_runtime_event"]

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


__all__ = ("ManagedNotebookServiceGrpcTransport",)
Expand Up @@ -513,5 +513,8 @@ def report_runtime_event(
)
return self._stubs["report_runtime_event"]

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


__all__ = ("ManagedNotebookServiceGrpcAsyncIOTransport",)
Expand Up @@ -2530,6 +2530,12 @@ async def create_execution(
# 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/notebooks_v1/services/notebook_service/client.py
Expand Up @@ -400,10 +400,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 list_instances(
Expand Down Expand Up @@ -2778,6 +2775,19 @@ def create_execution(
# 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 @@ -270,6 +270,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 @@ -1099,5 +1099,8 @@ def create_execution(
)
return self._stubs["create_execution"]

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


__all__ = ("NotebookServiceGrpcTransport",)
Expand Up @@ -1146,5 +1146,8 @@ def create_execution(
)
return self._stubs["create_execution"]

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


__all__ = ("NotebookServiceGrpcAsyncIOTransport",)
3 changes: 3 additions & 0 deletions google/cloud/notebooks_v1/types/execution.py
Expand Up @@ -25,6 +25,7 @@

class ExecutionTemplate(proto.Message):
r"""The description a notebook execution workload.

Attributes:
scale_tier (google.cloud.notebooks_v1.types.ExecutionTemplate.ScaleTier):
Required. Scale tier of the hardware used for
Expand Down Expand Up @@ -174,6 +175,7 @@ class SchedulerAcceleratorConfig(proto.Message):

class DataprocParameters(proto.Message):
r"""Parameters used in Dataproc JobType executions.

Attributes:
cluster (str):
URI for cluster used to run Dataproc execution. Format:
Expand Down Expand Up @@ -202,6 +204,7 @@ class DataprocParameters(proto.Message):

class Execution(proto.Message):
r"""The definition of a single executed notebook.

Attributes:
execution_template (google.cloud.notebooks_v1.types.ExecutionTemplate):
execute metadata including name, hardware
Expand Down
1 change: 1 addition & 0 deletions google/cloud/notebooks_v1/types/instance_config.py
Expand Up @@ -23,6 +23,7 @@

class InstanceConfig(proto.Message):
r"""Notebook instance configurations that can be updated.

Attributes:
notebook_upgrade_schedule (str):
Cron expression in UTC timezone, used to schedule instance
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/notebooks_v1/types/managed_service.py
Expand Up @@ -38,6 +38,7 @@

class ListRuntimesRequest(proto.Message):
r"""Request for listing Managed Notebook Runtimes.

Attributes:
parent (str):
Required. Format:
Expand All @@ -56,6 +57,7 @@ class ListRuntimesRequest(proto.Message):

class ListRuntimesResponse(proto.Message):
r"""Response for listing Managed Notebook Runtimes.

Attributes:
runtimes (Sequence[google.cloud.notebooks_v1.types.Runtime]):
A list of returned Runtimes.
Expand All @@ -82,6 +84,7 @@ def raw_page(self):

class GetRuntimeRequest(proto.Message):
r"""Request for getting a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -93,6 +96,7 @@ class GetRuntimeRequest(proto.Message):

class CreateRuntimeRequest(proto.Message):
r"""Request for creating a Managed Notebook Runtime.

Attributes:
parent (str):
Required. Format:
Expand All @@ -111,6 +115,7 @@ class CreateRuntimeRequest(proto.Message):

class DeleteRuntimeRequest(proto.Message):
r"""Request for deleting a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -122,6 +127,7 @@ class DeleteRuntimeRequest(proto.Message):

class StartRuntimeRequest(proto.Message):
r"""Request for starting a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -133,6 +139,7 @@ class StartRuntimeRequest(proto.Message):

class StopRuntimeRequest(proto.Message):
r"""Request for stopping a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -144,6 +151,7 @@ class StopRuntimeRequest(proto.Message):

class SwitchRuntimeRequest(proto.Message):
r"""Request for switching a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -163,6 +171,7 @@ class SwitchRuntimeRequest(proto.Message):

class ResetRuntimeRequest(proto.Message):
r"""Request for reseting a Managed Notebook Runtime.

Attributes:
name (str):
Required. Format:
Expand All @@ -174,6 +183,7 @@ class ResetRuntimeRequest(proto.Message):

class ReportRuntimeEventRequest(proto.Message):
r"""Request for reporting a Managed Notebook Event.

Attributes:
name (str):
Required. Format:
Expand Down
5 changes: 5 additions & 0 deletions google/cloud/notebooks_v1/types/runtime.py
Expand Up @@ -39,6 +39,7 @@

class Runtime(proto.Message):
r"""The definition of a Runtime for a managed notebook instance.

Attributes:
name (str):
Output only. The resource name of the runtime. Format:
Expand Down Expand Up @@ -158,6 +159,7 @@ class EncryptionConfig(proto.Message):

class LocalDisk(proto.Message):
r"""An Local attached disk resource.

Attributes:
auto_delete (bool):
Optional. Output only. Specifies whether the
Expand Down Expand Up @@ -331,6 +333,7 @@ class DiskType(proto.Enum):

class RuntimeAccessConfig(proto.Message):
r"""Specifies the login configuration for Runtime

Attributes:
access_type (google.cloud.notebooks_v1.types.RuntimeAccessConfig.RuntimeAccessType):
The type of access mode this instance.
Expand Down Expand Up @@ -446,6 +449,7 @@ class RuntimeShieldedInstanceConfig(proto.Message):

class VirtualMachine(proto.Message):
r"""Runtime using Virtual Machine for computing.

Attributes:
instance_name (str):
Output only. The user-friendly name of the
Expand All @@ -466,6 +470,7 @@ class VirtualMachine(proto.Message):

class VirtualMachineConfig(proto.Message):
r"""The config settings for virtual machine.

Attributes:
zone (str):
Output only. The zone where the virtual machine is located.
Expand Down
1 change: 1 addition & 0 deletions google/cloud/notebooks_v1/types/schedule.py
Expand Up @@ -26,6 +26,7 @@

class Schedule(proto.Message):
r"""The definition of a schedule.

Attributes:
name (str):
Output only. The name of this schedule. Format:
Expand Down