From 4f8dad8b24e7e7e1f47c70a6682664d45ce445a3 Mon Sep 17 00:00:00 2001 From: JialinXin Date: Thu, 1 Jul 2021 13:25:07 +0800 Subject: [PATCH] [WebPubSubFunction] Add AWPS function samples. (#22140) # All SDK Contribution checklist: This checklist is used to make sure that common guidelines for a pull request are followed. - [ ] **Please open PR in `Draft` mode if it is:** - Work in progress or not intended to be merged. - Encountering multiple pipeline failures and working on fixes. - [ ] If an SDK is being regenerated based on a new swagger spec, a link to the pull request containing these swagger spec changes has been included above. - [ ] **I have read the [contribution guidelines](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md).** - [ ] **The pull request does not introduce [breaking changes](https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/breaking-change-rules.md).** ### [General Guidelines and Best Practices](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md#general-guidelines) - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### [Testing Guidelines](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md#testing-guidelines) - [ ] Pull request includes test coverage for the included changes. ### [SDK Generation Guidelines](https://github.com/Azure/azure-sdk-for-net/blob/main/CONTRIBUTING.md#sdk-generation-guidelines) - [ ] The generate.cmd file for the SDK has been updated with the version of AutoRest, as well as the commitid of your swagger spec or link to the swagger spec, used to generate the code. (Track 2 only) - [ ] The `*.csproj` and `AssemblyInfo.cs` files have been updated with the new version of the SDK. Please double check nuget.org current release version. ## Additional management plane SDK specific contribution checklist: Note: Only applies to `Microsoft.Azure.Management.[RP]` or `Azure.ResourceManager.[RP]` - [ ] Include updated [management metadata](https://github.com/Azure/azure-sdk-for-net/tree/main/eng/mgmt/mgmtmetadata). - [ ] Update AzureRP.props to add/remove version info to maintain up to date API versions. ### Management plane SDK Troubleshooting - If this is very first SDK for a services and you are adding new service folders directly under /SDK, please add `new service` label and/or contact assigned reviewer. - If the check fails at the `Verify Code Generation` step, please ensure: - Do not modify any code in generated folders. - Do not selectively include/remove generated files in the PR. - Do use `generate.ps1/cmd` to generate this PR instead of calling `autorest` directly. Please pay attention to the @microsoft.csharp version output after running `generate.ps1`. If it is lower than current released version (2.3.82), please run it again as it should pull down the latest version. **Note: We have recently updated the PSH module called by `generate.ps1` to emit additional data. This would help reduce/eliminate the Code Verification check error. Please run following command**: `dotnet msbuild eng/mgmt.proj /t:Util /p:UtilityName=InstallPsModules` ### Old outstanding PR cleanup Please note: If PRs (including draft) has been out for more than 60 days and there are no responses from our query or followups, they will be closed to maintain a concise list for our reviewers. --- .../README.md | 85 +++++++++++-------- .../WebPubSubConnectionBindingFunction.cs | 25 ++++++ .../samples/WebPubSubOutputBindingFunction.cs | 28 ++++++ .../samples/WebPubSubTriggerFunction.cs | 24 ++++++ .../WebPubSubTriggerReturnValueFunction.cs | 24 ++++++ ....WebJobs.Extensions.WebPubSub.Tests.csproj | 3 +- 6 files changed, 154 insertions(+), 35 deletions(-) create mode 100644 sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubConnectionBindingFunction.cs create mode 100644 sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubOutputBindingFunction.cs create mode 100644 sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerFunction.cs create mode 100644 sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerReturnValueFunction.cs diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/README.md b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/README.md index 0adcb2fb1a9a..f64da2ede16c 100644 --- a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/README.md +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/README.md @@ -2,6 +2,12 @@ This extension provides functionality for receiving Web PubSub webhook calls in Azure Functions, allowing you to easily write functions that respond to any event published to Web PubSub. +[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/src) | +[Package](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.WebPubSub) | +[API reference documentation](https://azure.github.io/azure-webpubsub/references/functions-bindings) | +[Product documentation](https://aka.ms/awps/doc) | +[Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/) + ## Getting started ### Install the package @@ -56,61 +62,72 @@ In `Connect` and `Message` events, function will respect return values to send b ### Functions that uses Web PubSub input binding -```cs -[FunctionName("WebPubSubInputBindingFunction")] -public static WebPubSubConnection Run( - [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, - [WebPubSubConnection(Hub = "simplechat", UserId = "{query.userid}")] WebPubSubConnection connection) +```C# Snippet:WebPubSubConnectionBindingFunction +public static class WebPubSubConnectionBindingFunction { - Console.WriteLine("login"); - return connection; + [FunctionName("WebPubSubConnectionBindingFunction")] + public static WebPubSubConnection Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, + [WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection) + { + Console.WriteLine("login"); + return connection; + } } ``` ### Functions that uses Web PubSub output binding -```cs -[FunctionName("WebPubSubOutputBindingFunction")] -public static async Task RunAsync( - [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, - [WebPubSub(Hub = "simplechat")] IAsyncCollector operation) +```C# Snippet:WebPubSubOutputBindingFunction +public static class WebPubSubOutputBindingFunction { - await operation.AddAsync(new SendToAll + [FunctionName("WebPubSubOutputBindingFunction")] + public static async Task RunAsync( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, + [WebPubSub(Hub = "hub")] IAsyncCollector operation) { - Message = BinaryData.FromString("Hello Web PubSub"), - DataType = MessageDataType.Text - }); + await operation.AddAsync(new SendToAll + { + Message = BinaryData.FromString("Hello Web PubSub"), + DataType = MessageDataType.Text + }); + } } ``` ### Functions that uses Web PubSub trigger -```cs -[FunctionName("WebPubSubTriggerFunction")] -public static void Run( - [WebPubSubTrigger("message", WebPubSubEventType.User)] - ConnectionContext context, - string message, - MessageDataType dataType) +```C# Snippet:WebPubSubTriggerFunction +public static class WebPubSubTriggerFunction { - Console.WriteLine($"Request from: {context.userId}"); - Console.WriteLine($"Request message: {message}"); - Console.WriteLine($"Request message DataType: {dataType}"); + [FunctionName("WebPubSubTriggerFunction")] + public static void Run( + ILogger logger, + [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context, + string message, + MessageDataType dataType) + { + logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}", + context.UserId, message, dataType); + } } ``` ### Functions that uses Web PubSub trigger return value -```cs -[FunctionName("WebPubSubTriggerReturnValueFunction")] -public static MessageResponse RunAsync( - [WebPubSubTrigger("message", WebPubSubEventType.User)] ConnectionContext context) +```C# Snippet:WebPubSubTriggerReturnValueFunction +public static class WebPubSubTriggerReturnValueFunction { - return new MessageResponse + [FunctionName("WebPubSubTriggerReturnValueFunction")] + public static MessageResponse Run( + [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context) { - Message = BinaryData.FromString("ack"), - DataType = MessageDataType.Text - }; + return new MessageResponse + { + Message = BinaryData.FromString("ack"), + DataType = MessageDataType.Text + }; + } } ``` diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubConnectionBindingFunction.cs b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubConnectionBindingFunction.cs new file mode 100644 index 000000000000..b2a0bbfa41a7 --- /dev/null +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubConnectionBindingFunction.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Azure.WebJobs.Extensions.Http; +using Microsoft.Azure.WebJobs.Extensions.WebPubSub; + +namespace Microsoft.Azure.WebJobs.Samples +{ + #region Snippet:WebPubSubConnectionBindingFunction + public static class WebPubSubConnectionBindingFunction + { + [FunctionName("WebPubSubConnectionBindingFunction")] + public static WebPubSubConnection Run( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, + [WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection) + { + Console.WriteLine("login"); + return connection; + } + } + #endregion +} diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubOutputBindingFunction.cs b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubOutputBindingFunction.cs new file mode 100644 index 000000000000..238aa164cbbe --- /dev/null +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubOutputBindingFunction.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Azure.WebJobs.Extensions.Http; +using Microsoft.Azure.WebJobs.Extensions.WebPubSub; + +namespace Microsoft.Azure.WebJobs.Samples +{ + #region Snippet:WebPubSubOutputBindingFunction + public static class WebPubSubOutputBindingFunction + { + [FunctionName("WebPubSubOutputBindingFunction")] + public static async Task RunAsync( + [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, + [WebPubSub(Hub = "hub")] IAsyncCollector operation) + { + await operation.AddAsync(new SendToAll + { + Message = BinaryData.FromString("Hello Web PubSub"), + DataType = MessageDataType.Text + }); + } + } + #endregion +} diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerFunction.cs b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerFunction.cs new file mode 100644 index 000000000000..3687f9d33716 --- /dev/null +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerFunction.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Microsoft.Azure.WebJobs.Extensions.WebPubSub; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.WebJobs.Samples +{ + #region Snippet:WebPubSubTriggerFunction + public static class WebPubSubTriggerFunction + { + [FunctionName("WebPubSubTriggerFunction")] + public static void Run( + ILogger logger, + [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context, + string message, + MessageDataType dataType) + { + logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}", + context.UserId, message, dataType); + } + } + #endregion +} diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerReturnValueFunction.cs b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerReturnValueFunction.cs new file mode 100644 index 000000000000..739d59fddf45 --- /dev/null +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/WebPubSubTriggerReturnValueFunction.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Microsoft.Azure.WebJobs.Extensions.WebPubSub; + +namespace Microsoft.Azure.WebJobs.Samples +{ + #region Snippet:WebPubSubTriggerReturnValueFunction + public static class WebPubSubTriggerReturnValueFunction + { + [FunctionName("WebPubSubTriggerReturnValueFunction")] + public static MessageResponse Run( + [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context) + { + return new MessageResponse + { + Message = BinaryData.FromString("ack"), + DataType = MessageDataType.Text + }; + } + } + #endregion +} diff --git a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/tests/Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/tests/Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj index 502eea7d5a0c..f920786f3a0f 100644 --- a/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/tests/Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj +++ b/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/tests/Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj @@ -7,10 +7,11 @@ + - +