Skip to content

Commit e50e0c4

Browse files
authored
Response streaming (#9975)
* Add python sample for response streaming. * Add python response streaming sample. * Changed prefix to include functions. * Removed long list of dependencies * Adding pytest dependency.
1 parent 0836aff commit e50e0c4

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_response_streaming]
16+
from flask import Response, stream_with_context
17+
18+
import functions_framework
19+
from google.cloud import bigquery
20+
21+
# Construct a BigQuery client object.
22+
client = bigquery.Client()
23+
24+
25+
@functions_framework.http
26+
def stream_big_query_output(request):
27+
"""HTTP Cloud Function.
28+
Args:
29+
request (flask.Request): The request object.
30+
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
31+
Returns:
32+
The response text, or any set of values that can be turned into a
33+
Response object using `make_response`
34+
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
35+
"""
36+
# Example large query from public dataset
37+
query = """
38+
SELECT abstract
39+
FROM `bigquery-public-data.breathe.bioasq`
40+
LIMIT 1000
41+
"""
42+
query_job = client.query(query) # Make an API request.
43+
44+
def generate():
45+
for row in query_job:
46+
yield row[0]
47+
48+
return Response(stream_with_context(generate()))
49+
50+
51+
# [END functions_response_streaming]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import flask
15+
import pytest
16+
17+
import main
18+
19+
20+
# Create a fake "app" for generating test request contexts.
21+
@pytest.fixture(scope="module")
22+
def app() -> flask.Flask:
23+
return flask.Flask(__name__)
24+
25+
26+
def test_main(app):
27+
with app.test_request_context():
28+
response = main.stream_big_query_output(flask.request)
29+
assert response.is_streamed
30+
assert response.status_code == 200
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==2.2.2
2+
functions-framework==3.3.0
3+
google-cloud-bigquery==3.6.0
4+
pytest==7.2.1

0 commit comments

Comments
 (0)