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

Commit 98d60e9

Browse files
feat: add context manager support in client (#73)
- [ ] 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 da97687 commit 98d60e9

File tree

18 files changed

+185
-8
lines changed

18 files changed

+185
-8
lines changed

google/cloud/servicecontrol_v1/services/quota_controller/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ async def allocate_quota(
217217
# Done; return the response.
218218
return response
219219

220+
async def __aenter__(self):
221+
return self
222+
223+
async def __aexit__(self, exc_type, exc, tb):
224+
await self.transport.close()
225+
220226

221227
try:
222228
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicecontrol_v1/services/quota_controller/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ def __init__(
332332
client_cert_source_for_mtls=client_cert_source_func,
333333
quota_project_id=client_options.quota_project_id,
334334
client_info=client_info,
335-
always_use_jwt_access=(
336-
Transport == type(self).get_transport_class("grpc")
337-
or Transport == type(self).get_transport_class("grpc_asyncio")
338-
),
335+
always_use_jwt_access=True,
339336
)
340337

341338
def allocate_quota(
@@ -393,6 +390,19 @@ def allocate_quota(
393390
# Done; return the response.
394391
return response
395392

393+
def __enter__(self):
394+
return self
395+
396+
def __exit__(self, type, value, traceback):
397+
"""Releases underlying transport's resources.
398+
399+
.. warning::
400+
ONLY use as a context manager if the transport is NOT shared
401+
with other clients! Exiting the with block will CLOSE the transport
402+
and may cause errors in other clients!
403+
"""
404+
self.transport.close()
405+
396406

397407
try:
398408
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicecontrol_v1/services/quota_controller/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ def _prep_wrapped_messages(self, client_info):
162162
),
163163
}
164164

165+
def close(self):
166+
"""Closes resources associated with the transport.
167+
168+
.. warning::
169+
Only call this method if the transport is NOT shared
170+
with other clients - this may cause errors in other clients!
171+
"""
172+
raise NotImplementedError()
173+
165174
@property
166175
def allocate_quota(
167176
self,

google/cloud/servicecontrol_v1/services/quota_controller/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,8 @@ def allocate_quota(
267267
)
268268
return self._stubs["allocate_quota"]
269269

270+
def close(self):
271+
self.grpc_channel.close()
272+
270273

271274
__all__ = ("QuotaControllerGrpcTransport",)

google/cloud/servicecontrol_v1/services/quota_controller/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,8 @@ def allocate_quota(
271271
)
272272
return self._stubs["allocate_quota"]
273273

274+
def close(self):
275+
return self.grpc_channel.close()
276+
274277

275278
__all__ = ("QuotaControllerGrpcAsyncIOTransport",)

google/cloud/servicecontrol_v1/services/service_controller/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ async def report(
293293
# Done; return the response.
294294
return response
295295

296+
async def __aenter__(self):
297+
return self
298+
299+
async def __aexit__(self, exc_type, exc, tb):
300+
await self.transport.close()
301+
296302

297303
try:
298304
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicecontrol_v1/services/service_controller/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ def __init__(
334334
client_cert_source_for_mtls=client_cert_source_func,
335335
quota_project_id=client_options.quota_project_id,
336336
client_info=client_info,
337-
always_use_jwt_access=(
338-
Transport == type(self).get_transport_class("grpc")
339-
or Transport == type(self).get_transport_class("grpc_asyncio")
340-
),
337+
always_use_jwt_access=True,
341338
)
342339

343340
def check(
@@ -463,6 +460,19 @@ def report(
463460
# Done; return the response.
464461
return response
465462

463+
def __enter__(self):
464+
return self
465+
466+
def __exit__(self, type, value, traceback):
467+
"""Releases underlying transport's resources.
468+
469+
.. warning::
470+
ONLY use as a context manager if the transport is NOT shared
471+
with other clients! Exiting the with block will CLOSE the transport
472+
and may cause errors in other clients!
473+
"""
474+
self.transport.close()
475+
466476

467477
try:
468478
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/servicecontrol_v1/services/service_controller/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ def _prep_wrapped_messages(self, client_info):
176176
),
177177
}
178178

179+
def close(self):
180+
"""Closes resources associated with the transport.
181+
182+
.. warning::
183+
Only call this method if the transport is NOT shared
184+
with other clients - this may cause errors in other clients!
185+
"""
186+
raise NotImplementedError()
187+
179188
@property
180189
def check(
181190
self,

google/cloud/servicecontrol_v1/services/service_controller/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,8 @@ def report(
317317
)
318318
return self._stubs["report"]
319319

320+
def close(self):
321+
self.grpc_channel.close()
322+
320323

321324
__all__ = ("ServiceControllerGrpcTransport",)

google/cloud/servicecontrol_v1/services/service_controller/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,8 @@ def report(
322322
)
323323
return self._stubs["report"]
324324

325+
def close(self):
326+
return self.grpc_channel.close()
327+
325328

326329
__all__ = ("ServiceControllerGrpcAsyncIOTransport",)

0 commit comments

Comments
 (0)