Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

feat: add context manager support in client #84

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
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ async def delete_attestor(
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 @@ -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 get_policy(
Expand Down Expand Up @@ -956,6 +953,19 @@ def delete_attestor(
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 @@ -251,6 +251,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 get_policy(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,5 +450,8 @@ def delete_attestor(
)
return self._stubs["delete_attestor"]

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


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

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


__all__ = ("BinauthzManagementServiceV1GrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ async def get_system_policy(
# 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 @@ -340,10 +340,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_system_policy(
Expand Down Expand Up @@ -419,6 +416,19 @@ def get_system_policy(
# 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 @@ -160,6 +160,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 get_system_policy(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,8 @@ def get_system_policy(
)
return self._stubs["get_system_policy"]

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


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

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


__all__ = ("SystemPolicyV1GrpcAsyncIOTransport",)
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ async def validate_attestation_occurrence(
# 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 @@ -329,10 +329,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 validate_attestation_occurrence(
Expand Down Expand Up @@ -388,6 +385,19 @@ def validate_attestation_occurrence(
# 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 @@ -161,6 +161,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 validate_attestation_occurrence(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,8 @@ def validate_attestation_occurrence(
)
return self._stubs["validate_attestation_occurrence"]

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


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

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


__all__ = ("ValidationHelperV1GrpcAsyncIOTransport",)
9 changes: 9 additions & 0 deletions google/cloud/binaryauthorization_v1/types/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

class GetPolicyRequest(proto.Message):
r"""Request message for [BinauthzManagementService.GetPolicy][].

Attributes:
name (str):
Required. The resource name of the
Expand All @@ -51,6 +52,7 @@ class GetPolicyRequest(proto.Message):

class UpdatePolicyRequest(proto.Message):
r"""Request message for [BinauthzManagementService.UpdatePolicy][].

Attributes:
policy (google.cloud.binaryauthorization_v1.types.Policy):
Required. A new or updated
Expand All @@ -66,6 +68,7 @@ class UpdatePolicyRequest(proto.Message):

class CreateAttestorRequest(proto.Message):
r"""Request message for [BinauthzManagementService.CreateAttestor][].

Attributes:
parent (str):
Required. The parent of this
Expand All @@ -90,6 +93,7 @@ class CreateAttestorRequest(proto.Message):

class GetAttestorRequest(proto.Message):
r"""Request message for [BinauthzManagementService.GetAttestor][].

Attributes:
name (str):
Required. The name of the
Expand All @@ -102,6 +106,7 @@ class GetAttestorRequest(proto.Message):

class UpdateAttestorRequest(proto.Message):
r"""Request message for [BinauthzManagementService.UpdateAttestor][].

Attributes:
attestor (google.cloud.binaryauthorization_v1.types.Attestor):
Required. The updated
Expand All @@ -117,6 +122,7 @@ class UpdateAttestorRequest(proto.Message):

class ListAttestorsRequest(proto.Message):
r"""Request message for [BinauthzManagementService.ListAttestors][].

Attributes:
parent (str):
Required. The resource name of the project associated with
Expand All @@ -142,6 +148,7 @@ class ListAttestorsRequest(proto.Message):

class ListAttestorsResponse(proto.Message):
r"""Response message for [BinauthzManagementService.ListAttestors][].

Attributes:
attestors (Sequence[google.cloud.binaryauthorization_v1.types.Attestor]):
The list of
Expand All @@ -166,6 +173,7 @@ def raw_page(self):

class DeleteAttestorRequest(proto.Message):
r"""Request message for [BinauthzManagementService.DeleteAttestor][].

Attributes:
name (str):
Required. The name of the
Expand All @@ -178,6 +186,7 @@ class DeleteAttestorRequest(proto.Message):

class GetSystemPolicyRequest(proto.Message):
r"""Request to read the current system policy.

Attributes:
name (str):
Required. The resource name, in the format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,12 @@ async def delete_attestor(
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 @@ -374,10 +374,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_policy(
Expand Down Expand Up @@ -965,6 +962,19 @@ def delete_attestor(
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 @@ -251,6 +251,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 get_policy(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,5 +456,8 @@ def delete_attestor(
)
return self._stubs["delete_attestor"]

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


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

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


__all__ = ("BinauthzManagementServiceV1Beta1GrpcAsyncIOTransport",)
Loading