From 3cfe0caef2cd7116abbadc983623ecba72f74e43 Mon Sep 17 00:00:00 2001 From: Kyle Miller Date: Fri, 2 Feb 2024 15:50:34 -0500 Subject: [PATCH] Finished Client class --- RDSService/Services/RdsSessionService.cs | 2 +- RDSServiceClient/RDSServiceClient.csproj | 4 ++ RDSServiceClient/RdsServiceClientOptions.cs | 23 +++++++ RDSServiceClient/RdsSessionService.cs | 60 +++++++++++++++++++ .../Services/RdsSessionService.cs | 26 -------- .../Interfaces/IRdsSessionService.cs | 2 +- RDSServiceTester/RdsServiceTest.cs | 1 + 7 files changed, 90 insertions(+), 28 deletions(-) create mode 100644 RDSServiceClient/RdsServiceClientOptions.cs create mode 100644 RDSServiceClient/RdsSessionService.cs delete mode 100644 RDSServiceClient/Services/RdsSessionService.cs diff --git a/RDSService/Services/RdsSessionService.cs b/RDSService/Services/RdsSessionService.cs index 18c5126..c65b401 100644 --- a/RDSService/Services/RdsSessionService.cs +++ b/RDSService/Services/RdsSessionService.cs @@ -20,7 +20,7 @@ public async Task GetActiveManagementServer(string? connectionBroker = n return output.FirstOrDefault()!.Properties["ActiveManagementServer"].Value.ToString()!; } - public async Task> GetSessions(string? connectionBroker = null) + public async Task?> GetSessions(string? connectionBroker = null) { connectionBroker = GetConnectionBroker(connectionBroker); var output = await ExecutePowerShellCommand("Get-RDUserSession", diff --git a/RDSServiceClient/RDSServiceClient.csproj b/RDSServiceClient/RDSServiceClient.csproj index d06dbf3..185ad86 100644 --- a/RDSServiceClient/RDSServiceClient.csproj +++ b/RDSServiceClient/RDSServiceClient.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/RDSServiceClient/RdsServiceClientOptions.cs b/RDSServiceClient/RdsServiceClientOptions.cs new file mode 100644 index 0000000..392fcca --- /dev/null +++ b/RDSServiceClient/RdsServiceClientOptions.cs @@ -0,0 +1,23 @@ +namespace RDSServiceClient; + +public class RdsServiceClientOptions +{ + public RdsServiceClientOptions(string baseUrl, + string disconnectUrl = "DisconnectSession", + string logoffUrl = "LogOffSession", + string sessionsUrl = "GetSessions", + string activeManagementServerUrl = "GetActiveManagementServer") + { + BaseUrl = baseUrl; + DisconnectUrl = disconnectUrl; + LogoffUrl = logoffUrl; + SessionsUrl = sessionsUrl; + ActiveManagementServerUrl = activeManagementServerUrl; + } + + public string BaseUrl { get; set; } + public string DisconnectUrl { get; set; } + public string LogoffUrl { get; set; } + public string SessionsUrl { get; set; } + public string ActiveManagementServerUrl { get; set; } +} \ No newline at end of file diff --git a/RDSServiceClient/RdsSessionService.cs b/RDSServiceClient/RdsSessionService.cs new file mode 100644 index 0000000..d770237 --- /dev/null +++ b/RDSServiceClient/RdsSessionService.cs @@ -0,0 +1,60 @@ +using System.Net.Http.Headers; +using System.Net.Http.Json; +using Microsoft.Extensions.Options; +using RDSServiceLibrary; +using RDSServiceLibrary.Interfaces; +using RDSServiceLibrary.Models; + +namespace RDSServiceClient +{ + public class RdsSessionService : IRdsSessionService + { + private readonly HttpClient _httpClient; + private readonly IOptions _options; + + public RdsSessionService(IHttpClientFactory httpClientFactory, IOptions options) + { + _options = options; + _httpClient = httpClientFactory.CreateClient("RdsServiceClient"); + _httpClient.BaseAddress = new Uri(options.Value.BaseUrl); + _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + public Task GetActiveManagementServer(string? connectionBroker = null) => + ProcessRequest(_options.Value.ActiveManagementServerUrl); + + public Task> GetSessions(string? connectionBroker = null) => + ProcessRequest>(_options.Value.SessionsUrl); + + public Task DisconnectSession(SessionInfo sessionInfo, string? connectionBroker = null) => + ProcessPostRequest(_options.Value.DisconnectUrl, sessionInfo); + + public Task LogOffSession(SessionInfo sessionInfo, string? connectionBroker = null) => + ProcessPostRequest(_options.Value.LogoffUrl, sessionInfo); + + private async Task ProcessRequest(string url) + { + var response = await _httpClient.GetAsync(url); + await HandleError(response); + return await response.Content.ReadFromJsonAsync() ?? + throw new RdsServiceException("An error occurred processing the response from the RDS service"); + } + + private async Task ProcessPostRequest(string url, SessionInfo sessionInfo) + { + var response = await _httpClient.PostAsJsonAsync(url, sessionInfo); + await HandleError(response); + return true; + } + + private static async Task HandleError(HttpResponseMessage response) + { + if (!response.IsSuccessStatusCode) + { + var result = await response.Content.ReadFromJsonAsync(); + if (result != null) throw result; + throw new RdsServiceException("An error occurred calling the RDS service"); + } + } + } +} \ No newline at end of file diff --git a/RDSServiceClient/Services/RdsSessionService.cs b/RDSServiceClient/Services/RdsSessionService.cs deleted file mode 100644 index 5446f6c..0000000 --- a/RDSServiceClient/Services/RdsSessionService.cs +++ /dev/null @@ -1,26 +0,0 @@ -using RDSServiceLibrary.Interfaces; -using RDSServiceLibrary.Models; - -namespace RDSServiceClient.Services; -public class RdsSessionService : IRdsSessionService -{ - public async Task GetActiveManagementServer(string? connectionBroker = null) - { - throw new NotImplementedException(); - } - - public async Task> GetSessions(string? connectionBroker = null) - { - throw new NotImplementedException(); - } - - public async Task DisconnectSession(SessionInfo sessionInfo, string? connectionBroker = null) - { - throw new NotImplementedException(); - } - - public async Task LogOffSession(SessionInfo sessionInfo, string? connectionBroker = null) - { - throw new NotImplementedException(); - } -} \ No newline at end of file diff --git a/RDSServiceLibrary/Interfaces/IRdsSessionService.cs b/RDSServiceLibrary/Interfaces/IRdsSessionService.cs index 6ec8ab4..9d006c0 100644 --- a/RDSServiceLibrary/Interfaces/IRdsSessionService.cs +++ b/RDSServiceLibrary/Interfaces/IRdsSessionService.cs @@ -18,7 +18,7 @@ public interface IRdsSessionService /// Gets the sessions from the specified active management server. /// /// A Task representing the asynchronous operation, with an as the result representing the sessions. - Task> GetSessions(string? connectionBroker = null); + Task?> GetSessions(string? connectionBroker = null); /// /// Disconnects a session from the specified host server and active management server. diff --git a/RDSServiceTester/RdsServiceTest.cs b/RDSServiceTester/RdsServiceTest.cs index 2ee5f2e..0d88c0c 100644 --- a/RDSServiceTester/RdsServiceTest.cs +++ b/RDSServiceTester/RdsServiceTest.cs @@ -1,5 +1,6 @@ using System.Text.Json; using RDSService.Services; +using RDSServiceLibrary.Models; using Xunit.Abstractions; namespace RDSServiceTester;