Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context manager support in client #255

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 @@ -1641,6 +1641,12 @@ async def list_reservation_topics(
# 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/pubsublite_v1/services/admin_service/client.py
Expand Up @@ -381,10 +381,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_topic(
Expand Down Expand Up @@ -1861,6 +1858,19 @@ def list_reservation_topics(
# 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 @@ -223,6 +223,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 @@ -767,5 +767,8 @@ def list_reservation_topics(
)
return self._stubs["list_reservation_topics"]

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


__all__ = ("AdminServiceGrpcTransport",)
Expand Up @@ -784,5 +784,8 @@ def list_reservation_topics(
)
return self._stubs["list_reservation_topics"]

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


__all__ = ("AdminServiceGrpcAsyncIOTransport",)
Expand Up @@ -370,6 +370,12 @@ async def list_partition_cursors(
# 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/pubsublite_v1/services/cursor_service/client.py
Expand Up @@ -346,10 +346,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 streaming_commit_cursor(
Expand Down Expand Up @@ -519,6 +516,19 @@ def list_partition_cursors(
# 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 @@ -197,6 +197,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 streaming_commit_cursor(
self,
Expand Down
Expand Up @@ -312,5 +312,8 @@ def list_partition_cursors(
)
return self._stubs["list_partition_cursors"]

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


__all__ = ("CursorServiceGrpcTransport",)
Expand Up @@ -317,5 +317,8 @@ def list_partition_cursors(
)
return self._stubs["list_partition_cursors"]

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


__all__ = ("CursorServiceGrpcAsyncIOTransport",)
Expand Up @@ -228,6 +228,12 @@ def assign_partitions(
# 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 @@ -331,10 +331,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 assign_partitions(
Expand Down Expand Up @@ -385,6 +382,19 @@ def assign_partitions(
# 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 @@ -159,6 +159,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 assign_partitions(
self,
Expand Down
Expand Up @@ -262,5 +262,8 @@ def assign_partitions(
)
return self._stubs["assign_partitions"]

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


__all__ = ("PartitionAssignmentServiceGrpcTransport",)
Expand Up @@ -268,5 +268,8 @@ def assign_partitions(
)
return self._stubs["assign_partitions"]

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


__all__ = ("PartitionAssignmentServiceGrpcAsyncIOTransport",)
Expand Up @@ -219,6 +219,12 @@ def publish(
# 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/pubsublite_v1/services/publisher_service/client.py
Expand Up @@ -332,10 +332,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 publish(
Expand Down Expand Up @@ -381,6 +378,19 @@ def publish(
# 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 @@ -159,6 +159,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 publish(
self,
Expand Down
Expand Up @@ -263,5 +263,8 @@ def publish(
)
return self._stubs["publish"]

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


__all__ = ("PublisherServiceGrpcTransport",)
Expand Up @@ -266,5 +266,8 @@ def publish(
)
return self._stubs["publish"]

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


__all__ = ("PublisherServiceGrpcAsyncIOTransport",)
Expand Up @@ -209,6 +209,12 @@ def subscribe(
# 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 @@ -331,10 +331,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 subscribe(
Expand Down Expand Up @@ -373,6 +370,19 @@ def subscribe(
# 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 @@ -159,6 +159,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 subscribe(
self,
Expand Down
Expand Up @@ -253,5 +253,8 @@ def subscribe(
)
return self._stubs["subscribe"]

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


__all__ = ("SubscriberServiceGrpcTransport",)
Expand Up @@ -258,5 +258,8 @@ def subscribe(
)
return self._stubs["subscribe"]

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


__all__ = ("SubscriberServiceGrpcAsyncIOTransport",)