Skip to content

fix(v1): resolve issue handling protobuf responses in rest streaming #608

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions google/api_core/rest_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,31 @@

from collections import deque
import string
from typing import Deque
from typing import Deque, Union

import proto
import requests
import google.protobuf.message
from google.protobuf.json_format import Parse


class ResponseIterator:
"""Iterator over REST API responses.

Args:
response (requests.Response): An API response object.
response_message_cls (Callable[proto.Message]): A proto
response_message_cls (Union[proto.Message, google.protobuf.message.Message]): A response
class expected to be returned from an API.

Raises:
ValueError: If `response_message_cls` is not a subclass of `proto.Message` or `google.protobuf.message.Message`.
"""

def __init__(self, response: requests.Response, response_message_cls):
def __init__(
self,
response: requests.Response,
response_message_cls: Union[proto.Message, google.protobuf.message.Message],
):
self._response = response
self._response_message_cls = response_message_cls
# Inner iterator over HTTP response's content.
Expand Down Expand Up @@ -107,7 +117,14 @@ def __next__(self):

def _grab(self):
# Add extra quotes to make json.loads happy.
return self._response_message_cls.from_json(self._ready_objs.popleft())
if issubclass(self._response_message_cls, proto.Message):
return self._response_message_cls.from_json(self._ready_objs.popleft())
elif issubclass(self._response_message_cls, google.protobuf.message.Message):
return Parse(self._ready_objs.popleft(), self._response_message_cls())
else:
raise ValueError(
"Response message class must be a subclass of proto.Message or google.protobuf.message.Message."
)

def __iter__(self):
return self
36 changes: 29 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def lint(session):
Returns a failure if the linters find linting errors or sufficiently
serious code quality issues.
"""
session.install("flake8", BLACK_VERSION)
session.install("flake8==6.0.0", BLACK_VERSION)
session.install(".")
session.run(
"black",
Expand Down Expand Up @@ -97,9 +97,7 @@ def default(session, install_grpc=True):
session.install(
"dataclasses",
"mock",
# Revert to just "pytest" once
# https://github.com/pytest-dev/pytest/issues/10451 is fixed
"pytest<7.2.0",
"pytest",
"pytest-cov",
"pytest-xdist",
)
Expand All @@ -112,7 +110,7 @@ def default(session, install_grpc=True):
pytest_args = [
"python",
"-m",
"py.test",
"pytest",
*(
# Helpful for running a single test or testfile.
session.posargs
Expand Down Expand Up @@ -217,7 +215,20 @@ def docs(session):
"""Build the docs for this library."""

session.install("-e", ".[grpc]")
session.install("sphinx==4.2.0", "alabaster", "recommonmark")
session.install(
# We need to pin to specific versions of the `sphinxcontrib-*` packages
# which still support sphinx 4.x.
# See https://github.com/googleapis/sphinx-docfx-yaml/issues/344
# and https://github.com/googleapis/sphinx-docfx-yaml/issues/345.
"sphinxcontrib-applehelp==1.0.4",
"sphinxcontrib-devhelp==1.0.2",
"sphinxcontrib-htmlhelp==2.0.1",
"sphinxcontrib-qthelp==1.0.3",
"sphinxcontrib-serializinghtml==1.1.5",
"sphinx==4.5.0",
"alabaster",
"recommonmark",
)

shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
session.run(
Expand All @@ -240,7 +251,18 @@ def docfx(session):

session.install("-e", ".")
session.install(
"sphinx==4.0.1", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml"
# We need to pin to specific versions of the `sphinxcontrib-*` packages
# which still support sphinx 4.x.
# See https://github.com/googleapis/sphinx-docfx-yaml/issues/344
# and https://github.com/googleapis/sphinx-docfx-yaml/issues/345.
"sphinxcontrib-applehelp==1.0.4",
"sphinxcontrib-devhelp==1.0.2",
"sphinxcontrib-htmlhelp==2.0.1",
"sphinxcontrib-qthelp==1.0.3",
"sphinxcontrib-serializinghtml==1.1.5",
"gcp-sphinx-docfx-yaml",
"alabaster",
"recommonmark",
)

shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
Expand Down
Loading