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

Commit 5b43933

Browse files
feat: add context manager support in client (#74)
- [ ] 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 f8b49a1 commit 5b43933

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

google/cloud/apigateway_v1/services/api_gateway_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,12 @@ async def delete_api_config(
15881588
# Done; return the response.
15891589
return response
15901590

1591+
async def __aenter__(self):
1592+
return self
1593+
1594+
async def __aexit__(self, exc_type, exc, tb):
1595+
await self.transport.close()
1596+
15911597

15921598
try:
15931599
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/apigateway_v1/services/api_gateway_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,7 @@ def __init__(
424424
client_cert_source_for_mtls=client_cert_source_func,
425425
quota_project_id=client_options.quota_project_id,
426426
client_info=client_info,
427-
always_use_jwt_access=(
428-
Transport == type(self).get_transport_class("grpc")
429-
or Transport == type(self).get_transport_class("grpc_asyncio")
430-
),
427+
always_use_jwt_access=True,
431428
)
432429

433430
def list_gateways(
@@ -1755,6 +1752,19 @@ def delete_api_config(
17551752
# Done; return the response.
17561753
return response
17571754

1755+
def __enter__(self):
1756+
return self
1757+
1758+
def __exit__(self, type, value, traceback):
1759+
"""Releases underlying transport's resources.
1760+
1761+
.. warning::
1762+
ONLY use as a context manager if the transport is NOT shared
1763+
with other clients! Exiting the with block will CLOSE the transport
1764+
and may cause errors in other clients!
1765+
"""
1766+
self.transport.close()
1767+
17581768

17591769
try:
17601770
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/apigateway_v1/services/api_gateway_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ def _prep_wrapped_messages(self, client_info):
302302
),
303303
}
304304

305+
def close(self):
306+
"""Closes resources associated with the transport.
307+
308+
.. warning::
309+
Only call this method if the transport is NOT shared
310+
with other clients - this may cause errors in other clients!
311+
"""
312+
raise NotImplementedError()
313+
305314
@property
306315
def operations_client(self) -> operations_v1.OperationsClient:
307316
"""Return the client designed to process long-running operations."""

google/cloud/apigateway_v1/services/api_gateway_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,5 +635,8 @@ def delete_api_config(
635635
)
636636
return self._stubs["delete_api_config"]
637637

638+
def close(self):
639+
self.grpc_channel.close()
640+
638641

639642
__all__ = ("ApiGatewayServiceGrpcTransport",)

google/cloud/apigateway_v1/services/api_gateway_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,5 +656,8 @@ def delete_api_config(
656656
)
657657
return self._stubs["delete_api_config"]
658658

659+
def close(self):
660+
return self.grpc_channel.close()
661+
659662

660663
__all__ = ("ApiGatewayServiceGrpcAsyncIOTransport",)

google/cloud/apigateway_v1/types/apigateway.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
class Api(proto.Message):
5252
r"""An API that can be served by one or more Gateways.
53+
5354
Attributes:
5455
name (str):
5556
Output only. Resource name of the API.
@@ -166,6 +167,7 @@ class State(proto.Enum):
166167

167168
class File(proto.Message):
168169
r"""A lightweight description of a file.
170+
169171
Attributes:
170172
path (str):
171173
The file path (full or relative path). This
@@ -180,6 +182,7 @@ class File(proto.Message):
180182

181183
class OpenApiDocument(proto.Message):
182184
r"""An OpenAPI Specification Document describing an API.
185+
183186
Attributes:
184187
document (google.cloud.apigateway_v1.types.ApiConfig.File):
185188
The OpenAPI Specification document file.
@@ -189,6 +192,7 @@ class OpenApiDocument(proto.Message):
189192

190193
class GrpcServiceDefinition(proto.Message):
191194
r"""A gRPC service definition.
195+
192196
Attributes:
193197
file_descriptor_set (google.cloud.apigateway_v1.types.ApiConfig.File):
194198
Input only. File descriptor set, generated by protoc.
@@ -285,6 +289,7 @@ class State(proto.Enum):
285289

286290
class ListGatewaysRequest(proto.Message):
287291
r"""Request message for ApiGatewayService.ListGateways
292+
288293
Attributes:
289294
parent (str):
290295
Required. Parent resource of the Gateway, of the form:
@@ -308,6 +313,7 @@ class ListGatewaysRequest(proto.Message):
308313

309314
class ListGatewaysResponse(proto.Message):
310315
r"""Response message for ApiGatewayService.ListGateways
316+
311317
Attributes:
312318
gateways (Sequence[google.cloud.apigateway_v1.types.Gateway]):
313319
Gateways.
@@ -328,6 +334,7 @@ def raw_page(self):
328334

329335
class GetGatewayRequest(proto.Message):
330336
r"""Request message for ApiGatewayService.GetGateway
337+
331338
Attributes:
332339
name (str):
333340
Required. Resource name of the form:
@@ -339,6 +346,7 @@ class GetGatewayRequest(proto.Message):
339346

340347
class CreateGatewayRequest(proto.Message):
341348
r"""Request message for ApiGatewayService.CreateGateway
349+
342350
Attributes:
343351
parent (str):
344352
Required. Parent resource of the Gateway, of the form:
@@ -358,6 +366,7 @@ class CreateGatewayRequest(proto.Message):
358366

359367
class UpdateGatewayRequest(proto.Message):
360368
r"""Request message for ApiGatewayService.UpdateGateway
369+
361370
Attributes:
362371
update_mask (google.protobuf.field_mask_pb2.FieldMask):
363372
Field mask is used to specify the fields to be overwritten
@@ -378,6 +387,7 @@ class UpdateGatewayRequest(proto.Message):
378387

379388
class DeleteGatewayRequest(proto.Message):
380389
r"""Request message for ApiGatewayService.DeleteGateway
390+
381391
Attributes:
382392
name (str):
383393
Required. Resource name of the form:
@@ -389,6 +399,7 @@ class DeleteGatewayRequest(proto.Message):
389399

390400
class ListApisRequest(proto.Message):
391401
r"""Request message for ApiGatewayService.ListApis
402+
392403
Attributes:
393404
parent (str):
394405
Required. Parent resource of the API, of the form:
@@ -412,6 +423,7 @@ class ListApisRequest(proto.Message):
412423

413424
class ListApisResponse(proto.Message):
414425
r"""Response message for ApiGatewayService.ListApis
426+
415427
Attributes:
416428
apis (Sequence[google.cloud.apigateway_v1.types.Api]):
417429
APIs.
@@ -432,6 +444,7 @@ def raw_page(self):
432444

433445
class GetApiRequest(proto.Message):
434446
r"""Request message for ApiGatewayService.GetApi
447+
435448
Attributes:
436449
name (str):
437450
Required. Resource name of the form:
@@ -443,6 +456,7 @@ class GetApiRequest(proto.Message):
443456

444457
class CreateApiRequest(proto.Message):
445458
r"""Request message for ApiGatewayService.CreateApi
459+
446460
Attributes:
447461
parent (str):
448462
Required. Parent resource of the API, of the form:
@@ -462,6 +476,7 @@ class CreateApiRequest(proto.Message):
462476

463477
class UpdateApiRequest(proto.Message):
464478
r"""Request message for ApiGatewayService.UpdateApi
479+
465480
Attributes:
466481
update_mask (google.protobuf.field_mask_pb2.FieldMask):
467482
Field mask is used to specify the fields to be overwritten
@@ -482,6 +497,7 @@ class UpdateApiRequest(proto.Message):
482497

483498
class DeleteApiRequest(proto.Message):
484499
r"""Request message for ApiGatewayService.DeleteApi
500+
485501
Attributes:
486502
name (str):
487503
Required. Resource name of the form:
@@ -493,6 +509,7 @@ class DeleteApiRequest(proto.Message):
493509

494510
class ListApiConfigsRequest(proto.Message):
495511
r"""Request message for ApiGatewayService.ListApiConfigs
512+
496513
Attributes:
497514
parent (str):
498515
Required. Parent resource of the API Config, of the form:
@@ -516,6 +533,7 @@ class ListApiConfigsRequest(proto.Message):
516533

517534
class ListApiConfigsResponse(proto.Message):
518535
r"""Response message for ApiGatewayService.ListApiConfigs
536+
519537
Attributes:
520538
api_configs (Sequence[google.cloud.apigateway_v1.types.ApiConfig]):
521539
API Configs.
@@ -536,6 +554,7 @@ def raw_page(self):
536554

537555
class GetApiConfigRequest(proto.Message):
538556
r"""Request message for ApiGatewayService.GetApiConfig
557+
539558
Attributes:
540559
name (str):
541560
Required. Resource name of the form:
@@ -559,6 +578,7 @@ class ConfigView(proto.Enum):
559578

560579
class CreateApiConfigRequest(proto.Message):
561580
r"""Request message for ApiGatewayService.CreateApiConfig
581+
562582
Attributes:
563583
parent (str):
564584
Required. Parent resource of the API Config, of the form:
@@ -578,6 +598,7 @@ class CreateApiConfigRequest(proto.Message):
578598

579599
class UpdateApiConfigRequest(proto.Message):
580600
r"""Request message for ApiGatewayService.UpdateApiConfig
601+
581602
Attributes:
582603
update_mask (google.protobuf.field_mask_pb2.FieldMask):
583604
Field mask is used to specify the fields to be overwritten
@@ -598,6 +619,7 @@ class UpdateApiConfigRequest(proto.Message):
598619

599620
class DeleteApiConfigRequest(proto.Message):
600621
r"""Request message for ApiGatewayService.DeleteApiConfig
622+
601623
Attributes:
602624
name (str):
603625
Required. Resource name of the form:
@@ -609,6 +631,7 @@ class DeleteApiConfigRequest(proto.Message):
609631

610632
class OperationMetadata(proto.Message):
611633
r"""Represents the metadata of the long-running operation.
634+
612635
Attributes:
613636
create_time (google.protobuf.timestamp_pb2.Timestamp):
614637
Output only. The time the operation was
@@ -641,6 +664,7 @@ class OperationMetadata(proto.Message):
641664

642665
class Diagnostic(proto.Message):
643666
r"""Diagnostic information from configuration processing.
667+
644668
Attributes:
645669
location (str):
646670
Location of the diagnostic.

tests/unit/gapic/apigateway_v1/test_api_gateway_service.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from google.api_core import grpc_helpers_async
3333
from google.api_core import operation_async # type: ignore
3434
from google.api_core import operations_v1
35+
from google.api_core import path_template
3536
from google.auth import credentials as ga_credentials
3637
from google.auth.exceptions import MutualTLSChannelError
3738
from google.cloud.apigateway_v1.services.api_gateway_service import (
@@ -4248,6 +4249,9 @@ def test_api_gateway_service_base_transport():
42484249
with pytest.raises(NotImplementedError):
42494250
getattr(transport, method)(request=object())
42504251

4252+
with pytest.raises(NotImplementedError):
4253+
transport.close()
4254+
42514255
# Additionally, the LRO client (a property) should
42524256
# also raise NotImplementedError
42534257
with pytest.raises(NotImplementedError):
@@ -4871,3 +4875,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
48714875
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
48724876
)
48734877
prep.assert_called_once_with(client_info)
4878+
4879+
4880+
@pytest.mark.asyncio
4881+
async def test_transport_close_async():
4882+
client = ApiGatewayServiceAsyncClient(
4883+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
4884+
)
4885+
with mock.patch.object(
4886+
type(getattr(client.transport, "grpc_channel")), "close"
4887+
) as close:
4888+
async with client:
4889+
close.assert_not_called()
4890+
close.assert_called_once()
4891+
4892+
4893+
def test_transport_close():
4894+
transports = {
4895+
"grpc": "_grpc_channel",
4896+
}
4897+
4898+
for transport, close_name in transports.items():
4899+
client = ApiGatewayServiceClient(
4900+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
4901+
)
4902+
with mock.patch.object(
4903+
type(getattr(client.transport, close_name)), "close"
4904+
) as close:
4905+
with client:
4906+
close.assert_not_called()
4907+
close.assert_called_once()
4908+
4909+
4910+
def test_client_ctx():
4911+
transports = [
4912+
"grpc",
4913+
]
4914+
for transport in transports:
4915+
client = ApiGatewayServiceClient(
4916+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
4917+
)
4918+
# Test client calls underlying transport.
4919+
with mock.patch.object(type(client.transport), "close") as close:
4920+
close.assert_not_called()
4921+
with client:
4922+
pass
4923+
close.assert_called()

0 commit comments

Comments
 (0)