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

Commit f031d91

Browse files
feat: add context manager support in client (#51)
- [ ] 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 96f365c commit f031d91

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

google/cloud/vpcaccess_v1/services/vpc_access_service/async_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ async def delete_connector(
514514
# Done; return the response.
515515
return response
516516

517+
async def __aenter__(self):
518+
return self
519+
520+
async def __aexit__(self, exc_type, exc, tb):
521+
await self.transport.close()
522+
517523

518524
try:
519525
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vpcaccess_v1/services/vpc_access_service/client.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,7 @@ def __init__(
350350
client_cert_source_for_mtls=client_cert_source_func,
351351
quota_project_id=client_options.quota_project_id,
352352
client_info=client_info,
353-
always_use_jwt_access=(
354-
Transport == type(self).get_transport_class("grpc")
355-
or Transport == type(self).get_transport_class("grpc_asyncio")
356-
),
353+
always_use_jwt_access=True,
357354
)
358355

359356
def create_connector(
@@ -703,6 +700,19 @@ def delete_connector(
703700
# Done; return the response.
704701
return response
705702

703+
def __enter__(self):
704+
return self
705+
706+
def __exit__(self, type, value, traceback):
707+
"""Releases underlying transport's resources.
708+
709+
.. warning::
710+
ONLY use as a context manager if the transport is NOT shared
711+
with other clients! Exiting the with block will CLOSE the transport
712+
and may cause errors in other clients!
713+
"""
714+
self.transport.close()
715+
706716

707717
try:
708718
DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(

google/cloud/vpcaccess_v1/services/vpc_access_service/transports/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ def _prep_wrapped_messages(self, client_info):
170170
),
171171
}
172172

173+
def close(self):
174+
"""Closes resources associated with the transport.
175+
176+
.. warning::
177+
Only call this method if the transport is NOT shared
178+
with other clients - this may cause errors in other clients!
179+
"""
180+
raise NotImplementedError()
181+
173182
@property
174183
def operations_client(self) -> operations_v1.OperationsClient:
175184
"""Return the client designed to process long-running operations."""

google/cloud/vpcaccess_v1/services/vpc_access_service/transports/grpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,8 @@ def delete_connector(
353353
)
354354
return self._stubs["delete_connector"]
355355

356+
def close(self):
357+
self.grpc_channel.close()
358+
356359

357360
__all__ = ("VpcAccessServiceGrpcTransport",)

google/cloud/vpcaccess_v1/services/vpc_access_service/transports/grpc_asyncio.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,8 @@ def delete_connector(
362362
)
363363
return self._stubs["delete_connector"]
364364

365+
def close(self):
366+
return self.grpc_channel.close()
367+
365368

366369
__all__ = ("VpcAccessServiceGrpcAsyncIOTransport",)

google/cloud/vpcaccess_v1/types/vpc_access.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
class Connector(proto.Message):
3636
r"""Definition of a Serverless VPC Access connector.
37+
3738
Attributes:
3839
name (str):
3940
The resource name in the format
@@ -71,6 +72,7 @@ class State(proto.Enum):
7172

7273
class Subnet(proto.Message):
7374
r"""The subnet in which to house the connector
75+
7476
Attributes:
7577
name (str):
7678
Subnet name (relative, not fully qualified).
@@ -100,6 +102,7 @@ class Subnet(proto.Message):
100102

101103
class CreateConnectorRequest(proto.Message):
102104
r"""Request for creating a Serverless VPC Access connector.
105+
103106
Attributes:
104107
parent (str):
105108
Required. The project and location in which the
@@ -118,6 +121,7 @@ class CreateConnectorRequest(proto.Message):
118121

119122
class GetConnectorRequest(proto.Message):
120123
r"""Request for getting a Serverless VPC Access connector.
124+
121125
Attributes:
122126
name (str):
123127
Required. Name of a Serverless VPC Access
@@ -149,6 +153,7 @@ class ListConnectorsRequest(proto.Message):
149153

150154
class ListConnectorsResponse(proto.Message):
151155
r"""Response for listing Serverless VPC Access connectors.
156+
152157
Attributes:
153158
connectors (Sequence[google.cloud.vpcaccess_v1.types.Connector]):
154159
List of Serverless VPC Access connectors.
@@ -166,6 +171,7 @@ def raw_page(self):
166171

167172
class DeleteConnectorRequest(proto.Message):
168173
r"""Request for deleting a Serverless VPC Access connector.
174+
169175
Attributes:
170176
name (str):
171177
Required. Name of a Serverless VPC Access
@@ -177,6 +183,7 @@ class DeleteConnectorRequest(proto.Message):
177183

178184
class OperationMetadata(proto.Message):
179185
r"""Metadata for google.longrunning.Operation.
186+
180187
Attributes:
181188
method (str):
182189
Output only. Method that initiated the

tests/unit/gapic/vpcaccess_v1/test_vpc_access_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.vpcaccess_v1.services.vpc_access_service import (
@@ -1611,6 +1612,9 @@ def test_vpc_access_service_base_transport():
16111612
with pytest.raises(NotImplementedError):
16121613
getattr(transport, method)(request=object())
16131614

1615+
with pytest.raises(NotImplementedError):
1616+
transport.close()
1617+
16141618
# Additionally, the LRO client (a property) should
16151619
# also raise NotImplementedError
16161620
with pytest.raises(NotImplementedError):
@@ -2124,3 +2128,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
21242128
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
21252129
)
21262130
prep.assert_called_once_with(client_info)
2131+
2132+
2133+
@pytest.mark.asyncio
2134+
async def test_transport_close_async():
2135+
client = VpcAccessServiceAsyncClient(
2136+
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
2137+
)
2138+
with mock.patch.object(
2139+
type(getattr(client.transport, "grpc_channel")), "close"
2140+
) as close:
2141+
async with client:
2142+
close.assert_not_called()
2143+
close.assert_called_once()
2144+
2145+
2146+
def test_transport_close():
2147+
transports = {
2148+
"grpc": "_grpc_channel",
2149+
}
2150+
2151+
for transport, close_name in transports.items():
2152+
client = VpcAccessServiceClient(
2153+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
2154+
)
2155+
with mock.patch.object(
2156+
type(getattr(client.transport, close_name)), "close"
2157+
) as close:
2158+
with client:
2159+
close.assert_not_called()
2160+
close.assert_called_once()
2161+
2162+
2163+
def test_client_ctx():
2164+
transports = [
2165+
"grpc",
2166+
]
2167+
for transport in transports:
2168+
client = VpcAccessServiceClient(
2169+
credentials=ga_credentials.AnonymousCredentials(), transport=transport
2170+
)
2171+
# Test client calls underlying transport.
2172+
with mock.patch.object(type(client.transport), "close") as close:
2173+
close.assert_not_called()
2174+
with client:
2175+
pass
2176+
close.assert_called()

0 commit comments

Comments
 (0)