From 576ca14267317ccd0f68412d079a9c50cf969c5c Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 27 Jun 2024 13:51:40 +0800 Subject: [PATCH 01/15] Support generate client access uri for MQTT --- .../Azure.Messaging.WebPubSub/CHANGELOG.md | 2 + ...zure.Messaging.WebPubSub.netstandard2.0.cs | 20 +- .../src/Azure.Messaging.WebPubSub.csproj | 2 +- .../src/ClientEndpointType.cs | 20 ++ .../src/Generated/WebPubSubServiceClient.cs | 16 +- ...bSubServiceClientOptions.ServiceVersion.cs | 13 +- .../src/WebPubSubServiceClient_extensions.cs | 2 + .../src/WebPubSubServiceClient_helpers.cs | 257 ++++++++++++------ .../Azure.Messaging.WebPubSub/src/autorest.md | 43 ++- .../tests/WebPubSubGenerateUriTests.cs | 66 ++++- 10 files changed, 335 insertions(+), 106 deletions(-) create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md index 29c3d38c0f50..46a43ac025df 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- Added method overloads `serviceClient.GetClientAccessUri`, `serviceClient.GetClientAccessUri` for MQTT clients. +- ### Breaking Changes ### Bugs Fixed diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index 903575a7a73f..b647c96f14d6 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -5,6 +5,11 @@ public static partial class ClientConnectionFilter public static string Create(System.FormattableString filter) { throw null; } public static string Create(System.FormattableString filter, System.IFormatProvider formatProvider) { throw null; } } + public enum ClientEndpointType + { + Default = 0, + MQTT = 1, + } public enum WebPubSubPermission { SendToGroup = 1, @@ -38,13 +43,17 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task CloseUserConnectionsAsync(string userId, System.Collections.Generic.IEnumerable excluded = null, string reason = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual Azure.Response GrantPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task GrantPermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } @@ -87,11 +96,12 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC } public partial class WebPubSubServiceClientOptions : Azure.Core.ClientOptions { - public WebPubSubServiceClientOptions(Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion version = Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion.V2021_10_01) { } + public WebPubSubServiceClientOptions(Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion version = Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { } public System.Uri ReverseProxyEndpoint { get { throw null; } set { } } public enum ServiceVersion { V2021_10_01 = 1, + V2024_01_01 = 2, } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj index d326eb3323a2..cf2818ed87b2 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj @@ -1,4 +1,4 @@ - + Azure SDK client library for the WebPubSub service Azure SDK for WebPubSub diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs new file mode 100644 index 000000000000..d1710fe55308 --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +namespace Azure.Messaging.WebPubSub; + +/// +/// The type of client endpoint. +/// +public enum ClientEndpointType +{ + /// + /// Default client endpoint type, whose path starts with "/client". + /// + Default, + + /// + /// MQTT client endpoint, whose path starts with "/clients/mqtt". + /// + MQTT, +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs index cff76222e668..2b1b17be0007 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -111,16 +111,17 @@ public virtual Response CloseAllConnections(IEnumerable excluded = null, /// Roles that the connection with the generated token will have. /// The expire time of the generated token. /// Groups that the connection will join when it connects. + /// The type of client. Case-insensitive. If not set, it's "Default". For Web PubSub for Socket.IO, only the default value is supported. For Web PubSub, the valid values are 'Default' and 'MQTT'. Allowed values: "Default" | "MQTT". /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// Service returned a non-success status code. /// The response returned from the service. - internal virtual async Task GenerateClientTokenImplAsync(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, RequestContext context = null) + internal virtual async Task GenerateClientTokenImplAsync(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, string clientType = null, RequestContext context = null) { using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.GenerateClientTokenImpl"); scope.Start(); try { - using HttpMessage message = CreateGenerateClientTokenImplRequest(userId, role, minutesToExpire, group, context); + using HttpMessage message = CreateGenerateClientTokenImplRequest(userId, role, minutesToExpire, group, clientType, context); return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); } catch (Exception e) @@ -144,16 +145,17 @@ internal virtual async Task GenerateClientTokenImplAsync(string userId /// Roles that the connection with the generated token will have. /// The expire time of the generated token. /// Groups that the connection will join when it connects. + /// The type of client. Case-insensitive. If not set, it's "Default". For Web PubSub for Socket.IO, only the default value is supported. For Web PubSub, the valid values are 'Default' and 'MQTT'. Allowed values: "Default" | "MQTT". /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// Service returned a non-success status code. /// The response returned from the service. - internal virtual Response GenerateClientTokenImpl(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, RequestContext context = null) + internal virtual Response GenerateClientTokenImpl(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, string clientType = null, RequestContext context = null) { using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.GenerateClientTokenImpl"); scope.Start(); try { - using HttpMessage message = CreateGenerateClientTokenImplRequest(userId, role, minutesToExpire, group, context); + using HttpMessage message = CreateGenerateClientTokenImplRequest(userId, role, minutesToExpire, group, clientType, context); return _pipeline.ProcessMessage(message, context); } catch (Exception e) @@ -1432,7 +1434,7 @@ internal HttpMessage CreateCloseAllConnectionsRequest(IEnumerable exclud return message; } - internal HttpMessage CreateGenerateClientTokenImplRequest(string userId, IEnumerable role, int? minutesToExpire, IEnumerable group, RequestContext context) + internal HttpMessage CreateGenerateClientTokenImplRequest(string userId, IEnumerable role, int? minutesToExpire, IEnumerable group, string clientType, RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier200); var request = message.Request; @@ -1465,6 +1467,10 @@ internal HttpMessage CreateGenerateClientTokenImplRequest(string userId, IEnumer uri.AppendQuery("group", param, true); } } + if (clientType != null) + { + uri.AppendQuery("clientType", clientType, true); + } request.Uri = uri; request.Headers.Add("Accept", "application/json, text/json"); return message; diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.ServiceVersion.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.ServiceVersion.cs index a791880e0616..b44536ed6cd0 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.ServiceVersion.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.ServiceVersion.cs @@ -26,19 +26,28 @@ public enum ServiceVersion #pragma warning disable CA1707 // Identifiers should not contain underscores /// The 2021_10_01_stable version of the Azure WebPubSub service. V2021_10_01 = 1, + /// + /// The 2024_01_01_stable version of the Azure WebPubSub service. + /// + V2024_01_01 = 2, #pragma warning restore CA1707 // Identifiers should not contain underscores } /// /// The Latest supported by this client library. /// - private const ServiceVersion LatestVersion = ServiceVersion.V2021_10_01; + private const ServiceVersion LatestVersion = ServiceVersion.V2024_01_01; /// /// Gets the version of the service API used when making requests. /// internal string Version { get; } + /// + /// Gets the version enum of the service API used when making requests. + /// + internal ServiceVersion VersionEnum { get; } + /// Initializes a new instance of the . /// /// An optional to specify the version of the REST API to use. @@ -51,6 +60,7 @@ public enum ServiceVersion /// public WebPubSubServiceClientOptions(ServiceVersion version = LatestVersion) { + VersionEnum = version; Version = version.ToVersionString(); } } @@ -74,6 +84,7 @@ public static string ToVersionString(this WebPubSubServiceClientOptions.ServiceV version switch { WebPubSubServiceClientOptions.ServiceVersion.V2021_10_01 => "2021-10-01", + WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01 => "2024-01-01", _ => throw CreateInvalidVersionException(version) }; diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index bc89dc5b8a36..b19d73ae0845 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -27,6 +27,7 @@ namespace Azure.Messaging.WebPubSub [CodeGenSuppress("RemoveUserFromGroupAsync", typeof(string), typeof(string), typeof(RequestContext))] public partial class WebPubSubServiceClient { + private readonly WebPubSubServiceClientOptions.ServiceVersion _apiVersionEnum; private AzureKeyCredential _credential; private TokenCredential _tokenCredential; @@ -160,6 +161,7 @@ private WebPubSubServiceClient(Uri endpoint, string hub, WebPubSubServiceClientO options ??= new WebPubSubServiceClientOptions(); ClientDiagnostics = new ClientDiagnostics(options, true); _apiVersion = options.Version; + _apiVersionEnum = options.VersionEnum; } /// Broadcast message to all the connected client connections. diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index 3c7fabeaa4ec..8a52d935539b 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -30,7 +30,7 @@ public partial class WebPubSubServiceClient internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group"); /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// UTC time when the token expires. /// User Id. @@ -40,10 +40,10 @@ public partial class WebPubSubServiceClient #pragma warning disable AZC0015 // Unexpected client method return type. public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, null, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// UTC time when the token expires. /// User Id. @@ -52,12 +52,27 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I /// Groups that the connection with the generated token will join when it connects. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, CancellationToken cancellationToken = default) + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// + /// UTC time when the token expires. + /// User Id. + /// Cancellation token. + /// Roles that the connection with the generated token will have. + /// Groups that the connection with the generated token will join when it connects. + /// The client endpoint type. + /// +#pragma warning disable AZC0015 // Unexpected client method return type. + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) +#pragma warning restore AZC0015 // Unexpected client method return type. + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: false, cancellationToken).EnsureCompleted(); + + /// + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// UTC time when the token expires. /// User Id. @@ -66,29 +81,65 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = /// #pragma warning disable AZC0015 // Unexpected client method return type. public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, null, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: true, cancellationToken).ConfigureAwait(false); +#pragma warning restore AZC0015 // Unexpected client method return type. + + /// + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// + /// UTC time when the token expires. + /// User Id. + /// Cancellation token. + /// Roles that the connection with the generated token will have. + /// Groups that the connection with the generated token will join when it connects. + /// +#pragma warning disable AZC0015 // Unexpected client method return type. + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// UTC time when the token expires. + /// The client endpoint type. /// User Id. /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, CancellationToken cancellationToken = default) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, async: true, cancellationToken).ConfigureAwait(false); + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: true, cancellationToken).ConfigureAwait(false); +#pragma warning restore AZC0015 // Unexpected client method return type. + + /// + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// + /// Not applicable for MQTT clients who connect to "/clients/mqtt/hubs/{hub}" + /// Defaults to one hour, if not specified. Must be greater or equal zero. + /// User Id. + /// Roles that the connection with the generated token will have. + /// Cancellation token. + /// +#pragma warning disable AZC0015 // Unexpected client method return type. + public virtual Uri GetClientAccessUri( + TimeSpan expiresAfter, + string userId, + IEnumerable roles, + CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. + { + return GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientEndpointType.Default, false, cancellationToken).EnsureCompleted(); + } /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. + /// Groups that the connection with the generated token will join when it connects. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -96,19 +147,21 @@ public virtual Uri GetClientAccessUri( TimeSpan expiresAfter, string userId, IEnumerable roles, + IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, null, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientEndpointType.Default, false, cancellationToken).EnsureCompleted(); } /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. + /// The client endpoint type. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -117,18 +170,40 @@ public virtual Uri GetClientAccessUri( string userId = default, IEnumerable roles = default, IEnumerable groups = default, + ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientEndpointType, false, cancellationToken).EnsureCompleted(); + } + + /// + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// + /// Not applicable for MQTT clients who connect to "/clients/mqtt/hubs/{hub}" + /// Defaults to one hour, if not specified. Must be greater or equal zero. + /// User Id. + /// Roles that the connection with the generated token will have. + /// Cancellation token. + /// +#pragma warning disable AZC0015 // Unexpected client method return type. + public virtual async Task GetClientAccessUriAsync( + TimeSpan expiresAfter, + string userId, + IEnumerable roles, + CancellationToken cancellationToken) +#pragma warning restore AZC0015 // Unexpected client method return type. + { + return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientEndpointType.Default, true, cancellationToken).ConfigureAwait(false); } /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. + /// Groups that the connection with the generated token will join when it connects. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -136,19 +211,21 @@ public virtual async Task GetClientAccessUriAsync( TimeSpan expiresAfter, string userId, IEnumerable roles, + IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientEndpointType.Default, true, cancellationToken).ConfigureAwait(false); } /// - /// Creates a URI with authentication token. + /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. + /// The client endpoint type. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -157,10 +234,11 @@ public virtual async Task GetClientAccessUriAsync( string userId = default, IEnumerable roles = default, IEnumerable groups = default, + ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientEndpointType, true, cancellationToken).ConfigureAwait(false); } internal static int GetMinutesToExpire(TimeSpan expiresAfter) => Math.Max((int)expiresAfter.TotalMinutes, 1); @@ -168,85 +246,28 @@ public virtual async Task GetClientAccessUriAsync( internal static int GetMinutesToExpire(DateTimeOffset expiresAt) => Math.Max((int)expiresAt.Subtract(DateTimeOffset.UtcNow).TotalMinutes, 1); private async Task GetClientAccessUriInternal( - DateTimeOffset expiresAt, - string userId = default, - IEnumerable roles = default, - IEnumerable groups = default, - bool async = true, - CancellationToken cancellationToken = default) + DateTimeOffset expiresAt, + string userId = default, + IEnumerable roles = default, + IEnumerable groups = default, + ClientEndpointType clientEndpointType = default, + bool async = true, + CancellationToken cancellationToken = default) { - string token; - - if (_tokenCredential != null) - { - RequestContext context = new() { CancellationToken = cancellationToken }; - - var minutesToExpire = GetMinutesToExpire(expiresAt); - - Response clientTokenResponse = async ? - await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, context).ConfigureAwait(false) : - GenerateClientTokenImpl(userId, roles, minutesToExpire, null, context); - using var jsonDocument = JsonDocument.Parse(clientTokenResponse.Content); - token = jsonDocument.RootElement.GetProperty(ClientTokenResponseTokenPropertyName).GetString(); - } - else if (_credential != null) - { - token = GenerateTokenFromAzureKeyCredential(expiresAt, userId, roles, groups); - } - else - { - throw new InvalidOperationException($"{nameof(WebPubSubServiceClient)} must be constructed with either a {typeof(TokenCredential)} or {typeof(AzureKeyCredential)} to generate client access URIs."); - } - - UriBuilder clientEndpoint = new(Endpoint) - { - Scheme = Endpoint.Scheme == "http" ? "ws" : "wss" - }; - - return new Uri($"{clientEndpoint}client/hubs/{_hub}?access_token={token}"); + var token = await GetClientAccessTokenCore(expiresAt, userId, roles, groups, clientEndpointType, async, cancellationToken: cancellationToken).ConfigureAwait(false); + return GetClientAccessUriInternal(token, clientEndpointType); } - private async Task GetClientAccessUriInternal( + private Task GetClientAccessUriInternal( TimeSpan expireAfter, string userId = default, IEnumerable roles = default, IEnumerable groups = default, + ClientEndpointType clientEndpointType = default, bool async = true, CancellationToken cancellationToken = default) { - string token; - - if (_tokenCredential != null) - { - RequestContext context = new() { CancellationToken = cancellationToken }; - - var minutesToExpire = GetMinutesToExpire(expireAfter); - - Response clientTokenResponse = async ? - await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, context).ConfigureAwait(false) : - GenerateClientTokenImpl(userId, roles, minutesToExpire, null, context); - using var jsonDocument = JsonDocument.Parse(clientTokenResponse.Content); - token = jsonDocument.RootElement.GetProperty(ClientTokenResponseTokenPropertyName).GetString(); - } - else if (_credential != null) - { - if (expireAfter == default) - { - expireAfter = TimeSpan.FromHours(1); - } - token = GenerateTokenFromAzureKeyCredential(DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups); - } - else - { - throw new InvalidOperationException($"{nameof(WebPubSubServiceClient)} must be constructed with either a {typeof(TokenCredential)} or {typeof(AzureKeyCredential)} to generate client access URIs."); - } - - UriBuilder clientEndpoint = new(Endpoint) - { - Scheme = Endpoint.Scheme == "http" ? "ws" : "wss" - }; - - return new Uri($"{clientEndpoint}client/hubs/{_hub}?access_token={token}"); + return GetClientAccessUriInternal(expireAfter == default ? DateTimeOffset.UtcNow.Add(TimeSpan.FromHours(1)) : DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups, clientEndpointType, async, cancellationToken); } /// @@ -321,7 +342,7 @@ internal static string PermissionToString(WebPubSubPermission permission) } } - private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default) + private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, ClientEndpointType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) { var keyBytes = Encoding.UTF8.GetBytes(_credential.Key); @@ -333,7 +354,7 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, str { endpoint += "/"; } - var audience = $"{endpoint}client/hubs/{_hub}"; + var audience = $"{endpoint}{GetRelativeClientEndpoint(endpointType)}"; if (userId != default) { @@ -354,5 +375,63 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, str return jwt.BuildString(); } + + private string GetRelativeClientEndpoint(ClientEndpointType endpointType) => endpointType switch + { + ClientEndpointType.Default => $"client/hubs/{_hub}", + ClientEndpointType.MQTT => $"clients/mqtt/hubs/{_hub}", + _ => throw new ArgumentOutOfRangeException(nameof(endpointType)) + }; + + private async Task GetClientAccessTokenCore( + DateTimeOffset expiresAt, + string userId, + IEnumerable roles, + IEnumerable groups, + ClientEndpointType clientEndpointType, + bool async, + CancellationToken cancellationToken = default) + { + if (clientEndpointType == ClientEndpointType.MQTT && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + { + throw new NotSupportedException($"Getting client access token for MQTT is supported since API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()}. Current version is {_apiVersion}."); + } + var clientEndpointString = clientEndpointType switch + { + ClientEndpointType.Default => "default", + ClientEndpointType.MQTT => "mqtt", + _ => throw new ArgumentOutOfRangeException(nameof(clientEndpointType)) + }; + if (_tokenCredential != null) + { + RequestContext context = new() { CancellationToken = cancellationToken }; + + var minutesToExpire = GetMinutesToExpire(expiresAt); + + Response clientTokenResponse = async ? + await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clientEndpointString, context).ConfigureAwait(false) : + GenerateClientTokenImpl(userId, roles, minutesToExpire, null, clientEndpointString, context); + using var jsonDocument = JsonDocument.Parse(clientTokenResponse.Content); + return jsonDocument.RootElement.GetProperty(ClientTokenResponseTokenPropertyName).GetString(); + } + else if (_credential != null) + { + return GenerateTokenFromAzureKeyCredential(expiresAt, clientEndpointType, userId, roles, groups); + } + else + { + throw new InvalidOperationException($"{nameof(WebPubSubServiceClient)} must be constructed with either a {typeof(TokenCredential)} or {typeof(AzureKeyCredential)} to generate client access URIs."); + } + } + + private Uri GetClientAccessUriInternal(string token, ClientEndpointType endpointType) + { + UriBuilder clientEndpoint = new(Endpoint) + { + Scheme = Endpoint.Scheme == "http" ? "ws" : "wss" + }; + + return new Uri($"{clientEndpoint}{GetRelativeClientEndpoint(endpointType)}?access_token={token}"); + } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md index 3a4fe81c8418..e0d3fdf00a77 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md @@ -9,7 +9,8 @@ Run `dotnet build /t:GenerateCode` to generate code. ``` yaml title: WebPubSubServiceClient input-file: -- https://github.com/Azure/azure-rest-api-specs/blob/1735a92bdc79b446385a36ba063ea5235680709f/specification/webpubsub/data-plane/WebPubSub/stable/2022-11-01/webpubsub.json +- https://github.com/Azure/azure-rest-api-specs/blob/356aa5174e8eec6ed904bf5ff104595aec8c0411/specification/webpubsub/data-plane/WebPubSub/stable/2024-01-01/webpubsub.json + credential-types: AzureKeyCredential credential-header-name: Ocp-Apim-Subscription-Key keep-non-overloadable-protocol-signature: true @@ -23,6 +24,46 @@ directive: transform: $.modelAsString = false; ``` +### Restore the "host" parameter to be a string instead of a URL to avoid breaking change +``` yaml +directive: +- from: swagger-document + where: $.x-ms-parameterized-host.parameters[0].format + transform: return "string"; +``` + +### Remove Operations AddConnectionsToGroupsAsync and RemoveConnectionsFromGroups. Add them back if required. +``` yaml +directive: +- remove-operation: WebPubSub_AddConnectionsToGroups +- remove-operation: WebPubSub_RemoveConnectionsFromGroups +``` + +### Remove "messageTtlSeconds" parameter from all operations as it introduces breaking change. +``` yaml +directive: +- where-operation: WebPubSub_SendToConnection + remove-parameter: + debug: true + in: query + name: messageTtlSeconds +- where-operation: WebPubSub_SendToGroup + remove-parameter: + debug: true + in: query + name: messageTtlSeconds +- where-operation: WebPubSub_SendToUser + remove-parameter: + debug: true + in: query + name: messageTtlSeconds +- where-operation: WebPubSub_SendToAll + remove-parameter: + debug: true + in: query + name: messageTtlSeconds +``` + ### GenerateClientTokenImpl ``` yaml directive: diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs index ae152b39c3bd..2221d03ab0a6 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs @@ -2,11 +2,15 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Threading; +using System.Threading.Tasks; using System.Web; - +using Azure.Core; +using Azure.Identity; +using Moq; using NUnit.Framework; namespace Azure.Messaging.WebPubSub.Tests @@ -18,6 +22,60 @@ public class WebPubSubGenerateUriTests private static readonly JwtSecurityTokenHandler s_jwtTokenHandler = new(); + [TestCase(ClientEndpointType.Default, "/client")] + [TestCase(ClientEndpointType.MQTT, "/clients/mqtt")] + public async Task GetClientAccessUri_AccessKey_Test(ClientEndpointType clientType, string clientUriPrefix) + { + var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); + var expectedUriPrefix = $"ws://localhost:8080{clientUriPrefix}/hubs/hub?access_token="; + // Synchronize + Assert.True(serviceClient.GetClientAccessUri(TimeSpan.FromMinutes(1), default, default, default, clientType, default).ToString().StartsWith(expectedUriPrefix)); + Assert.True(serviceClient.GetClientAccessUri(DateTimeOffset.UtcNow.AddMinutes(1), default, default, default, clientType, default).ToString().StartsWith(expectedUriPrefix)); + // Asynchronize + Assert.True((await serviceClient.GetClientAccessUriAsync(TimeSpan.FromMinutes(1), default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); + Assert.True((await serviceClient.GetClientAccessUriAsync(DateTimeOffset.Now, default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); + } + + [TestCase(ClientEndpointType.Default, "/client", "default")] + [TestCase(ClientEndpointType.MQTT, "/clients/mqtt", "mqtt")] + public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(ClientEndpointType clientType, string clientUriPrefix, string clientTypeString) + { + var serviceClient = new WebPubSubServiceSubClass(new Uri("https://localhost"), "hub", new DefaultAzureCredential()); + var expectedUri = new Uri($"wss://localhost{clientUriPrefix}/hubs/hub?access_token=fakeToken"); + Assert.AreEqual(expectedUri, serviceClient.GetClientAccessUri(TimeSpan.FromMinutes(1), default, default, default, clientType, default)); + Assert.AreEqual(expectedUri, serviceClient.GetClientAccessUri(DateTime.UtcNow, default, default, default, clientType, default)); + Assert.AreEqual(expectedUri, await serviceClient.GetClientAccessUriAsync(TimeSpan.FromMinutes(1), default, default, default, clientType, default)); + Assert.AreEqual(expectedUri, await serviceClient.GetClientAccessUriAsync(DateTime.UtcNow, default, default, default, clientType, default)); + for (var i = 0; i < 4; i++) + { + // Validate the "clientType" parameter passed to the GenerateClientTokenImpl(Async) method + Assert.True(clientTypeString == serviceClient.InvocationParameters[i][4].ToString()); + } + } + + /// + /// A subclass of WebPubSubServiceClient to mock method GenerateClientTokenImpl(Async) + /// + private class WebPubSubServiceSubClass : WebPubSubServiceClient + { + public WebPubSubServiceSubClass(Uri endpoint, string hub, TokenCredential credential) + : base(endpoint, hub, credential) + { + } + public List InvocationParameters { get; } = new(); + internal override Task GenerateClientTokenImplAsync(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, string clientType = null, RequestContext context = null) + { + InvocationParameters.Add(new object[] { userId, role, minutesToExpire, group, clientType, context }); + return Task.FromResult(Mock.Of(r => r.Content == new BinaryData("{\"token\":\"fakeToken\"}"))); + } + + internal override Response GenerateClientTokenImpl(string userId = null, IEnumerable role = null, int? minutesToExpire = null, IEnumerable group = null, string clientType = null, RequestContext context = null) + { + InvocationParameters.Add(new object[] { userId, role, minutesToExpire, group, clientType, context }); + return Mock.Of(r => r.Content == new BinaryData("{\"token\":\"fakeToken\"}")); + } + } + [TestCase(0, 60)] [TestCase(1, 1)] [TestCase(2, 2)] @@ -29,7 +87,7 @@ public void AccessKeyExpiresAfterTests(int minutesToExpire, int expectedMinutesA var utcnow = DateTimeOffset.UtcNow; var expiresAfter = TimeSpan.FromMinutes(minutesToExpire); - Uri uri = serviceClient.GetClientAccessUri(expiresAfter, "foo", null ); + Uri uri = serviceClient.GetClientAccessUri(expiresAfter, "foo", null); var token = HttpUtility.ParseQueryString(uri.Query).Get("access_token"); Assert.NotNull(token); var jwt = s_jwtTokenHandler.ReadJwtToken(token); @@ -48,7 +106,7 @@ public void AccessKeyExpireAtTests(int minutesToExpire) var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); var expireAt = DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(minutesToExpire)); - Uri uri = serviceClient.GetClientAccessUri(expireAt, "foo", null ); + Uri uri = serviceClient.GetClientAccessUri(expireAt, "foo", null); var token = HttpUtility.ParseQueryString(uri.Query).Get("access_token"); Assert.NotNull(token); var jwt = s_jwtTokenHandler.ReadJwtToken(token); @@ -94,7 +152,7 @@ public void CreateGenerateClientTokenImplRequestTest() var source = new CancellationTokenSource(); RequestContext context = new() { CancellationToken = source.Token }; var expectRoles = new[] { "a", "b" }; - var request = client.CreateGenerateClientTokenImplRequest("foo", new[] {"a", "b"}, 1, null, context); + var request = client.CreateGenerateClientTokenImplRequest("foo", new[] { "a", "b" }, 1, null, "default", context); var url = request.Request.Uri.ToString(); var queryString = url.Substring(url.IndexOf('?')); From 7e879a26b6bbfa95ba0ecb025b5680ebf1f4ae7b Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 27 Jun 2024 16:35:00 +0800 Subject: [PATCH 02/15] Update test environment --- sdk/webpubsub/Azure.Messaging.WebPubSub/assets.json | 2 +- .../Azure.Messaging.WebPubSub/tests/WebPubSubTestEnvironment.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/assets.json b/sdk/webpubsub/Azure.Messaging.WebPubSub/assets.json index e0b58bff7849..e64dce29305c 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/assets.json +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "net", "TagPrefix": "net/webpubsub/Azure.Messaging.WebPubSub", - "Tag": "net/webpubsub/Azure.Messaging.WebPubSub_1d3c499946" + "Tag": "net/webpubsub/Azure.Messaging.WebPubSub_7d6164a51f" } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubTestEnvironment.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubTestEnvironment.cs index 0a2362121269..fbaa0c7cbf35 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubTestEnvironment.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubTestEnvironment.cs @@ -7,6 +7,6 @@ namespace Azure.Rest.WebPubSub.Tests { public class WebPubSubTestEnvironment : TestEnvironment { - public string ConnectionString => GetRecordedVariable("WEBPUBSUB_CONNECTIONSTRING", options => options.HasSecretConnectionStringParameter("secret", SanitizedValue.Base64)); + public string ConnectionString => GetRecordedVariable("WEBPUBSUB_CONNECTIONSTRING", options => options.HasSecretConnectionStringParameter("accessKey", SanitizedValue.Base64)); } } From ff5e6f2e13a41b00ebcc29b7ae3201e735d901d1 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Fri, 28 Jun 2024 12:32:46 +0800 Subject: [PATCH 03/15] Fix doc --- .../src/WebPubSubServiceClient_helpers.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index 8a52d935539b..e36bbd873ee2 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -57,7 +57,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. /// User Id. @@ -99,14 +99,14 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. - /// The client endpoint type. /// User Id. /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. + /// The client endpoint type. /// #pragma warning disable AZC0015 // Unexpected client method return type. public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) @@ -116,7 +116,6 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// - /// Not applicable for MQTT clients who connect to "/clients/mqtt/hubs/{hub}" /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. @@ -155,7 +154,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -180,7 +179,6 @@ public virtual Uri GetClientAccessUri( /// /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". /// - /// Not applicable for MQTT clients who connect to "/clients/mqtt/hubs/{hub}" /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. @@ -219,7 +217,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. From a81b8222dc3e1acae3f8dc945f3017593949519f Mon Sep 17 00:00:00 2001 From: Siyuan <87063252+xingsy97@users.noreply.github.com> Date: Tue, 2 Jul 2024 14:38:58 +0800 Subject: [PATCH 04/15] Add convenient method for new APIs --- .../Azure.Messaging.WebPubSub/CHANGELOG.md | 4 +- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 4 ++ .../src/WebPubSubServiceClient_extensions.cs | 68 +++++++++++++++++++ .../Samples/Samples_WebPubSubServiceClient.cs | 44 ++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md index 46a43ac025df..113d3bba227d 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/CHANGELOG.md @@ -5,7 +5,9 @@ ### Features Added - Added method overloads `serviceClient.GetClientAccessUri`, `serviceClient.GetClientAccessUri` for MQTT clients. -- +- Added method `serviceClient.AddConnectionsToGroups` to add filtered connections to specified multiple groups. +- Added method `serviceClient.RemoveConnectionsFromGroups` to remove filtered connections from specified multiple groups. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index b647c96f14d6..658856158ed9 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -29,6 +29,8 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } public virtual Azure.Response AddConnectionToGroup(string group, string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task AddConnectionToGroupAsync(string group, string connectionId, Azure.RequestContext context = null) { throw null; } + public virtual Azure.Response AddConnectionsToGroups(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } + public virtual System.Threading.Tasks.Task AddConnectionsToGroupsAsync(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response AddUserToGroup(string group, string userId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task AddUserToGroupAsync(string group, string userId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response CheckPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } @@ -63,6 +65,8 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task RemoveConnectionFromAllGroupsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveConnectionFromGroup(string group, string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task RemoveConnectionFromGroupAsync(string group, string connectionId, Azure.RequestContext context = null) { throw null; } + public virtual Azure.Response RemoveConnectionsFromGroups(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } + public virtual System.Threading.Tasks.Task RemoveConnectionsFromGroupsAsync(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveUserFromAllGroups(string userId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task RemoveUserFromAllGroupsAsync(string userId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveUserFromGroup(string group, string userId, Azure.RequestContext context = null) { throw null; } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index b19d73ae0845..329a53abac01 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -551,5 +551,73 @@ public virtual Response RemoveUserFromGroup(string group, string userId, Request throw; } } + + /// + /// Add filtered connections to multiple groups. + /// + /// An OData filter which target connections satisfy. + /// A list of groups which target connections will be added into. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// A if successful. + public virtual Response AddConnectionsToGroups(string filter, IEnumerable groups, RequestContext context = null) + { + Argument.AssertNotNull(filter, nameof(filter)); + Argument.AssertNotNull(groups, nameof(groups)); + + string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); + + return AddConnectionsToGroups(RequestContent.Create(json), context); + } + + /// + /// Add filtered connections to multiple groups. + /// + /// An OData filter which target connections satisfy. + /// A list of groups which target connections will be added into. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// A if successful. + public virtual async Task AddConnectionsToGroupsAsync(string filter, IEnumerable groups, RequestContext context = null) + { + Argument.AssertNotNull(filter, nameof(filter)); + Argument.AssertNotNull(groups, nameof(groups)); + + string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); + + return await AddConnectionsToGroupsAsync(RequestContent.Create(json), context).ConfigureAwait(false); + } + + /// + /// Remove filtered connections from multiple groups. + /// + /// An OData filter which target connections satisfy. + /// A list of groups which target connections will be added into. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// A if successful. + public virtual Response RemoveConnectionsFromGroups(string filter, IEnumerable groups, RequestContext context = null) + { + Argument.AssertNotNull(filter, nameof(filter)); + Argument.AssertNotNull(groups, nameof(groups)); + + string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); + + return RemoveConnectionsFromGroups(RequestContent.Create(json), context); + } + + /// + /// Remove filtered connections from multiple groups. + /// + /// An OData filter which target connections satisfy. + /// A list of groups which target connections will be added into. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// A if successful. + public virtual async Task RemoveConnectionsFromGroupsAsync(string filter, IEnumerable groups, RequestContext context = null) + { + Argument.AssertNotNull(filter, nameof(filter)); + Argument.AssertNotNull(groups, nameof(groups)); + + string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); + + return await RemoveConnectionsFromGroupsAsync(RequestContent.Create(json), context).ConfigureAwait(false); + } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs index af48d1f468e6..0611b6eb1afe 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs @@ -559,5 +559,49 @@ public async Task Example_RemoveUserFromAllGroups_AllParameters_Async() Console.WriteLine(response.Status); } + + [Test] + [Ignore("Only validating compilation of examples")] + public void Example_AddConnectionsToGroups_AllParameters() + { + WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); + + Response response = client.AddConnectionsToGroups("", new string[] { "group1", "group2" }); + + Console.WriteLine(response.Status); + } + + [Test] + [Ignore("Only validating compilation of examples")] + public async Task Example_AddConnectionsToGroups_AllParameters_Async() + { + WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); + + Response response = await client.AddConnectionsToGroupsAsync("", new string[] { "group1", "group2" }); + + Console.WriteLine(response.Status); + } + + [Test] + [Ignore("Only validating compilation of examples")] + public void Example_RemoveConnectionsFromGroups_AllParameters() + { + WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); + + Response response = client.RemoveConnectionsFromGroups("", new string[] { "group1", "group2" }); + + Console.WriteLine(response.Status); + } + + [Test] + [Ignore("Only validating compilation of examples")] + public async Task Example_RemoveConnectionsFromGroups_AllParameters_Async() + { + WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); + + Response response = await client.RemoveConnectionsFromGroupsAsync("", new string[] { "group1", "group2" }); + + Console.WriteLine(response.Status); + } } } From 443880d36f731d5d82fa3ea9f29ae4fab664f565 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Wed, 3 Jul 2024 18:16:20 +0800 Subject: [PATCH 05/15] Fix build error and make filter optional --- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 8 +- .../src/Generated/WebPubSubServiceClient.cs | 172 +++++++++++++++++- .../src/WebPubSubServiceClient_extensions.cs | 18 +- .../Azure.Messaging.WebPubSub/src/autorest.md | 37 +++- .../Samples/Samples_WebPubSubServiceClient.cs | 44 ----- 5 files changed, 211 insertions(+), 68 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index 658856158ed9..b08f231b2d47 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -27,10 +27,10 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Uri Endpoint { get { throw null; } } public virtual string Hub { get { throw null; } } public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } + public virtual Azure.Response AddConnectionsToGroups(System.Collections.Generic.IEnumerable groups, string filter, Azure.RequestContext context = null) { throw null; } + public virtual System.Threading.Tasks.Task AddConnectionsToGroupsAsync(System.Collections.Generic.IEnumerable groups, string filter, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response AddConnectionToGroup(string group, string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task AddConnectionToGroupAsync(string group, string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual Azure.Response AddConnectionsToGroups(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } - public virtual System.Threading.Tasks.Task AddConnectionsToGroupsAsync(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response AddUserToGroup(string group, string userId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task AddUserToGroupAsync(string group, string userId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response CheckPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } @@ -65,8 +65,8 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task RemoveConnectionFromAllGroupsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveConnectionFromGroup(string group, string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task RemoveConnectionFromGroupAsync(string group, string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual Azure.Response RemoveConnectionsFromGroups(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } - public virtual System.Threading.Tasks.Task RemoveConnectionsFromGroupsAsync(string filter, IEnumerable groups, Azure.RequestContext context = null) { throw null; } + public virtual Azure.Response RemoveConnectionsFromGroups(System.Collections.Generic.IEnumerable groups, string filter = null, Azure.RequestContext context = null) { throw null; } + public virtual System.Threading.Tasks.Task RemoveConnectionsFromGroupsAsync(System.Collections.Generic.IEnumerable groups, string filter = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveUserFromAllGroups(string userId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task RemoveUserFromAllGroupsAsync(string userId, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response RemoveUserFromGroup(string group, string userId, Azure.RequestContext context = null) { throw null; } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs index 2b1b17be0007..c413f7de0c9e 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -33,6 +33,72 @@ protected WebPubSubServiceClient() { } + /// + /// [Protocol Method] Add filtered connections to multiple groups. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual async Task AddConnectionsToGroupsAsync(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.AddConnectionsToGroups"); + scope.Start(); + try + { + using HttpMessage message = CreateAddConnectionsToGroupsRequest(content, context); + return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// + /// [Protocol Method] Add filtered connections to multiple groups. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual Response AddConnectionsToGroups(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.AddConnectionsToGroups"); + scope.Start(); + try + { + using HttpMessage message = CreateAddConnectionsToGroupsRequest(content, context); + return _pipeline.ProcessMessage(message, context); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + /// /// [Protocol Method] Close the connections in the hub. /// @@ -165,6 +231,72 @@ internal virtual Response GenerateClientTokenImpl(string userId = null, IEnumera } } + /// + /// [Protocol Method] Remove filtered connections from multiple groups. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual async Task RemoveConnectionsFromGroupsAsync(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveConnectionsFromGroups"); + scope.Start(); + try + { + using HttpMessage message = CreateRemoveConnectionsFromGroupsRequest(content, context); + return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + + /// + /// [Protocol Method] Remove filtered connections from multiple groups. + /// + /// + /// + /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. + /// + /// + /// + /// + /// The content to send as the body of the request. + /// The request context, which can override default behaviors of the client pipeline on a per-call basis. + /// is null. + /// Service returned a non-success status code. + /// The response returned from the service. + internal virtual Response RemoveConnectionsFromGroups(RequestContent content, RequestContext context = null) + { + Argument.AssertNotNull(content, nameof(content)); + + using var scope = ClientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveConnectionsFromGroups"); + scope.Start(); + try + { + using HttpMessage message = CreateRemoveConnectionsFromGroupsRequest(content, context); + return _pipeline.ProcessMessage(message, context); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } + } + /// /// [Protocol Method] Broadcast content inside request body to all the connected client connections. /// @@ -1407,6 +1539,24 @@ public virtual Response RemoveUserFromAllGroups(string userId, RequestContext co } } + internal HttpMessage CreateAddConnectionsToGroupsRequest(RequestContent content, RequestContext context) + { + var message = _pipeline.CreateMessage(context, ResponseClassifier200); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.AppendRaw(_endpoint, false); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(_hub, true); + uri.AppendPath("/:addToGroups", false); + uri.AppendQuery("api-version", _apiVersion, true); + request.Uri = uri; + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("Content-Type", "application/json"); + request.Content = content; + return message; + } + internal HttpMessage CreateCloseAllConnectionsRequest(IEnumerable excluded, string reason, RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier204); @@ -1476,6 +1626,24 @@ internal HttpMessage CreateGenerateClientTokenImplRequest(string userId, IEnumer return message; } + internal HttpMessage CreateRemoveConnectionsFromGroupsRequest(RequestContent content, RequestContext context) + { + var message = _pipeline.CreateMessage(context, ResponseClassifier200); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.AppendRaw(_endpoint, false); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(_hub, true); + uri.AppendPath("/:removeFromGroups", false); + uri.AppendQuery("api-version", _apiVersion, true); + request.Uri = uri; + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("Content-Type", "application/json"); + request.Content = content; + return message; + } + internal HttpMessage CreateSendToAllRequest(RequestContent content, ContentType contentType, IEnumerable excluded, string filter, RequestContext context) { var message = _pipeline.CreateMessage(context, ResponseClassifier202); @@ -1887,10 +2055,10 @@ internal HttpMessage CreateAddUserToGroupRequest(string userId, string group, Re return message; } - private static ResponseClassifier _responseClassifier204; - private static ResponseClassifier ResponseClassifier204 => _responseClassifier204 ??= new StatusCodeClassifier(stackalloc ushort[] { 204 }); private static ResponseClassifier _responseClassifier200; private static ResponseClassifier ResponseClassifier200 => _responseClassifier200 ??= new StatusCodeClassifier(stackalloc ushort[] { 200 }); + private static ResponseClassifier _responseClassifier204; + private static ResponseClassifier ResponseClassifier204 => _responseClassifier204 ??= new StatusCodeClassifier(stackalloc ushort[] { 204 }); private static ResponseClassifier _responseClassifier202; private static ResponseClassifier ResponseClassifier202 => _responseClassifier202 ??= new StatusCodeClassifier(stackalloc ushort[] { 202 }); private static ResponseClassifier _responseClassifier200404; diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index 329a53abac01..a8f2ca103d44 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -555,11 +555,11 @@ public virtual Response RemoveUserFromGroup(string group, string userId, Request /// /// Add filtered connections to multiple groups. /// - /// An OData filter which target connections satisfy. /// A list of groups which target connections will be added into. + /// An OData filter which target connections satisfy. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// A if successful. - public virtual Response AddConnectionsToGroups(string filter, IEnumerable groups, RequestContext context = null) + public virtual Response AddConnectionsToGroups(IEnumerable groups, string filter, RequestContext context = null) { Argument.AssertNotNull(filter, nameof(filter)); Argument.AssertNotNull(groups, nameof(groups)); @@ -572,11 +572,11 @@ public virtual Response AddConnectionsToGroups(string filter, IEnumerable /// Add filtered connections to multiple groups. /// - /// An OData filter which target connections satisfy. /// A list of groups which target connections will be added into. + /// An OData filter which target connections satisfy. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// A if successful. - public virtual async Task AddConnectionsToGroupsAsync(string filter, IEnumerable groups, RequestContext context = null) + public virtual async Task AddConnectionsToGroupsAsync(IEnumerable groups, string filter, RequestContext context = null) { Argument.AssertNotNull(filter, nameof(filter)); Argument.AssertNotNull(groups, nameof(groups)); @@ -589,13 +589,12 @@ public virtual async Task AddConnectionsToGroupsAsync(string filter, I /// /// Remove filtered connections from multiple groups. /// - /// An OData filter which target connections satisfy. /// A list of groups which target connections will be added into. + /// An OData filter which target connections satisfy. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// A if successful. - public virtual Response RemoveConnectionsFromGroups(string filter, IEnumerable groups, RequestContext context = null) + public virtual Response RemoveConnectionsFromGroups(IEnumerable groups, string filter = null, RequestContext context = null) { - Argument.AssertNotNull(filter, nameof(filter)); Argument.AssertNotNull(groups, nameof(groups)); string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); @@ -606,13 +605,12 @@ public virtual Response RemoveConnectionsFromGroups(string filter, IEnumerable /// Remove filtered connections from multiple groups. /// - /// An OData filter which target connections satisfy. /// A list of groups which target connections will be added into. + /// An OData filter which target connections satisfy. /// The request context, which can override default behaviors of the client pipeline on a per-call basis. /// A if successful. - public virtual async Task RemoveConnectionsFromGroupsAsync(string filter, IEnumerable groups, RequestContext context = null) + public virtual async Task RemoveConnectionsFromGroupsAsync(IEnumerable groups, string filter = null, RequestContext context = null) { - Argument.AssertNotNull(filter, nameof(filter)); Argument.AssertNotNull(groups, nameof(groups)); string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md index e0d3fdf00a77..576a2507a5a4 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md @@ -32,14 +32,7 @@ directive: transform: return "string"; ``` -### Remove Operations AddConnectionsToGroupsAsync and RemoveConnectionsFromGroups. Add them back if required. -``` yaml -directive: -- remove-operation: WebPubSub_AddConnectionsToGroups -- remove-operation: WebPubSub_RemoveConnectionsFromGroups -``` - -### Remove "messageTtlSeconds" parameter from all operations as it introduces breaking change. +### Remove "messageTtlSeconds" parameter from all operations as the generated sample uses a invalid value for it. ``` yaml directive: - where-operation: WebPubSub_SendToConnection @@ -64,6 +57,34 @@ directive: name: messageTtlSeconds ``` +### AddConnectionsToGroupsAsyncImpl +``` yaml +directive: +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:addToGroups"].post.operationId + transform: return "WebPubSubService_AddConnectionsToGroups"; +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:addToGroups"].post.parameters["0"] + transform: $["x-ms-parameter-location"] = "client" +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:addToGroups"].post + transform: $["x-accessibility"] = "internal" +``` + +### RemoveConnectionsFromGroups +``` yaml +directive: +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:removeFromGroups"].post.operationId + transform: return "WebPubSubService_RemoveConnectionsFromGroups"; +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:removeFromGroups"].post.parameters["0"] + transform: $["x-ms-parameter-location"] = "client" +- from: swagger-document + where: $.paths["/api/hubs/{hub}/:removeFromGroups"].post + transform: $["x-accessibility"] = "internal" +``` + ### GenerateClientTokenImpl ``` yaml directive: diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs index 0611b6eb1afe..af48d1f468e6 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Generated/Samples/Samples_WebPubSubServiceClient.cs @@ -559,49 +559,5 @@ public async Task Example_RemoveUserFromAllGroups_AllParameters_Async() Console.WriteLine(response.Status); } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_AddConnectionsToGroups_AllParameters() - { - WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); - - Response response = client.AddConnectionsToGroups("", new string[] { "group1", "group2" }); - - Console.WriteLine(response.Status); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_AddConnectionsToGroups_AllParameters_Async() - { - WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); - - Response response = await client.AddConnectionsToGroupsAsync("", new string[] { "group1", "group2" }); - - Console.WriteLine(response.Status); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_RemoveConnectionsFromGroups_AllParameters() - { - WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); - - Response response = client.RemoveConnectionsFromGroups("", new string[] { "group1", "group2" }); - - Console.WriteLine(response.Status); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_RemoveConnectionsFromGroups_AllParameters_Async() - { - WebPubSubServiceClient client = new WebPubSubServiceClient("", ""); - - Response response = await client.RemoveConnectionsFromGroupsAsync("", new string[] { "group1", "group2" }); - - Console.WriteLine(response.Status); - } } } From 3d37418970323bc2a43f1cc99fa1e25f532151b8 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 4 Jul 2024 14:22:57 +0800 Subject: [PATCH 06/15] update description --- .../src/WebPubSubServiceClient_helpers.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index e36bbd873ee2..172c8c342c0d 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -30,7 +30,7 @@ public partial class WebPubSubServiceClient internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group"); /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. /// User Id. @@ -43,7 +43,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. /// User Id. @@ -72,7 +72,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. /// User Id. @@ -85,7 +85,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// UTC time when the token expires. /// User Id. @@ -99,7 +99,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -114,7 +114,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -133,7 +133,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -154,7 +154,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -177,7 +177,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -196,7 +196,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for clients who connect to "/clients/hubs/{hub}". + /// Creates a URI with authentication token for clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -217,7 +217,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. From 99b8d15b5b43dc51765db34d412a645b2a7c39ad Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 4 Jul 2024 14:42:59 +0800 Subject: [PATCH 07/15] update --- .../src/WebPubSubServiceClient_helpers.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index 172c8c342c0d..d390be18c517 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -30,7 +30,7 @@ public partial class WebPubSubServiceClient internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group"); /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -43,7 +43,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -57,7 +57,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -72,7 +72,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -85,7 +85,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -99,7 +99,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -114,7 +114,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -154,7 +154,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -177,7 +177,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -196,7 +196,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for clients. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -217,7 +217,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for clients.Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. From ac4ec8e86f33a23e4ce977a1d417fb5ea92a70c5 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 4 Jul 2024 14:59:38 +0800 Subject: [PATCH 08/15] update --- .../src/WebPubSubServiceClient_helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index d390be18c517..ac0b83b774c8 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -99,7 +99,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.. /// /// UTC time when the token expires. /// User Id. From cae523b51b388795a27b50c0e0bd21afc323a2f9 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Thu, 4 Jul 2024 15:21:26 +0800 Subject: [PATCH 09/15] add --- .../src/WebPubSubServiceClient_helpers.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index ac0b83b774c8..ff1c5f870286 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -154,7 +154,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -217,7 +217,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.Creates a URI with authentication token for the clients. For MQTT clients, specify "clientEndpointType" to MQTT. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. From ed4cfc2311b444738a822ac7244fa237af978aaf Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Mon, 15 Jul 2024 15:44:08 +0800 Subject: [PATCH 10/15] Rename `ClientEndpointType` to `ClientType` --- .../{ClientEndpointType.cs => ClientType.cs} | 4 +- .../src/WebPubSubServiceClient_helpers.cs | 98 +++++++++---------- .../tests/WebPubSubGenerateUriTests.cs | 12 +-- 3 files changed, 57 insertions(+), 57 deletions(-) rename sdk/webpubsub/Azure.Messaging.WebPubSub/src/{ClientEndpointType.cs => ClientType.cs} (86%) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs similarity index 86% rename from sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs rename to sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs index d1710fe55308..5de512cf3891 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientEndpointType.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs @@ -4,9 +4,9 @@ namespace Azure.Messaging.WebPubSub; /// -/// The type of client endpoint. +/// The type of client. /// -public enum ClientEndpointType +public enum ClientType { /// /// Default client endpoint type, whose path starts with "/client". diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index ff1c5f870286..f8b1c9213c43 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -30,7 +30,7 @@ public partial class WebPubSubServiceClient internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group"); /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -40,10 +40,10 @@ public partial class WebPubSubServiceClient #pragma warning disable AZC0015 // Unexpected client method return type. public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -54,25 +54,25 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I #pragma warning disable AZC0015 // Unexpected client method return type. public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientType.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client endpoint type. + /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -81,11 +81,11 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = /// #pragma warning disable AZC0015 // Unexpected client method return type. public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientEndpointType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientType.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// UTC time when the token expires. /// User Id. @@ -95,26 +95,26 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// #pragma warning disable AZC0015 // Unexpected client method return type. public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientEndpointType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientType.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one.. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one.. /// /// UTC time when the token expires. /// User Id. /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client endpoint type. + /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientEndpointType clientEndpointType = default, CancellationToken cancellationToken = default) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientEndpointType, async: true, cancellationToken).ConfigureAwait(false); + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientType clientType = default, CancellationToken cancellationToken = default) + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -129,11 +129,11 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientEndpointType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientType.Default, false, cancellationToken).EnsureCompleted(); } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -150,17 +150,17 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientEndpointType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientType.Default, false, cancellationToken).EnsureCompleted(); } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client endpoint type. + /// The client type. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -169,15 +169,15 @@ public virtual Uri GetClientAccessUri( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientEndpointType clientEndpointType = default, + ClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientEndpointType, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientType, false, cancellationToken).EnsureCompleted(); } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -192,11 +192,11 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientEndpointType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientType.Default, true, cancellationToken).ConfigureAwait(false); } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -213,17 +213,17 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientEndpointType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientType.Default, true, cancellationToken).ConfigureAwait(false); } /// - /// Creates a URI with authentication token for the clients. Specify clientEndpointType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client endpoint type. + /// The client type. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -232,11 +232,11 @@ public virtual async Task GetClientAccessUriAsync( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientEndpointType clientEndpointType = default, + ClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientEndpointType, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientType, true, cancellationToken).ConfigureAwait(false); } internal static int GetMinutesToExpire(TimeSpan expiresAfter) => Math.Max((int)expiresAfter.TotalMinutes, 1); @@ -248,12 +248,12 @@ private async Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientEndpointType clientEndpointType = default, + ClientType clientType = default, bool async = true, CancellationToken cancellationToken = default) { - var token = await GetClientAccessTokenCore(expiresAt, userId, roles, groups, clientEndpointType, async, cancellationToken: cancellationToken).ConfigureAwait(false); - return GetClientAccessUriInternal(token, clientEndpointType); + var token = await GetClientAccessTokenCore(expiresAt, userId, roles, groups, clientType, async, cancellationToken: cancellationToken).ConfigureAwait(false); + return GetClientAccessUriInternal(token, clientType); } private Task GetClientAccessUriInternal( @@ -261,11 +261,11 @@ private Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientEndpointType clientEndpointType = default, + ClientType clientType = default, bool async = true, CancellationToken cancellationToken = default) { - return GetClientAccessUriInternal(expireAfter == default ? DateTimeOffset.UtcNow.Add(TimeSpan.FromHours(1)) : DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups, clientEndpointType, async, cancellationToken); + return GetClientAccessUriInternal(expireAfter == default ? DateTimeOffset.UtcNow.Add(TimeSpan.FromHours(1)) : DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups, clientType, async, cancellationToken); } /// @@ -340,7 +340,7 @@ internal static string PermissionToString(WebPubSubPermission permission) } } - private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, ClientEndpointType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) + private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, ClientType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) { var keyBytes = Encoding.UTF8.GetBytes(_credential.Key); @@ -374,10 +374,10 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, Cli return jwt.BuildString(); } - private string GetRelativeClientEndpoint(ClientEndpointType endpointType) => endpointType switch + private string GetRelativeClientEndpoint(ClientType endpointType) => endpointType switch { - ClientEndpointType.Default => $"client/hubs/{_hub}", - ClientEndpointType.MQTT => $"clients/mqtt/hubs/{_hub}", + ClientType.Default => $"client/hubs/{_hub}", + ClientType.MQTT => $"clients/mqtt/hubs/{_hub}", _ => throw new ArgumentOutOfRangeException(nameof(endpointType)) }; @@ -386,19 +386,19 @@ private async Task GetClientAccessTokenCore( string userId, IEnumerable roles, IEnumerable groups, - ClientEndpointType clientEndpointType, + ClientType clientType, bool async, CancellationToken cancellationToken = default) { - if (clientEndpointType == ClientEndpointType.MQTT && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + if (clientType == ClientType.MQTT && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { throw new NotSupportedException($"Getting client access token for MQTT is supported since API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()}. Current version is {_apiVersion}."); } - var clientEndpointString = clientEndpointType switch + var clientEndpointString = clientType switch { - ClientEndpointType.Default => "default", - ClientEndpointType.MQTT => "mqtt", - _ => throw new ArgumentOutOfRangeException(nameof(clientEndpointType)) + ClientType.Default => "default", + ClientType.MQTT => "mqtt", + _ => throw new ArgumentOutOfRangeException(nameof(clientType)) }; if (_tokenCredential != null) { @@ -414,7 +414,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } else if (_credential != null) { - return GenerateTokenFromAzureKeyCredential(expiresAt, clientEndpointType, userId, roles, groups); + return GenerateTokenFromAzureKeyCredential(expiresAt, clientType, userId, roles, groups); } else { @@ -422,7 +422,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } } - private Uri GetClientAccessUriInternal(string token, ClientEndpointType endpointType) + private Uri GetClientAccessUriInternal(string token, ClientType endpointType) { UriBuilder clientEndpoint = new(Endpoint) { diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs index 2221d03ab0a6..61c5575687ec 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs @@ -22,9 +22,9 @@ public class WebPubSubGenerateUriTests private static readonly JwtSecurityTokenHandler s_jwtTokenHandler = new(); - [TestCase(ClientEndpointType.Default, "/client")] - [TestCase(ClientEndpointType.MQTT, "/clients/mqtt")] - public async Task GetClientAccessUri_AccessKey_Test(ClientEndpointType clientType, string clientUriPrefix) + [TestCase(ClientType.Default, "/client")] + [TestCase(ClientType.MQTT, "/clients/mqtt")] + public async Task GetClientAccessUri_AccessKey_Test(ClientType clientType, string clientUriPrefix) { var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); var expectedUriPrefix = $"ws://localhost:8080{clientUriPrefix}/hubs/hub?access_token="; @@ -36,9 +36,9 @@ public async Task GetClientAccessUri_AccessKey_Test(ClientEndpointType clientTyp Assert.True((await serviceClient.GetClientAccessUriAsync(DateTimeOffset.Now, default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); } - [TestCase(ClientEndpointType.Default, "/client", "default")] - [TestCase(ClientEndpointType.MQTT, "/clients/mqtt", "mqtt")] - public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(ClientEndpointType clientType, string clientUriPrefix, string clientTypeString) + [TestCase(ClientType.Default, "/client", "default")] + [TestCase(ClientType.MQTT, "/clients/mqtt", "mqtt")] + public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(ClientType clientType, string clientUriPrefix, string clientTypeString) { var serviceClient = new WebPubSubServiceSubClass(new Uri("https://localhost"), "hub", new DefaultAzureCredential()); var expectedUri = new Uri($"wss://localhost{clientUriPrefix}/hubs/hub?access_token=fakeToken"); From 69336f7e7c3347a883c949c07a7d8bfc1f767a0b Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Mon, 15 Jul 2024 16:02:16 +0800 Subject: [PATCH 11/15] commit --- .../api/Azure.Messaging.WebPubSub.netstandard2.0.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index b08f231b2d47..1dcb58487266 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -5,7 +5,7 @@ public static partial class ClientConnectionFilter public static string Create(System.FormattableString filter) { throw null; } public static string Create(System.FormattableString filter, System.IFormatProvider formatProvider) { throw null; } } - public enum ClientEndpointType + public enum ClientType { Default = 0, MQTT = 1, @@ -45,16 +45,16 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task CloseUserConnectionsAsync(string userId, System.Collections.Generic.IEnumerable excluded = null, string reason = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientEndpointType clientEndpointType = Azure.Messaging.WebPubSub.ClientEndpointType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual Azure.Response GrantPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } From 96ddbda4fb610e7d8d40249998b6b93cfc621a1a Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Tue, 16 Jul 2024 15:11:47 +0800 Subject: [PATCH 12/15] fix comments --- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 20 +++++-- .../{ClientType.cs => WebPubSubClientType.cs} | 4 +- .../src/WebPubSubServiceClient_extensions.cs | 44 +++++++-------- .../src/WebPubSubServiceClient_helpers.cs | 55 +++++++++++-------- .../tests/WebPubSubGenerateUriTests.cs | 12 ++-- 5 files changed, 76 insertions(+), 59 deletions(-) rename sdk/webpubsub/Azure.Messaging.WebPubSub/src/{ClientType.cs => WebPubSubClientType.cs} (91%) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index 1dcb58487266..51ee7a09917d 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -5,10 +5,10 @@ public static partial class ClientConnectionFilter public static string Create(System.FormattableString filter) { throw null; } public static string Create(System.FormattableString filter, System.IFormatProvider formatProvider) { throw null; } } - public enum ClientType + public enum WebPubSubClientType { Default = 0, - MQTT = 1, + Mqtt = 1, } public enum WebPubSubPermission { @@ -45,17 +45,25 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task CloseUserConnectionsAsync(string userId, System.Collections.Generic.IEnumerable excluded = null, string reason = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.ClientType clientType = Azure.Messaging.WebPubSub.ClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } public virtual Azure.Response GrantPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task GrantPermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, Azure.RequestContext context = null) { throw null; } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs similarity index 91% rename from sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs rename to sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs index 5de512cf3891..606f66751caa 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/ClientType.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs @@ -6,7 +6,7 @@ namespace Azure.Messaging.WebPubSub; /// /// The type of client. /// -public enum ClientType +public enum WebPubSubClientType { /// /// Default client endpoint type, whose path starts with "/client". @@ -16,5 +16,5 @@ public enum ClientType /// /// MQTT client endpoint, whose path starts with "/clients/mqtt". /// - MQTT, + Mqtt, } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index a8f2ca103d44..53417ab8c5a9 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -18,7 +18,7 @@ namespace Azure.Messaging.WebPubSub [CodeGenSuppress("SendToConnection", typeof(string), typeof(RequestContent), typeof(RequestContext))] [CodeGenSuppress("SendToConnectionAsync", typeof(string), typeof(RequestContent), typeof(RequestContext))] [CodeGenSuppress("SendToGroup", typeof(string), typeof(RequestContent), typeof(IEnumerable), typeof(RequestContext))] - [CodeGenSuppress("SendToGroupAsync", typeof(string), typeof(RequestContent), typeof(IEnumerable), typeof(RequestContext))] + [CodeGenSuppress("SendToGroupAsync", typeof(string), typeof(RequestContent), typeof(IEnumerable), typeof(RequestContext))] [CodeGenSuppress("SendToUser", typeof(string), typeof(RequestContent), typeof(RequestContext))] [CodeGenSuppress("SendToUserAsync", typeof(string), typeof(RequestContent), typeof(RequestContext))] [CodeGenSuppress("AddUserToGroup", typeof(string), typeof(string), typeof(RequestContext))] @@ -172,7 +172,8 @@ public virtual async Task SendToAllAsync(string content, ContentType c { Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return await SendToAllAsync(RequestContent.Create(content), contentType.ToString(), default, context: default).ConfigureAwait(false); } @@ -185,7 +186,8 @@ public virtual Response SendToAll(string content, ContentType contentType = defa { Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return SendToAll(RequestContent.Create(content), contentType, excluded: default, context: default); } @@ -202,7 +204,8 @@ public virtual async Task SendToUserAsync(string userId, string conten Argument.AssertNotNull(userId, nameof(userId)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return await SendToUserAsync(userId, RequestContent.Create(content), contentType, context: default).ConfigureAwait(false); } @@ -219,7 +222,8 @@ public virtual Response SendToUser(string userId, string content, ContentType co Argument.AssertNotNull(userId, nameof(userId)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return SendToUser(userId, RequestContent.Create(content), contentType, context: default); } @@ -236,7 +240,8 @@ public virtual async Task SendToConnectionAsync(string connectionId, s Argument.AssertNotNull(connectionId, nameof(connectionId)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return await SendToConnectionAsync(connectionId, RequestContent.Create(content), contentType, context: default).ConfigureAwait(false); } @@ -253,7 +258,8 @@ public virtual Response SendToConnection(string connectionId, string content, Co Argument.AssertNotNull(connectionId, nameof(connectionId)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return SendToConnection(connectionId, RequestContent.Create(content), contentType, context: default); } @@ -270,9 +276,10 @@ public virtual async Task SendToGroupAsync(string group, string conten Argument.AssertNotNull(group, nameof(group)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; - return await SendToGroupAsync(group, RequestContent.Create(content), contentType, excluded : default, context: default).ConfigureAwait(false); + return await SendToGroupAsync(group, RequestContent.Create(content), contentType, excluded: default, context: default).ConfigureAwait(false); } /// @@ -287,7 +294,8 @@ public virtual Response SendToGroup(string group, string content, ContentType co Argument.AssertNotNull(group, nameof(group)); Argument.AssertNotNull(content, nameof(content)); - if (contentType == default) contentType = ContentType.TextPlain; + if (contentType == default) + contentType = ContentType.TextPlain; return SendToGroup(group, RequestContent.Create(content), contentType, excluded: default, context: default); } @@ -564,9 +572,7 @@ public virtual Response AddConnectionsToGroups(IEnumerable groups, strin Argument.AssertNotNull(filter, nameof(filter)); Argument.AssertNotNull(groups, nameof(groups)); - string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); - - return AddConnectionsToGroups(RequestContent.Create(json), context); + return AddConnectionsToGroups(RequestContent.Create(new { filter = filter, groups = groups }), context); } /// @@ -581,9 +587,7 @@ public virtual async Task AddConnectionsToGroupsAsync(IEnumerable @@ -597,9 +601,7 @@ public virtual Response RemoveConnectionsFromGroups(IEnumerable groups, { Argument.AssertNotNull(groups, nameof(groups)); - string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); - - return RemoveConnectionsFromGroups(RequestContent.Create(json), context); + return RemoveConnectionsFromGroups(RequestContent.Create(new { filter = filter, groups = groups }), context); } /// @@ -613,9 +615,7 @@ public virtual async Task RemoveConnectionsFromGroupsAsync(IEnumerable { Argument.AssertNotNull(groups, nameof(groups)); - string json = System.Text.Json.JsonSerializer.Serialize(new { filter = filter, groups = groups }); - - return await RemoveConnectionsFromGroupsAsync(RequestContent.Create(json), context).ConfigureAwait(false); + return await RemoveConnectionsFromGroupsAsync(RequestContent.Create(new { filter = filter, groups = groups }), context).ConfigureAwait(false); } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index f8b1c9213c43..dcd483e8eeff 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Text.Json; @@ -38,9 +39,10 @@ public partial class WebPubSubServiceClient /// Roles that the connection with the generated token will have. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientType.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. @@ -52,9 +54,10 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I /// Groups that the connection with the generated token will join when it connects. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientType.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. @@ -67,7 +70,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientType clientType = default, CancellationToken cancellationToken = default) + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: false, cancellationToken).EnsureCompleted(); @@ -80,8 +83,9 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = /// Roles that the connection with the generated token will have. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, null, ClientType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientType.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -94,8 +98,9 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// Groups that the connection with the generated token will join when it connects. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, ClientType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientType.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -109,7 +114,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, ClientType clientType = default, CancellationToken cancellationToken = default) + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. @@ -122,6 +127,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri( TimeSpan expiresAfter, string userId, @@ -129,7 +135,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientType.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -142,6 +148,7 @@ public virtual Uri GetClientAccessUri( /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri( TimeSpan expiresAfter, string userId, @@ -150,7 +157,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientType.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -169,7 +176,7 @@ public virtual Uri GetClientAccessUri( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientType clientType = default, + WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { @@ -185,6 +192,7 @@ public virtual Uri GetClientAccessUri( /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync( TimeSpan expiresAfter, string userId, @@ -192,7 +200,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, ClientType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientType.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -205,6 +213,7 @@ public virtual async Task GetClientAccessUriAsync( /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. + [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync( TimeSpan expiresAfter, string userId, @@ -213,7 +222,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, ClientType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientType.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -232,7 +241,7 @@ public virtual async Task GetClientAccessUriAsync( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientType clientType = default, + WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { @@ -248,7 +257,7 @@ private async Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientType clientType = default, + WebPubSubClientType clientType = default, bool async = true, CancellationToken cancellationToken = default) { @@ -261,7 +270,7 @@ private Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - ClientType clientType = default, + WebPubSubClientType clientType = default, bool async = true, CancellationToken cancellationToken = default) { @@ -340,7 +349,7 @@ internal static string PermissionToString(WebPubSubPermission permission) } } - private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, ClientType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) + private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, WebPubSubClientType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) { var keyBytes = Encoding.UTF8.GetBytes(_credential.Key); @@ -374,10 +383,10 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, Cli return jwt.BuildString(); } - private string GetRelativeClientEndpoint(ClientType endpointType) => endpointType switch + private string GetRelativeClientEndpoint(WebPubSubClientType endpointType) => endpointType switch { - ClientType.Default => $"client/hubs/{_hub}", - ClientType.MQTT => $"clients/mqtt/hubs/{_hub}", + WebPubSubClientType.Default => $"client/hubs/{_hub}", + WebPubSubClientType.Mqtt => $"clients/mqtt/hubs/{_hub}", _ => throw new ArgumentOutOfRangeException(nameof(endpointType)) }; @@ -386,18 +395,18 @@ private async Task GetClientAccessTokenCore( string userId, IEnumerable roles, IEnumerable groups, - ClientType clientType, + WebPubSubClientType clientType, bool async, CancellationToken cancellationToken = default) { - if (clientType == ClientType.MQTT && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + if (clientType == WebPubSubClientType.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { throw new NotSupportedException($"Getting client access token for MQTT is supported since API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()}. Current version is {_apiVersion}."); } var clientEndpointString = clientType switch { - ClientType.Default => "default", - ClientType.MQTT => "mqtt", + WebPubSubClientType.Default => "default", + WebPubSubClientType.Mqtt => "mqtt", _ => throw new ArgumentOutOfRangeException(nameof(clientType)) }; if (_tokenCredential != null) @@ -422,7 +431,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } } - private Uri GetClientAccessUriInternal(string token, ClientType endpointType) + private Uri GetClientAccessUriInternal(string token, WebPubSubClientType endpointType) { UriBuilder clientEndpoint = new(Endpoint) { diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs index 61c5575687ec..ee71fc66eef1 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs @@ -22,9 +22,9 @@ public class WebPubSubGenerateUriTests private static readonly JwtSecurityTokenHandler s_jwtTokenHandler = new(); - [TestCase(ClientType.Default, "/client")] - [TestCase(ClientType.MQTT, "/clients/mqtt")] - public async Task GetClientAccessUri_AccessKey_Test(ClientType clientType, string clientUriPrefix) + [TestCase(WebPubSubClientType.Default, "/client")] + [TestCase(WebPubSubClientType.Mqtt, "/clients/mqtt")] + public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientType clientType, string clientUriPrefix) { var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); var expectedUriPrefix = $"ws://localhost:8080{clientUriPrefix}/hubs/hub?access_token="; @@ -36,9 +36,9 @@ public async Task GetClientAccessUri_AccessKey_Test(ClientType clientType, strin Assert.True((await serviceClient.GetClientAccessUriAsync(DateTimeOffset.Now, default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); } - [TestCase(ClientType.Default, "/client", "default")] - [TestCase(ClientType.MQTT, "/clients/mqtt", "mqtt")] - public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(ClientType clientType, string clientUriPrefix, string clientTypeString) + [TestCase(WebPubSubClientType.Default, "/client", "default")] + [TestCase(WebPubSubClientType.Mqtt, "/clients/mqtt", "mqtt")] + public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(WebPubSubClientType clientType, string clientUriPrefix, string clientTypeString) { var serviceClient = new WebPubSubServiceSubClass(new Uri("https://localhost"), "hub", new DefaultAzureCredential()); var expectedUri = new Uri($"wss://localhost{clientUriPrefix}/hubs/hub?access_token=fakeToken"); From 6d6a56b56ee721a664f27c050a798c990ba461f8 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Wed, 17 Jul 2024 11:10:22 +0800 Subject: [PATCH 13/15] Rename to `WebPubSubClientAccess` --- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 10 ++-- ...ClientType.cs => WebPubSubClientAccess.cs} | 8 ++-- .../src/WebPubSubServiceClient_helpers.cs | 48 +++++++++---------- .../tests/WebPubSubGenerateUriTests.cs | 12 ++--- 4 files changed, 39 insertions(+), 39 deletions(-) rename sdk/webpubsub/Azure.Messaging.WebPubSub/src/{WebPubSubClientType.cs => WebPubSubClientAccess.cs} (54%) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index 51ee7a09917d..6bcc84c644de 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -5,7 +5,7 @@ public static partial class ClientConnectionFilter public static string Create(System.FormattableString filter) { throw null; } public static string Create(System.FormattableString filter, System.IFormatProvider formatProvider) { throw null; } } - public enum WebPubSubClientType + public enum WebPubSubClientAccess { Default = 0, Mqtt = 1, @@ -45,22 +45,22 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task CloseUserConnectionsAsync(string userId, System.Collections.Generic.IEnumerable excluded = null, string reason = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientType clientType = Azure.Messaging.WebPubSub.WebPubSubClientType.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs similarity index 54% rename from sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs rename to sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs index 606f66751caa..f332cad5904c 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientType.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs @@ -4,17 +4,17 @@ namespace Azure.Messaging.WebPubSub; /// -/// The type of client. +/// The access type of clients. /// -public enum WebPubSubClientType +public enum WebPubSubClientAccess { /// - /// Default client endpoint type, whose path starts with "/client". + /// Default client access, whose access endpoint starts with "/client". /// Default, /// - /// MQTT client endpoint, whose path starts with "/clients/mqtt". + /// MQTT client access, whose access endpoint starts with "/clients/mqtt". /// Mqtt, } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index dcd483e8eeff..f2f8ddff6d64 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -42,7 +42,7 @@ public partial class WebPubSubServiceClient [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. @@ -57,7 +57,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientType.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. @@ -70,7 +70,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: false, cancellationToken).EnsureCompleted(); @@ -85,7 +85,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = #pragma warning disable AZC0015 // Unexpected client method return type. [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientAccess.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -100,7 +100,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning disable AZC0015 // Unexpected client method return type. [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientType.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientAccess.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -114,7 +114,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// The client type. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientType clientType = default, CancellationToken cancellationToken = default) + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. @@ -135,7 +135,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientAccess.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -157,7 +157,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientType.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientAccess.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -176,7 +176,7 @@ public virtual Uri GetClientAccessUri( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientType clientType = default, + WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { @@ -200,7 +200,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientAccess.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -222,7 +222,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientType.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientAccess.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -241,7 +241,7 @@ public virtual async Task GetClientAccessUriAsync( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientType clientType = default, + WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { @@ -257,7 +257,7 @@ private async Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientType clientType = default, + WebPubSubClientAccess clientType = default, bool async = true, CancellationToken cancellationToken = default) { @@ -270,7 +270,7 @@ private Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientType clientType = default, + WebPubSubClientAccess clientType = default, bool async = true, CancellationToken cancellationToken = default) { @@ -349,7 +349,7 @@ internal static string PermissionToString(WebPubSubPermission permission) } } - private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, WebPubSubClientType endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) + private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, WebPubSubClientAccess endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) { var keyBytes = Encoding.UTF8.GetBytes(_credential.Key); @@ -383,10 +383,10 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, Web return jwt.BuildString(); } - private string GetRelativeClientEndpoint(WebPubSubClientType endpointType) => endpointType switch + private string GetRelativeClientEndpoint(WebPubSubClientAccess endpointType) => endpointType switch { - WebPubSubClientType.Default => $"client/hubs/{_hub}", - WebPubSubClientType.Mqtt => $"clients/mqtt/hubs/{_hub}", + WebPubSubClientAccess.Default => $"client/hubs/{_hub}", + WebPubSubClientAccess.Mqtt => $"clients/mqtt/hubs/{_hub}", _ => throw new ArgumentOutOfRangeException(nameof(endpointType)) }; @@ -395,18 +395,18 @@ private async Task GetClientAccessTokenCore( string userId, IEnumerable roles, IEnumerable groups, - WebPubSubClientType clientType, + WebPubSubClientAccess clientType, bool async, CancellationToken cancellationToken = default) { - if (clientType == WebPubSubClientType.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + if (clientType == WebPubSubClientAccess.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { - throw new NotSupportedException($"Getting client access token for MQTT is supported since API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()}. Current version is {_apiVersion}."); + throw new NotSupportedException($"Generating a client access URI for MQTT is only supported in API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()} or later. You are currently using API version {_apiVersion}."); } var clientEndpointString = clientType switch { - WebPubSubClientType.Default => "default", - WebPubSubClientType.Mqtt => "mqtt", + WebPubSubClientAccess.Default => "default", + WebPubSubClientAccess.Mqtt => "mqtt", _ => throw new ArgumentOutOfRangeException(nameof(clientType)) }; if (_tokenCredential != null) @@ -431,7 +431,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } } - private Uri GetClientAccessUriInternal(string token, WebPubSubClientType endpointType) + private Uri GetClientAccessUriInternal(string token, WebPubSubClientAccess endpointType) { UriBuilder clientEndpoint = new(Endpoint) { diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs index ee71fc66eef1..1519820d9c82 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs @@ -22,9 +22,9 @@ public class WebPubSubGenerateUriTests private static readonly JwtSecurityTokenHandler s_jwtTokenHandler = new(); - [TestCase(WebPubSubClientType.Default, "/client")] - [TestCase(WebPubSubClientType.Mqtt, "/clients/mqtt")] - public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientType clientType, string clientUriPrefix) + [TestCase(WebPubSubClientAccess.Default, "/client")] + [TestCase(WebPubSubClientAccess.Mqtt, "/clients/mqtt")] + public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientAccess clientType, string clientUriPrefix) { var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); var expectedUriPrefix = $"ws://localhost:8080{clientUriPrefix}/hubs/hub?access_token="; @@ -36,9 +36,9 @@ public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientType clientTy Assert.True((await serviceClient.GetClientAccessUriAsync(DateTimeOffset.Now, default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); } - [TestCase(WebPubSubClientType.Default, "/client", "default")] - [TestCase(WebPubSubClientType.Mqtt, "/clients/mqtt", "mqtt")] - public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(WebPubSubClientType clientType, string clientUriPrefix, string clientTypeString) + [TestCase(WebPubSubClientAccess.Default, "/client", "default")] + [TestCase(WebPubSubClientAccess.Mqtt, "/clients/mqtt", "mqtt")] + public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(WebPubSubClientAccess clientType, string clientUriPrefix, string clientTypeString) { var serviceClient = new WebPubSubServiceSubClass(new Uri("https://localhost"), "hub", new DefaultAzureCredential()); var expectedUri = new Uri($"wss://localhost{clientUriPrefix}/hubs/hub?access_token=fakeToken"); From c4e97d56b734c8742405490e28c04a26702c4c14 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Wed, 17 Jul 2024 18:43:38 +0800 Subject: [PATCH 14/15] fix --- .../src/WebPubSubServiceClient_helpers.cs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index f2f8ddff6d64..dfe4ef31481c 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -31,7 +31,7 @@ public partial class WebPubSubServiceClient internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group"); /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// UTC time when the token expires. /// User Id. @@ -45,7 +45,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// UTC time when the token expires. /// User Id. @@ -60,7 +60,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// UTC time when the token expires. /// User Id. @@ -75,7 +75,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: false, cancellationToken).EnsureCompleted(); /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// UTC time when the token expires. /// User Id. @@ -89,7 +89,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// UTC time when the token expires. /// User Id. @@ -104,7 +104,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one.. + /// Creates a URI with authentication token for the clients.. /// /// UTC time when the token expires. /// User Id. @@ -119,7 +119,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning restore AZC0015 // Unexpected client method return type. /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -139,7 +139,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -161,7 +161,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -184,7 +184,7 @@ public virtual Uri GetClientAccessUri( } /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -204,7 +204,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -226,7 +226,7 @@ public virtual async Task GetClientAccessUriAsync( } /// - /// Creates a URI with authentication token for the clients. Specify clientType to MQTT for MQTT clients, others use the default one. + /// Creates a URI with authentication token for the clients. /// /// Defaults to one hour, if not specified. Must be greater or equal zero. /// User Id. @@ -395,19 +395,19 @@ private async Task GetClientAccessTokenCore( string userId, IEnumerable roles, IEnumerable groups, - WebPubSubClientAccess clientType, + WebPubSubClientAccess clientAccess, bool async, CancellationToken cancellationToken = default) { - if (clientType == WebPubSubClientAccess.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + if (clientAccess == WebPubSubClientAccess.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { throw new NotSupportedException($"Generating a client access URI for MQTT is only supported in API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()} or later. You are currently using API version {_apiVersion}."); } - var clientEndpointString = clientType switch + var clientEndpointString = clientAccess switch { WebPubSubClientAccess.Default => "default", WebPubSubClientAccess.Mqtt => "mqtt", - _ => throw new ArgumentOutOfRangeException(nameof(clientType)) + _ => throw new ArgumentOutOfRangeException(nameof(clientAccess)) }; if (_tokenCredential != null) { @@ -423,7 +423,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } else if (_credential != null) { - return GenerateTokenFromAzureKeyCredential(expiresAt, clientType, userId, roles, groups); + return GenerateTokenFromAzureKeyCredential(expiresAt, clientAccess, userId, roles, groups); } else { From 8461d49afc7152fc677cebb34a02ac9bd9aaa0d7 Mon Sep 17 00:00:00 2001 From: Zitong Yang Date: Fri, 19 Jul 2024 14:40:06 +0800 Subject: [PATCH 15/15] renaming to client protocol --- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 10 +-- ...ntAccess.cs => WebPubSubClientProtocol.cs} | 8 +-- .../src/WebPubSubServiceClient_helpers.cs | 72 +++++++++---------- .../tests/WebPubSubGenerateUriTests.cs | 12 ++-- 4 files changed, 51 insertions(+), 51 deletions(-) rename sdk/webpubsub/Azure.Messaging.WebPubSub/src/{WebPubSubClientAccess.cs => WebPubSubClientProtocol.cs} (54%) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs index 6bcc84c644de..bd877b6c98a4 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/api/Azure.Messaging.WebPubSub.netstandard2.0.cs @@ -5,7 +5,7 @@ public static partial class ClientConnectionFilter public static string Create(System.FormattableString filter) { throw null; } public static string Create(System.FormattableString filter, System.IFormatProvider formatProvider) { throw null; } } - public enum WebPubSubClientAccess + public enum WebPubSubClientProtocol { Default = 0, Mqtt = 1, @@ -45,22 +45,22 @@ public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.Core.TokenC public virtual System.Threading.Tasks.Task CloseUserConnectionsAsync(string userId, System.Collections.Generic.IEnumerable excluded = null, string reason = null, Azure.RequestContext context = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, Azure.RequestContext context = null) { throw null; } - public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientProtocol clientProtocol = Azure.Messaging.WebPubSub.WebPubSubClientProtocol.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientProtocol clientProtocol = Azure.Messaging.WebPubSub.WebPubSubClientProtocol.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Uri GetClientAccessUri(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientProtocol clientProtocol = Azure.Messaging.WebPubSub.WebPubSubClientProtocol.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.DateTimeOffset expiresAt, string userId, System.Collections.Generic.IEnumerable roles, System.Threading.CancellationToken cancellationToken) { throw null; } - public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientAccess clientType = Azure.Messaging.WebPubSub.WebPubSubClientAccess.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter = default(System.TimeSpan), string userId = null, System.Collections.Generic.IEnumerable roles = null, System.Collections.Generic.IEnumerable groups = null, Azure.Messaging.WebPubSub.WebPubSubClientProtocol clientProtocol = Azure.Messaging.WebPubSub.WebPubSubClientProtocol.Default, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public virtual System.Threading.Tasks.Task GetClientAccessUriAsync(System.TimeSpan expiresAfter, string userId, System.Collections.Generic.IEnumerable roles, System.Collections.Generic.IEnumerable groups, System.Threading.CancellationToken cancellationToken) { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientProtocol.cs similarity index 54% rename from sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs rename to sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientProtocol.cs index f332cad5904c..ea59d637dc07 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientAccess.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubClientProtocol.cs @@ -4,17 +4,17 @@ namespace Azure.Messaging.WebPubSub; /// -/// The access type of clients. +/// The client protocol. /// -public enum WebPubSubClientAccess +public enum WebPubSubClientProtocol { /// - /// Default client access, whose access endpoint starts with "/client". + /// Default client protocol, whose access endpoint starts with "/client". /// Default, /// - /// MQTT client access, whose access endpoint starts with "/clients/mqtt". + /// MQTT client protocol, whose access endpoint starts with "/clients/mqtt". /// Mqtt, } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index dfe4ef31481c..dcaef57f024f 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -42,7 +42,7 @@ public partial class WebPubSubServiceClient [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientProtocol.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. @@ -57,7 +57,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I [EditorBrowsable(EditorBrowsableState.Never)] public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientAccess.Default, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientProtocol.Default, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. @@ -67,12 +67,12 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, I /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client type. + /// The client protocol. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) + public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientProtocol clientProtocol = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. - => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: false, cancellationToken).EnsureCompleted(); + => GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientProtocol, async: false, cancellationToken).EnsureCompleted(); /// /// Creates a URI with authentication token for the clients. @@ -85,7 +85,7 @@ public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = #pragma warning disable AZC0015 // Unexpected client method return type. [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientAccess.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, null, WebPubSubClientProtocol.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -100,7 +100,7 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, #pragma warning disable AZC0015 // Unexpected client method return type. [EditorBrowsable(EditorBrowsableState.Never)] public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable roles, IEnumerable groups, CancellationToken cancellationToken) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientAccess.Default, async: true, cancellationToken).ConfigureAwait(false); + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, WebPubSubClientProtocol.Default, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -111,11 +111,11 @@ public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, /// Cancellation token. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client type. + /// The client protocol. /// #pragma warning disable AZC0015 // Unexpected client method return type. - public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientAccess clientType = default, CancellationToken cancellationToken = default) - => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientType, async: true, cancellationToken).ConfigureAwait(false); + public virtual async Task GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable roles = default, IEnumerable groups = default, WebPubSubClientProtocol clientProtocol = default, CancellationToken cancellationToken = default) + => await GetClientAccessUriInternal(expiresAt, userId, roles, groups, clientProtocol, async: true, cancellationToken).ConfigureAwait(false); #pragma warning restore AZC0015 // Unexpected client method return type. /// @@ -135,7 +135,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientAccess.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientProtocol.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -157,7 +157,7 @@ public virtual Uri GetClientAccessUri( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientAccess.Default, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientProtocol.Default, false, cancellationToken).EnsureCompleted(); } /// @@ -167,7 +167,7 @@ public virtual Uri GetClientAccessUri( /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client type. + /// The client protocol. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -176,11 +176,11 @@ public virtual Uri GetClientAccessUri( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientAccess clientType = default, + WebPubSubClientProtocol clientProtocol = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientType, false, cancellationToken).EnsureCompleted(); + return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientProtocol, false, cancellationToken).EnsureCompleted(); } /// @@ -200,7 +200,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientAccess.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, WebPubSubClientProtocol.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -222,7 +222,7 @@ public virtual async Task GetClientAccessUriAsync( CancellationToken cancellationToken) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientAccess.Default, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, WebPubSubClientProtocol.Default, true, cancellationToken).ConfigureAwait(false); } /// @@ -232,7 +232,7 @@ public virtual async Task GetClientAccessUriAsync( /// User Id. /// Roles that the connection with the generated token will have. /// Groups that the connection with the generated token will join when it connects. - /// The client type. + /// The client protocol. /// Cancellation token. /// #pragma warning disable AZC0015 // Unexpected client method return type. @@ -241,11 +241,11 @@ public virtual async Task GetClientAccessUriAsync( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientAccess clientType = default, + WebPubSubClientProtocol clientProtocol = default, CancellationToken cancellationToken = default) #pragma warning restore AZC0015 // Unexpected client method return type. { - return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientType, true, cancellationToken).ConfigureAwait(false); + return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, clientProtocol, true, cancellationToken).ConfigureAwait(false); } internal static int GetMinutesToExpire(TimeSpan expiresAfter) => Math.Max((int)expiresAfter.TotalMinutes, 1); @@ -257,12 +257,12 @@ private async Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientAccess clientType = default, + WebPubSubClientProtocol clientProtocol = default, bool async = true, CancellationToken cancellationToken = default) { - var token = await GetClientAccessTokenCore(expiresAt, userId, roles, groups, clientType, async, cancellationToken: cancellationToken).ConfigureAwait(false); - return GetClientAccessUriInternal(token, clientType); + var token = await GetClientAccessTokenCore(expiresAt, userId, roles, groups, clientProtocol, async, cancellationToken: cancellationToken).ConfigureAwait(false); + return GetClientAccessUriInternal(token, clientProtocol); } private Task GetClientAccessUriInternal( @@ -270,11 +270,11 @@ private Task GetClientAccessUriInternal( string userId = default, IEnumerable roles = default, IEnumerable groups = default, - WebPubSubClientAccess clientType = default, + WebPubSubClientProtocol clientProtocol = default, bool async = true, CancellationToken cancellationToken = default) { - return GetClientAccessUriInternal(expireAfter == default ? DateTimeOffset.UtcNow.Add(TimeSpan.FromHours(1)) : DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups, clientType, async, cancellationToken); + return GetClientAccessUriInternal(expireAfter == default ? DateTimeOffset.UtcNow.Add(TimeSpan.FromHours(1)) : DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups, clientProtocol, async, cancellationToken); } /// @@ -349,7 +349,7 @@ internal static string PermissionToString(WebPubSubPermission permission) } } - private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, WebPubSubClientAccess endpointType, string userId = default, IEnumerable roles = default, IEnumerable groups = default) + private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, WebPubSubClientProtocol clientProtocol, string userId = default, IEnumerable roles = default, IEnumerable groups = default) { var keyBytes = Encoding.UTF8.GetBytes(_credential.Key); @@ -361,7 +361,7 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, Web { endpoint += "/"; } - var audience = $"{endpoint}{GetRelativeClientEndpoint(endpointType)}"; + var audience = $"{endpoint}{GetRelativeClientEndpoint(clientProtocol)}"; if (userId != default) { @@ -383,11 +383,11 @@ private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, Web return jwt.BuildString(); } - private string GetRelativeClientEndpoint(WebPubSubClientAccess endpointType) => endpointType switch + private string GetRelativeClientEndpoint(WebPubSubClientProtocol clientProtocol) => clientProtocol switch { - WebPubSubClientAccess.Default => $"client/hubs/{_hub}", - WebPubSubClientAccess.Mqtt => $"clients/mqtt/hubs/{_hub}", - _ => throw new ArgumentOutOfRangeException(nameof(endpointType)) + WebPubSubClientProtocol.Default => $"client/hubs/{_hub}", + WebPubSubClientProtocol.Mqtt => $"clients/mqtt/hubs/{_hub}", + _ => throw new ArgumentOutOfRangeException(nameof(clientProtocol)) }; private async Task GetClientAccessTokenCore( @@ -395,18 +395,18 @@ private async Task GetClientAccessTokenCore( string userId, IEnumerable roles, IEnumerable groups, - WebPubSubClientAccess clientAccess, + WebPubSubClientProtocol clientAccess, bool async, CancellationToken cancellationToken = default) { - if (clientAccess == WebPubSubClientAccess.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) + if (clientAccess == WebPubSubClientProtocol.Mqtt && _apiVersionEnum < WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01) { throw new NotSupportedException($"Generating a client access URI for MQTT is only supported in API version {WebPubSubServiceClientOptions.ServiceVersion.V2024_01_01.ToVersionString()} or later. You are currently using API version {_apiVersion}."); } var clientEndpointString = clientAccess switch { - WebPubSubClientAccess.Default => "default", - WebPubSubClientAccess.Mqtt => "mqtt", + WebPubSubClientProtocol.Default => "default", + WebPubSubClientProtocol.Mqtt => "mqtt", _ => throw new ArgumentOutOfRangeException(nameof(clientAccess)) }; if (_tokenCredential != null) @@ -431,7 +431,7 @@ await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, clien } } - private Uri GetClientAccessUriInternal(string token, WebPubSubClientAccess endpointType) + private Uri GetClientAccessUriInternal(string token, WebPubSubClientProtocol endpointType) { UriBuilder clientEndpoint = new(Endpoint) { diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs index 1519820d9c82..ad7a43a3fa3a 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubGenerateUriTests.cs @@ -22,9 +22,9 @@ public class WebPubSubGenerateUriTests private static readonly JwtSecurityTokenHandler s_jwtTokenHandler = new(); - [TestCase(WebPubSubClientAccess.Default, "/client")] - [TestCase(WebPubSubClientAccess.Mqtt, "/clients/mqtt")] - public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientAccess clientType, string clientUriPrefix) + [TestCase(WebPubSubClientProtocol.Default, "/client")] + [TestCase(WebPubSubClientProtocol.Mqtt, "/clients/mqtt")] + public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientProtocol clientType, string clientUriPrefix) { var serviceClient = new WebPubSubServiceClient(string.Format("Endpoint=http://localhost;Port=8080;AccessKey={0};Version=1.0;", FakeAccessKey), "hub"); var expectedUriPrefix = $"ws://localhost:8080{clientUriPrefix}/hubs/hub?access_token="; @@ -36,9 +36,9 @@ public async Task GetClientAccessUri_AccessKey_Test(WebPubSubClientAccess client Assert.True((await serviceClient.GetClientAccessUriAsync(DateTimeOffset.Now, default, default, default, clientType, default)).ToString().StartsWith(expectedUriPrefix)); } - [TestCase(WebPubSubClientAccess.Default, "/client", "default")] - [TestCase(WebPubSubClientAccess.Mqtt, "/clients/mqtt", "mqtt")] - public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(WebPubSubClientAccess clientType, string clientUriPrefix, string clientTypeString) + [TestCase(WebPubSubClientProtocol.Default, "/client", "default")] + [TestCase(WebPubSubClientProtocol.Mqtt, "/clients/mqtt", "mqtt")] + public async Task GetClientAccessUri_MicrosoftEntraId_DefaultClient_Test(WebPubSubClientProtocol clientType, string clientUriPrefix, string clientTypeString) { var serviceClient = new WebPubSubServiceSubClass(new Uri("https://localhost"), "hub", new DefaultAzureCredential()); var expectedUri = new Uri($"wss://localhost{clientUriPrefix}/hubs/hub?access_token=fakeToken");