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

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 @@ -971,6 +971,12 @@ async def mark_recommendation_failed(
# 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/recommender_v1/services/recommender/client.py
Expand Up @@ -410,10 +410,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_insights(
Expand Down Expand Up @@ -1184,6 +1181,19 @@ def mark_recommendation_failed(
# 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 @@ -238,6 +238,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_insights(
self,
Expand Down
Expand Up @@ -500,5 +500,8 @@ def mark_recommendation_failed(
)
return self._stubs["mark_recommendation_failed"]

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


__all__ = ("RecommenderGrpcTransport",)
Expand Up @@ -506,5 +506,8 @@ def mark_recommendation_failed(
)
return self._stubs["mark_recommendation_failed"]

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


__all__ = ("RecommenderGrpcAsyncIOTransport",)
2 changes: 2 additions & 0 deletions google/cloud/recommender_v1/types/insight.py
Expand Up @@ -73,6 +73,7 @@ class Category(proto.Enum):

class RecommendationReference(proto.Message):
r"""Reference to an associated recommendation.

Attributes:
recommendation (str):
Recommendation resource name, e.g.
Expand Down Expand Up @@ -102,6 +103,7 @@ class RecommendationReference(proto.Message):

class InsightStateInfo(proto.Message):
r"""Information related to insight state.

Attributes:
state (google.cloud.recommender_v1.types.InsightStateInfo.State):
Insight state.
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/recommender_v1/types/recommendation.py
Expand Up @@ -87,6 +87,7 @@ class Recommendation(proto.Message):

class InsightReference(proto.Message):
r"""Reference to an associated insight.

Attributes:
insight (str):
Insight resource name, e.g.
Expand Down Expand Up @@ -132,6 +133,7 @@ class RecommendationContent(proto.Message):

class OperationGroup(proto.Message):
r"""Group of operations that need to be performed atomically.

Attributes:
operations (Sequence[google.cloud.recommender_v1.types.Operation]):
List of operations across one or more
Expand Down Expand Up @@ -301,6 +303,7 @@ class Category(proto.Enum):

class RecommendationStateInfo(proto.Message):
r"""Information for state. Contains state and metadata.

Attributes:
state (google.cloud.recommender_v1.types.RecommendationStateInfo.State):
The state of the recommendation, Eg ACTIVE,
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/recommender_v1/types/recommender_service.py
Expand Up @@ -38,6 +38,7 @@

class ListInsightsRequest(proto.Message):
r"""Request for the ``ListInsights`` method.

Attributes:
parent (str):
Required. The container resource on which to execute the
Expand Down Expand Up @@ -76,6 +77,7 @@ class ListInsightsRequest(proto.Message):

class ListInsightsResponse(proto.Message):
r"""Response to the ``ListInsights`` method.

Attributes:
insights (Sequence[google.cloud.recommender_v1.types.Insight]):
The set of insights for the ``parent`` resource.
Expand All @@ -95,6 +97,7 @@ def raw_page(self):

class GetInsightRequest(proto.Message):
r"""Request to the ``GetInsight`` method.

Attributes:
name (str):
Required. Name of the insight.
Expand All @@ -105,6 +108,7 @@ class GetInsightRequest(proto.Message):

class MarkInsightAcceptedRequest(proto.Message):
r"""Request for the ``MarkInsightAccepted`` method.

Attributes:
name (str):
Required. Name of the insight.
Expand All @@ -123,6 +127,7 @@ class MarkInsightAcceptedRequest(proto.Message):

class ListRecommendationsRequest(proto.Message):
r"""Request for the ``ListRecommendations`` method.

Attributes:
parent (str):
Required. The container resource on which to execute the
Expand Down Expand Up @@ -161,6 +166,7 @@ class ListRecommendationsRequest(proto.Message):

class ListRecommendationsResponse(proto.Message):
r"""Response to the ``ListRecommendations`` method.

Attributes:
recommendations (Sequence[google.cloud.recommender_v1.types.Recommendation]):
The set of recommendations for the ``parent`` resource.
Expand All @@ -182,6 +188,7 @@ def raw_page(self):

class GetRecommendationRequest(proto.Message):
r"""Request to the ``GetRecommendation`` method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -192,6 +199,7 @@ class GetRecommendationRequest(proto.Message):

class MarkRecommendationClaimedRequest(proto.Message):
r"""Request for the ``MarkRecommendationClaimed`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -212,6 +220,7 @@ class MarkRecommendationClaimedRequest(proto.Message):

class MarkRecommendationSucceededRequest(proto.Message):
r"""Request for the ``MarkRecommendationSucceeded`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -232,6 +241,7 @@ class MarkRecommendationSucceededRequest(proto.Message):

class MarkRecommendationFailedRequest(proto.Message):
r"""Request for the ``MarkRecommendationFailed`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand Down
Expand Up @@ -971,6 +971,12 @@ async def mark_recommendation_failed(
# 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/recommender_v1beta1/services/recommender/client.py
Expand Up @@ -410,10 +410,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_insights(
Expand Down Expand Up @@ -1184,6 +1181,19 @@ def mark_recommendation_failed(
# 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 @@ -238,6 +238,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_insights(
self,
Expand Down
Expand Up @@ -500,5 +500,8 @@ def mark_recommendation_failed(
)
return self._stubs["mark_recommendation_failed"]

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


__all__ = ("RecommenderGrpcTransport",)
Expand Up @@ -506,5 +506,8 @@ def mark_recommendation_failed(
)
return self._stubs["mark_recommendation_failed"]

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


__all__ = ("RecommenderGrpcAsyncIOTransport",)
2 changes: 2 additions & 0 deletions google/cloud/recommender_v1beta1/types/insight.py
Expand Up @@ -74,6 +74,7 @@ class Category(proto.Enum):

class RecommendationReference(proto.Message):
r"""Reference to an associated recommendation.

Attributes:
recommendation (str):
Recommendation resource name, e.g.
Expand Down Expand Up @@ -103,6 +104,7 @@ class RecommendationReference(proto.Message):

class InsightStateInfo(proto.Message):
r"""Information related to insight state.

Attributes:
state (google.cloud.recommender_v1beta1.types.InsightStateInfo.State):
Insight state.
Expand Down
3 changes: 3 additions & 0 deletions google/cloud/recommender_v1beta1/types/recommendation.py
Expand Up @@ -87,6 +87,7 @@ class Recommendation(proto.Message):

class InsightReference(proto.Message):
r"""Reference to an associated insight.

Attributes:
insight (str):
Insight resource name, e.g.
Expand Down Expand Up @@ -132,6 +133,7 @@ class RecommendationContent(proto.Message):

class OperationGroup(proto.Message):
r"""Group of operations that need to be performed atomically.

Attributes:
operations (Sequence[google.cloud.recommender_v1beta1.types.Operation]):
List of operations across one or more
Expand Down Expand Up @@ -301,6 +303,7 @@ class Category(proto.Enum):

class RecommendationStateInfo(proto.Message):
r"""Information for state. Contains state and metadata.

Attributes:
state (google.cloud.recommender_v1beta1.types.RecommendationStateInfo.State):
The state of the recommendation, Eg ACTIVE,
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/recommender_v1beta1/types/recommender_service.py
Expand Up @@ -38,6 +38,7 @@

class ListInsightsRequest(proto.Message):
r"""Request for the ``ListInsights`` method.

Attributes:
parent (str):
Required. The container resource on which to execute the
Expand Down Expand Up @@ -76,6 +77,7 @@ class ListInsightsRequest(proto.Message):

class ListInsightsResponse(proto.Message):
r"""Response to the ``ListInsights`` method.

Attributes:
insights (Sequence[google.cloud.recommender_v1beta1.types.Insight]):
The set of insights for the ``parent`` resource.
Expand All @@ -95,6 +97,7 @@ def raw_page(self):

class GetInsightRequest(proto.Message):
r"""Request to the ``GetInsight`` method.

Attributes:
name (str):
Required. Name of the insight.
Expand All @@ -105,6 +108,7 @@ class GetInsightRequest(proto.Message):

class MarkInsightAcceptedRequest(proto.Message):
r"""Request for the ``MarkInsightAccepted`` method.

Attributes:
name (str):
Required. Name of the insight.
Expand All @@ -123,6 +127,7 @@ class MarkInsightAcceptedRequest(proto.Message):

class ListRecommendationsRequest(proto.Message):
r"""Request for the ``ListRecommendations`` method.

Attributes:
parent (str):
Required. The container resource on which to execute the
Expand Down Expand Up @@ -161,6 +166,7 @@ class ListRecommendationsRequest(proto.Message):

class ListRecommendationsResponse(proto.Message):
r"""Response to the ``ListRecommendations`` method.

Attributes:
recommendations (Sequence[google.cloud.recommender_v1beta1.types.Recommendation]):
The set of recommendations for the ``parent`` resource.
Expand All @@ -182,6 +188,7 @@ def raw_page(self):

class GetRecommendationRequest(proto.Message):
r"""Request to the ``GetRecommendation`` method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -192,6 +199,7 @@ class GetRecommendationRequest(proto.Message):

class MarkRecommendationClaimedRequest(proto.Message):
r"""Request for the ``MarkRecommendationClaimed`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -212,6 +220,7 @@ class MarkRecommendationClaimedRequest(proto.Message):

class MarkRecommendationSucceededRequest(proto.Message):
r"""Request for the ``MarkRecommendationSucceeded`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand All @@ -232,6 +241,7 @@ class MarkRecommendationSucceededRequest(proto.Message):

class MarkRecommendationFailedRequest(proto.Message):
r"""Request for the ``MarkRecommendationFailed`` Method.

Attributes:
name (str):
Required. Name of the recommendation.
Expand Down