From d93305a83a81780f35733169a6a76bc2e5f1f251 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Tue, 4 May 2021 15:36:57 -0700 Subject: [PATCH 1/9] Move WebPubSub to LLC + extensions --- .../src/Azure.Messaging.WebPubSub.csproj | 28 +- .../src/Generated/HealthApiClient.cs | 66 + .../src/Generated/HealthApiRestClient.cs | 85 - .../src/Generated/Models/ContentType.cs | 51 - .../WebPubSubPermission.Serialization.cs | 28 - .../src/Generated/WebPubSubRestClient.cs | 1711 ----------------- .../src/Generated/WebPubSubServiceClient.cs | 768 ++++++++ .../WebPubSubServiceClientOptions.cs | 37 + .../src/HealthApiClient_extensions.cs | 40 + .../src/WebPubSubServiceClient.cs | 957 --------- ...WebPubSubServiceClientBuilderExtensions.cs | 4 +- .../src/WebPubSubServiceClientOptions.cs | 42 - .../src/WebPubSubServiceClient_extensions.cs | 173 ++ .../src/WebPubSubServiceClient_helpers.cs | 15 +- .../Azure.Messaging.WebPubSub/src/autorest.md | 4 + .../src/swagger/WebPubSub.json | 82 +- .../Samples/WebPubSubSamples.HelloWorld.cs | 18 +- .../WebPubSubParseConnectionStringTests.cs | 4 - 18 files changed, 1160 insertions(+), 2953 deletions(-) create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiRestClient.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/ContentType.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/WebPubSubPermission.Serialization.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClientOptions.cs create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient.cs delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.cs create mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs 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 e358358ade0b..61a86de806c9 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj @@ -1,10 +1,11 @@ - + Azure SDK client library for the WebPubSub service Azure SDK for WebPubSub 1.0.0-beta.2 Azure, WebPubSub, SignalR $(RequiredTargetFrameworks) + $(NoWarn);419 @@ -27,29 +28,4 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs new file mode 100644 index 000000000000..369814ca8faa --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Core.Pipeline; + +#pragma warning disable AZC0007 + +namespace Azure.Messaging.WebPubSub +{ + /// The HealthApi service client. + internal partial class HealthApiClient + { + /// The HTTP pipeline for sending and receiving REST requests and responses. + public virtual HttpPipeline Pipeline { get; } + private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; + private Uri endpoint; + private readonly string apiVersion; + + /// Initializes a new instance of HealthApiClient for mocking. + protected HealthApiClient() + { + } + + /// Get service health status. + /// The cancellation token to use. + public virtual async Task GetServiceStatusAsync(CancellationToken cancellationToken = default) + { + Request req = CreateGetServiceStatusRequest(); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Get service health status. + /// The cancellation token to use. + public virtual Response GetServiceStatus(CancellationToken cancellationToken = default) + { + Request req = CreateGetServiceStatusRequest(); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + private Request CreateGetServiceStatusRequest() + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Head; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/health", false); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + } +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiRestClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiRestClient.cs deleted file mode 100644 index 27bd6952b76b..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiRestClient.cs +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.Messaging.WebPubSub -{ - internal partial class HealthApiRestClient - { - private Uri endpoint; - private string apiVersion; - private ClientDiagnostics _clientDiagnostics; - private HttpPipeline _pipeline; - - /// Initializes a new instance of HealthApiRestClient. - /// The handler for diagnostic messaging in the client. - /// The HTTP pipeline for sending and receiving REST requests and responses. - /// server parameter. - /// Api Version. - public HealthApiRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string apiVersion = "2021-05-01-preview") - { - endpoint ??= new Uri(""); - - this.endpoint = endpoint; - this.apiVersion = apiVersion; - _clientDiagnostics = clientDiagnostics; - _pipeline = pipeline; - } - - internal HttpMessage CreateGetServiceStatusRequest() - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/health", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Get service health status. - /// The cancellation token to use. - public async Task GetServiceStatusAsync(CancellationToken cancellationToken = default) - { - using var message = CreateGetServiceStatusRequest(); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Get service health status. - /// The cancellation token to use. - public Response GetServiceStatus(CancellationToken cancellationToken = default) - { - using var message = CreateGetServiceStatusRequest(); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/ContentType.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/ContentType.cs deleted file mode 100644 index fc191368ef60..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/ContentType.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ComponentModel; - -namespace Azure.Messaging.WebPubSub.Models -{ - /// Content type for upload. - internal readonly partial struct ContentType : IEquatable - { - private readonly string _value; - - /// Determines if two values are the same. - /// is null. - public ContentType(string value) - { - _value = value ?? throw new ArgumentNullException(nameof(value)); - } - - private const string ApplicationJsonValue = "application/json"; - private const string ApplicationOctetStreamValue = "application/octet-stream"; - - /// Content Type 'application/json'. - public static ContentType ApplicationJson { get; } = new ContentType(ApplicationJsonValue); - /// Content Type 'application/octet-stream'. - public static ContentType ApplicationOctetStream { get; } = new ContentType(ApplicationOctetStreamValue); - /// Determines if two values are the same. - public static bool operator ==(ContentType left, ContentType right) => left.Equals(right); - /// Determines if two values are not the same. - public static bool operator !=(ContentType left, ContentType right) => !left.Equals(right); - /// Converts a string to a . - public static implicit operator ContentType(string value) => new ContentType(value); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => obj is ContentType other && Equals(other); - /// - public bool Equals(ContentType other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => _value?.GetHashCode() ?? 0; - /// - public override string ToString() => _value; - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/WebPubSubPermission.Serialization.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/WebPubSubPermission.Serialization.cs deleted file mode 100644 index a2e056ba992c..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/Models/WebPubSubPermission.Serialization.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; - -namespace Azure.Messaging.WebPubSub -{ - internal static partial class WebPubSubPermissionExtensions - { - public static string ToSerialString(this WebPubSubPermission value) => value switch - { - WebPubSubPermission.SendToGroup => "sendToGroup", - WebPubSubPermission.JoinLeaveGroup => "joinLeaveGroup", - _ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown WebPubSubPermission value.") - }; - - public static WebPubSubPermission ToWebPubSubPermission(this string value) - { - if (string.Equals(value, "sendToGroup", StringComparison.InvariantCultureIgnoreCase)) return WebPubSubPermission.SendToGroup; - if (string.Equals(value, "joinLeaveGroup", StringComparison.InvariantCultureIgnoreCase)) return WebPubSubPermission.JoinLeaveGroup; - throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown WebPubSubPermission value."); - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs deleted file mode 100644 index b5b86f0387f3..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs +++ /dev/null @@ -1,1711 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; -using Azure.Messaging.WebPubSub.Models; - -namespace Azure.Messaging.WebPubSub -{ - internal partial class WebPubSubRestClient - { - private Uri endpoint; - private string apiVersion; - private ClientDiagnostics _clientDiagnostics; - private HttpPipeline _pipeline; - - /// Initializes a new instance of WebPubSubRestClient. - /// The handler for diagnostic messaging in the client. - /// The HTTP pipeline for sending and receiving REST requests and responses. - /// server parameter. - /// Api Version. - public WebPubSubRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string apiVersion = "2021-05-01-preview") - { - endpoint ??= new Uri(""); - - this.endpoint = endpoint; - this.apiVersion = apiVersion; - _clientDiagnostics = clientDiagnostics; - _pipeline = pipeline; - } - - internal HttpMessage CreateSendToAllRequest(string hub, ContentType contentType, RequestContent message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public async Task SendToAllAsync(string hub, ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, contentType, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public Response SendToAll(string hub, ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, contentType, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToAllRequest(string hub, string message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public async Task SendToAllAsync(string hub, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public Response SendToAll(string hub, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateConnectionExistsRequest(string hub, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if the connection with the given connectionId exists. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The cancellation token to use. - /// or is null. - public async Task ConnectionExistsAsync(string hub, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateConnectionExistsRequest(hub, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if the connection with the given connectionId exists. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The cancellation token to use. - /// or is null. - public Response ConnectionExists(string hub, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateConnectionExistsRequest(hub, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateCloseClientConnectionRequest(string hub, string connectionId, string reason) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (reason != null) - { - uri.AppendQuery("reason", reason, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Close the client connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target connection Id. - /// The reason closing the client connection. - /// The cancellation token to use. - /// or is null. - public async Task CloseClientConnectionAsync(string hub, string connectionId, string reason = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCloseClientConnectionRequest(hub, connectionId, reason); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Close the client connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target connection Id. - /// The reason closing the client connection. - /// The cancellation token to use. - /// or is null. - public Response CloseClientConnection(string hub, string connectionId, string reason = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCloseClientConnectionRequest(hub, connectionId, reason); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToConnectionRequest(string hub, string connectionId, ContentType contentType, RequestContent message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToConnectionAsync(string hub, string connectionId, ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, contentType, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToConnection(string hub, string connectionId, ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, contentType, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToConnectionRequest(string hub, string connectionId, string message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToConnectionAsync(string hub, string connectionId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToConnection(string hub, string connectionId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateGroupExistsRequest(string hub, string group) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if there are any client connections inside the given group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - /// or is null. - public async Task GroupExistsAsync(string hub, string group, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - - using var message = CreateGroupExistsRequest(hub, group); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if there are any client connections inside the given group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - /// or is null. - public Response GroupExists(string hub, string group, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - - using var message = CreateGroupExistsRequest(hub, group); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToGroupRequest(string hub, string group, ContentType contentType, RequestContent message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToGroupAsync(string hub, string group, ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, contentType, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public Response SendToGroup(string hub, string group, ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, contentType, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToGroupRequest(string hub, string group, string message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToGroupAsync(string hub, string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public Response SendToGroup(string hub, string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateAddConnectionToGroupRequest(string hub, string group, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Add a connection to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public async Task AddConnectionToGroupAsync(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateAddConnectionToGroupRequest(hub, group, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Add a connection to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public Response AddConnectionToGroup(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateAddConnectionToGroupRequest(hub, group, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveConnectionFromGroupRequest(string hub, string group, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a connection from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public async Task RemoveConnectionFromGroupAsync(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRemoveConnectionFromGroupRequest(hub, group, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a connection from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public Response RemoveConnectionFromGroup(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRemoveConnectionFromGroupRequest(hub, group, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateUserExistsRequest(string hub, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if there are any client connections connected for the given user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public async Task UserExistsAsync(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateUserExistsRequest(hub, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if there are any client connections connected for the given user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public Response UserExists(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateUserExistsRequest(hub, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToUserRequest(string hub, string userId, ContentType contentType, RequestContent message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToUserAsync(string hub, string userId, ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, contentType, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToUser(string hub, string userId, ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, contentType, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToUserRequest(string hub, string userId, string message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToUserAsync(string hub, string userId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToUser(string hub, string userId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateAddUserToGroupRequest(string hub, string group, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Add a user to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public async Task AddUserToGroupAsync(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateAddUserToGroupRequest(hub, group, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Add a user to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public Response AddUserToGroup(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateAddUserToGroupRequest(hub, group, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveUserFromGroupRequest(string hub, string group, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a user from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public async Task RemoveUserFromGroupAsync(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromGroupRequest(hub, group, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a user from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public Response RemoveUserFromGroup(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromGroupRequest(hub, group, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveUserFromAllGroupsRequest(string hub, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a user from all groups. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public async Task RemoveUserFromAllGroupsAsync(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromAllGroupsRequest(hub, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a user from all groups. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public Response RemoveUserFromAllGroups(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromAllGroupsRequest(hub, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateGrantPermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Grant permission to the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task GrantPermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateGrantPermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Grant permission to the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response GrantPermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateGrantPermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRevokePermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Revoke permission for the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task RevokePermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRevokePermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Revoke permission for the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response RevokePermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRevokePermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateCheckPermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if a connection has permission to the specified action. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task CheckPermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCheckPermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if a connection has permission to the specified action. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response CheckPermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCheckPermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs new file mode 100644 index 000000000000..3f70ddf6fd0f --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -0,0 +1,768 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Core.Pipeline; + +#pragma warning disable AZC0007 + +namespace Azure.Messaging.WebPubSub +{ + /// The WebPubSubService service client. + public partial class WebPubSubServiceClient + { + /// The HTTP pipeline for sending and receiving REST requests and responses. + public virtual HttpPipeline Pipeline { get; } + private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; + private string hub; + private Uri endpoint; + private readonly string apiVersion; + + /// Initializes a new instance of WebPubSubServiceClient for mocking. + protected WebPubSubServiceClient() + { + } + + /// Broadcast content inside request body to all the connected client connections. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + /// The cancellation token to use. + public virtual async Task SendToAllAsync(string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + Request req = CreateSendToAllRequest(contentType, requestBody, excluded); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Broadcast content inside request body to all the connected client connections. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + /// The cancellation token to use. + public virtual Response SendToAll(string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + Request req = CreateSendToAllRequest(contentType, requestBody, excluded); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + private Request CreateSendToAllRequest(string contentType, RequestContent requestBody, IEnumerable excluded = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/:send", false); + if (excluded != null) + { + uri.AppendQueryDelimited("excluded", excluded, ",", true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + request.Headers.Add("Content-Type", contentType); + request.Content = requestBody; + return request; + } + + /// Check if the connection with the given connectionId exists. + /// The connection Id. + /// The cancellation token to use. + public virtual async Task ConnectionExistsAsync(string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateConnectionExistsRequest(connectionId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Check if the connection with the given connectionId exists. + /// The connection Id. + /// The cancellation token to use. + public virtual Response ConnectionExists(string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateConnectionExistsRequest(connectionId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The connection Id. + private Request CreateConnectionExistsRequest(string connectionId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Head; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Close the client connection. + /// Target connection Id. + /// The reason closing the client connection. + /// The cancellation token to use. + public virtual async Task CloseClientConnectionAsync(string connectionId, string reason = null, CancellationToken cancellationToken = default) + { + Request req = CreateCloseClientConnectionRequest(connectionId, reason); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Close the client connection. + /// Target connection Id. + /// The reason closing the client connection. + /// The cancellation token to use. + public virtual Response CloseClientConnection(string connectionId, string reason = null, CancellationToken cancellationToken = default) + { + Request req = CreateCloseClientConnectionRequest(connectionId, reason); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target connection Id. + /// The reason closing the client connection. + private Request CreateCloseClientConnectionRequest(string connectionId, string reason = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Delete; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (reason != null) + { + uri.AppendQuery("reason", reason, true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Send content inside request body to the specific connection. + /// The connection Id. + /// Upload file type. + /// The request body. + /// The cancellation token to use. + public virtual async Task SendToConnectionAsync(string connectionId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + { + Request req = CreateSendToConnectionRequest(connectionId, contentType, requestBody); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Send content inside request body to the specific connection. + /// The connection Id. + /// Upload file type. + /// The request body. + /// The cancellation token to use. + public virtual Response SendToConnection(string connectionId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + { + Request req = CreateSendToConnectionRequest(connectionId, contentType, requestBody); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The connection Id. + /// Upload file type. + /// The request body. + private Request CreateSendToConnectionRequest(string connectionId, string contentType, RequestContent requestBody) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + uri.AppendPath("/:send", false); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + request.Headers.Add("Content-Type", contentType); + request.Content = requestBody; + return request; + } + + /// Check if there are any client connections inside the given group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// The cancellation token to use. + public virtual async Task GroupExistsAsync(string group, CancellationToken cancellationToken = default) + { + Request req = CreateGroupExistsRequest(group); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Check if there are any client connections inside the given group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// The cancellation token to use. + public virtual Response GroupExists(string group, CancellationToken cancellationToken = default) + { + Request req = CreateGroupExistsRequest(group); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + private Request CreateGroupExistsRequest(string group) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Head; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Send content inside request body to a group of connections. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + /// The cancellation token to use. + public virtual async Task SendToGroupAsync(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + Request req = CreateSendToGroupRequest(group, contentType, requestBody, excluded); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Send content inside request body to a group of connections. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + /// The cancellation token to use. + public virtual Response SendToGroup(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + Request req = CreateSendToGroupRequest(group, contentType, requestBody, excluded); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Upload file type. + /// The request body. + /// Excluded connection Ids. + private Request CreateSendToGroupRequest(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + uri.AppendPath("/:send", false); + if (excluded != null) + { + uri.AppendQueryDelimited("excluded", excluded, ",", true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + request.Headers.Add("Content-Type", contentType); + request.Content = requestBody; + return request; + } + + /// Add a connection to the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + /// The cancellation token to use. + public virtual async Task AddConnectionToGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateAddConnectionToGroupRequest(group, connectionId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Add a connection to the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + /// The cancellation token to use. + public virtual Response AddConnectionToGroup(string group, string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateAddConnectionToGroupRequest(group, connectionId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + private Request CreateAddConnectionToGroupRequest(string group, string connectionId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Put; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Remove a connection from the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + /// The cancellation token to use. + public virtual async Task RemoveConnectionFromGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveConnectionFromGroupRequest(group, connectionId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Remove a connection from the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + /// The cancellation token to use. + public virtual Response RemoveConnectionFromGroup(string group, string connectionId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveConnectionFromGroupRequest(group, connectionId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target connection Id. + private Request CreateRemoveConnectionFromGroupRequest(string group, string connectionId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Delete; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Check if there are any client connections connected for the given user. + /// Target user Id. + /// The cancellation token to use. + public virtual async Task UserExistsAsync(string userId, CancellationToken cancellationToken = default) + { + Request req = CreateUserExistsRequest(userId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Check if there are any client connections connected for the given user. + /// Target user Id. + /// The cancellation token to use. + public virtual Response UserExists(string userId, CancellationToken cancellationToken = default) + { + Request req = CreateUserExistsRequest(userId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target user Id. + private Request CreateUserExistsRequest(string userId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Head; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/users/", false); + uri.AppendPath(userId, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Send content inside request body to the specific user. + /// The user Id. + /// Upload file type. + /// The request body. + /// The cancellation token to use. + public virtual async Task SendToUserAsync(string userId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + { + Request req = CreateSendToUserRequest(userId, contentType, requestBody); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Send content inside request body to the specific user. + /// The user Id. + /// Upload file type. + /// The request body. + /// The cancellation token to use. + public virtual Response SendToUser(string userId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + { + Request req = CreateSendToUserRequest(userId, contentType, requestBody); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The user Id. + /// Upload file type. + /// The request body. + private Request CreateSendToUserRequest(string userId, string contentType, RequestContent requestBody) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Post; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/users/", false); + uri.AppendPath(userId, true); + uri.AppendPath("/:send", false); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + request.Headers.Add("Content-Type", contentType); + request.Content = requestBody; + return request; + } + + /// Add a user to the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + /// The cancellation token to use. + public virtual async Task AddUserToGroupAsync(string group, string userId, CancellationToken cancellationToken = default) + { + Request req = CreateAddUserToGroupRequest(group, userId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Add a user to the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + /// The cancellation token to use. + public virtual Response AddUserToGroup(string group, string userId, CancellationToken cancellationToken = default) + { + Request req = CreateAddUserToGroupRequest(group, userId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + private Request CreateAddUserToGroupRequest(string group, string userId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Put; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/users/", false); + uri.AppendPath(userId, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Remove a user from the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + /// The cancellation token to use. + public virtual async Task RemoveUserFromGroupAsync(string group, string userId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveUserFromGroupRequest(group, userId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Remove a user from the target group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + /// The cancellation token to use. + public virtual Response RemoveUserFromGroup(string group, string userId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveUserFromGroupRequest(group, userId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target group name, which length should be greater than 0 and less than 1025. + /// Target user Id. + private Request CreateRemoveUserFromGroupRequest(string group, string userId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Delete; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/users/", false); + uri.AppendPath(userId, true); + uri.AppendPath("/groups/", false); + uri.AppendPath(group, true); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Remove a user from all groups. + /// Target user Id. + /// The cancellation token to use. + public virtual async Task RemoveUserFromAllGroupsAsync(string userId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveUserFromAllGroupsRequest(userId); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Remove a user from all groups. + /// Target user Id. + /// The cancellation token to use. + public virtual Response RemoveUserFromAllGroups(string userId, CancellationToken cancellationToken = default) + { + Request req = CreateRemoveUserFromAllGroupsRequest(userId); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// Target user Id. + private Request CreateRemoveUserFromAllGroupsRequest(string userId) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Delete; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/users/", false); + uri.AppendPath(userId, true); + uri.AppendPath("/groups", false); + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Grant permission to the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task GrantPermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateGrantPermissionRequest(permission, connectionId, targetName); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Grant permission to the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response GrantPermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateGrantPermissionRequest(permission, connectionId, targetName); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. + private Request CreateGrantPermissionRequest(string permission, string connectionId, string targetName = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Put; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/permissions/", false); + uri.AppendPath(permission, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (targetName != null) + { + uri.AppendQuery("targetName", targetName, true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Revoke permission for the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task RevokePermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateRevokePermissionRequest(permission, connectionId, targetName); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Revoke permission for the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response RevokePermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateRevokePermissionRequest(permission, connectionId, targetName); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. + private Request CreateRevokePermissionRequest(string permission, string connectionId, string targetName = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Delete; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/permissions/", false); + uri.AppendPath(permission, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (targetName != null) + { + uri.AppendQuery("targetName", targetName, true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + + /// Check if a connection has permission to the specified action. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task CheckPermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateCheckPermissionRequest(permission, connectionId, targetName); + return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + } + + /// Check if a connection has permission to the specified action. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response CheckPermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + Request req = CreateCheckPermissionRequest(permission, connectionId, targetName); + return Pipeline.SendRequest(req, cancellationToken); + } + + /// Create Request for and operations. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. + private Request CreateCheckPermissionRequest(string permission, string connectionId, string targetName = null) + { + var message = Pipeline.CreateMessage(); + var request = message.Request; + request.Method = RequestMethod.Head; + var uri = new RawRequestUriBuilder(); + uri.Reset(endpoint); + uri.AppendPath("/api/hubs/", false); + uri.AppendPath(hub, true); + uri.AppendPath("/permissions/", false); + uri.AppendPath(permission, true); + uri.AppendPath("/connections/", false); + uri.AppendPath(connectionId, true); + if (targetName != null) + { + uri.AppendQuery("targetName", targetName, true); + } + if (apiVersion != null) + { + uri.AppendQuery("api-version", apiVersion, true); + } + request.Uri = uri; + return request; + } + } +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClientOptions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClientOptions.cs new file mode 100644 index 000000000000..78fd8fd45bb4 --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClientOptions.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using Azure.Core; + +namespace Azure.Messaging.WebPubSub +{ + /// Client options for WebPubSubServiceClient. + public partial class WebPubSubServiceClientOptions : ClientOptions + { + private const ServiceVersion LatestVersion = ServiceVersion.V2021_05_01_preview; + + /// The version of the service to use. + public enum ServiceVersion + { + /// Service version "2021-05-01-preview". + V2021_05_01_preview = 1, + } + + internal string Version { get; } + + /// Initializes new instance of WebPubSubServiceClientOptions. + public WebPubSubServiceClientOptions(ServiceVersion version = LatestVersion) + { + Version = version switch + { + ServiceVersion.V2021_05_01_preview => "2021-05-01-preview", + _ => throw new NotSupportedException() + }; + } + } +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs new file mode 100644 index 000000000000..0be987b74a02 --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Threading; +using System.Threading.Tasks; +using Azure; +using Azure.Core; +using Azure.Core.Pipeline; + +#pragma warning disable AZC0007 + +namespace Azure.Messaging.WebPubSub +{ + /// The HealthApi service client. + internal partial class HealthApiClient + { + /// Initializes a new instance of HealthApiClient. + /// A credential used to authenticate to an Azure Service. + /// server parameter. + /// The options for configuring the client. + public HealthApiClient(AzureKeyCredential credential, Uri endpoint = null, WebPubSubServiceClientOptions options = null) + { + if (credential == null) + { + throw new ArgumentNullException(nameof(credential)); + } + endpoint ??= new Uri(""); + + options ??= new WebPubSubServiceClientOptions(); + Pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); + this.endpoint = endpoint; + apiVersion = options.Version; + } + } +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient.cs deleted file mode 100644 index 88d4e5778c07..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient.cs +++ /dev/null @@ -1,957 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.Messaging.WebPubSub -{ - /// - /// Azure Web PubSub Service Client. - /// - public partial class WebPubSubServiceClient - { - private const string JsonContent = "application/json"; - private const string TextContent = "text/plain"; - - private readonly Uri _endpoint; - private readonly string _hub; - private readonly ClientDiagnostics _clientDiagnostics; - private readonly AzureKeyCredential _credential; - - internal WebPubSubRestClient RestClient { get; } - - /// - /// Initializes a new instance of the . - /// - /// - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// - public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential credential) : - this(endpoint, hub, credential, new WebPubSubServiceClientOptions()) - { - } - - /// - /// Initializes a new instance of the . - /// - /// - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// - /// - public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential credential, WebPubSubServiceClientOptions options) : - this(endpoint, credential, options) - { - _endpoint = endpoint; - _hub = hub; - _credential = credential; - } - - /// - /// Initializes a new instance of the . - /// - /// Connection string contains Endpoint and AccessKey. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - public WebPubSubServiceClient(string connectionString, string hub): this(ParseConnectionString(connectionString), hub) - { - } - - /// - /// Initializes a new instance of the . - /// - /// Connection string contains Endpoint and AccessKey. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// - public WebPubSubServiceClient(string connectionString, string hub, WebPubSubServiceClientOptions options) : - this(ParseConnectionString(connectionString), hub, options) - { - } - - /// - /// This constructor is intended to be used for mocking. - /// - protected WebPubSubServiceClient() - { - } - - internal WebPubSubServiceClient(Uri endpoint, AzureKeyCredential credential, WebPubSubServiceClientOptions options) - { - if (credential == default) throw new ArgumentNullException(nameof(credential)); - if (options == default) options = new WebPubSubServiceClientOptions(); - - ClientDiagnostics clientDiagnostics = new ClientDiagnostics(options); - HttpPipeline pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); - RestClient = new WebPubSubRestClient(clientDiagnostics, pipeline, endpoint, options.Version); - _clientDiagnostics = clientDiagnostics; - } - - private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub) : - this(parsedConnectionString.Endpoint, hub, parsedConnectionString.Credential) - { - } - - private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub, WebPubSubServiceClientOptions options) : - this(parsedConnectionString.Endpoint, hub, parsedConnectionString.Credential, options) - { - } - - /// Broadcast message to all the connected client connections. - /// - /// Excluded connection Ids. - /// The cancellation token to use. - /// A if successful. - public virtual async Task SendToAllAsync(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope($"{nameof(WebPubSubServiceClient)}.{nameof(SendToAll)}"); - scope.Start(); - try - { - return await RestClient.SendToAllAsync(_hub, TextContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Broadcast message to all the connected client connections. - /// - /// Excluded connection Ids. - /// The cancellation token to use. - /// A if successful. - public virtual Response SendToAll(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToAll"); - scope.Start(); - try - { - return RestClient.SendToAll(_hub, TextContent, RequestContent.Create(message), excluded, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Broadcast message to all the connected client connections. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// Excluded connection Ids. - /// The cancellation token to use. - /// A if successful. - public virtual async Task SendToAllAsync(RequestContent message, string contentType = JsonContent, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToAllAsync"); - scope.Start(); - try - { - return await RestClient.SendToAllAsync(_hub, contentType, message, excluded, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Broadcast message to all the connected client connections. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// Excluded connection Ids. - /// The cancellation token to use. - /// A if successful. - public virtual Response SendToAll(RequestContent message, string contentType = JsonContent, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToAll"); - scope.Start(); - try - { - return RestClient.SendToAll(_hub, contentType, message, excluded, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific user. - /// - /// The user Id. - /// - /// - /// A if successful. - public virtual async Task SendToUserAsync(string userId, string message, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToUserAsync"); - scope.Start(); - try - { - return await RestClient.SendToUserAsync(_hub, userId, TextContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific user. - /// - /// The user Id. - /// - /// - /// A if successful. - public virtual Response SendToUser(string userId, string message, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToUser"); - scope.Start(); - try - { - return RestClient.SendToUser(_hub, userId, TextContent, RequestContent.Create(message), cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific user. - /// - /// The user Id. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// - /// A if successful. - public virtual async Task SendToUserAsync(string userId, RequestContent message, string contentType = JsonContent, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToUserAsync"); - scope.Start(); - try - { - return await RestClient.SendToUserAsync(_hub, userId, contentType, message, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific user. - /// - /// The user Id. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// - /// A if successful. - public virtual Response SendToUser(string userId, RequestContent message, string contentType = JsonContent, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToUser"); - scope.Start(); - try - { - return RestClient.SendToUser(_hub, userId, contentType, message, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific connection. - /// - /// The connection Id. - /// - /// - /// A if successful. - public virtual async Task SendToConnectionAsync(string connectionId, string message, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToConnectionAsync"); - scope.Start(); - try - { - return await RestClient.SendToConnectionAsync(_hub, connectionId, TextContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific connection. - /// - /// The connection Id. - /// - /// - /// A if successful. - public virtual Response SendToConnection(string connectionId, string message, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToConnection"); - scope.Start(); - try - { - return RestClient.SendToConnection(_hub, connectionId, TextContent, RequestContent.Create(message), cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific connection. - /// - /// The connection Id. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// - /// A if successful. - public virtual async Task SendToConnectionAsync(string connectionId, RequestContent message, string contentType = JsonContent, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToConnectionAsync"); - scope.Start(); - try - { - return await RestClient.SendToConnectionAsync(_hub, connectionId, contentType, message, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to the specific connection. - /// - /// The connection Id. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// - /// A if successful. - public virtual Response SendToConnection(string connectionId, RequestContent message, string contentType = JsonContent, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToConnection"); - scope.Start(); - try - { - return RestClient.SendToConnection(_hub, connectionId, contentType, message, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to a group of connections. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// Excluded connection Ids - /// - /// A if successful. - public virtual async Task SendToGroupAsync(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToGroupAsync"); - scope.Start(); - try - { - return await RestClient.SendToGroupAsync(_hub, group, TextContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to a group of connections. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// Excluded connection Ids - /// - /// A if successful. - public virtual Response SendToGroup(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToGroup"); - scope.Start(); - try - { - return RestClient.SendToGroup(_hub, group, TextContent, RequestContent.Create(message), excluded, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to a group of connections. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// Excluded connection Ids - /// - /// A if successful. - public virtual async Task SendToGroupAsync(string group, RequestContent message, string contentType = JsonContent, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToGroupAsync"); - scope.Start(); - try - { - return await RestClient.SendToGroupAsync(_hub, group, contentType, message, excluded, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Send message to a group of connections. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// Supported values are: application/json, application/octet-stream, and text/plain. - /// Excluded connection Ids - /// - /// A if successful. - public virtual Response SendToGroup(string group, RequestContent message, string contentType = JsonContent, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.SendToGroup"); - scope.Start(); - try - { - return RestClient.SendToGroup(_hub, group, contentType, message, excluded, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if the connection with the given connectionId exists - /// The connection Id. - /// - /// true if the specified connection exists; false otherwise. - public virtual async Task> ConnectionExistsAsync(string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.ConnectionExistsAsync"); - scope.Start(); - try - { - var response = await RestClient.ConnectionExistsAsync(_hub, connectionId, cancellationToken).ConfigureAwait(false); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if the connection with the given connectionId exists - /// The connection Id. - /// - /// true if the specified connection exists; false otherwise. - public virtual Response ConnectionExists(string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.ConnectionExists"); - scope.Start(); - try - { - var response = RestClient.ConnectionExists(_hub, connectionId, cancellationToken); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if the connection with the given connectionId exists - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// true if the specified group exists; false otherwise. - public virtual async Task> GroupExistsAsync(string group, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.GroupExistsAsync"); - scope.Start(); - try - { - var response = await RestClient.GroupExistsAsync(_hub, group, cancellationToken).ConfigureAwait(false); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if the connection with the given connectionId exists - /// Target group name, which length should be greater than 0 and less than 1025. - /// - /// true if the specified group exists; false otherwise. - public virtual Response GroupExists(string group, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.GroupExists"); - scope.Start(); - try - { - var response = RestClient.GroupExists(_hub, group, cancellationToken); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if there are any client connections connected for the given user - /// Target user Id - /// - /// true if the specified user exists; false otherwise. - public virtual async Task> UserExistsAsync(string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.UserExistsAsync"); - scope.Start(); - try - { - var response = await RestClient.UserExistsAsync(_hub, userId, cancellationToken).ConfigureAwait(false); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if there are any client connections connected for the given user - /// Target user Id - /// - /// true if the specified user exists; false otherwise. - public virtual Response UserExists(string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.UserExists"); - scope.Start(); - try - { - var response = RestClient.UserExists(_hub, userId, cancellationToken); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Add a connection to the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id - /// - /// A if successful. - public virtual async Task AddConnectionToGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.AddConnectionToGroupAsync"); - scope.Start(); - try - { - return await RestClient.AddConnectionToGroupAsync(_hub, group, connectionId, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Add a connection to the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id - /// - /// A if successful. - public virtual Response AddConnectionToGroup(string group, string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.AddConnectionToGroup"); - scope.Start(); - try - { - return RestClient.AddConnectionToGroup(_hub, group, connectionId, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Remove a user from all groups. - /// Target user Id - /// - /// A if successful. - public virtual async Task RemoveUserFromAllGroupsAsync(string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveUserFromAllGroupsAsync"); - scope.Start(); - try - { - return await RestClient.RemoveUserFromAllGroupsAsync(_hub, userId, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Remove a user from all groups. - /// Target user Id - /// - /// A if successful. - public virtual Response RemoveUserFromAllGroups(string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveUserFromAllGroups"); - scope.Start(); - try - { - return RestClient.RemoveUserFromAllGroups(_hub, userId, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Grant permission to the connection. - /// Current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// - /// A if successful. - public virtual async Task GrantPermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.GrantPermissionAsync"); - scope.Start(); - try - { - return await RestClient.GrantPermissionAsync(_hub, permission, connectionId, targetName, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Grant permission to the connection. - /// Current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// - /// A if successful. - public virtual Response GrantPermission(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.GrantPermission"); - scope.Start(); - try - { - return RestClient.GrantPermission(_hub, permission, connectionId, targetName, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Revoke permission for the connection. - /// Current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// - /// A if successful. - public virtual async Task RevokePermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RevokePermissionAsync"); - scope.Start(); - try - { - return await RestClient.RevokePermissionAsync(_hub, permission, connectionId, targetName, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Revoke permission for the connection. - /// Current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// - /// A if successful. - public virtual Response RevokePermission(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RevokePermission"); - scope.Start(); - try - {; - return RestClient.RevokePermission(_hub, permission, connectionId, targetName, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if a connection have permission to the specific action. - /// Current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// - /// A representing the result of the asynchronous operation. - public virtual async Task> CheckPermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.CheckPermissionAsync"); - scope.Start(); - try - { - var response = await RestClient.CheckPermissionAsync(_hub, permission, connectionId, targetName, cancellationToken).ConfigureAwait(false); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Check if a connection have permission to the specific action. - /// Body of the requerst. Either `joinLeaveGroup` or `sendToGroup`. - /// Target connection Id. - /// If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// - /// A representing the result of the operation. - public virtual Response CheckPermission(WebPubSubPermission permission, string connectionId, string targetName = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.CheckPermission"); - scope.Start(); - try - { - var response = RestClient.CheckPermission(_hub, permission, connectionId, targetName, cancellationToken); - return Response.FromValue(response.Status == 200, response); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Close the client connection. - /// - /// Target connection Id. - /// The reason closing the client connection. - /// - /// A if successful. - public virtual async Task CloseClientConnectionAsync(string connectionId, string reason = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.CloseClientConnectionAsync"); - scope.Start(); - try - { - var response = await RestClient.CloseClientConnectionAsync(_hub, connectionId, reason, cancellationToken).ConfigureAwait(false); - return response; - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Close the client connection. - /// - /// Target connection Id. - /// The reason closing the client connection. - /// - /// A if successful. - public virtual Response CloseClientConnection(string connectionId, string reason = default, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.CloseClientConnection"); - scope.Start(); - try - { - var response = RestClient.CloseClientConnection(_hub, connectionId, reason, cancellationToken); - return response; - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Remove a connection from the target group. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// - /// A if successful. - public virtual async Task RemoveConnectionFromGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveConnectionFromGroupAsync"); - scope.Start(); - try - { - var response = await RestClient.RemoveConnectionFromGroupAsync(_hub, group, connectionId, cancellationToken).ConfigureAwait(false); - return response; - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// Remove a connection from the target group. - /// - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// - /// A if successful. - public virtual Response RemoveConnectionFromGroup(string group, string connectionId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveConnectionFromGroup"); - scope.Start(); - try - { - var response = RestClient.RemoveConnectionFromGroup(_hub, group, connectionId, cancellationToken); - return response; - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Add a user to the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// - /// A if successful. - public virtual async Task AddUserToGroupAsync(string group, string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.AddUserToGroupAsync"); - scope.Start(); - try - { - return await RestClient.AddUserToGroupAsync(_hub, group, userId, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Add a user to the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// - /// A if successful. - public virtual Response AddUserToGroup(string group, string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.AddUserToGroup"); - scope.Start(); - try - { - return RestClient.AddUserToGroup(_hub, group, userId, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Remove a user from the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id - /// - /// A if successful. - public virtual async Task RemoveUserFromGroupAsync(string group, string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveUserFromGroupAsync"); - scope.Start(); - try - { - return await RestClient.RemoveUserFromGroupAsync(_hub, group, userId, cancellationToken).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// Remove a user from the target group. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id - /// - /// A if successful. - public virtual Response RemoveUserFromGroup(string group, string userId, CancellationToken cancellationToken = default) - { - using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceRestClient.RemoveUserFromGroup"); - scope.Start(); - try - { - return RestClient.RemoveUserFromGroup(_hub, group, userId, cancellationToken); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs index 748df1f39fe7..e6bfdba767cc 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs @@ -19,7 +19,7 @@ public static class WebPubSubServiceClientBuilderExtensions public static IAzureClientBuilder AddWebPubSubServiceClient(this TBuilder builder, string connectionString, string hub) where TBuilder : IAzureClientFactoryBuilder { - return builder.RegisterClientFactory(options => new WebPubSubServiceClient(connectionString, hub, options)); + return builder.RegisterClientFactory(options => new WebPubSubServiceClient(hub, connectionString, options)); } /// @@ -28,7 +28,7 @@ public static IAzureClientBuilder AddWebPubSubServiceClient(this TBuilder builder, Uri endpoint, string hub, AzureKeyCredential credential) where TBuilder : IAzureClientFactoryBuilder { - return builder.RegisterClientFactory(options => new WebPubSubServiceClient(endpoint, hub, credential, options)); + return builder.RegisterClientFactory(options => new WebPubSubServiceClient(hub, credential, endpoint, options)); } /// diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.cs deleted file mode 100644 index d5ba3a065d58..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientOptions.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using Azure.Core; - -namespace Azure.Messaging.WebPubSub -{ - /// - /// The options for . - /// - public class WebPubSubServiceClientOptions : ClientOptions - { - internal string Version { get; } - - /// - /// Initializes a new instance of the . - /// - /// Service API version - public WebPubSubServiceClientOptions(ServiceVersion version = ServiceVersion.V2021_05_01) - { - Version = version switch - { - ServiceVersion.V2021_05_01 => "2021-05-01-preview", - _ => throw new ArgumentException($"The service version {version} is not supported by this library.", nameof(version)) - }; - } - - /// - /// The Azure Web PubSub service version. - /// - public enum ServiceVersion - { - /// - /// The 2021.05.01 beta of Azure Web PubSub. - /// -#pragma warning disable CA1707 // Remove the underscores from member name - V2021_05_01 = 1 -#pragma warning restore - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs new file mode 100644 index 000000000000..c5ef301042bd --- /dev/null +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -0,0 +1,173 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Azure.Core; +using Azure.Core.Pipeline; + +namespace Azure.Messaging.WebPubSub +{ + /// + /// Azure Web PubSub Service Client. + /// + public partial class WebPubSubServiceClient + { + private AzureKeyCredential credential; + + private const string JsonContent = "application/json"; + + /// Initializes a new instance of WebPubSubServiceClient. + /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. + /// A credential used to authenticate to an Azure Service. + /// server parameter. + /// The options for configuring the client. + public WebPubSubServiceClient(string hub, AzureKeyCredential credential, Uri endpoint = null, WebPubSubServiceClientOptions options = null) + { + this.credential = credential; + + if (hub == null) + { + throw new ArgumentNullException(nameof(hub)); + } + if (credential == null) + { + throw new ArgumentNullException(nameof(credential)); + } + endpoint ??= new Uri(""); + + options ??= new WebPubSubServiceClientOptions(); + Pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); + this.hub = hub; + this.endpoint = endpoint; + apiVersion = options.Version; + } + + /// + /// Initializes a new instance of the . + /// + /// Connection string contains Endpoint and AccessKey. + /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. + public WebPubSubServiceClient(string connectionString, string hub) : this(ParseConnectionString(connectionString), hub) + { + } + + /// + /// Initializes a new instance of the . + /// + /// Connection string contains Endpoint and AccessKey. + /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. + /// + public WebPubSubServiceClient(string connectionString, string hub, WebPubSubServiceClientOptions options) : + this(ParseConnectionString(connectionString), hub, options) + { + } + + private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub) : + this(hub, parsedConnectionString.Credential, parsedConnectionString.Endpoint) + { + } + + private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub, WebPubSubServiceClientOptions options) : + this(hub, parsedConnectionString.Credential, parsedConnectionString.Endpoint, options) + { + } + + /// Broadcast message to all the connected client connections. + /// + /// Excluded connection Ids. + /// The cancellation token to use. + /// A if successful. + public virtual async Task SendToAllAsync(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + return await SendToAllAsync(JsonContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); + } + + /// Broadcast message to all the connected client connections. + /// + /// Excluded connection Ids. + /// The cancellation token to use. + /// A if successful. + public virtual Response SendToAll(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + return SendToAll(JsonContent, RequestContent.Create(message), excluded, cancellationToken); + } + + /// + /// Send message to the specific user. + /// + /// The user Id. + /// + /// + /// A if successful. + public virtual async Task SendToUserAsync(string userId, string message, CancellationToken cancellationToken = default) + { + return await SendToUserAsync(userId, JsonContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); + } + + /// + /// Send message to the specific user. + /// + /// The user Id. + /// + /// + /// A if successful. + public virtual Response SendToUser(string userId, string message, CancellationToken cancellationToken = default) + { + return SendToUser(userId, JsonContent, RequestContent.Create(message), cancellationToken); + } + + /// + /// Send message to the specific connection. + /// + /// The connection Id. + /// + /// + /// A if successful. + public virtual async Task SendToConnectionAsync(string connectionId, string message, CancellationToken cancellationToken = default) + { + return await SendToConnectionAsync(connectionId, JsonContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); + } + + /// + /// Send message to the specific connection. + /// + /// The connection Id. + /// + /// + /// A if successful. + public virtual Response SendToConnection(string connectionId, string message, CancellationToken cancellationToken = default) + { + return SendToConnection(connectionId, JsonContent, RequestContent.Create(message), cancellationToken); + } + + /// + /// Send message to a group of connections. + /// + /// Target group name, which length should be greater than 0 and less than 1025. + /// + /// Excluded connection Ids + /// + /// A if successful. + public virtual async Task SendToGroupAsync(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + return await SendToGroupAsync(group, JsonContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); + } + + /// + /// Send message to a group of connections. + /// + /// Target group name, which length should be greater than 0 and less than 1025. + /// + /// Excluded connection Ids + /// + /// A if successful. + public virtual Response SendToGroup(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) + { + return SendToGroup(group, JsonContent, RequestContent.Create(message), excluded, cancellationToken); + } + } +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index 3180b0da964c..e2a311cd425d 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Security.Claims; +using Azure.Core.Pipeline; namespace Azure.Messaging.WebPubSub { @@ -42,18 +43,24 @@ public virtual Uri GetClientAccessUri(string userId = default, string[] roles = claims.Add(role); } - string endpoint = _endpoint.AbsoluteUri; + string endpoint = this.endpoint.AbsoluteUri; if (!endpoint.EndsWith("/", StringComparison.Ordinal)) { endpoint += "/"; } - var audience = $"{endpoint}client/hubs/{_hub}"; + var audience = $"{endpoint}client/hubs/{hub}"; - string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, _credential, expireAfter); + // this is a hack. The generator needs to store the credential in a field. + if (credential == null) + { + HttpPipeline pipeline = Pipeline; + } + + string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, credential, expireAfter); var clientEndpoint = new UriBuilder(endpoint); clientEndpoint.Scheme = "wss"; - var uriString = $"{clientEndpoint}client/hubs/{_hub}?access_token={token}"; + var uriString = $"{clientEndpoint}client/hubs/{hub}?access_token={token}"; return new Uri(uriString); } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md index 9a5de4ca78f0..c1d12ec5e5da 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/autorest.md @@ -3,8 +3,12 @@ Run `dotnet build /t:GenerateCode` to generate code. ``` yaml +title: WebPubSubServiceClient input-file: - $(this-folder)/swagger/WebPubSub.json +low-level-client: true +credential-types: AzureKeyCredential +credential-header-name: Ocp-Apim-Subscription-Key ``` ### Make WebPubSubPermission a regular enum diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json index d9a0e53140b5..c9729aca2284 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json @@ -38,7 +38,7 @@ "webpubsub" ], "summary": "Broadcast content inside request body to all the connected client connections.", - "operationId": "WebPubSub_SendToAll", + "operationId": "WebPubSubService_SendToAll", "consumes": [ "application/octet-stream", "text/plain", @@ -51,7 +51,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "query", @@ -98,7 +99,7 @@ "webpubsub" ], "summary": "Check if the connection with the given connectionId exists.", - "operationId": "WebPubSub_ConnectionExists", + "operationId": "WebPubSubService_ConnectionExists", "parameters": [ { "in": "path", @@ -106,7 +107,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -142,7 +144,7 @@ "webpubsub" ], "summary": "Close the client connection.", - "operationId": "WebPubSub_CloseClientConnection", + "operationId": "WebPubSubService_CloseClientConnection", "parameters": [ { "in": "path", @@ -150,7 +152,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -191,7 +194,7 @@ "webpubsub" ], "summary": "Send content inside request body to the specific connection.", - "operationId": "WebPubSub_SendToConnection", + "operationId": "WebPubSubService_SendToConnection", "consumes": [ "application/octet-stream", "text/plain", @@ -204,7 +207,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -249,7 +253,7 @@ "webpubsub" ], "summary": "Check if there are any client connections inside the given group", - "operationId": "WebPubSub_GroupExists", + "operationId": "WebPubSubService_GroupExists", "parameters": [ { "in": "path", @@ -257,7 +261,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -296,7 +301,7 @@ "webpubsub" ], "summary": "Send content inside request body to a group of connections.", - "operationId": "WebPubSub_SendToGroup", + "operationId": "WebPubSubService_SendToGroup", "consumes": [ "application/octet-stream", "text/plain", @@ -309,7 +314,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -365,7 +371,7 @@ "webpubsub" ], "summary": "Add a connection to the target group.", - "operationId": "WebPubSub_AddConnectionToGroup", + "operationId": "WebPubSubService_AddConnectionToGroup", "parameters": [ { "in": "path", @@ -373,7 +379,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -418,7 +425,7 @@ "webpubsub" ], "summary": "Remove a connection from the target group.", - "operationId": "WebPubSub_RemoveConnectionFromGroup", + "operationId": "WebPubSubService_RemoveConnectionFromGroup", "parameters": [ { "in": "path", @@ -426,7 +433,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -470,7 +478,7 @@ "webpubsub" ], "summary": "Check if there are any client connections connected for the given user.", - "operationId": "WebPubSub_UserExists", + "operationId": "WebPubSubService_UserExists", "parameters": [ { "in": "path", @@ -478,7 +486,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -516,7 +525,7 @@ "webpubsub" ], "summary": "Send content inside request body to the specific user.", - "operationId": "WebPubSub_SendToUser", + "operationId": "WebPubSubService_SendToUser", "consumes": [ "application/octet-stream", "text/plain", @@ -529,7 +538,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -574,7 +584,7 @@ "webpubsub" ], "summary": "Add a user to the target group.", - "operationId": "WebPubSub_AddUserToGroup", + "operationId": "WebPubSubService_AddUserToGroup", "parameters": [ { "in": "path", @@ -582,7 +592,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -627,7 +638,7 @@ "webpubsub" ], "summary": "Remove a user from the target group.", - "operationId": "WebPubSub_RemoveUserFromGroup", + "operationId": "WebPubSubService_RemoveUserFromGroup", "parameters": [ { "in": "path", @@ -635,7 +646,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -679,7 +691,7 @@ "webpubsub" ], "summary": "Remove a user from all groups.", - "operationId": "WebPubSub_RemoveUserFromAllGroups", + "operationId": "WebPubSubService_RemoveUserFromAllGroups", "parameters": [ { "in": "path", @@ -687,7 +699,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -722,7 +735,7 @@ "webpubsub" ], "summary": "Grant permission to the connection.", - "operationId": "WebPubSub_GrantPermission", + "operationId": "WebPubSubService_GrantPermission", "parameters": [ { "in": "path", @@ -730,7 +743,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -784,7 +798,7 @@ "webpubsub" ], "summary": "Revoke permission for the connection.", - "operationId": "WebPubSub_RevokePermission", + "operationId": "WebPubSubService_RevokePermission", "parameters": [ { "in": "path", @@ -792,7 +806,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -846,7 +861,7 @@ "webpubsub" ], "summary": "Check if a connection has permission to the specified action.", - "operationId": "WebPubSub_CheckPermission", + "operationId": "WebPubSubService_CheckPermission", "parameters": [ { "in": "path", @@ -854,7 +869,8 @@ "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore.", "required": true, "type": "string", - "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$" + "pattern": "^[A-Za-z][A-Za-z0-9_`,.[\\]]{0,127}$", + "x-ms-parameter-location": "client" }, { "in": "path", @@ -908,4 +924,4 @@ } } } -} \ No newline at end of file +} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs index 958a5098d03f..6fb15b2b552e 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs @@ -20,7 +20,7 @@ public void HelloWorld() var key = TestEnvironment.Key; #region Snippet:WebPubSubHelloWorld - var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); serviceClient.SendToAll("Hello World!"); #endregion @@ -32,7 +32,7 @@ public void Authenticate() var key = TestEnvironment.Key; #region Snippet:WebPubSubAuthenticate - var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); #endregion } @@ -53,9 +53,9 @@ public void JsonMessage() var key = TestEnvironment.Key; #region Snippet:WebPubSubSendJson - var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); - serviceClient.SendToAll( + serviceClient.SendToAll("application/json", RequestContent.Create( new { @@ -71,12 +71,10 @@ public void BinaryMessage() var key = TestEnvironment.Key; #region Snippet:WebPubSubSendBinary - var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); Stream stream = BinaryData.FromString("Hello World!").ToStream(); - serviceClient.SendToAll( - RequestContent.Create(stream), - HttpHeader.Common.OctetStreamContentType.Value); + serviceClient.SendToAll("application/octet-stream", RequestContent.Create(stream)); #endregion } @@ -86,12 +84,12 @@ public void AddUserToGroup() var key = TestEnvironment.Key; #region Snippet:WebPubAddUserToGroup - var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); + var client = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); client.AddUserToGroup("some_group", "some_user"); // Avoid sending messages to users who do not exist. - if (client.UserExists("some_user")) + if (client.UserExists("some_user").Content.ToObjectFromJson()) { client.SendToUser("some_user", "Hi, I am glad you exist!"); } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs index 0e9fbeef75e7..8224ecd39235 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs @@ -2,11 +2,7 @@ // Licensed under the MIT License. using System; -using System.Collections.Concurrent; -using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; -using System.Linq; -using System.Threading.Tasks; using System.Web; using NUnit.Framework; From 5abae0cb6445587c5ea5f4b05dfbb5f4081d28f6 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Mon, 10 May 2021 09:35:56 -0700 Subject: [PATCH 2/9] Small changes --- .../src/WebPubSubServiceClient_extensions.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index c5ef301042bd..6f36702f7e21 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -20,6 +20,16 @@ public partial class WebPubSubServiceClient private const string JsonContent = "application/json"; + /// + /// The hub. + /// + public string Hub => hub; + + /// + /// The service endpoint. + /// + public Uri Endpoint => endpoint; + /// Initializes a new instance of WebPubSubServiceClient. /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. /// A credential used to authenticate to an Azure Service. From 85ebf55b87cddf2235677b43f141ae09f6a60932 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 13 May 2021 09:09:46 -0700 Subject: [PATCH 3/9] Moved to LLC + extensions --- .../src/Generated/HealthApiClient.cs | 87 +- .../src/Generated/WebPubSubRestClient.cs | 1711 ----------------- .../src/Generated/WebPubSubServiceClient.cs | 1344 +++++++++++-- .../src/HealthApiClient_extensions.cs | 4 +- .../src/WebPubSubServiceClient_extensions.cs | 49 +- 5 files changed, 1299 insertions(+), 1896 deletions(-) delete mode 100644 sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs index 369814ca8faa..ddefd9818f9f 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs @@ -6,14 +6,11 @@ #nullable disable using System; -using System.Threading; using System.Threading.Tasks; using Azure; using Azure.Core; using Azure.Core.Pipeline; -#pragma warning disable AZC0007 - namespace Azure.Messaging.WebPubSub { /// The HealthApi service client. @@ -24,6 +21,7 @@ internal partial class HealthApiClient private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; private Uri endpoint; private readonly string apiVersion; + private readonly ClientDiagnostics _clientDiagnostics; /// Initializes a new instance of HealthApiClient for mocking. protected HealthApiClient() @@ -31,23 +29,86 @@ protected HealthApiClient() } /// Get service health status. - /// The cancellation token to use. - public virtual async Task GetServiceStatusAsync(CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task GetServiceStatusAsync(RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGetServiceStatusRequest(); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGetServiceStatusRequest(requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("HealthApiClient.GetServiceStatus"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Get service health status. - /// The cancellation token to use. - public virtual Response GetServiceStatus(CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response GetServiceStatus(RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGetServiceStatusRequest(); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGetServiceStatusRequest(requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("HealthApiClient.GetServiceStatus"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. - private Request CreateGetServiceStatusRequest() + /// The request options. + private HttpMessage CreateGetServiceStatusRequest(RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -60,7 +121,7 @@ private Request CreateGetServiceStatusRequest() uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs deleted file mode 100644 index 692a456341e0..000000000000 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubRestClient.cs +++ /dev/null @@ -1,1711 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; -using Azure.Messaging.WebPubSub.Models; - -namespace Azure.Messaging.WebPubSub -{ - internal partial class WebPubSubRestClient - { - private Uri endpoint; - private string apiVersion; - private ClientDiagnostics _clientDiagnostics; - private HttpPipeline _pipeline; - - /// Initializes a new instance of WebPubSubRestClient. - /// The handler for diagnostic messaging in the client. - /// The HTTP pipeline for sending and receiving REST requests and responses. - /// server parameter. - /// Api Version. - public WebPubSubRestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null, string apiVersion = "2021-05-01-preview") - { - endpoint ??= new Uri(""); - - this.endpoint = endpoint; - this.apiVersion = apiVersion; - _clientDiagnostics = clientDiagnostics; - _pipeline = pipeline; - } - - internal HttpMessage CreateSendToAllRequest(string hub, Models.ContentType contentType, RequestContent message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public async Task SendToAllAsync(string hub, Models.ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, contentType, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public Response SendToAll(string hub, Models.ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, contentType, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToAllRequest(string hub, string message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public async Task SendToAllAsync(string hub, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Broadcast content inside request body to all the connected client connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// or is null. - public Response SendToAll(string hub, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToAllRequest(hub, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateConnectionExistsRequest(string hub, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if the connection with the given connectionId exists. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The cancellation token to use. - /// or is null. - public async Task ConnectionExistsAsync(string hub, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateConnectionExistsRequest(hub, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if the connection with the given connectionId exists. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The cancellation token to use. - /// or is null. - public Response ConnectionExists(string hub, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateConnectionExistsRequest(hub, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateCloseClientConnectionRequest(string hub, string connectionId, string reason) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (reason != null) - { - uri.AppendQuery("reason", reason, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Close the client connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target connection Id. - /// The reason closing the client connection. - /// The cancellation token to use. - /// or is null. - public async Task CloseClientConnectionAsync(string hub, string connectionId, string reason = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCloseClientConnectionRequest(hub, connectionId, reason); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Close the client connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target connection Id. - /// The reason closing the client connection. - /// The cancellation token to use. - /// or is null. - public Response CloseClientConnection(string hub, string connectionId, string reason = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCloseClientConnectionRequest(hub, connectionId, reason); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToConnectionRequest(string hub, string connectionId, Models.ContentType contentType, RequestContent message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToConnectionAsync(string hub, string connectionId, Models.ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, contentType, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToConnection(string hub, string connectionId, Models.ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, contentType, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToConnectionRequest(string hub, string connectionId, string message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToConnectionAsync(string hub, string connectionId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The connection Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToConnection(string hub, string connectionId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToConnectionRequest(hub, connectionId, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateGroupExistsRequest(string hub, string group) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if there are any client connections inside the given group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - /// or is null. - public async Task GroupExistsAsync(string hub, string group, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - - using var message = CreateGroupExistsRequest(hub, group); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if there are any client connections inside the given group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - /// or is null. - public Response GroupExists(string hub, string group, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - - using var message = CreateGroupExistsRequest(hub, group); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToGroupRequest(string hub, string group, Models.ContentType contentType, RequestContent message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToGroupAsync(string hub, string group, Models.ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, contentType, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Upload file type. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public Response SendToGroup(string hub, string group, Models.ContentType contentType, RequestContent message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, contentType, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToGroupRequest(string hub, string group, string message, IEnumerable excluded) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/:send", false); - if (excluded != null) - { - uri.AppendQueryDelimited("excluded", excluded, ",", true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToGroupAsync(string hub, string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, message, excluded); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to a group of connections. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// The payload body. - /// Excluded connection Ids. - /// The cancellation token to use. - /// , , or is null. - public Response SendToGroup(string hub, string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToGroupRequest(hub, group, message, excluded); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateAddConnectionToGroupRequest(string hub, string group, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Add a connection to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public async Task AddConnectionToGroupAsync(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateAddConnectionToGroupRequest(hub, group, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Add a connection to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public Response AddConnectionToGroup(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateAddConnectionToGroupRequest(hub, group, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveConnectionFromGroupRequest(string hub, string group, string connectionId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a connection from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public async Task RemoveConnectionFromGroupAsync(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRemoveConnectionFromGroupRequest(hub, group, connectionId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a connection from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target connection Id. - /// The cancellation token to use. - /// , , or is null. - public Response RemoveConnectionFromGroup(string hub, string group, string connectionId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRemoveConnectionFromGroupRequest(hub, group, connectionId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateUserExistsRequest(string hub, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if there are any client connections connected for the given user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public async Task UserExistsAsync(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateUserExistsRequest(hub, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if there are any client connections connected for the given user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public Response UserExists(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateUserExistsRequest(hub, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateSendToUserRequest(string hub, string userId, Models.ContentType contentType, RequestContent message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", contentType.ToString()); - request.Content = message; - return message0; - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToUserAsync(string hub, string userId, Models.ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, contentType, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// Upload file type. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToUser(string hub, string userId, Models.ContentType contentType, RequestContent message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, contentType, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateSendToUserRequest(string hub, string userId, string message) - { - var message0 = _pipeline.CreateMessage(); - var request = message0.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/:send", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - request.Headers.Add("Content-Type", "text/plain"); - request.Content = new StringRequestContent(message); - return message0; - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public async Task SendToUserAsync(string hub, string userId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, message); - await _pipeline.SendAsync(message0, cancellationToken).ConfigureAwait(false); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message0.Response).ConfigureAwait(false); - } - } - - /// Send content inside request body to the specific user. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The user Id. - /// The payload body. - /// The cancellation token to use. - /// , , or is null. - public Response SendToUser(string hub, string userId, string message, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - if (message == null) - { - throw new ArgumentNullException(nameof(message)); - } - - using var message0 = CreateSendToUserRequest(hub, userId, message); - _pipeline.Send(message0, cancellationToken); - switch (message0.Response.Status) - { - case 202: - return message0.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message0.Response); - } - } - - internal HttpMessage CreateAddUserToGroupRequest(string hub, string group, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Add a user to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public async Task AddUserToGroupAsync(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateAddUserToGroupRequest(hub, group, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Add a user to the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public Response AddUserToGroup(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateAddUserToGroupRequest(hub, group, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveUserFromGroupRequest(string hub, string group, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups/", false); - uri.AppendPath(group, true); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a user from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public async Task RemoveUserFromGroupAsync(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromGroupRequest(hub, group, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a user from the target group. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target group name, which length should be greater than 0 and less than 1025. - /// Target user Id. - /// The cancellation token to use. - /// , , or is null. - public Response RemoveUserFromGroup(string hub, string group, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (group == null) - { - throw new ArgumentNullException(nameof(group)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromGroupRequest(hub, group, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRemoveUserFromAllGroupsRequest(string hub, string userId) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/users/", false); - uri.AppendPath(userId, true); - uri.AppendPath("/groups", false); - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Remove a user from all groups. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public async Task RemoveUserFromAllGroupsAsync(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromAllGroupsRequest(hub, userId); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Remove a user from all groups. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// Target user Id. - /// The cancellation token to use. - /// or is null. - public Response RemoveUserFromAllGroups(string hub, string userId, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (userId == null) - { - throw new ArgumentNullException(nameof(userId)); - } - - using var message = CreateRemoveUserFromAllGroupsRequest(hub, userId); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateGrantPermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Put; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Grant permission to the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task GrantPermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateGrantPermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Grant permission to the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response GrantPermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateGrantPermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateRevokePermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Delete; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Revoke permission for the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task RevokePermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRevokePermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Revoke permission for the connection. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response RevokePermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateRevokePermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - - internal HttpMessage CreateCheckPermissionRequest(string hub, WebPubSubPermission permission, string connectionId, string targetName) - { - var message = _pipeline.CreateMessage(); - var request = message.Request; - request.Method = RequestMethod.Head; - var uri = new RawRequestUriBuilder(); - uri.Reset(endpoint); - uri.AppendPath("/api/hubs/", false); - uri.AppendPath(hub, true); - uri.AppendPath("/permissions/", false); - uri.AppendPath(permission.ToSerialString(), true); - uri.AppendPath("/connections/", false); - uri.AppendPath(connectionId, true); - if (targetName != null) - { - uri.AppendQuery("targetName", targetName, true); - } - if (apiVersion != null) - { - uri.AppendQuery("api-version", apiVersion, true); - } - request.Uri = uri; - return message; - } - - /// Check if a connection has permission to the specified action. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public async Task CheckPermissionAsync(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCheckPermissionRequest(hub, permission, connectionId, targetName); - await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); - } - } - - /// Check if a connection has permission to the specified action. - /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. - /// The permission: current supported actions are joinLeaveGroup and sendToGroup. - /// Target connection Id. - /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - /// or is null. - public Response CheckPermission(string hub, WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) - { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (connectionId == null) - { - throw new ArgumentNullException(nameof(connectionId)); - } - - using var message = CreateCheckPermissionRequest(hub, permission, connectionId, targetName); - _pipeline.Send(message, cancellationToken); - switch (message.Response.Status) - { - case 200: - case 404: - return message.Response; - default: - throw _clientDiagnostics.CreateRequestFailedException(message.Response); - } - } - } -} diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs index 3f70ddf6fd0f..4b80a69f6084 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -7,14 +7,11 @@ using System; using System.Collections.Generic; -using System.Threading; using System.Threading.Tasks; using Azure; using Azure.Core; using Azure.Core.Pipeline; -#pragma warning disable AZC0007 - namespace Azure.Messaging.WebPubSub { /// The WebPubSubService service client. @@ -26,6 +23,7 @@ public partial class WebPubSubServiceClient private string hub; private Uri endpoint; private readonly string apiVersion; + private readonly ClientDiagnostics _clientDiagnostics; /// Initializes a new instance of WebPubSubServiceClient for mocking. protected WebPubSubServiceClient() @@ -36,29 +34,92 @@ protected WebPubSubServiceClient() /// Upload file type. /// The request body. /// Excluded connection Ids. - /// The cancellation token to use. - public virtual async Task SendToAllAsync(string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task SendToAllAsync(string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToAllRequest(contentType, requestBody, excluded); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToAllRequest(contentType, requestBody, excluded, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToAll"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Broadcast content inside request body to all the connected client connections. /// Upload file type. /// The request body. /// Excluded connection Ids. - /// The cancellation token to use. - public virtual Response SendToAll(string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response SendToAll(string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToAllRequest(contentType, requestBody, excluded); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToAllRequest(contentType, requestBody, excluded, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToAll"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Upload file type. /// The request body. /// Excluded connection Ids. - private Request CreateSendToAllRequest(string contentType, RequestContent requestBody, IEnumerable excluded = null) + /// The request options. + private HttpMessage CreateSendToAllRequest(string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -79,30 +140,95 @@ private Request CreateSendToAllRequest(string contentType, RequestContent reques request.Uri = uri; request.Headers.Add("Content-Type", contentType); request.Content = requestBody; - return request; + return message; } /// Check if the connection with the given connectionId exists. /// The connection Id. - /// The cancellation token to use. - public virtual async Task ConnectionExistsAsync(string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task ConnectionExistsAsync(string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateConnectionExistsRequest(connectionId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateConnectionExistsRequest(connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.ConnectionExists"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Check if the connection with the given connectionId exists. /// The connection Id. - /// The cancellation token to use. - public virtual Response ConnectionExists(string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response ConnectionExists(string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateConnectionExistsRequest(connectionId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateConnectionExistsRequest(connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.ConnectionExists"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The connection Id. - private Request CreateConnectionExistsRequest(string connectionId) + /// The request options. + private HttpMessage CreateConnectionExistsRequest(string connectionId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -118,33 +244,96 @@ private Request CreateConnectionExistsRequest(string connectionId) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Close the client connection. /// Target connection Id. /// The reason closing the client connection. - /// The cancellation token to use. - public virtual async Task CloseClientConnectionAsync(string connectionId, string reason = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task CloseClientConnectionAsync(string connectionId, string reason = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateCloseClientConnectionRequest(connectionId, reason); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateCloseClientConnectionRequest(connectionId, reason, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.CloseClientConnection"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Close the client connection. /// Target connection Id. /// The reason closing the client connection. - /// The cancellation token to use. - public virtual Response CloseClientConnection(string connectionId, string reason = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response CloseClientConnection(string connectionId, string reason = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateCloseClientConnectionRequest(connectionId, reason); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateCloseClientConnectionRequest(connectionId, reason, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.CloseClientConnection"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target connection Id. /// The reason closing the client connection. - private Request CreateCloseClientConnectionRequest(string connectionId, string reason = null) + /// The request options. + private HttpMessage CreateCloseClientConnectionRequest(string connectionId, string reason = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -164,36 +353,99 @@ private Request CreateCloseClientConnectionRequest(string connectionId, string r uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Send content inside request body to the specific connection. /// The connection Id. /// Upload file type. /// The request body. - /// The cancellation token to use. - public virtual async Task SendToConnectionAsync(string connectionId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task SendToConnectionAsync(string connectionId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToConnectionRequest(connectionId, contentType, requestBody); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToConnectionRequest(connectionId, contentType, requestBody, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToConnection"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Send content inside request body to the specific connection. /// The connection Id. /// Upload file type. /// The request body. - /// The cancellation token to use. - public virtual Response SendToConnection(string connectionId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response SendToConnection(string connectionId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToConnectionRequest(connectionId, contentType, requestBody); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToConnectionRequest(connectionId, contentType, requestBody, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToConnection"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The connection Id. /// Upload file type. /// The request body. - private Request CreateSendToConnectionRequest(string connectionId, string contentType, RequestContent requestBody) + /// The request options. + private HttpMessage CreateSendToConnectionRequest(string connectionId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -212,30 +464,95 @@ private Request CreateSendToConnectionRequest(string connectionId, string conten request.Uri = uri; request.Headers.Add("Content-Type", contentType); request.Content = requestBody; - return request; + return message; } /// Check if there are any client connections inside the given group. /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - public virtual async Task GroupExistsAsync(string group, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + internal virtual async Task GroupExistsAsync(string group, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGroupExistsRequest(group); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGroupExistsRequest(group, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.GroupExists"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Check if there are any client connections inside the given group. /// Target group name, which length should be greater than 0 and less than 1025. - /// The cancellation token to use. - public virtual Response GroupExists(string group, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + internal virtual Response GroupExists(string group, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGroupExistsRequest(group); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGroupExistsRequest(group, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.GroupExists"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target group name, which length should be greater than 0 and less than 1025. - private Request CreateGroupExistsRequest(string group) + /// The request options. + private HttpMessage CreateGroupExistsRequest(string group, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -251,7 +568,7 @@ private Request CreateGroupExistsRequest(string group) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Send content inside request body to a group of connections. @@ -259,11 +576,42 @@ private Request CreateGroupExistsRequest(string group) /// Upload file type. /// The request body. /// Excluded connection Ids. - /// The cancellation token to use. - public virtual async Task SendToGroupAsync(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task SendToGroupAsync(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToGroupRequest(group, contentType, requestBody, excluded); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToGroupRequest(group, contentType, requestBody, excluded, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToGroup"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Send content inside request body to a group of connections. @@ -271,11 +619,42 @@ public virtual async Task SendToGroupAsync(string group, string conten /// Upload file type. /// The request body. /// Excluded connection Ids. - /// The cancellation token to use. - public virtual Response SendToGroup(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response SendToGroup(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToGroupRequest(group, contentType, requestBody, excluded); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToGroupRequest(group, contentType, requestBody, excluded, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToGroup"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. @@ -283,7 +662,8 @@ public virtual Response SendToGroup(string group, string contentType, RequestCon /// Upload file type. /// The request body. /// Excluded connection Ids. - private Request CreateSendToGroupRequest(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null) + /// The request options. + private HttpMessage CreateSendToGroupRequest(string group, string contentType, RequestContent requestBody, IEnumerable excluded = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -306,33 +686,98 @@ private Request CreateSendToGroupRequest(string group, string contentType, Reque request.Uri = uri; request.Headers.Add("Content-Type", contentType); request.Content = requestBody; - return request; + return message; } /// Add a connection to the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - /// The cancellation token to use. - public virtual async Task AddConnectionToGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task AddConnectionToGroupAsync(string group, string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateAddConnectionToGroupRequest(group, connectionId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateAddConnectionToGroupRequest(group, connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.AddConnectionToGroup"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Add a connection to the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - /// The cancellation token to use. - public virtual Response AddConnectionToGroup(string group, string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response AddConnectionToGroup(string group, string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateAddConnectionToGroupRequest(group, connectionId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateAddConnectionToGroupRequest(group, connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.AddConnectionToGroup"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - private Request CreateAddConnectionToGroupRequest(string group, string connectionId) + /// The request options. + private HttpMessage CreateAddConnectionToGroupRequest(string group, string connectionId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -350,33 +795,96 @@ private Request CreateAddConnectionToGroupRequest(string group, string connectio uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Remove a connection from the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - /// The cancellation token to use. - public virtual async Task RemoveConnectionFromGroupAsync(string group, string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task RemoveConnectionFromGroupAsync(string group, string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveConnectionFromGroupRequest(group, connectionId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveConnectionFromGroupRequest(group, connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveConnectionFromGroup"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Remove a connection from the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - /// The cancellation token to use. - public virtual Response RemoveConnectionFromGroup(string group, string connectionId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response RemoveConnectionFromGroup(string group, string connectionId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveConnectionFromGroupRequest(group, connectionId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveConnectionFromGroupRequest(group, connectionId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveConnectionFromGroup"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target group name, which length should be greater than 0 and less than 1025. /// Target connection Id. - private Request CreateRemoveConnectionFromGroupRequest(string group, string connectionId) + /// The request options. + private HttpMessage CreateRemoveConnectionFromGroupRequest(string group, string connectionId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -394,30 +902,95 @@ private Request CreateRemoveConnectionFromGroupRequest(string group, string conn uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Check if there are any client connections connected for the given user. /// Target user Id. - /// The cancellation token to use. - public virtual async Task UserExistsAsync(string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task UserExistsAsync(string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateUserExistsRequest(userId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateUserExistsRequest(userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.UserExists"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Check if there are any client connections connected for the given user. /// Target user Id. - /// The cancellation token to use. - public virtual Response UserExists(string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response UserExists(string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateUserExistsRequest(userId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateUserExistsRequest(userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.UserExists"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target user Id. - private Request CreateUserExistsRequest(string userId) + /// The request options. + private HttpMessage CreateUserExistsRequest(string userId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -433,36 +1006,99 @@ private Request CreateUserExistsRequest(string userId) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Send content inside request body to the specific user. /// The user Id. /// Upload file type. /// The request body. - /// The cancellation token to use. - public virtual async Task SendToUserAsync(string userId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task SendToUserAsync(string userId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToUserRequest(userId, contentType, requestBody); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToUserRequest(userId, contentType, requestBody, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToUser"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Send content inside request body to the specific user. /// The user Id. /// Upload file type. /// The request body. - /// The cancellation token to use. - public virtual Response SendToUser(string userId, string contentType, RequestContent requestBody, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response SendToUser(string userId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateSendToUserRequest(userId, contentType, requestBody); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateSendToUserRequest(userId, contentType, requestBody, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.SendToUser"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 202: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The user Id. /// Upload file type. /// The request body. - private Request CreateSendToUserRequest(string userId, string contentType, RequestContent requestBody) + /// The request options. + private HttpMessage CreateSendToUserRequest(string userId, string contentType, RequestContent requestBody, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -481,33 +1117,98 @@ private Request CreateSendToUserRequest(string userId, string contentType, Reque request.Uri = uri; request.Headers.Add("Content-Type", contentType); request.Content = requestBody; - return request; + return message; } /// Add a user to the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - /// The cancellation token to use. - public virtual async Task AddUserToGroupAsync(string group, string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task AddUserToGroupAsync(string group, string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateAddUserToGroupRequest(group, userId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateAddUserToGroupRequest(group, userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.AddUserToGroup"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Add a user to the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - /// The cancellation token to use. - public virtual Response AddUserToGroup(string group, string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response AddUserToGroup(string group, string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateAddUserToGroupRequest(group, userId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateAddUserToGroupRequest(group, userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.AddUserToGroup"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - private Request CreateAddUserToGroupRequest(string group, string userId) + /// The request options. + private HttpMessage CreateAddUserToGroupRequest(string group, string userId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -525,33 +1226,96 @@ private Request CreateAddUserToGroupRequest(string group, string userId) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Remove a user from the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - /// The cancellation token to use. - public virtual async Task RemoveUserFromGroupAsync(string group, string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task RemoveUserFromGroupAsync(string group, string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveUserFromGroupRequest(group, userId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveUserFromGroupRequest(group, userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveUserFromGroup"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Remove a user from the target group. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - /// The cancellation token to use. - public virtual Response RemoveUserFromGroup(string group, string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response RemoveUserFromGroup(string group, string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveUserFromGroupRequest(group, userId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveUserFromGroupRequest(group, userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveUserFromGroup"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target group name, which length should be greater than 0 and less than 1025. /// Target user Id. - private Request CreateRemoveUserFromGroupRequest(string group, string userId) + /// The request options. + private HttpMessage CreateRemoveUserFromGroupRequest(string group, string userId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -569,30 +1333,93 @@ private Request CreateRemoveUserFromGroupRequest(string group, string userId) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Remove a user from all groups. /// Target user Id. - /// The cancellation token to use. - public virtual async Task RemoveUserFromAllGroupsAsync(string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task RemoveUserFromAllGroupsAsync(string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveUserFromAllGroupsRequest(userId); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveUserFromAllGroupsRequest(userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveUserFromAllGroups"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Remove a user from all groups. /// Target user Id. - /// The cancellation token to use. - public virtual Response RemoveUserFromAllGroups(string userId, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response RemoveUserFromAllGroups(string userId, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRemoveUserFromAllGroupsRequest(userId); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRemoveUserFromAllGroupsRequest(userId, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RemoveUserFromAllGroups"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// Target user Id. - private Request CreateRemoveUserFromAllGroupsRequest(string userId) + /// The request options. + private HttpMessage CreateRemoveUserFromAllGroupsRequest(string userId, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -609,36 +1436,99 @@ private Request CreateRemoveUserFromAllGroupsRequest(string userId) uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Grant permission to the connection. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual async Task GrantPermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task GrantPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGrantPermissionRequest(permission, connectionId, targetName); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGrantPermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.GrantPermission"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Grant permission to the connection. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual Response GrantPermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response GrantPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateGrantPermissionRequest(permission, connectionId, targetName); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateGrantPermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.GrantPermission"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. - private Request CreateGrantPermissionRequest(string permission, string connectionId, string targetName = null) + /// The request options. + private HttpMessage CreateGrantPermissionRequest(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -660,36 +1550,99 @@ private Request CreateGrantPermissionRequest(string permission, string connectio uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Revoke permission for the connection. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual async Task RevokePermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task RevokePermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRevokePermissionRequest(permission, connectionId, targetName); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRevokePermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RevokePermission"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Revoke permission for the connection. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual Response RevokePermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response RevokePermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateRevokePermissionRequest(permission, connectionId, targetName); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateRevokePermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.RevokePermission"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. - private Request CreateRevokePermissionRequest(string permission, string connectionId, string targetName = null) + /// The request options. + private HttpMessage CreateRevokePermissionRequest(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -711,36 +1664,101 @@ private Request CreateRevokePermissionRequest(string permission, string connecti uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } /// Check if a connection has permission to the specified action. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual async Task CheckPermissionAsync(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual async Task CheckPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateCheckPermissionRequest(permission, connectionId, targetName); - return await Pipeline.SendRequestAsync(req, cancellationToken).ConfigureAwait(false); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateCheckPermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.CheckPermission"); + scope.Start(); + try + { + await Pipeline.SendAsync(message, requestOptions.CancellationToken).ConfigureAwait(false); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Check if a connection has permission to the specified action. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - /// The cancellation token to use. - public virtual Response CheckPermission(string permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + /// The request options. +#pragma warning disable AZC0002 + public virtual Response CheckPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) +#pragma warning restore AZC0002 { - Request req = CreateCheckPermissionRequest(permission, connectionId, targetName); - return Pipeline.SendRequest(req, cancellationToken); + requestOptions ??= new RequestOptions(); + HttpMessage message = CreateCheckPermissionRequest(permission, connectionId, targetName, requestOptions); + if (requestOptions.PerCallPolicy != null) + { + message.SetProperty("RequestOptionsPerCallPolicyCallback", requestOptions.PerCallPolicy); + } + using var scope = _clientDiagnostics.CreateScope("WebPubSubServiceClient.CheckPermission"); + scope.Start(); + try + { + Pipeline.Send(message, requestOptions.CancellationToken); + if (requestOptions.StatusOption == ResponseStatusOption.Default) + { + switch (message.Response.Status) + { + case 200: + case 404: + return message.Response; + default: + throw _clientDiagnostics.CreateRequestFailedException(message.Response); + } + } + else + { + return message.Response; + } + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// Create Request for and operations. /// The permission: current supported actions are joinLeaveGroup and sendToGroup. /// Target connection Id. /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. - private Request CreateCheckPermissionRequest(string permission, string connectionId, string targetName = null) + /// The request options. + private HttpMessage CreateCheckPermissionRequest(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) { var message = Pipeline.CreateMessage(); var request = message.Request; @@ -762,7 +1780,7 @@ private Request CreateCheckPermissionRequest(string permission, string connectio uri.AppendQuery("api-version", apiVersion, true); } request.Uri = uri; - return request; + return message; } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs index 0be987b74a02..609a1e11fc25 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs @@ -32,7 +32,9 @@ public HealthApiClient(AzureKeyCredential credential, Uri endpoint = null, WebPu endpoint ??= new Uri(""); options ??= new WebPubSubServiceClientOptions(); - Pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); + _clientDiagnostics = new ClientDiagnostics(options); + var authPolicy = new WebPubSubAuthenticationPolicy(credential); + Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { authPolicy, new LowLevelCallbackPolicy() }); this.endpoint = endpoint; apiVersion = options.Version; } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index b54b70552957..63d1eaf04ebd 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -50,6 +50,7 @@ public WebPubSubServiceClient(string hub, AzureKeyCredential credential, Uri end endpoint ??= new Uri(""); options ??= new WebPubSubServiceClientOptions(); + _clientDiagnostics = new ClientDiagnostics(options); Pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); this.hub = hub; this.endpoint = endpoint; @@ -93,7 +94,11 @@ private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) par /// A if successful. public virtual async Task SendToAllAsync(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) { - return await SendToAllAsync(JsonContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return await SendToAllAsync(JsonContent, RequestContent.Create(message), excluded, options).ConfigureAwait(false); } /// Broadcast message to all the connected client connections. @@ -103,7 +108,11 @@ public virtual async Task SendToAllAsync(string message, IEnumerableA if successful. public virtual Response SendToAll(string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) { - return SendToAll(JsonContent, RequestContent.Create(message), excluded, cancellationToken); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return SendToAll(JsonContent, RequestContent.Create(message), excluded, options); } /// @@ -115,7 +124,11 @@ public virtual Response SendToAll(string message, IEnumerable excluded = /// A if successful. public virtual async Task SendToUserAsync(string userId, string message, CancellationToken cancellationToken = default) { - return await SendToUserAsync(userId, JsonContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return await SendToUserAsync(userId, JsonContent, RequestContent.Create(message), options).ConfigureAwait(false); } /// @@ -127,7 +140,11 @@ public virtual async Task SendToUserAsync(string userId, string messag /// A if successful. public virtual Response SendToUser(string userId, string message, CancellationToken cancellationToken = default) { - return SendToUser(userId, JsonContent, RequestContent.Create(message), cancellationToken); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return SendToUser(userId, JsonContent, RequestContent.Create(message), options); } /// @@ -139,7 +156,11 @@ public virtual Response SendToUser(string userId, string message, CancellationTo /// A if successful. public virtual async Task SendToConnectionAsync(string connectionId, string message, CancellationToken cancellationToken = default) { - return await SendToConnectionAsync(connectionId, JsonContent, RequestContent.Create(message), cancellationToken).ConfigureAwait(false); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return await SendToConnectionAsync(connectionId, JsonContent, RequestContent.Create(message), options).ConfigureAwait(false); } /// @@ -151,7 +172,11 @@ public virtual async Task SendToConnectionAsync(string connectionId, s /// A if successful. public virtual Response SendToConnection(string connectionId, string message, CancellationToken cancellationToken = default) { - return SendToConnection(connectionId, JsonContent, RequestContent.Create(message), cancellationToken); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return SendToConnection(connectionId, JsonContent, RequestContent.Create(message), options); } /// @@ -164,7 +189,11 @@ public virtual Response SendToConnection(string connectionId, string message, Ca /// A if successful. public virtual async Task SendToGroupAsync(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) { - return await SendToGroupAsync(group, JsonContent, RequestContent.Create(message), excluded, cancellationToken).ConfigureAwait(false); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return await SendToGroupAsync(group, JsonContent, RequestContent.Create(message), excluded, options).ConfigureAwait(false); } /// @@ -177,7 +206,11 @@ public virtual async Task SendToGroupAsync(string group, string messag /// A if successful. public virtual Response SendToGroup(string group, string message, IEnumerable excluded = null, CancellationToken cancellationToken = default) { - return SendToGroup(group, JsonContent, RequestContent.Create(message), excluded, cancellationToken); + RequestOptions options = default; + if (cancellationToken != default) + options = new RequestOptions() { CancellationToken = cancellationToken }; + + return SendToGroup(group, JsonContent, RequestContent.Create(message), excluded, options); } ///// Check if there are any client connections inside the given group. From 493b0f43e51ed8436df8fe961fc0db9b1deb5a65 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 13 May 2021 11:38:19 -0700 Subject: [PATCH 4/9] changes --- .../src/Generated/WebPubSubServiceClient.cs | 8 +- .../src/WebPubSubServiceClient_extensions.cs | 78 ++++++++++++++----- .../src/swagger/WebPubSub.json | 2 + .../Samples/WebPubSubSamples.HelloWorld.cs | 3 +- 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs index 4b80a69f6084..0c859bdf2b16 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -147,7 +147,7 @@ private HttpMessage CreateSendToAllRequest(string contentType, RequestContent re /// The connection Id. /// The request options. #pragma warning disable AZC0002 - public virtual async Task ConnectionExistsAsync(string connectionId, RequestOptions requestOptions = null) + internal virtual async Task ConnectionExistsAsync(string connectionId, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -188,7 +188,7 @@ public virtual async Task ConnectionExistsAsync(string connectionId, R /// The connection Id. /// The request options. #pragma warning disable AZC0002 - public virtual Response ConnectionExists(string connectionId, RequestOptions requestOptions = null) + internal virtual Response ConnectionExists(string connectionId, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -909,7 +909,7 @@ private HttpMessage CreateRemoveConnectionFromGroupRequest(string group, string /// Target user Id. /// The request options. #pragma warning disable AZC0002 - public virtual async Task UserExistsAsync(string userId, RequestOptions requestOptions = null) + internal virtual async Task UserExistsAsync(string userId, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -950,7 +950,7 @@ public virtual async Task UserExistsAsync(string userId, RequestOption /// Target user Id. /// The request options. #pragma warning disable AZC0002 - public virtual Response UserExists(string userId, RequestOptions requestOptions = null) + internal virtual Response UserExists(string userId, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index 63d1eaf04ebd..0d90af94c9a7 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -213,24 +213,64 @@ public virtual Response SendToGroup(string group, string message, IEnumerable Check if there are any client connections inside the given group. - ///// Target group name, which length should be greater than 0 and less than 1025. - ///// The cancellation token to use. - //public virtual async Task> GroupExistsAsync(string group, CancellationToken cancellationToken = default) - //{ - // var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; - // var response = await GroupExistsAsync(group, options).ConfigureAwait(false); - // return Response.FromValue(response.Status == 200, response); - //} - - ///// Check if there are any client connections inside the given group. - ///// Target group name, which length should be greater than 0 and less than 1025. - ///// The cancellation token to use. - //public virtual Response GroupExists(string group, CancellationToken cancellationToken = default) - //{ - // var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; - // var response = GroupExists(group, options); - // return Response.FromValue(response.Status == 200, response); - //} + /// Check if there are any client connections inside the given group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// The cancellation token to use. + public virtual async Task> GroupExistsAsync(string group, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await GroupExistsAsync(group, options).ConfigureAwait(false); + return Response.FromValue(response.Status == 200, response); + } + + /// Check if there are any client connections inside the given group. + /// Target group name, which length should be greater than 0 and less than 1025. + /// The cancellation token to use. + public virtual Response GroupExists(string group, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = GroupExists(group, options); + return Response.FromValue(response.Status == 200, response); + } + + /// Check if there are any client connections connected for the given user. + /// Target user Id. + /// The cancellation token to use. + public virtual async Task> UserExistsAsync(string userId, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await UserExistsAsync(userId, options).ConfigureAwait(false); + return Response.FromValue(response.Status == 200, response); + } + + /// Check if there are any client connections connected for the given user. + /// Target user Id. + /// The cancellation token to use. + public virtual Response UserExists(string userId, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = UserExists(userId, options); + return Response.FromValue(response.Status == 200, response); + } + + /// Check if the connection with the given connectionId exists. + /// The connection Id. + /// The cancellation token to use. + public virtual async Task> ConnectionExistsAsync(string connectionId, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await ConnectionExistsAsync(connectionId, options).ConfigureAwait(false); + return Response.FromValue(response.Status == 200, response); + } + + /// Check if the connection with the given connectionId exists. + /// The connection Id. + /// The cancellation token to use. + public virtual Response ConnectionExists(string connectionId, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = ConnectionExists(connectionId, options); + return Response.FromValue(response.Status == 200, response); + } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json index 2d783620a754..3eec49c0a1c5 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json @@ -100,6 +100,7 @@ ], "summary": "Check if the connection with the given connectionId exists.", "operationId": "WebPubSubService_ConnectionExists", + "x-accessibility": "internal", "parameters": [ { "in": "path", @@ -480,6 +481,7 @@ ], "summary": "Check if there are any client connections connected for the given user.", "operationId": "WebPubSubService_UserExists", + "x-accessibility": "internal", "parameters": [ { "in": "path", diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs index 6fb15b2b552e..a1c87fdc80ae 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs @@ -4,6 +4,7 @@ using System; using System.IO; using System.Text; +using System.Threading; using Azure.Core; using Azure.Core.TestFramework; using Azure.Messaging.WebPubSub; @@ -89,7 +90,7 @@ public void AddUserToGroup() client.AddUserToGroup("some_group", "some_user"); // Avoid sending messages to users who do not exist. - if (client.UserExists("some_user").Content.ToObjectFromJson()) + if (client.UserExists("some_user", CancellationToken.None).Value) { client.SendToUser("some_user", "Hi, I am glad you exist!"); } From be1b3418d951ca45fb2175656ccfb553ea52f9b4 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 3 Jun 2021 08:51:48 -0700 Subject: [PATCH 5/9] Finished moving to generated surface area --- .../src/Generated/HealthApiClient.cs | 15 ++- .../src/Generated/WebPubSubServiceClient.cs | 13 +-- ...WebPubSubServiceClientBuilderExtensions.cs | 2 +- .../src/WebPubSubServiceClient_extensions.cs | 109 ++++++++++++++++-- .../src/WebPubSubServiceClient_helpers.cs | 8 +- .../src/swagger/WebPubSub.json | 3 + .../Samples/WebPubSubSamples.HelloWorld.cs | 10 +- .../WebPubSubParseConnectionStringTests.cs | 4 +- 8 files changed, 135 insertions(+), 29 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs index ddefd9818f9f..d6a6a848df57 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/HealthApiClient.cs @@ -18,7 +18,6 @@ internal partial class HealthApiClient { /// The HTTP pipeline for sending and receiving REST requests and responses. public virtual HttpPipeline Pipeline { get; } - private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; private Uri endpoint; private readonly string apiVersion; private readonly ClientDiagnostics _clientDiagnostics; @@ -28,6 +27,20 @@ protected HealthApiClient() { } + /// Initializes a new instance of HealthApiClient. + /// server parameter. + /// The options for configuring the client. + public HealthApiClient(Uri endpoint = null, WebPubSubServiceClientOptions options = null) + { + endpoint ??= new Uri(""); + + options ??= new WebPubSubServiceClientOptions(); + _clientDiagnostics = new ClientDiagnostics(options); + Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, Array.Empty(), new ResponseClassifier()); + this.endpoint = endpoint; + apiVersion = options.Version; + } + /// Get service health status. /// The request options. #pragma warning disable AZC0002 diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs index 0c859bdf2b16..2df95503c458 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Generated/WebPubSubServiceClient.cs @@ -19,7 +19,6 @@ public partial class WebPubSubServiceClient { /// The HTTP pipeline for sending and receiving REST requests and responses. public virtual HttpPipeline Pipeline { get; } - private const string AuthorizationHeader = "Ocp-Apim-Subscription-Key"; private string hub; private Uri endpoint; private readonly string apiVersion; @@ -1445,7 +1444,7 @@ private HttpMessage CreateRemoveUserFromAllGroupsRequest(string userId, RequestO /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual async Task GrantPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual async Task GrantPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -1487,7 +1486,7 @@ public virtual async Task GrantPermissionAsync(string permission, stri /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual Response GrantPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual Response GrantPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -1559,7 +1558,7 @@ private HttpMessage CreateGrantPermissionRequest(string permission, string conne /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual async Task RevokePermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual async Task RevokePermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -1601,7 +1600,7 @@ public virtual async Task RevokePermissionAsync(string permission, str /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual Response RevokePermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual Response RevokePermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -1673,7 +1672,7 @@ private HttpMessage CreateRevokePermissionRequest(string permission, string conn /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual async Task CheckPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual async Task CheckPermissionAsync(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); @@ -1716,7 +1715,7 @@ public virtual async Task CheckPermissionAsync(string permission, stri /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. /// The request options. #pragma warning disable AZC0002 - public virtual Response CheckPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) + internal virtual Response CheckPermission(string permission, string connectionId, string targetName = null, RequestOptions requestOptions = null) #pragma warning restore AZC0002 { requestOptions ??= new RequestOptions(); diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs index e6bfdba767cc..7effc309e7e0 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs @@ -28,7 +28,7 @@ public static IAzureClientBuilder AddWebPubSubServiceClient(this TBuilder builder, Uri endpoint, string hub, AzureKeyCredential credential) where TBuilder : IAzureClientFactoryBuilder { - return builder.RegisterClientFactory(options => new WebPubSubServiceClient(hub, credential, endpoint, options)); + return builder.RegisterClientFactory(options => new WebPubSubServiceClient(endpoint, hub, credential, options)); } /// diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index 0d90af94c9a7..ebbee362857a 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -14,6 +14,7 @@ namespace Azure.Messaging.WebPubSub /// /// Azure Web PubSub Service Client. /// + [CodeGenSuppress("WebPubSubServiceClient", typeof(string), typeof(Uri), typeof(WebPubSubServiceClientOptions))] public partial class WebPubSubServiceClient { private AzureKeyCredential credential; @@ -31,14 +32,21 @@ public partial class WebPubSubServiceClient public Uri Endpoint => endpoint; /// Initializes a new instance of WebPubSubServiceClient. + /// server parameter. /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. /// A credential used to authenticate to an Azure Service. + public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential credential) + : this(endpoint, hub, credential, new WebPubSubServiceClientOptions()) + { + } + + /// Initializes a new instance of WebPubSubServiceClient. /// server parameter. + /// Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore. + /// A credential used to authenticate to an Azure Service. /// The options for configuring the client. - public WebPubSubServiceClient(string hub, AzureKeyCredential credential, Uri endpoint = null, WebPubSubServiceClientOptions options = null) + public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential credential, WebPubSubServiceClientOptions options) { - this.credential = credential; - if (hub == null) { throw new ArgumentNullException(nameof(hub)); @@ -47,14 +55,25 @@ public WebPubSubServiceClient(string hub, AzureKeyCredential credential, Uri end { throw new ArgumentNullException(nameof(credential)); } - endpoint ??= new Uri(""); + if (endpoint == null) + { + throw new ArgumentNullException(nameof(endpoint)); + } - options ??= new WebPubSubServiceClientOptions(); - _clientDiagnostics = new ClientDiagnostics(options); - Pipeline = HttpPipelineBuilder.Build(options, new WebPubSubAuthenticationPolicy(credential)); + this.credential = credential; this.hub = hub; this.endpoint = endpoint; + + options ??= new WebPubSubServiceClientOptions(); + _clientDiagnostics = new ClientDiagnostics(options); apiVersion = options.Version; + + Pipeline = HttpPipelineBuilder.Build( + options, + new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, + new HttpPipelinePolicy[] { new WebPubSubAuthenticationPolicy(credential) }, + new ResponseClassifier() + ); } /// @@ -78,12 +97,12 @@ public WebPubSubServiceClient(string connectionString, string hub, WebPubSubServ } private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub) : - this(hub, parsedConnectionString.Credential, parsedConnectionString.Endpoint) + this(parsedConnectionString.Endpoint, hub, parsedConnectionString.Credential) { } private WebPubSubServiceClient((Uri Endpoint, AzureKeyCredential Credential) parsedConnectionString, string hub, WebPubSubServiceClientOptions options) : - this(hub, parsedConnectionString.Credential, parsedConnectionString.Endpoint, options) + this(parsedConnectionString.Endpoint, hub, parsedConnectionString.Credential, options) { } @@ -272,5 +291,77 @@ public virtual Response ConnectionExists(string connectionId, Cancellation var response = ConnectionExists(connectionId, options); return Response.FromValue(response.Status == 200, response); } + + /// Grant permission to the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task GrantPermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await GrantPermissionAsync(permission.ToString(), connectionId, targetName, options).ConfigureAwait(false); + return response; + } + + /// Grant permission to the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, grant the permission to all the targets. If set, grant the permission to the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response GrantPermission(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = GrantPermission(permission.ToString(), connectionId, targetName, options); + return response; + } + + /// Revoke permission for the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task RevokePermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await RevokePermissionAsync(permission.ToString(), connectionId, targetName, options).ConfigureAwait(false); + return response; + } + + /// Revoke permission for the connection. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, revoke the permission for all targets. If set, revoke the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response RevokePermission(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = RevokePermission(permission.ToString(), connectionId, targetName, options); + return response; + } + + /// Check if a connection has permission to the specified action. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual async Task CheckPermissionAsync(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = await CheckPermissionAsync(permission.ToString(), connectionId, targetName, options).ConfigureAwait(false); + return response; + } + + /// Check if a connection has permission to the specified action. + /// The permission: current supported actions are joinLeaveGroup and sendToGroup. + /// Target connection Id. + /// Optional. If not set, get the permission for all targets. If set, get the permission for the specific target. The meaning of the target depends on the specific permission. + /// The cancellation token to use. + public virtual Response CheckPermission(WebPubSubPermission permission, string connectionId, string targetName = null, CancellationToken cancellationToken = default) + { + var options = new RequestOptions() { StatusOption = ResponseStatusOption.NoThrow, CancellationToken = cancellationToken }; + var response = CheckPermission(permission.ToString(), connectionId, targetName, options); + return response; + } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index e2a311cd425d..3e62e026888f 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -23,11 +23,11 @@ public partial class WebPubSubServiceClient /// Creates a URI with authentication token. /// /// - public virtual Uri GetClientAccessUri(string userId = default, string[] roles = default, TimeSpan expireAfter = default) + public virtual Uri GenerateClientAccessUri(string userId = default, string[] roles = default, TimeSpan expiresAfter = default) { - if (expireAfter == default) + if (expiresAfter == default) { - expireAfter = TimeSpan.FromHours(1); + expiresAfter = TimeSpan.FromHours(1); } List claims = new List(); @@ -56,7 +56,7 @@ public virtual Uri GetClientAccessUri(string userId = default, string[] roles = HttpPipeline pipeline = Pipeline; } - string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, credential, expireAfter); + string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, credential, expiresAfter); var clientEndpoint = new UriBuilder(endpoint); clientEndpoint.Scheme = "wss"; diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json index 3eec49c0a1c5..ded8d9684bdd 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/swagger/WebPubSub.json @@ -739,6 +739,7 @@ ], "summary": "Grant permission to the connection.", "operationId": "WebPubSubService_GrantPermission", + "x-accessibility": "internal", "parameters": [ { "in": "path", @@ -802,6 +803,7 @@ ], "summary": "Revoke permission for the connection.", "operationId": "WebPubSubService_RevokePermission", + "x-accessibility": "internal", "parameters": [ { "in": "path", @@ -865,6 +867,7 @@ ], "summary": "Check if a connection has permission to the specified action.", "operationId": "WebPubSubService_CheckPermission", + "x-accessibility": "internal", "parameters": [ { "in": "path", diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs index a1c87fdc80ae..03f95c389a58 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/Samples/WebPubSubSamples.HelloWorld.cs @@ -21,7 +21,7 @@ public void HelloWorld() var key = TestEnvironment.Key; #region Snippet:WebPubSubHelloWorld - var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); serviceClient.SendToAll("Hello World!"); #endregion @@ -33,7 +33,7 @@ public void Authenticate() var key = TestEnvironment.Key; #region Snippet:WebPubSubAuthenticate - var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); #endregion } @@ -54,7 +54,7 @@ public void JsonMessage() var key = TestEnvironment.Key; #region Snippet:WebPubSubSendJson - var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); serviceClient.SendToAll("application/json", RequestContent.Create( @@ -72,7 +72,7 @@ public void BinaryMessage() var key = TestEnvironment.Key; #region Snippet:WebPubSubSendBinary - var serviceClient = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); + var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); Stream stream = BinaryData.FromString("Hello World!").ToStream(); serviceClient.SendToAll("application/octet-stream", RequestContent.Create(stream)); @@ -85,7 +85,7 @@ public void AddUserToGroup() var key = TestEnvironment.Key; #region Snippet:WebPubAddUserToGroup - var client = new WebPubSubServiceClient("some_hub", new AzureKeyCredential(key), new Uri(endpoint)); + var client = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); client.AddUserToGroup("some_group", "some_user"); diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs index 8224ecd39235..bbf6c03afef1 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/tests/WebPubSubParseConnectionStringTests.cs @@ -30,8 +30,8 @@ public void ParseConnectionStringTests(string connectionString, string url, stri public void TestGenerateUriUseSameKidWithSameKey(string connectionString) { var serviceClient = new WebPubSubServiceClient(" Endpoint=http://localhost;Port=8080;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGH;Version=1.0;", "hub"); - var uri1 = serviceClient.GetClientAccessUri(); - var uri2 = serviceClient.GetClientAccessUri(); + var uri1 = serviceClient.GenerateClientAccessUri(); + var uri2 = serviceClient.GenerateClientAccessUri(); Assert.AreEqual("localhost:8080", uri1.Authority); Assert.AreEqual("/client/hubs/hub", uri1.AbsolutePath); From 2702228840cb1d4728cb24121041b65107d44142 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 3 Jun 2021 08:58:32 -0700 Subject: [PATCH 6/9] updated API file --- .../Azure.Messaging.WebPubSub/README.md | 6 +-- ...zure.Messaging.WebPubSub.netstandard2.0.cs | 53 ++++++++++--------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md b/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md index 0aa2a29a1b35..f4c6f308ab06 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/README.md @@ -80,7 +80,7 @@ serviceClient.SendToAll("Hello World!"); ```C# Snippet:WebPubSubSendJson var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); -serviceClient.SendToAll( +serviceClient.SendToAll("application/json", RequestContent.Create( new { @@ -95,9 +95,7 @@ serviceClient.SendToAll( var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key)); Stream stream = BinaryData.FromString("Hello World!").ToStream(); -serviceClient.SendToAll( - RequestContent.Create(stream), - HttpHeader.Common.OctetStreamContentType.Value); +serviceClient.SendToAll("application/octet-stream", RequestContent.Create(stream)); ``` ## Troubleshooting 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 a1b20ad8e31d..6a1037b39530 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 @@ -12,54 +12,57 @@ public WebPubSubServiceClient(string connectionString, string hub) { } public WebPubSubServiceClient(string connectionString, string hub, Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions options) { } public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.AzureKeyCredential credential) { } public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.AzureKeyCredential credential, Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions options) { } - public virtual Azure.Response AddConnectionToGroup(string group, string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task AddConnectionToGroupAsync(string group, string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response AddUserToGroup(string group, string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task AddUserToGroupAsync(string group, string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response CheckPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task> CheckPermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response CloseClientConnection(string connectionId, string reason = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task CloseClientConnectionAsync(string connectionId, string reason = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public System.Uri Endpoint { get { throw null; } } + public string Hub { get { throw null; } } + public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } + public virtual Azure.Response AddConnectionToGroup(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task AddConnectionToGroupAsync(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual Azure.Response AddUserToGroup(string group, string userId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task AddUserToGroupAsync(string group, string userId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual Azure.Response CheckPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task CheckPermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response CloseClientConnection(string connectionId, string reason = null, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task CloseClientConnectionAsync(string connectionId, string reason = null, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response ConnectionExists(string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task> ConnectionExistsAsync(string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Uri GetClientAccessUri(string userId = null, string[] roles = null, System.TimeSpan expireAfter = default(System.TimeSpan)) { throw null; } + public virtual System.Uri GenerateClientAccessUri(string userId = null, string[] roles = null, System.TimeSpan expiresAfter = default(System.TimeSpan)) { throw null; } public virtual Azure.Response GrantPermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task GrantPermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response GroupExists(string group, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task> GroupExistsAsync(string group, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response RemoveConnectionFromGroup(string group, string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task RemoveConnectionFromGroupAsync(string group, string connectionId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response RemoveUserFromAllGroups(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task RemoveUserFromAllGroupsAsync(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response RemoveUserFromGroup(string group, string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task RemoveUserFromGroupAsync(string group, string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response RemoveConnectionFromGroup(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task RemoveConnectionFromGroupAsync(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual Azure.Response RemoveUserFromAllGroups(string userId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task RemoveUserFromAllGroupsAsync(string userId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual Azure.Response RemoveUserFromGroup(string group, string userId, Azure.RequestOptions requestOptions = null) { throw null; } + public virtual System.Threading.Tasks.Task RemoveUserFromGroupAsync(string group, string userId, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response RevokePermission(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task RevokePermissionAsync(Azure.Messaging.WebPubSub.WebPubSubPermission permission, string connectionId, string targetName = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response SendToAll(Azure.Core.RequestContent message, string contentType = "application/json", System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response SendToAll(string contentType, Azure.Core.RequestContent requestBody, System.Collections.Generic.IEnumerable excluded = null, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response SendToAll(string message, System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task SendToAllAsync(Azure.Core.RequestContent message, string contentType = "application/json", System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task SendToAllAsync(string contentType, Azure.Core.RequestContent requestBody, System.Collections.Generic.IEnumerable excluded = null, Azure.RequestOptions requestOptions = null) { throw null; } public virtual System.Threading.Tasks.Task SendToAllAsync(string message, System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response SendToConnection(string connectionId, Azure.Core.RequestContent message, string contentType = "application/json", System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response SendToConnection(string connectionId, string contentType, Azure.Core.RequestContent requestBody, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response SendToConnection(string connectionId, string message, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task SendToConnectionAsync(string connectionId, Azure.Core.RequestContent message, string contentType = "application/json", System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task SendToConnectionAsync(string connectionId, string contentType, Azure.Core.RequestContent requestBody, Azure.RequestOptions requestOptions = null) { throw null; } public virtual System.Threading.Tasks.Task SendToConnectionAsync(string connectionId, string message, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response SendToGroup(string group, Azure.Core.RequestContent message, string contentType = "application/json", System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response SendToGroup(string group, string contentType, Azure.Core.RequestContent requestBody, System.Collections.Generic.IEnumerable excluded = null, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response SendToGroup(string group, string message, System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task SendToGroupAsync(string group, Azure.Core.RequestContent message, string contentType = "application/json", System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task SendToGroupAsync(string group, string contentType, Azure.Core.RequestContent requestBody, System.Collections.Generic.IEnumerable excluded = null, Azure.RequestOptions requestOptions = null) { throw null; } public virtual System.Threading.Tasks.Task SendToGroupAsync(string group, string message, System.Collections.Generic.IEnumerable excluded = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual Azure.Response SendToUser(string userId, Azure.Core.RequestContent message, string contentType = "application/json", System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response SendToUser(string userId, string contentType, Azure.Core.RequestContent requestBody, Azure.RequestOptions requestOptions = null) { throw null; } public virtual Azure.Response SendToUser(string userId, string message, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } - public virtual System.Threading.Tasks.Task SendToUserAsync(string userId, Azure.Core.RequestContent message, string contentType = "application/json", System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task SendToUserAsync(string userId, string contentType, Azure.Core.RequestContent requestBody, Azure.RequestOptions requestOptions = null) { throw null; } public virtual System.Threading.Tasks.Task SendToUserAsync(string userId, string message, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response UserExists(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task> UserExistsAsync(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } public partial class WebPubSubServiceClientOptions : Azure.Core.ClientOptions { - public WebPubSubServiceClientOptions(Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion version = Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion.V2021_05_01) { } + public WebPubSubServiceClientOptions(Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion version = Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions.ServiceVersion.V2021_05_01_preview) { } public enum ServiceVersion { - V2021_05_01 = 1, + V2021_05_01_preview = 1, } } } From 6d4c97f9c60268ec3f6a4d0e7daef8a41ed574bc Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Thu, 3 Jun 2021 17:09:32 -0700 Subject: [PATCH 7/9] PR review feedback incorporated --- .../src/Azure.Messaging.WebPubSub.csproj | 1 + .../src/HealthApiClient_extensions.cs | 33 ------------------- ...WebPubSubServiceClientBuilderExtensions.cs | 2 +- .../src/WebPubSubServiceClient_extensions.cs | 29 ++++++---------- .../src/WebPubSubServiceClient_helpers.cs | 11 +++---- 5 files changed, 16 insertions(+), 60 deletions(-) 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 19109763b8d2..77800fbcbd71 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/Azure.Messaging.WebPubSub.csproj @@ -26,6 +26,7 @@ + diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs index 609a1e11fc25..c65ec963fa47 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/HealthApiClient_extensions.cs @@ -1,42 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -// - -#nullable disable - -using System; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; - -#pragma warning disable AZC0007 - namespace Azure.Messaging.WebPubSub { - /// The HealthApi service client. internal partial class HealthApiClient { - /// Initializes a new instance of HealthApiClient. - /// A credential used to authenticate to an Azure Service. - /// server parameter. - /// The options for configuring the client. - public HealthApiClient(AzureKeyCredential credential, Uri endpoint = null, WebPubSubServiceClientOptions options = null) - { - if (credential == null) - { - throw new ArgumentNullException(nameof(credential)); - } - endpoint ??= new Uri(""); - - options ??= new WebPubSubServiceClientOptions(); - _clientDiagnostics = new ClientDiagnostics(options); - var authPolicy = new WebPubSubAuthenticationPolicy(credential); - Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { authPolicy, new LowLevelCallbackPolicy() }); - this.endpoint = endpoint; - apiVersion = options.Version; - } } } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs index 7effc309e7e0..748df1f39fe7 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClientBuilderExtensions.cs @@ -19,7 +19,7 @@ public static class WebPubSubServiceClientBuilderExtensions public static IAzureClientBuilder AddWebPubSubServiceClient(this TBuilder builder, string connectionString, string hub) where TBuilder : IAzureClientFactoryBuilder { - return builder.RegisterClientFactory(options => new WebPubSubServiceClient(hub, connectionString, options)); + return builder.RegisterClientFactory(options => new WebPubSubServiceClient(connectionString, hub, options)); } /// diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index ebbee362857a..e875a320c2a8 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -17,19 +17,19 @@ namespace Azure.Messaging.WebPubSub [CodeGenSuppress("WebPubSubServiceClient", typeof(string), typeof(Uri), typeof(WebPubSubServiceClientOptions))] public partial class WebPubSubServiceClient { - private AzureKeyCredential credential; + private AzureKeyCredential _credential; private const string JsonContent = "application/json"; /// /// The hub. /// - public string Hub => hub; + public virtual string Hub => hub; /// /// The service endpoint. /// - public Uri Endpoint => endpoint; + public virtual Uri Endpoint => endpoint; /// Initializes a new instance of WebPubSubServiceClient. /// server parameter. @@ -47,20 +47,11 @@ public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential crede /// The options for configuring the client. public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential credential, WebPubSubServiceClientOptions options) { - if (hub == null) - { - throw new ArgumentNullException(nameof(hub)); - } - if (credential == null) - { - throw new ArgumentNullException(nameof(credential)); - } - if (endpoint == null) - { - throw new ArgumentNullException(nameof(endpoint)); - } - - this.credential = credential; + Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(hub, nameof(hub)); + Argument.AssertNotNull(credential, nameof(credential)); + + this._credential = credential; this.hub = hub; this.endpoint = endpoint; @@ -70,8 +61,8 @@ public WebPubSubServiceClient(Uri endpoint, string hub, AzureKeyCredential crede Pipeline = HttpPipelineBuilder.Build( options, - new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, - new HttpPipelinePolicy[] { new WebPubSubAuthenticationPolicy(credential) }, + perCallPolicies: new HttpPipelinePolicy[] { new LowLevelCallbackPolicy() }, + perRetryPolicies: new HttpPipelinePolicy[] { new WebPubSubAuthenticationPolicy(credential) }, new ResponseClassifier() ); } diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs index 3e62e026888f..d3d8d7abb9a3 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_helpers.cs @@ -22,6 +22,9 @@ public partial class WebPubSubServiceClient /// /// Creates a URI with authentication token. /// + /// + /// + /// Defaults to one hour, if not specified. /// public virtual Uri GenerateClientAccessUri(string userId = default, string[] roles = default, TimeSpan expiresAfter = default) { @@ -50,13 +53,7 @@ public virtual Uri GenerateClientAccessUri(string userId = default, string[] rol } var audience = $"{endpoint}client/hubs/{hub}"; - // this is a hack. The generator needs to store the credential in a field. - if (credential == null) - { - HttpPipeline pipeline = Pipeline; - } - - string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, credential, expiresAfter); + string token = WebPubSubAuthenticationPolicy.GenerateAccessToken(audience, claims, _credential, expiresAfter); var clientEndpoint = new UriBuilder(endpoint); clientEndpoint.Scheme = "wss"; From 38fa14bf86a073a77f48a9388b5c823e283484ab Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Fri, 4 Jun 2021 09:46:20 -0700 Subject: [PATCH 8/9] regenerated APIs --- .../api/Azure.Messaging.WebPubSub.netstandard2.0.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 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 6a1037b39530..94563ad6befa 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 @@ -12,8 +12,8 @@ public WebPubSubServiceClient(string connectionString, string hub) { } public WebPubSubServiceClient(string connectionString, string hub, Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions options) { } public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.AzureKeyCredential credential) { } public WebPubSubServiceClient(System.Uri endpoint, string hub, Azure.AzureKeyCredential credential, Azure.Messaging.WebPubSub.WebPubSubServiceClientOptions options) { } - public System.Uri Endpoint { get { throw null; } } - public string Hub { get { throw null; } } + 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 AddConnectionToGroup(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } public virtual System.Threading.Tasks.Task AddConnectionToGroupAsync(string group, string connectionId, Azure.RequestOptions requestOptions = null) { throw null; } From b336aa5826a3eecb9eaa1d62fb6bfd41ba273461 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Fri, 4 Jun 2021 11:28:18 -0700 Subject: [PATCH 9/9] fixed a bug --- .../src/WebPubSubServiceClient_extensions.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs index e875a320c2a8..365a0d23eca9 100644 --- a/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs +++ b/sdk/webpubsub/Azure.Messaging.WebPubSub/src/WebPubSubServiceClient_extensions.cs @@ -108,7 +108,7 @@ public virtual async Task SendToAllAsync(string message, IEnumerableBroadcast message to all the connected client connections. @@ -122,7 +122,7 @@ public virtual Response SendToAll(string message, IEnumerable excluded = if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return SendToAll(JsonContent, RequestContent.Create(message), excluded, options); + return SendToAll(JsonContent, RequestContent.Create((object)message), excluded, options); } /// @@ -138,7 +138,7 @@ public virtual async Task SendToUserAsync(string userId, string messag if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return await SendToUserAsync(userId, JsonContent, RequestContent.Create(message), options).ConfigureAwait(false); + return await SendToUserAsync(userId, JsonContent, RequestContent.Create((object)message), options).ConfigureAwait(false); } /// @@ -154,7 +154,7 @@ public virtual Response SendToUser(string userId, string message, CancellationTo if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return SendToUser(userId, JsonContent, RequestContent.Create(message), options); + return SendToUser(userId, JsonContent, RequestContent.Create((object)message), options); } /// @@ -170,7 +170,7 @@ public virtual async Task SendToConnectionAsync(string connectionId, s if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return await SendToConnectionAsync(connectionId, JsonContent, RequestContent.Create(message), options).ConfigureAwait(false); + return await SendToConnectionAsync(connectionId, JsonContent, RequestContent.Create((object)message), options).ConfigureAwait(false); } /// @@ -186,7 +186,7 @@ public virtual Response SendToConnection(string connectionId, string message, Ca if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return SendToConnection(connectionId, JsonContent, RequestContent.Create(message), options); + return SendToConnection(connectionId, JsonContent, RequestContent.Create((object)message), options); } /// @@ -203,7 +203,7 @@ public virtual async Task SendToGroupAsync(string group, string messag if (cancellationToken != default) options = new RequestOptions() { CancellationToken = cancellationToken }; - return await SendToGroupAsync(group, JsonContent, RequestContent.Create(message), excluded, options).ConfigureAwait(false); + return await SendToGroupAsync(group, JsonContent, RequestContent.Create((object)message), excluded, options).ConfigureAwait(false); } /// @@ -220,7 +220,7 @@ public virtual Response SendToGroup(string group, string message, IEnumerable Check if there are any client connections inside the given group.