Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit ce3b84c

Browse files
authored
fix: streaming for sync REST API calls (#2204)
1 parent c3d9414 commit ce3b84c

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _get_http_options():
152152
return http_options
153153
{% endmacro %}
154154

155-
{% macro response_method(body_spec, is_async=False) %}
155+
{% macro response_method(body_spec, is_async=False, is_streaming_method=False) %}
156156
{% set async_prefix = "async " if is_async else "" %}
157157
{% set await_prefix = "await " if is_async else "" %}
158158
@staticmethod
@@ -177,6 +177,14 @@ def _get_http_options():
177177
{% if body_spec %}
178178
data=body,
179179
{% endif %}
180+
{% if not is_async and is_streaming_method %}
181+
{# NOTE: The underlying `requests` library used for making a sync request
182+
# requires us to set `stream=True` to avoid loading the entire response
183+
# into memory at once. For an async request, given its nature where it
184+
# reads data chunk by chunk, this is not required.
185+
#}
186+
stream=True,
187+
{% endif %}
180188
)
181189
return response
182190
{% endmacro %}
@@ -400,7 +408,7 @@ class _{{ name }}(_Base{{ service.name }}RestTransport._Base{{name}}, {{ async_m
400408
return hash("{{ async_method_name_prefix }}{{ service.name }}RestTransport.{{ name }}")
401409

402410
{% set body_spec = api.mixin_http_options["{}".format(name)][0].body %}
403-
{{ response_method(body_spec) | indent(4) }}
411+
{{ response_method(body_spec, is_async=is_async, is_streaming_method=None) | indent(4) }}
404412

405413
{{ async_prefix }}def __call__(self,
406414
request: {{ sig.request_type }}, *,

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class {{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
196196
{% if method.http_options and not method.client_streaming %}
197197

198198
{% set body_spec = method.http_options[0].body %}
199-
{{ shared_macros.response_method(body_spec)|indent(8) }}
199+
{{ shared_macros.response_method(body_spec, is_async=False, is_streaming_method=method.server_streaming)|indent(8) }}
200200
{% endif %}{# method.http_options and not method.client_streaming #}
201201

202202
def __call__(self,

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/rest_asyncio.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class Async{{service.name}}RestTransport(_Base{{ service.name }}RestTransport):
155155
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2169): Implement client streaming method. #}
156156
{% if method.http_options and not method.client_streaming %}
157157
{% set body_spec = method.http_options[0].body %}
158-
{{ shared_macros.response_method(body_spec, is_async=True)|indent(8) }}
158+
{{ shared_macros.response_method(body_spec, is_async=True, is_streaming_method=None)|indent(8) }}
159159

160160
{% endif %}{# method.http_options and not method.client_streaming and not method.paged_result_field #}
161161
async def __call__(self,

tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/transports/rest_asyncio.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ def __hash__(self):
16701670
return hash("AsyncCloudRedisRestTransport.GetLocation")
16711671

16721672
@staticmethod
1673-
def _get_response(
1673+
async def _get_response(
16741674
host,
16751675
metadata,
16761676
query_params,
@@ -1683,7 +1683,7 @@ def _get_response(
16831683
method = transcoded_request['method']
16841684
headers = dict(metadata)
16851685
headers['Content-Type'] = 'application/json'
1686-
response = getattr(session, method)(
1686+
response = await getattr(session, method)(
16871687
"{host}{uri}".format(host=host, uri=uri),
16881688
timeout=timeout,
16891689
headers=headers,
@@ -1747,7 +1747,7 @@ def __hash__(self):
17471747
return hash("AsyncCloudRedisRestTransport.ListLocations")
17481748

17491749
@staticmethod
1750-
def _get_response(
1750+
async def _get_response(
17511751
host,
17521752
metadata,
17531753
query_params,
@@ -1760,7 +1760,7 @@ def _get_response(
17601760
method = transcoded_request['method']
17611761
headers = dict(metadata)
17621762
headers['Content-Type'] = 'application/json'
1763-
response = getattr(session, method)(
1763+
response = await getattr(session, method)(
17641764
"{host}{uri}".format(host=host, uri=uri),
17651765
timeout=timeout,
17661766
headers=headers,
@@ -1824,7 +1824,7 @@ def __hash__(self):
18241824
return hash("AsyncCloudRedisRestTransport.CancelOperation")
18251825

18261826
@staticmethod
1827-
def _get_response(
1827+
async def _get_response(
18281828
host,
18291829
metadata,
18301830
query_params,
@@ -1837,7 +1837,7 @@ def _get_response(
18371837
method = transcoded_request['method']
18381838
headers = dict(metadata)
18391839
headers['Content-Type'] = 'application/json'
1840-
response = getattr(session, method)(
1840+
response = await getattr(session, method)(
18411841
"{host}{uri}".format(host=host, uri=uri),
18421842
timeout=timeout,
18431843
headers=headers,
@@ -1894,7 +1894,7 @@ def __hash__(self):
18941894
return hash("AsyncCloudRedisRestTransport.DeleteOperation")
18951895

18961896
@staticmethod
1897-
def _get_response(
1897+
async def _get_response(
18981898
host,
18991899
metadata,
19001900
query_params,
@@ -1907,7 +1907,7 @@ def _get_response(
19071907
method = transcoded_request['method']
19081908
headers = dict(metadata)
19091909
headers['Content-Type'] = 'application/json'
1910-
response = getattr(session, method)(
1910+
response = await getattr(session, method)(
19111911
"{host}{uri}".format(host=host, uri=uri),
19121912
timeout=timeout,
19131913
headers=headers,
@@ -1964,7 +1964,7 @@ def __hash__(self):
19641964
return hash("AsyncCloudRedisRestTransport.GetOperation")
19651965

19661966
@staticmethod
1967-
def _get_response(
1967+
async def _get_response(
19681968
host,
19691969
metadata,
19701970
query_params,
@@ -1977,7 +1977,7 @@ def _get_response(
19771977
method = transcoded_request['method']
19781978
headers = dict(metadata)
19791979
headers['Content-Type'] = 'application/json'
1980-
response = getattr(session, method)(
1980+
response = await getattr(session, method)(
19811981
"{host}{uri}".format(host=host, uri=uri),
19821982
timeout=timeout,
19831983
headers=headers,
@@ -2041,7 +2041,7 @@ def __hash__(self):
20412041
return hash("AsyncCloudRedisRestTransport.ListOperations")
20422042

20432043
@staticmethod
2044-
def _get_response(
2044+
async def _get_response(
20452045
host,
20462046
metadata,
20472047
query_params,
@@ -2054,7 +2054,7 @@ def _get_response(
20542054
method = transcoded_request['method']
20552055
headers = dict(metadata)
20562056
headers['Content-Type'] = 'application/json'
2057-
response = getattr(session, method)(
2057+
response = await getattr(session, method)(
20582058
"{host}{uri}".format(host=host, uri=uri),
20592059
timeout=timeout,
20602060
headers=headers,

0 commit comments

Comments
 (0)