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 new sample for response streaming #2222

Merged
merged 15 commits into from Jul 11, 2023
Merged
28 changes: 28 additions & 0 deletions functions/responsestreaming/ResponseStreaming.sln
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamBigQuery", "StreamBigQuery\StreamBigQuery.csproj", "{3B0C08AE-367D-4F0F-8B1D-C74093C760F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamBigQuery.Tests", "StreamBigQuery.Tests\StreamBigQuery.Tests.csproj", "{6774A3EE-4121-4DD9-9ABD-EFB675CD6E03}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B0C08AE-367D-4F0F-8B1D-C74093C760F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B0C08AE-367D-4F0F-8B1D-C74093C760F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B0C08AE-367D-4F0F-8B1D-C74093C760F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B0C08AE-367D-4F0F-8B1D-C74093C760F8}.Release|Any CPU.Build.0 = Release|Any CPU
{6774A3EE-4121-4DD9-9ABD-EFB675CD6E03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6774A3EE-4121-4DD9-9ABD-EFB675CD6E03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6774A3EE-4121-4DD9-9ABD-EFB675CD6E03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6774A3EE-4121-4DD9-9ABD-EFB675CD6E03}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
janell-chen marked this conversation as resolved.
Show resolved Hide resolved

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
<PackageReference Include="Google.Cloud.Functions.Testing" Version="2.1.0" />
<PackageReference Include="Google.Events.Protobuf" Version="1.3.0" />
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\StreamBigQuery\StreamBigQuery.csproj" />
</ItemGroup>

</Project>
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 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
*
* http://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.
*/
using Google.Cloud.Functions.Testing;
using System.Net;
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
using System.Net.Http;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace StreamBigQuery.Tests;

public class StreamBigQueryTest : FunctionTestBase<StreamBigQuery.Function>
{
[Fact]
public void TestRunStreamBigQuery()
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
{
var request = new HttpRequestMessage
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
{
Method = HttpMethod.Post,
Content = new StringContent("")
};
var responseBody = ExecuteHttpGetRequestAsync().Result;
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
var outputLines = responseBody.Split('\n');
Assert.Equal(1001, outputLines.Length);
}
}
jskeet marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 45 additions & 0 deletions functions/responsestreaming/StreamBigQuery/Function.cs
@@ -0,0 +1,45 @@
// Copyright 2023 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.

// [START functions_response_streaming]

using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Google.Cloud.BigQuery.V2;
using System;
janell-chen marked this conversation as resolved.
Show resolved Hide resolved

namespace StreamBigQuery;

public class Function : IHttpFunction
{
public async Task HandleAsync(HttpContext context)
{
string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
BigQueryClient client = BigQueryClient.Create(projectId).WithDefaultLocation(Locations.UnitedStates);

// Example to retrieve a large payload from BigQuery public dataset
string query = "SELECT abstract FROM `bigquery-public-data.breathe.bioasq` LIMIT 1000";
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
BigQueryParameter[] parameters = null;
BigQueryResults results = client.ExecuteQuery(query, parameters);

// Stream out the payload response by iterating rows (payload chunked by row).
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
foreach (BigQueryRow row in results)
{
await context.Response.WriteAsync($"{row["abstract"]}\n");
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

janell-chen marked this conversation as resolved.
Show resolved Hide resolved
// [END functions_response_streaming]
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Cloud.BigQuery.V2" Version="3.2.0" />
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="2.0.0" />
janell-chen marked this conversation as resolved.
Show resolved Hide resolved
<None Include="appsettings*.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions functions/responsestreaming/runTests.ps1
@@ -0,0 +1,16 @@
# Copyright 2023 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.

dotnet restore --force
dotnet test --no-restore --test-adapter-path:. --logger:junit 2>&1 | %{ "$_" }