Skip to content

Commit 33c0805

Browse files
authored
Add sample for functions : Response streaming (GoogleCloudPlatform#1854)
* Add response streaming sample in PHP * Remove test * Add unit test * Add region tags * Address comments * lint fix * fix lint
1 parent 1904e96 commit 33c0805

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^1.1",
4+
"google/cloud-bigquery": "^1.24"
5+
}
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START functions_response_streaming]
19+
use Psr\Http\Message\ServerRequestInterface;
20+
use Google\Cloud\BigQuery\BigQueryClient;
21+
22+
function streamBigQuery(ServerRequestInterface $request)
23+
{
24+
// Provide the Cloud Project ID by setting env var.
25+
$projectId = getenv('GOOGLE_PROJECT_ID');
26+
// Example large payload from BigQuery's public dataset.
27+
$bigQuery = new BigQueryClient(['projectId' => $projectId]);
28+
$queryJobConfig = $bigQuery->query(
29+
'SELECT abstract FROM `bigquery-public-data.breathe.bioasq` LIMIT 1000'
30+
);
31+
$queryResults = $bigQuery->runQuery($queryJobConfig);
32+
33+
// Stream out large payload by iterating rows and flushing output.
34+
foreach ($queryResults as $row) {
35+
foreach ($row as $column => $value) {
36+
printf('%s' . PHP_EOL, json_encode($value));
37+
flush();
38+
}
39+
}
40+
printf('Successfully streamed rows');
41+
}
42+
// [END functions_response_streaming]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2020 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions HTTP Methods Test Suite">
20+
<directory>test</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
</logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.</directory>
29+
<exclude>
30+
<directory>./vendor</directory>
31+
</exclude>
32+
</whitelist>
33+
</filter>
34+
</phpunit>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the samples:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/dlp/README.md
22+
*/
23+
declare(strict_types=1);
24+
25+
namespace Google\Cloud\Samples\Functions\ResponseStreaming\Test;
26+
27+
use GuzzleHttp\Psr7\ServerRequest;
28+
use PHPUnit\Framework\TestCase;
29+
30+
/**
31+
* Unit tests for the Cloud Function.
32+
*/
33+
class UnitTest extends TestCase
34+
{
35+
private static $entryPoint = 'streamBigQuery';
36+
37+
public static function setUpBeforeClass(): void
38+
{
39+
require_once __DIR__ . '/../index.php';
40+
}
41+
42+
public function testFunction(): void
43+
{
44+
$request = new ServerRequest('POST', '/', [], '');
45+
ob_start();
46+
$this->runFunction(self::$entryPoint, [$request]);
47+
$result = ob_get_clean();
48+
$this->assertStringContainsString('Successfully streamed rows', $result);
49+
}
50+
51+
private static function runFunction($functionName, array $params = []): void
52+
{
53+
call_user_func_array($functionName, $params);
54+
}
55+
}

0 commit comments

Comments
 (0)