-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c87ad33
commit 3cfe0ca
Showing
7 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build
Check warning on line 26 in RDSServiceClient/RdsSessionService.cs GitHub Actions / build
|
||
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"); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters