Skip to content

Commit 4f8dad8

Browse files
authored
[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.
1 parent a1645de commit 4f8dad8

File tree

6 files changed

+154
-35
lines changed

6 files changed

+154
-35
lines changed

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/README.md

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
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.
44

5+
[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/src) |
6+
[Package](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.WebPubSub) |
7+
[API reference documentation](https://azure.github.io/azure-webpubsub/references/functions-bindings) |
8+
[Product documentation](https://aka.ms/awps/doc) |
9+
[Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/)
10+
511
## Getting started
612

713
### Install the package
@@ -56,61 +62,72 @@ In `Connect` and `Message` events, function will respect return values to send b
5662

5763
### Functions that uses Web PubSub input binding
5864

59-
```cs
60-
[FunctionName("WebPubSubInputBindingFunction")]
61-
public static WebPubSubConnection Run(
62-
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
63-
[WebPubSubConnection(Hub = "simplechat", UserId = "{query.userid}")] WebPubSubConnection connection)
65+
```C# Snippet:WebPubSubConnectionBindingFunction
66+
public static class WebPubSubConnectionBindingFunction
6467
{
65-
Console.WriteLine("login");
66-
return connection;
68+
[FunctionName("WebPubSubConnectionBindingFunction")]
69+
public static WebPubSubConnection Run(
70+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
71+
[WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection)
72+
{
73+
Console.WriteLine("login");
74+
return connection;
75+
}
6776
}
6877
```
6978

7079
### Functions that uses Web PubSub output binding
7180

72-
```cs
73-
[FunctionName("WebPubSubOutputBindingFunction")]
74-
public static async Task RunAsync(
75-
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
76-
[WebPubSub(Hub = "simplechat")] IAsyncCollector<WebPubSubOperation> operation)
81+
```C# Snippet:WebPubSubOutputBindingFunction
82+
public static class WebPubSubOutputBindingFunction
7783
{
78-
await operation.AddAsync(new SendToAll
84+
[FunctionName("WebPubSubOutputBindingFunction")]
85+
public static async Task RunAsync(
86+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
87+
[WebPubSub(Hub = "hub")] IAsyncCollector<WebPubSubOperation> operation)
7988
{
80-
Message = BinaryData.FromString("Hello Web PubSub"),
81-
DataType = MessageDataType.Text
82-
});
89+
await operation.AddAsync(new SendToAll
90+
{
91+
Message = BinaryData.FromString("Hello Web PubSub"),
92+
DataType = MessageDataType.Text
93+
});
94+
}
8395
}
8496
```
8597

8698
### Functions that uses Web PubSub trigger
8799

88-
```cs
89-
[FunctionName("WebPubSubTriggerFunction")]
90-
public static void Run(
91-
[WebPubSubTrigger("message", WebPubSubEventType.User)]
92-
ConnectionContext context,
93-
string message,
94-
MessageDataType dataType)
100+
```C# Snippet:WebPubSubTriggerFunction
101+
public static class WebPubSubTriggerFunction
95102
{
96-
Console.WriteLine($"Request from: {context.userId}");
97-
Console.WriteLine($"Request message: {message}");
98-
Console.WriteLine($"Request message DataType: {dataType}");
103+
[FunctionName("WebPubSubTriggerFunction")]
104+
public static void Run(
105+
ILogger logger,
106+
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context,
107+
string message,
108+
MessageDataType dataType)
109+
{
110+
logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}",
111+
context.UserId, message, dataType);
112+
}
99113
}
100114
```
101115

102116
### Functions that uses Web PubSub trigger return value
103117

104-
```cs
105-
[FunctionName("WebPubSubTriggerReturnValueFunction")]
106-
public static MessageResponse RunAsync(
107-
[WebPubSubTrigger("message", WebPubSubEventType.User)] ConnectionContext context)
118+
```C# Snippet:WebPubSubTriggerReturnValueFunction
119+
public static class WebPubSubTriggerReturnValueFunction
108120
{
109-
return new MessageResponse
121+
[FunctionName("WebPubSubTriggerReturnValueFunction")]
122+
public static MessageResponse Run(
123+
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context)
110124
{
111-
Message = BinaryData.FromString("ack"),
112-
DataType = MessageDataType.Text
113-
};
125+
return new MessageResponse
126+
{
127+
Message = BinaryData.FromString("ack"),
128+
DataType = MessageDataType.Text
129+
};
130+
}
114131
}
115132
```
116133

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Azure.WebJobs.Extensions.Http;
8+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
9+
10+
namespace Microsoft.Azure.WebJobs.Samples
11+
{
12+
#region Snippet:WebPubSubConnectionBindingFunction
13+
public static class WebPubSubConnectionBindingFunction
14+
{
15+
[FunctionName("WebPubSubConnectionBindingFunction")]
16+
public static WebPubSubConnection Run(
17+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
18+
[WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection)
19+
{
20+
Console.WriteLine("login");
21+
return connection;
22+
}
23+
}
24+
#endregion
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Azure.WebJobs.Extensions.Http;
8+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
9+
10+
namespace Microsoft.Azure.WebJobs.Samples
11+
{
12+
#region Snippet:WebPubSubOutputBindingFunction
13+
public static class WebPubSubOutputBindingFunction
14+
{
15+
[FunctionName("WebPubSubOutputBindingFunction")]
16+
public static async Task RunAsync(
17+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
18+
[WebPubSub(Hub = "hub")] IAsyncCollector<WebPubSubOperation> operation)
19+
{
20+
await operation.AddAsync(new SendToAll
21+
{
22+
Message = BinaryData.FromString("Hello Web PubSub"),
23+
DataType = MessageDataType.Text
24+
});
25+
}
26+
}
27+
#endregion
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Microsoft.Azure.WebJobs.Samples
8+
{
9+
#region Snippet:WebPubSubTriggerFunction
10+
public static class WebPubSubTriggerFunction
11+
{
12+
[FunctionName("WebPubSubTriggerFunction")]
13+
public static void Run(
14+
ILogger logger,
15+
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context,
16+
string message,
17+
MessageDataType dataType)
18+
{
19+
logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}",
20+
context.UserId, message, dataType);
21+
}
22+
}
23+
#endregion
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
6+
7+
namespace Microsoft.Azure.WebJobs.Samples
8+
{
9+
#region Snippet:WebPubSubTriggerReturnValueFunction
10+
public static class WebPubSubTriggerReturnValueFunction
11+
{
12+
[FunctionName("WebPubSubTriggerReturnValueFunction")]
13+
public static MessageResponse Run(
14+
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context)
15+
{
16+
return new MessageResponse
17+
{
18+
Message = BinaryData.FromString("ack"),
19+
DataType = MessageDataType.Text
20+
};
21+
}
22+
}
23+
#endregion
24+
}

sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/tests/Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" />
1011
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1112
<PackageReference Include="Moq" />
1213
<PackageReference Include="NUnit" />
13-
<PackageReference Include="NUnit3TestAdapter"/>
14+
<PackageReference Include="NUnit3TestAdapter" />
1415
</ItemGroup>
1516

1617
<ItemGroup>

0 commit comments

Comments
 (0)