Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
feat: add context manager support in client (#114)
Browse files Browse the repository at this point in the history
- [ ] 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[bot] committed Oct 8, 2021
1 parent 02409d9 commit 13240ae
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions grafeas/grafeas_v1/services/grafeas/async_client.py
Expand Up @@ -1316,6 +1316,12 @@ async def list_note_occurrences(
# 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(
Expand Down
13 changes: 13 additions & 0 deletions grafeas/grafeas_v1/services/grafeas/client.py
Expand Up @@ -1373,6 +1373,19 @@ def list_note_occurrences(
# 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(
Expand Down
9 changes: 9 additions & 0 deletions grafeas/grafeas_v1/services/grafeas/transports/base.py
Expand Up @@ -295,6 +295,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 get_occurrence(
self,
Expand Down
3 changes: 3 additions & 0 deletions grafeas/grafeas_v1/services/grafeas/transports/grpc.py
Expand Up @@ -608,5 +608,8 @@ def list_note_occurrences(
)
return self._stubs["list_note_occurrences"]

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


__all__ = ("GrafeasGrpcTransport",)
Expand Up @@ -623,5 +623,8 @@ def list_note_occurrences(
)
return self._stubs["list_note_occurrences"]

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


__all__ = ("GrafeasGrpcAsyncIOTransport",)
2 changes: 2 additions & 0 deletions grafeas/grafeas_v1/types/upgrade.py
Expand Up @@ -120,6 +120,7 @@ class WindowsUpdate(proto.Message):

class Identity(proto.Message):
r"""The unique identifier of the update.
Attributes:
update_id (str):
The revision independent identifier of the
Expand All @@ -133,6 +134,7 @@ class Identity(proto.Message):

class Category(proto.Message):
r"""The category to which the update belongs.
Attributes:
category_id (str):
The identifier of the category.
Expand Down
2 changes: 2 additions & 0 deletions grafeas/grafeas_v1/types/vulnerability.py
Expand Up @@ -153,6 +153,7 @@ class Detail(proto.Message):

class WindowsDetail(proto.Message):
r"""
Attributes:
cpe_uri (str):
Required. The `CPE
Expand All @@ -173,6 +174,7 @@ class WindowsDetail(proto.Message):

class KnowledgeBase(proto.Message):
r"""
Attributes:
name (str):
The KB name (generally of the form KB[0-9]+ (e.g.,
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/gapic/grafeas_v1/test_grafeas.py
Expand Up @@ -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.oauth2 import service_account
Expand Down Expand Up @@ -3495,6 +3496,9 @@ def test_grafeas_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_grafeas_base_transport_with_credentials_file():
Expand Down Expand Up @@ -3947,3 +3951,43 @@ def test_parse_common_location_path():
# Check that the path construction is reversible.
actual = GrafeasClient.parse_common_location_path(path)
assert expected == actual


@pytest.mark.asyncio
async def test_transport_close_async():
client = GrafeasAsyncClient(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 = GrafeasClient(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 = GrafeasClient(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()

0 comments on commit 13240ae

Please sign in to comment.