Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for context manager in client #987

Merged
merged 15 commits into from
Oct 4, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
{{ "\n" }}
{% endfor %}

def __enter__(self):
self.transport.__enter__()
return self
atulep marked this conversation as resolved.
Show resolved Hide resolved

def __exit__(self, type, value, traceback):
atulep marked this conversation as resolved.
Show resolved Hide resolved
"""Releases underlying transport's resources.

WARNING: This makes the transport unusable.
"""
return self.transport.__exit__(type, value, traceback)

{% if opts.add_iam_methods %}
def set_iam_policy(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ class {{ service.name }}Transport(abc.ABC):
{% endfor %} {# precomputed wrappers loop #}
}

def __enter__(self):
raise NotImplementedError()

def __exit__(self, type, value, traceback):
raise NotImplementedError()

{% if service.has_lro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
"""Return the channel designed to connect to this service.
"""
return self._grpc_channel

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Closes underlying gRPC channel.
atulep marked this conversation as resolved.
Show resolved Hide resolved
"""
self.grpc_channel.close()
atulep marked this conversation as resolved.
Show resolved Hide resolved

{% if service.has_lro %}

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class {{ service.name }}RestTransport({{ service.name }}Transport):
{% for method in service.methods.values() %}
{% if method.http_opt %}

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Closes underlying HTTP session.
"""
self._session.close()

def {{ method.name|snake_case }}(self,
request: {{ method.input.ident }}, *,
retry: retries.Retry = gapic_v1.method.DEFAULT,
Expand Down
34 changes: 34 additions & 0 deletions tests/system/test_client_context_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import pytest

from conftest import construct_client, EchoClient


def test_client():
with construct_client(EchoClient, False) as c:
resp = c.echo({
'content': 'hello'
})
assert resp.content == 'hello'


def test_client_destroyed(echo):
echo.__exit__(None, None, None)
with pytest.raises(ValueError):
echo.echo({
'content': 'hello'
})