Skip to content

Commit

Permalink
Finished Client class
Browse files Browse the repository at this point in the history
  • Loading branch information
kylemiller-rum committed Feb 2, 2024
1 parent c87ad33 commit 3cfe0ca
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
2 changes: 1 addition & 1 deletion RDSService/Services/RdsSessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<string> GetActiveManagementServer(string? connectionBroker = n
return output.FirstOrDefault()!.Properties["ActiveManagementServer"].Value.ToString()!;
}

public async Task<List<RdsSession>> GetSessions(string? connectionBroker = null)
public async Task<List<RdsSession>?> GetSessions(string? connectionBroker = null)
{
connectionBroker = GetConnectionBroker(connectionBroker);
var output = await ExecutePowerShellCommand("Get-RDUserSession",
Expand Down
4 changes: 4 additions & 0 deletions RDSServiceClient/RDSServiceClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\RDSServiceLibrary\RDSServiceLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions RDSServiceClient/RdsServiceClientOptions.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
60 changes: 60 additions & 0 deletions RDSServiceClient/RdsSessionService.cs
Original file line number Diff line number Diff line change
@@ -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<RdsServiceClientOptions> _options;

public RdsSessionService(IHttpClientFactory httpClientFactory, IOptions<RdsServiceClientOptions> options)
{
_options = options;
_httpClient = httpClientFactory.CreateClient("RdsServiceClient");
_httpClient.BaseAddress = new Uri(options.Value.BaseUrl);
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}

public Task<string> GetActiveManagementServer(string? connectionBroker = null) =>
ProcessRequest<string>(_options.Value.ActiveManagementServerUrl);

public Task<List<RdsSession>> GetSessions(string? connectionBroker = null) =>

Check warning on line 26 in RDSServiceClient/RdsSessionService.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in return type of 'Task<List<RdsSession>> RdsSessionService.GetSessions(string? connectionBroker = null)' doesn't match implicitly implemented member 'Task<List<RdsSession>?> IRdsSessionService.GetSessions(string? connectionBroker = null)'.

Check warning on line 26 in RDSServiceClient/RdsSessionService.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in return type of 'Task<List<RdsSession>> RdsSessionService.GetSessions(string? connectionBroker = null)' doesn't match implicitly implemented member 'Task<List<RdsSession>?> IRdsSessionService.GetSessions(string? connectionBroker = null)'.
ProcessRequest<List<RdsSession>>(_options.Value.SessionsUrl);

public Task<bool> DisconnectSession(SessionInfo sessionInfo, string? connectionBroker = null) =>
ProcessPostRequest(_options.Value.DisconnectUrl, sessionInfo);

public Task<bool> LogOffSession(SessionInfo sessionInfo, string? connectionBroker = null) =>
ProcessPostRequest(_options.Value.LogoffUrl, sessionInfo);

private async Task<T> ProcessRequest<T>(string url)
{
var response = await _httpClient.GetAsync(url);
await HandleError(response);
return await response.Content.ReadFromJsonAsync<T>() ??
throw new RdsServiceException("An error occurred processing the response from the RDS service");
}

private async Task<bool> 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<RdsServiceException>();
if (result != null) throw result;
throw new RdsServiceException("An error occurred calling the RDS service");
}
}
}
}
26 changes: 0 additions & 26 deletions RDSServiceClient/Services/RdsSessionService.cs

This file was deleted.

2 changes: 1 addition & 1 deletion RDSServiceLibrary/Interfaces/IRdsSessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IRdsSessionService
/// Gets the sessions from the specified active management server.
/// </summary>
/// <returns>A Task representing the asynchronous operation, with an <see cref="PSObject"/> as the result representing the sessions.</returns>
Task<List<RdsSession>> GetSessions(string? connectionBroker = null);
Task<List<RdsSession>?> GetSessions(string? connectionBroker = null);

/// <summary>
/// Disconnects a session from the specified host server and active management server.
Expand Down
1 change: 1 addition & 0 deletions RDSServiceTester/RdsServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using RDSService.Services;
using RDSServiceLibrary.Models;
using Xunit.Abstractions;

namespace RDSServiceTester;
Expand Down

0 comments on commit 3cfe0ca

Please sign in to comment.