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

Commit 815739b

Browse files
feat: add context manager support in client (#136)
- [ ] Regenerate this pull request now. chore: fix docstring for first attribute of protos committer: @busunkim96 PiperOrigin-RevId: 401271153 Source-Link: googleapis/googleapis@787f8c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/81decffe9fc72396a8153e756d1d67a6eecfd620 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
1 parent bb6f070 commit 815739b

File tree

18 files changed

+200
-8
lines changed

18 files changed

+200
-8
lines changed

google/cloud/recommender_v1/services/recommender/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,12 @@ async def mark_recommendation_failed(
971971
# Done; return the response.
972972
return response
973973

974+
async def __aenter__(self):
975+
return self
976+
977+
async def __aexit__(self, exc_type, exc, tb):
978+
await self.transport.close()
979+
974980

975981
try:
976982
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/recommender_v1/services/recommender/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,7 @@ def __init__(
410410
client_cert_source_for_mtls=client_cert_source_func,
411411
quota_project_id=client_options.quota_project_id,
412412
client_info=client_info,
413-
always_use_jwt_access=(
414-
Transport == type(self).get_transport_class("grpc")
415-
or Transport == type(self).get_transport_class("grpc_asyncio")
416-
),
413+
always_use_jwt_access=True,
417414
)
418415

419416
def list_insights(
@@ -1184,6 +1181,19 @@ def mark_recommendation_failed(
11841181
# Done; return the response.
11851182
return response
11861183

1184+
def __enter__(self):
1185+
return self
1186+
1187+
def __exit__(self, type, value, traceback):
1188+
"""Releases underlying transport's resources.
1189+
1190+
.. warning::
1191+
ONLY use as a context manager if the transport is NOT shared
1192+
with other clients! Exiting the with block will CLOSE the transport
1193+
and may cause errors in other clients!
1194+
"""
1195+
self.transport.close()
1196+
11871197

11881198
try:
11891199
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/recommender_v1/services/recommender/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ def _prep_wrapped_messages(self, client_info):
238238
),
239239
}
240240

241+
def close(self):
242+
"""Closes resources associated with the transport.
243+
244+
.. warning::
245+
Only call this method if the transport is NOT shared
246+
with other clients - this may cause errors in other clients!
247+
"""
248+
raise NotImplementedError()
249+
241250
@property
242251
def list_insights(
243252
self,

google/cloud/recommender_v1/services/recommender/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,5 +500,8 @@ def mark_recommendation_failed(
500500
)
501501
return self._stubs["mark_recommendation_failed"]
502502

503+
def close(self):
504+
self.grpc_channel.close()
505+
503506

504507
__all__ = ("RecommenderGrpcTransport",)

google/cloud/recommender_v1/services/recommender/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,5 +506,8 @@ def mark_recommendation_failed(
506506
)
507507
return self._stubs["mark_recommendation_failed"]
508508

509+
def close(self):
510+
return self.grpc_channel.close()
511+
509512

510513
__all__ = ("RecommenderGrpcAsyncIOTransport",)

google/cloud/recommender_v1/types/insight.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Category(proto.Enum):
7373

7474
class RecommendationReference(proto.Message):
7575
r"""Reference to an associated recommendation.
76+
7677
Attributes:
7778
recommendation (str):
7879
Recommendation resource name, e.g.
@@ -102,6 +103,7 @@ class RecommendationReference(proto.Message):
102103

103104
class InsightStateInfo(proto.Message):
104105
r"""Information related to insight state.
106+
105107
Attributes:
106108
state (google.cloud.recommender_v1.types.InsightStateInfo.State):
107109
Insight state.

google/cloud/recommender_v1/types/recommendation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Recommendation(proto.Message):
8787

8888
class InsightReference(proto.Message):
8989
r"""Reference to an associated insight.
90+
9091
Attributes:
9192
insight (str):
9293
Insight resource name, e.g.
@@ -132,6 +133,7 @@ class RecommendationContent(proto.Message):
132133

133134
class OperationGroup(proto.Message):
134135
r"""Group of operations that need to be performed atomically.
136+
135137
Attributes:
136138
operations (Sequence[google.cloud.recommender_v1.types.Operation]):
137139
List of operations across one or more
@@ -301,6 +303,7 @@ class Category(proto.Enum):
301303

302304
class RecommendationStateInfo(proto.Message):
303305
r"""Information for state. Contains state and metadata.
306+
304307
Attributes:
305308
state (google.cloud.recommender_v1.types.RecommendationStateInfo.State):
306309
The state of the recommendation, Eg ACTIVE,

google/cloud/recommender_v1/types/recommender_service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
class ListInsightsRequest(proto.Message):
4040
r"""Request for the ``ListInsights`` method.
41+
4142
Attributes:
4243
parent (str):
4344
Required. The container resource on which to execute the
@@ -76,6 +77,7 @@ class ListInsightsRequest(proto.Message):
7677

7778
class ListInsightsResponse(proto.Message):
7879
r"""Response to the ``ListInsights`` method.
80+
7981
Attributes:
8082
insights (Sequence[google.cloud.recommender_v1.types.Insight]):
8183
The set of insights for the ``parent`` resource.
@@ -95,6 +97,7 @@ def raw_page(self):
9597

9698
class GetInsightRequest(proto.Message):
9799
r"""Request to the ``GetInsight`` method.
100+
98101
Attributes:
99102
name (str):
100103
Required. Name of the insight.
@@ -105,6 +108,7 @@ class GetInsightRequest(proto.Message):
105108

106109
class MarkInsightAcceptedRequest(proto.Message):
107110
r"""Request for the ``MarkInsightAccepted`` method.
111+
108112
Attributes:
109113
name (str):
110114
Required. Name of the insight.
@@ -123,6 +127,7 @@ class MarkInsightAcceptedRequest(proto.Message):
123127

124128
class ListRecommendationsRequest(proto.Message):
125129
r"""Request for the ``ListRecommendations`` method.
130+
126131
Attributes:
127132
parent (str):
128133
Required. The container resource on which to execute the
@@ -161,6 +166,7 @@ class ListRecommendationsRequest(proto.Message):
161166

162167
class ListRecommendationsResponse(proto.Message):
163168
r"""Response to the ``ListRecommendations`` method.
169+
164170
Attributes:
165171
recommendations (Sequence[google.cloud.recommender_v1.types.Recommendation]):
166172
The set of recommendations for the ``parent`` resource.
@@ -182,6 +188,7 @@ def raw_page(self):
182188

183189
class GetRecommendationRequest(proto.Message):
184190
r"""Request to the ``GetRecommendation`` method.
191+
185192
Attributes:
186193
name (str):
187194
Required. Name of the recommendation.
@@ -192,6 +199,7 @@ class GetRecommendationRequest(proto.Message):
192199

193200
class MarkRecommendationClaimedRequest(proto.Message):
194201
r"""Request for the ``MarkRecommendationClaimed`` Method.
202+
195203
Attributes:
196204
name (str):
197205
Required. Name of the recommendation.
@@ -212,6 +220,7 @@ class MarkRecommendationClaimedRequest(proto.Message):
212220

213221
class MarkRecommendationSucceededRequest(proto.Message):
214222
r"""Request for the ``MarkRecommendationSucceeded`` Method.
223+
215224
Attributes:
216225
name (str):
217226
Required. Name of the recommendation.
@@ -232,6 +241,7 @@ class MarkRecommendationSucceededRequest(proto.Message):
232241

233242
class MarkRecommendationFailedRequest(proto.Message):
234243
r"""Request for the ``MarkRecommendationFailed`` Method.
244+
235245
Attributes:
236246
name (str):
237247
Required. Name of the recommendation.

google/cloud/recommender_v1beta1/services/recommender/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,12 @@ async def mark_recommendation_failed(
971971
# Done; return the response.
972972
return response
973973

974+
async def __aenter__(self):
975+
return self
976+
977+
async def __aexit__(self, exc_type, exc, tb):
978+
await self.transport.close()
979+
974980

975981
try:
976982
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/recommender_v1beta1/services/recommender/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,7 @@ def __init__(
410410
client_cert_source_for_mtls=client_cert_source_func,
411411
quota_project_id=client_options.quota_project_id,
412412
client_info=client_info,
413-
always_use_jwt_access=(
414-
Transport == type(self).get_transport_class("grpc")
415-
or Transport == type(self).get_transport_class("grpc_asyncio")
416-
),
413+
always_use_jwt_access=True,
417414
)
418415

419416
def list_insights(
@@ -1184,6 +1181,19 @@ def mark_recommendation_failed(
11841181
# Done; return the response.
11851182
return response
11861183

1184+
def __enter__(self):
1185+
return self
1186+
1187+
def __exit__(self, type, value, traceback):
1188+
"""Releases underlying transport's resources.
1189+
1190+
.. warning::
1191+
ONLY use as a context manager if the transport is NOT shared
1192+
with other clients! Exiting the with block will CLOSE the transport
1193+
and may cause errors in other clients!
1194+
"""
1195+
self.transport.close()
1196+
11871197

11881198
try:
11891199
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

0 commit comments

Comments
 (0)