From c951ffe6c2c14d3c5f91ba1b7f935a814dd9c49b Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 21 Nov 2024 07:50:48 -0500 Subject: [PATCH] chore: removes duplication in server removal implementation Signed-off-by: Vincent Biret --- src/kiota/Rpc/Server.cs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/kiota/Rpc/Server.cs b/src/kiota/Rpc/Server.cs index 66cc24c251..b60155710b 100644 --- a/src/kiota/Rpc/Server.cs +++ b/src/kiota/Rpc/Server.cs @@ -300,37 +300,28 @@ private static string NormalizeSlashesInPath(string path) return path.Replace('\\', '/'); } - public async Task> RemoveClientAsync(string clientName, bool cleanOutput, CancellationToken cancellationToken) + public Task> RemoveClientAsync(string clientName, bool cleanOutput, CancellationToken cancellationToken) + => RemoveClientOrPluginAsync(clientName, cleanOutput, "Client", (workspaceManagementService, clientName, cleanOutput, cancellationToken) => workspaceManagementService.RemoveClientAsync(clientName, cleanOutput, cancellationToken), cancellationToken); + + private static async Task> RemoveClientOrPluginAsync(string clientName, bool cleanOutput, string typeName, Func removal, CancellationToken cancellationToken) { ArgumentException.ThrowIfNullOrEmpty(clientName); + ArgumentException.ThrowIfNullOrEmpty(typeName); + ArgumentNullException.ThrowIfNull(removal); var logger = new ForwardedLogger(); try { var workspaceManagementService = new WorkspaceManagementService(logger, httpClient, IsConfigPreviewEnabled.Value); - await workspaceManagementService.RemoveClientAsync(clientName, cleanOutput, cancellationToken).ConfigureAwait(false); - logger.LogInformation($"Client {clientName} removed successfully!"); + await removal(workspaceManagementService, clientName, cleanOutput, cancellationToken).ConfigureAwait(false); + logger.LogInformation("{TypeName} {ClientName} removed successfully!", typeName, clientName); } catch (Exception ex) { - logger.LogCritical(ex, "error removing the client: {exceptionMessage}", ex.Message); + logger.LogCritical(ex, "error removing the {TypeName}: {ExceptionMessage}", typeName.ToLowerInvariant(), ex.Message); } return logger.LogEntries; } public async Task> RemovePluginAsync(string pluginName, bool cleanOutput, CancellationToken cancellationToken) - { - ArgumentException.ThrowIfNullOrEmpty(pluginName); - var logger = new ForwardedLogger(); - try - { - var workspaceManagementService = new WorkspaceManagementService(logger, httpClient, IsConfigPreviewEnabled.Value); - await workspaceManagementService.RemovePluginAsync(pluginName, cleanOutput, cancellationToken).ConfigureAwait(false); - logger.LogInformation($"Plugin {pluginName} removed successfully!"); - } - catch (Exception ex) - { - logger.LogCritical(ex, "error removing the plugin: {exceptionMessage}", ex.Message); - } - return logger.LogEntries; - } + => await RemoveClientOrPluginAsync(pluginName, cleanOutput, "Plugin", (workspaceManagementService, pluginName, cleanOutput, cancellationToken) => workspaceManagementService.RemovePluginAsync(pluginName, cleanOutput, cancellationToken), cancellationToken); }