Skip to content

Commit b150b82

Browse files
authored
[WebPubSub] Init the Microsoft.Azure.WebPubSub.Common proj. (Azure#24359)
# 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 dc94a67 commit b150b82

26 files changed

+939
-0
lines changed

eng/Packages.Data.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<PackageReference Update="System.Globalization" Version="4.3.0" />
3838
<PackageReference Update="System.IdentityModel.Tokens.Jwt" Version="5.4.0" />
3939
<PackageReference Update="System.Linq" Version="4.3.0" />
40+
<PackageReference Update="System.Memory.Data" Version="1.0.2" />
4041
<PackageReference Update="System.Net.Http" Version="4.3.4" />
4142
<PackageReference Update="System.Reflection.TypeExtensions" Version="[4.5.1, 4.9.0)" />
4243
<PackageReference Update="System.Runtime.Extensions" Version="4.3.0" />
@@ -46,6 +47,7 @@
4647
<PackageReference Update="System.Security.Cryptography.Cng" Version="4.3.0" />
4748
<PackageReference Update="System.Security.Cryptography.Primitives" Version="4.3.0" />
4849
<PackageReference Update="System.Text.Encodings.Web" Version="4.5.1" />
50+
<PackageReference Update="System.Text.Json" Version="4.6.0" />
4951
<PackageReference Update="System.ValueTuple" Version="4.5.0" />
5052

5153
<PackageReference Update="WindowsAzure.Storage" Version="9.3.3" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Release History
2+
3+
## 1.0.0-beta.1 (Unreleased)
4+
5+
### Features Added
6+
7+
- Common library to define the upstream requests and responses from Service to Server in Azure Web PubSub service.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<!--
3+
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
4+
-->
5+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" />
6+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Azure Web PubSub Event Handler events data model client library for .NET
2+
3+
This library defines the class to process with Azure Web PubSub service upstream requests.
4+
5+
## Getting started
6+
7+
### Install the package
8+
9+
Install the client library from [NuGet](https://www.nuget.org/):
10+
11+
```dotnetcli
12+
dotnet add package Microsoft.Azure.WebPubSub.Common
13+
```
14+
15+
### Prerequisites
16+
17+
- An [Azure subscription][azure_sub].
18+
- An existing Azure Web PubSub service instance.
19+
20+
### Authenticate the client
21+
22+
Not applicable for the library. You should work with a client library to deserialize service requests in a friendly way.
23+
24+
## Key concepts
25+
26+
### Events
27+
28+
Connect, Connected, Disconnected are system events indicate connection stage. And Connect is a blocking event that service will wait for the response to determine next action. Any error returned will drop the connection.
29+
30+
User events are message event. It's also a blocking event which service is waiting for response. And server can return information in the response which will be sent to the caller directly.
31+
32+
### WebPubSubEventRequest
33+
34+
WebPubSubEventRequest, represents a abstract request come from service side. In detail, it should be ValidationRequest or one of the 4 events, which are ConnectEventRequest, ConnectedEventRequest, UserEventRequest and DisconnectedEventRequest. ValidationRequest represent the request for [Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0.1/http-webhook.md#4-abuse-protection).
35+
36+
### WebPubSubEventResponse
37+
38+
WebPubSubEventResponse, represents a abstract response should return to service. In detail, it should be EventErrorResponse or one of the 2 blocking events, which are ConnectEventResponse and UserEventResponse.
39+
40+
## Examples
41+
42+
Check Microsoft.Azure.WebPubSub.AspNetCore for E2E using examples.
43+
44+
## Troubleshooting
45+
46+
You can also easily [enable console logging](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#logging) if you want to dig deeper into the requests you're making against the service.
47+
48+
## Next steps
49+
50+
Please take a look at the
51+
[samples][samples_ref]
52+
directory for detailed examples on how to use this library.
53+
54+
## Contributing
55+
56+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit <https://cla.microsoft.com>.
57+
58+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
59+
60+
This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][code_of_conduct_faq] or contact opencode@microsoft.com with any additional questions or comments.
61+
62+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net%2Fsdk%2Feventgrid%2FMicrosoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents%2FREADME.png)
63+
64+
[azure_sub]: https://azure.microsoft.com/free/dotnet/
65+
[samples_ref]: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace System
9+
{
10+
internal class BinaryDataJsonConverter : JsonConverter<BinaryData>
11+
{
12+
public override BinaryData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
if (reader.TokenType == JsonTokenType.String)
15+
{
16+
return BinaryData.FromString(JsonSerializer.Deserialize<string>(ref reader));
17+
}
18+
19+
if (TryLoadBinary(ref reader, out var bytes))
20+
{
21+
return BinaryData.FromBytes(bytes);
22+
}
23+
24+
// string object
25+
return BinaryData.FromString(reader.GetString());
26+
}
27+
28+
public override void Write(Utf8JsonWriter writer, BinaryData value, JsonSerializerOptions options)
29+
{
30+
writer.WriteStringValue(value.ToString());
31+
}
32+
33+
private static bool TryLoadBinary(ref Utf8JsonReader input, out byte[] output)
34+
{
35+
var doc = JsonDocument.ParseValue(ref input);
36+
if (doc.RootElement.TryGetProperty("type", out var value) && value.GetString().Equals("Buffer", StringComparison.OrdinalIgnoreCase)
37+
&& doc.RootElement.TryGetProperty("data", out var data))
38+
{
39+
output = JsonSerializer.Deserialize<List<byte>>(data.GetRawText()).ToArray();
40+
return true;
41+
}
42+
output = null;
43+
return false;
44+
}
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
9+
namespace Microsoft.Azure.WebPubSub.Common
10+
{
11+
internal class ConnectEventRequestJsonConverter : JsonConverter<ConnectEventRequest>
12+
{
13+
public override ConnectEventRequest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
{
15+
var element = JsonDocument.ParseValue(ref reader).RootElement;
16+
17+
return new ConnectEventRequest(
18+
element.ReadToObject<Dictionary<string, string[]>>(ConnectEventRequest.ClaimsProperty),
19+
element.ReadToObject<Dictionary<string, string[]>>(ConnectEventRequest.QueryProperty),
20+
element.ReadToObject<string[]>(ConnectEventRequest.SubprotocolsProperty),
21+
element.ReadToObject<WebPubSubClientCertificate[]>(ConnectEventRequest.ClientCertificatesProperty));
22+
}
23+
24+
public override void Write(Utf8JsonWriter writer, ConnectEventRequest value, JsonSerializerOptions options)
25+
{
26+
writer.WriteStartObject();
27+
writer.WritePropertyName(ConnectEventRequest.ClaimsProperty);
28+
JsonSerializer.Serialize(writer, value.Claims, options);
29+
writer.WritePropertyName(ConnectEventRequest.QueryProperty);
30+
JsonSerializer.Serialize(writer, value.Query, options);
31+
writer.WritePropertyName(ConnectEventRequest.SubprotocolsProperty);
32+
JsonSerializer.Serialize(writer, value.Subprotocols, options);
33+
writer.WritePropertyName(ConnectEventRequest.ClientCertificatesProperty);
34+
JsonSerializer.Serialize(writer, value.ClientCertificates, options);
35+
if (value.ConnectionContext != null)
36+
{
37+
writer.WritePropertyName(WebPubSubEventRequest.ConnectionContextProperty);
38+
JsonSerializer.Serialize(writer, value.ConnectionContext, options);
39+
}
40+
writer.WriteEndObject();
41+
}
42+
}
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Microsoft.Azure.WebPubSub.Common
9+
{
10+
internal class DisconnectedEventRequestJsonConverter : JsonConverter<DisconnectedEventRequest>
11+
{
12+
public override DisconnectedEventRequest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
var element = JsonDocument.ParseValue(ref reader).RootElement;
15+
16+
return new DisconnectedEventRequest(element.ReadString(DisconnectedEventRequest.ReasonProperty));
17+
}
18+
19+
public override void Write(Utf8JsonWriter writer, DisconnectedEventRequest value, JsonSerializerOptions options)
20+
{
21+
writer.WriteStartObject();
22+
writer.WritePropertyName(DisconnectedEventRequest.ReasonProperty);
23+
JsonSerializer.Serialize(writer, value.Reason, options);
24+
if (value.ConnectionContext != null)
25+
{
26+
writer.WritePropertyName(WebPubSubEventRequest.ConnectionContextProperty);
27+
JsonSerializer.Serialize(writer, value.ConnectionContext, options);
28+
}
29+
writer.WriteEndObject();
30+
}
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace System.Text.Json
5+
{
6+
internal static class JsonElementExtensions
7+
{
8+
public static string ReadString(this JsonElement element, string name)
9+
{
10+
return element.GetProperty(name).GetString();
11+
}
12+
13+
public static TValue ReadToObject<TValue>(this JsonElement element, string name)
14+
{
15+
var value = element.GetProperty(name);
16+
return JsonSerializer.Deserialize<TValue>(value.GetRawText());
17+
}
18+
}
19+
}
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.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Microsoft.Azure.WebPubSub.Common
9+
{
10+
internal class WebPubSubClientCertificateJsonConverter : JsonConverter<WebPubSubClientCertificate>
11+
{
12+
public override WebPubSubClientCertificate Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
var element = JsonDocument.ParseValue(ref reader).RootElement;
15+
16+
return new WebPubSubClientCertificate(
17+
element.ReadString(WebPubSubClientCertificate.ThumbprintProperty));
18+
}
19+
20+
public override void Write(Utf8JsonWriter writer, WebPubSubClientCertificate value, JsonSerializerOptions options)
21+
{
22+
writer.WriteStartObject();
23+
writer.WritePropertyName(WebPubSubClientCertificate.ThumbprintProperty);
24+
JsonSerializer.Serialize(writer, value.Thumbprint, options);
25+
writer.WriteEndObject();
26+
}
27+
}
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+
namespace Microsoft.Azure.WebPubSub.Common
5+
{
6+
/// <summary>
7+
/// Message data type.
8+
/// </summary>
9+
public enum MessageDataType
10+
{
11+
/// <summary>
12+
/// binary of content type application/octet-stream.
13+
/// </summary>
14+
Binary,
15+
/// <summary>
16+
/// json of content type application/json.
17+
/// </summary>
18+
Json,
19+
/// <summary>
20+
/// text of content type text/plain.
21+
/// </summary>
22+
Text
23+
}
24+
}

0 commit comments

Comments
 (0)