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

feat: add context manager support in client #416

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
6 changes: 6 additions & 0 deletions google/cloud/dialogflow_v2/services/agents/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,12 @@ async def get_validation_result(
# 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/dialogflow_v2/services/agents/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,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_agent(
Expand Down Expand Up @@ -1110,6 +1107,19 @@ def get_validation_result(
# 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
9 changes: 9 additions & 0 deletions google/cloud/dialogflow_v2/services/agents/transports/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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
3 changes: 3 additions & 0 deletions google/cloud/dialogflow_v2/services/agents/transports/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,5 +565,8 @@ def get_validation_result(
)
return self._stubs["get_validation_result"]

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


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

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


__all__ = ("AgentsGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ async def update_answer_record(
# 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/dialogflow_v2/services/answer_records/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,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_answer_records(
Expand Down Expand Up @@ -545,6 +542,19 @@ def update_answer_record(
# 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 @@ -168,6 +168,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 list_answer_records(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,8 @@ def update_answer_record(
)
return self._stubs["update_answer_record"]

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


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

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


__all__ = ("AnswerRecordsGrpcAsyncIOTransport",)
6 changes: 6 additions & 0 deletions google/cloud/dialogflow_v2/services/contexts/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,12 @@ async def delete_all_contexts(
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/dialogflow_v2/services/contexts/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,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_contexts(
Expand Down Expand Up @@ -867,6 +864,19 @@ def delete_all_contexts(
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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 list_contexts(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,8 @@ def delete_all_contexts(
)
return self._stubs["delete_all_contexts"]

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


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

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


__all__ = ("ContextsGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ async def delete_conversation_profile(
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
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,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_conversation_profiles(
Expand Down Expand Up @@ -842,6 +839,19 @@ def delete_conversation_profile(
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,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 list_conversation_profiles(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,8 @@ def delete_conversation_profile(
)
return self._stubs["delete_conversation_profile"]

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


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

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


__all__ = ("ConversationProfilesGrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,12 @@ async def list_messages(
# 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/dialogflow_v2/services/conversations/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,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_conversation(
Expand Down Expand Up @@ -811,6 +808,19 @@ def list_messages(
# 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 @@ -177,6 +177,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_conversation(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,5 +392,8 @@ def list_messages(
)
return self._stubs["list_messages"]

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


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

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


__all__ = ("ConversationsGrpcAsyncIOTransport",)
Loading