-
Notifications
You must be signed in to change notification settings - Fork 44
First commit of extra examples #230
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" /> | ||
<None Include="appsettings*.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using Google.Cloud.Functions.Framework; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CustomPayload1 | ||
{ | ||
public class Function : ICloudEventFunction<Payload> | ||
{ | ||
public Task HandleAsync(CloudEvent cloudEvent, Payload data, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"ID: {cloudEvent.Id}"); | ||
Console.WriteLine($"Text: {data.Text}"); | ||
Console.WriteLine($"Number: {data.Number}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using CloudNative.CloudEvents.SystemTextJson; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CustomPayload1 | ||
{ | ||
[CloudEventFormatter(typeof(JsonEventFormatter<Payload>))] | ||
public class Payload | ||
{ | ||
// Note: System.Text.Json is case-sensitive. The Newtonsoft.Json | ||
// deserializer wouldn't require the attributes. (Or if the JSON | ||
// matched the property name casing, we wouldn't need them then either.) | ||
[JsonPropertyName("text")] | ||
public string Text { get; set; } | ||
|
||
[JsonPropertyName("number")] | ||
public double Number { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CloudNative.CloudEvents.NewtonsoftJson" Version="2.1.1" /> | ||
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" /> | ||
<None Include="appsettings*.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using CloudNative.CloudEvents.NewtonsoftJson; | ||
using Google.Cloud.Functions.Framework; | ||
using Google.Cloud.Functions.Hosting; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ThirdPartyPackage; | ||
|
||
namespace CustomPayload2 | ||
{ | ||
public class Startup : FunctionsStartup | ||
{ | ||
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) => | ||
services.AddSingleton<CloudEventFormatter, JsonEventFormatter<Payload>>(); | ||
} | ||
|
||
[FunctionsStartup(typeof(Startup))] | ||
public class Function : ICloudEventFunction<Payload> | ||
{ | ||
public Task HandleAsync(CloudEvent cloudEvent, Payload data, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"ID: {cloudEvent.Id}"); | ||
Console.WriteLine($"Text: {data.Text}"); | ||
Console.WriteLine($"Number: {data.Number}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2021, 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. | ||
|
||
namespace ThirdPartyPackage | ||
{ | ||
// The event payload, as defined by a third-party. The third-party | ||
// doesn't depend on the CloudEvents SDK at all. | ||
public class Payload | ||
{ | ||
public string Text { get; set; } | ||
public double Number { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using CloudNative.CloudEvents.NewtonsoftJson; | ||
using Google.Cloud.Functions.Hosting; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ThirdPartyPackage1; | ||
|
||
namespace ThirdPartyPackage2 | ||
{ | ||
public class CloudEventFormatterStartup : FunctionsStartup | ||
{ | ||
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) => | ||
services.AddSingleton<CloudEventFormatter, JsonEventFormatter<Payload>>(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CloudNative.CloudEvents.NewtonsoftJson" Version="2.1.1" /> | ||
<PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" /> | ||
<None Include="appsettings*.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using Google.Cloud.Functions.Framework; | ||
using Google.Cloud.Functions.Hosting; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ThirdPartyPackage1; | ||
using ThirdPartyPackage2; | ||
|
||
namespace CustomPayload3 | ||
{ | ||
[FunctionsStartup(typeof(CloudEventFormatterStartup))] | ||
public class Function : ICloudEventFunction<Payload> | ||
{ | ||
public Task HandleAsync(CloudEvent cloudEvent, Payload data, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"ID: {cloudEvent.Id}"); | ||
Console.WriteLine($"Text: {data.Text}"); | ||
Console.WriteLine($"Number: {data.Number}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2021, 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. | ||
|
||
namespace ThirdPartyPackage1 | ||
{ | ||
// The event payload, as defined by a third-party. The package | ||
// defining the payload doesn't depend on the CloudEvents SDK. | ||
public class Payload | ||
{ | ||
public string Text { get; set; } | ||
public double Number { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2021, 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. | ||
|
||
using CloudNative.CloudEvents; | ||
using Google.Cloud.Functions.Framework; | ||
using Google.Cloud.Functions.Hosting; | ||
using Google.Events.Protobuf.Cloud.PubSub.V1; | ||
using Google.Events.Protobuf.Cloud.Storage.V1; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace GoogleTypeDetection | ||
{ | ||
public class Startup : FunctionsStartup | ||
{ | ||
public override void ConfigureServices(WebHostBuilderContext context, IServiceCollection services) => | ||
services.AddSingleton<CloudEventFormatter, GoogleEventFormatter>(); | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open question: can we get to a point where this is the pre-configured default in the Functions Framework so developers don't need to provide a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the readme for more thoughts on this. We could potentially have an Having this as a vanilla default is also easy if we're happy to a) make the Functions Framework depend on the Google Events library; b) favour Google event payloads over other payloads. Lots of options to talk about... |
||
} | ||
|
||
[FunctionsStartup(typeof(Startup))] | ||
public class Function : ICloudEventFunction | ||
{ | ||
public Task HandleAsync(CloudEvent cloudEvent, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine($"ID: {cloudEvent.Id}"); | ||
switch (cloudEvent.Data) | ||
{ | ||
case StorageObjectData storage: | ||
Console.WriteLine($"Storage Bucket: {storage.Bucket}"); | ||
break; | ||
case MessagePublishedData pubsub: | ||
Console.WriteLine($"PubSub subscription: {pubsub.Subscription}"); | ||
break; | ||
default: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is possible to combine this with the 3rd party sample somehow? So I can have a case MyServiceSpecificPayload payload:
Console.WriteLine("This isn't a Google Event but it is a known event");
break; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that gets tricky - somewhere you've got to know where to get all the event types from. But we can think about it... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is probably less common / lower priority use case so I don't think we need to go out of our way to optimize for it. |
||
Console.WriteLine($"Unhandled event type {cloudEvent.Type}"); | ||
break; | ||
} | ||
Comment on lines
+40
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😍 |
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit odd to me that the formatter is configured as a Functions Framework service and not on the Google Events library. Said another way: how do we expect customers will use the Google Events library outside of the Functions Framework? For example consider the use case of deploying an ASP.NET app to CloudRun and point an EventArc subscription at it. I would expect to be able to do something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in a scenario where you're not using the Google Events library at all - it's not a Google event, it's a custom event payload.
Using the Google Events library outside the Functions Framework, you'd need to set up code to receive the CloudEvent - that's one area of the SDK that's still slightly TBD, but doesn't feel like it's in the remit of the Google Events library particularly - that should make it easy to parse the payload as a Google event payload, but that's all. (If you're writing a web server to handle a non-Google-payload CloudEvent request, it's not really our business, even if it happens that the request is made by Eventarc.)