Skip to content
Permalink
Browse files
feat: add context manager support in client (#122)
- [ ] 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: googleapis/googleapis-gen@81decff
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiODFkZWNmZmU5ZmM3MjM5NmE4MTUzZTc1NmQxZDY3YTZlZWNmZDYyMCJ9
  • Loading branch information
gcf-owl-bot committed Oct 7, 2021
1 parent 2fa295c commit 59229ebe9f17be35cb39119cbae2e19e7a4c8732
Showing with 340 additions and 16 deletions.
  1. +6 −0 google/cloud/servicedirectory_v1/services/lookup_service/async_client.py
  2. +14 −4 google/cloud/servicedirectory_v1/services/lookup_service/client.py
  3. +9 −0 google/cloud/servicedirectory_v1/services/lookup_service/transports/base.py
  4. +3 −0 google/cloud/servicedirectory_v1/services/lookup_service/transports/grpc.py
  5. +3 −0 google/cloud/servicedirectory_v1/services/lookup_service/transports/grpc_asyncio.py
  6. +6 −0 google/cloud/servicedirectory_v1/services/registration_service/async_client.py
  7. +14 −4 google/cloud/servicedirectory_v1/services/registration_service/client.py
  8. +9 −0 google/cloud/servicedirectory_v1/services/registration_service/transports/base.py
  9. +3 −0 google/cloud/servicedirectory_v1/services/registration_service/transports/grpc.py
  10. +3 −0 google/cloud/servicedirectory_v1/services/registration_service/transports/grpc_asyncio.py
  11. +6 −0 google/cloud/servicedirectory_v1beta1/services/lookup_service/async_client.py
  12. +14 −4 google/cloud/servicedirectory_v1beta1/services/lookup_service/client.py
  13. +9 −0 google/cloud/servicedirectory_v1beta1/services/lookup_service/transports/base.py
  14. +3 −0 google/cloud/servicedirectory_v1beta1/services/lookup_service/transports/grpc.py
  15. +3 −0 google/cloud/servicedirectory_v1beta1/services/lookup_service/transports/grpc_asyncio.py
  16. +6 −0 google/cloud/servicedirectory_v1beta1/services/registration_service/async_client.py
  17. +14 −4 google/cloud/servicedirectory_v1beta1/services/registration_service/client.py
  18. +9 −0 google/cloud/servicedirectory_v1beta1/services/registration_service/transports/base.py
  19. +3 −0 google/cloud/servicedirectory_v1beta1/services/registration_service/transports/grpc.py
  20. +3 −0 google/cloud/servicedirectory_v1beta1/services/registration_service/transports/grpc_asyncio.py
  21. +50 −0 tests/unit/gapic/servicedirectory_v1/test_lookup_service.py
  22. +50 −0 tests/unit/gapic/servicedirectory_v1/test_registration_service.py
  23. +50 −0 tests/unit/gapic/servicedirectory_v1beta1/test_lookup_service.py
  24. +50 −0 tests/unit/gapic/servicedirectory_v1beta1/test_registration_service.py
@@ -216,6 +216,12 @@ async def resolve_service(
# 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(
@@ -364,10 +364,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 resolve_service(
@@ -424,6 +421,19 @@ def resolve_service(
# 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(
@@ -159,6 +159,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 resolve_service(
self,
@@ -255,5 +255,8 @@ def resolve_service(
)
return self._stubs["resolve_service"]

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


__all__ = ("LookupServiceGrpcTransport",)
@@ -259,5 +259,8 @@ def resolve_service(
)
return self._stubs["resolve_service"]

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


__all__ = ("LookupServiceGrpcAsyncIOTransport",)
@@ -1658,6 +1658,12 @@ async def test_iam_permissions(
# 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(
@@ -408,10 +408,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 create_namespace(
@@ -1879,6 +1876,19 @@ def test_iam_permissions(
# 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(
@@ -221,6 +221,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 create_namespace(
self,
@@ -737,5 +737,8 @@ def test_iam_permissions(
)
return self._stubs["test_iam_permissions"]

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


__all__ = ("RegistrationServiceGrpcTransport",)
@@ -760,5 +760,8 @@ def test_iam_permissions(
)
return self._stubs["test_iam_permissions"]

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


__all__ = ("RegistrationServiceGrpcAsyncIOTransport",)
@@ -219,6 +219,12 @@ async def resolve_service(
# 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(
@@ -380,10 +380,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 resolve_service(
@@ -441,6 +438,19 @@ def resolve_service(
# 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(
@@ -159,6 +159,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 resolve_service(
self,
@@ -256,5 +256,8 @@ def resolve_service(
)
return self._stubs["resolve_service"]

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


__all__ = ("LookupServiceGrpcTransport",)
@@ -260,5 +260,8 @@ def resolve_service(
)
return self._stubs["resolve_service"]

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


__all__ = ("LookupServiceGrpcAsyncIOTransport",)
@@ -1662,6 +1662,12 @@ async def test_iam_permissions(
# 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(
@@ -426,10 +426,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 create_namespace(
@@ -1897,6 +1894,19 @@ def test_iam_permissions(
# 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(
@@ -221,6 +221,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 create_namespace(
self,
@@ -738,5 +738,8 @@ def test_iam_permissions(
)
return self._stubs["test_iam_permissions"]

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


__all__ = ("RegistrationServiceGrpcTransport",)
@@ -761,5 +761,8 @@ def test_iam_permissions(
)
return self._stubs["test_iam_permissions"]

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


__all__ = ("RegistrationServiceGrpcAsyncIOTransport",)
@@ -29,6 +29,7 @@
from google.api_core import gapic_v1
from google.api_core import grpc_helpers
from google.api_core import grpc_helpers_async
from google.api_core import path_template
from google.auth import credentials as ga_credentials
from google.auth.exceptions import MutualTLSChannelError
from google.cloud.servicedirectory_v1.services.lookup_service import (
@@ -729,6 +730,9 @@ def test_lookup_service_base_transport():
with pytest.raises(NotImplementedError):
getattr(transport, method)(request=object())

with pytest.raises(NotImplementedError):
transport.close()


@requires_google_auth_gte_1_25_0
def test_lookup_service_base_transport_with_credentials_file():
@@ -1245,3 +1249,49 @@ def test_client_withDEFAULT_CLIENT_INFO():
credentials=ga_credentials.AnonymousCredentials(), client_info=client_info,
)
prep.assert_called_once_with(client_info)


@pytest.mark.asyncio
async def test_transport_close_async():
client = LookupServiceAsyncClient(
credentials=ga_credentials.AnonymousCredentials(), transport="grpc_asyncio",
)
with mock.patch.object(
type(getattr(client.transport, "grpc_channel")), "close"
) as close:
async with client:
close.assert_not_called()
close.assert_called_once()


def test_transport_close():
transports = {
"grpc": "_grpc_channel",
}

for transport, close_name in transports.items():
client = LookupServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
with mock.patch.object(
type(getattr(client.transport, close_name)), "close"
) as close:
with client:
close.assert_not_called()
close.assert_called_once()


def test_client_ctx():
transports = [
"grpc",
]
for transport in transports:
client = LookupServiceClient(
credentials=ga_credentials.AnonymousCredentials(), transport=transport
)
# Test client calls underlying transport.
with mock.patch.object(type(client.transport), "close") as close:
close.assert_not_called()
with client:
pass
close.assert_called()
Loading

0 comments on commit 59229eb

Please sign in to comment.