Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Configuration/WebSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class WebSettings
/// </summary>
public string LearningHubApiUrl { get; set; }

/// <summary>
/// Gets or sets the OpenApiUrl.
/// </summary>
public string OpenApiUrl { get; set; }

/// <summary>
/// Gets or sets the user api url.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Helpers/IOpenApiFacade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace LearningHub.Nhs.AdminUI.Helpers
{
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Common;

/// <summary>
/// Defines the <see cref="IOpenApiFacade" />.
/// </summary>
public interface IOpenApiFacade
{
/// <summary>
/// The GetAsync.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="url">The url.</param>
/// <returns>The <see cref="Task{T}"/>.</returns>
Task<T> GetAsync<T>(string url)
where T : class, new();

/// <summary>
/// The PostAsync.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task"/>.</returns>
Task PostAsync<T>(string url, T body)
where T : class, new();

/// <summary>
/// The PostAsync.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <typeparam name="TBody">.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task{T}"/>.</returns>
Task<ApiResponse> PostAsync<T, TBody>(string url, TBody body)
where T : class, new()
where TBody : class, new();

/// <summary>
/// The PutAsync.
/// </summary>
/// <param name="url">The url.</param>
/// <returns>The <see cref="Task"/>.</returns>
Task<ApiResponse> PutAsync(string url);

/// <summary>
/// The PutAsync.
/// </summary>
/// <typeparam name="T">.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task"/>.</returns>
Task<ApiResponse> PutAsync<T>(string url, T body)
where T : class, new();
}
}
241 changes: 241 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Helpers/OpenApiFacade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
namespace LearningHub.Nhs.AdminUI.Helpers
{
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using LearningHub.Nhs.AdminUI.Interfaces;
using LearningHub.Nhs.Models.Common;
using Newtonsoft.Json;

/// <summary>
/// Defines the <see cref="OpenApiFacade" />.
/// </summary>
public class OpenApiFacade : IOpenApiFacade
{
/// <summary>
/// Defines the _client.
/// </summary>
private readonly IOpenApiHttpClient client;

/// <summary>
/// Initializes a new instance of the <see cref="OpenApiFacade"/> class.
/// </summary>
/// <param name="client">The client<see cref="IOpenApiHttpClient"/>.</param>
public OpenApiFacade(IOpenApiHttpClient client)
{
this.client = client;
}

/// <summary>
/// The GetAsync.
/// </summary>
/// <typeparam name="T">.</typeparam>
/// <param name="url">The url.</param>
/// <returns>The <see cref="Task{T}"/>.</returns>
public async Task<T> GetAsync<T>(string url)
where T : class, new()
{
var client = await this.client.GetClientAsync();

var vm = new T();

var response = await client.GetAsync(url).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
vm = JsonConvert.DeserializeObject<T>(result);

return vm;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}
else
{
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
}
}

/// <summary>
/// The PostAsync.
/// </summary>
/// <typeparam name="T">.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task"/>.</returns>
public async Task PostAsync<T>(string url, T body)
where T : class, new()
{
var client = await this.client.GetClientAsync();

var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
return;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}
else
{
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
}
}

/// <summary>
/// The PostAsync.
/// </summary>
/// <typeparam name="T">The return type.</typeparam>
/// <typeparam name="TBody">The type of body parameter.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task{T}"/>.</returns>
public async Task<ApiResponse> PostAsync<T, TBody>(string url, TBody body)
where TBody : class, new()
where T : class, new()
{
var client = await this.client.GetClientAsync();

var vm = new T();
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
if (apiResponse.Success)
{
return apiResponse;
}
else
{
string details = string.Empty;
if (apiResponse.ValidationResult != null)
{
if (apiResponse.ValidationResult.Details != null)
{
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
}
}

throw new Exception($"PostAsync ApiResponse returned False: {details}");
}
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("Access Denied");
}
else
{
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
}
}

/// <summary>
/// The PutAsync.
/// </summary>
/// <param name="url">The url.</param>
/// <returns>The <see cref="Task"/>.</returns>
public async Task<ApiResponse> PutAsync(string url)
{
var client = await this.client.GetClientAsync();

var response = await client.PutAsync(url, null).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
if (apiResponse.Success)
{
return apiResponse;
}
else
{
string details = string.Empty;
if (apiResponse.ValidationResult != null)
{
if (apiResponse.ValidationResult.Details != null)
{
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
}
}

throw new Exception($"PutAsync ApiResponse returned False: {details}");
}
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("Access Denied");
}
else
{
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
}
}

/// <summary>
/// The PutAsync.
/// </summary>
/// <typeparam name="T">.</typeparam>
/// <param name="url">The url.</param>
/// <param name="body">The body.</param>
/// <returns>The <see cref="Task"/>.</returns>
public async Task<ApiResponse> PutAsync<T>(string url, T body)
where T : class, new()
{
var client = await this.client.GetClientAsync();

var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
var response = await client.PutAsync(url, content).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
if (apiResponse.Success)
{
return apiResponse;
}
else
{
string details = string.Empty;
if (apiResponse.ValidationResult != null)
{
if (apiResponse.ValidationResult.Details != null)
{
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
}
}

throw new Exception($"PutAsync ApiResponse returned False: {details}");
}
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("Access Denied");
}
else
{
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace LearningHub.Nhs.WebUI.Interfaces
namespace LearningHub.Nhs.AdminUI.Interfaces
{
using System.Net.Http;
using System.Threading.Tasks;

/// <summary>
/// The User Api HttpClient interface.
/// The OpenApiHttpClient interface.
/// </summary>
public interface IUserApiHttpClient
public interface IOpenApiHttpClient
{
/// <summary>
/// The get client.
Expand Down
9 changes: 9 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
services.AddSingleton(configuration);

services.AddScoped<ILearningHubApiFacade, LearningHubApiFacade>();
services.AddScoped<IOpenApiFacade, OpenApiFacade>();
services.AddScoped<IResourceService, ResourceService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IUserGroupService, UserGroupService>();
Expand Down Expand Up @@ -132,11 +133,19 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
});
services.AddHttpClient<IOpenApiHttpClient, OpenApiHttpClient>()
.ConfigurePrimaryHttpMessageHandler(
() => new HttpClientHandler
{
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
});
}
else
{
services.AddHttpClient<ILearningHubHttpClient, LearningHubHttpClient>();
services.AddHttpClient<IUserApiHttpClient, UserApiHttpClient>();
services.AddHttpClient<IOpenApiHttpClient, OpenApiHttpClient>();
}

services.AddTransient<CookieEventHandler>();
Expand Down
21 changes: 21 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Services/BaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class BaseService
/// </summary>
private ILearningHubHttpClient learningHubHttpClient;

/// <summary>
/// Defines the openApiHttpClient.
/// </summary>
private IOpenApiHttpClient openApiHttpClient;

/// <summary>
/// Initializes a new instance of the <see cref="BaseService"/> class.
/// </summary>
Expand All @@ -21,9 +26,25 @@ protected BaseService(ILearningHubHttpClient learningHubHttpClient)
this.learningHubHttpClient = learningHubHttpClient;
}

/// <summary>
/// Initializes a new instance of the <see cref="BaseService"/> class.
/// </summary>
/// <param name="learningHubHttpClient">The learningHubHttpClient<see cref="ILearningHubHttpClient"/>.</param>
/// <param name="openApiHttpClient">The openApiHttpClient<see cref="IOpenApiHttpClient"/>.</param>
protected BaseService(ILearningHubHttpClient learningHubHttpClient, IOpenApiHttpClient openApiHttpClient)
{
this.learningHubHttpClient = learningHubHttpClient;
this.openApiHttpClient = openApiHttpClient;
}

/// <summary>
/// Gets the LearningHubHttpClient.
/// </summary>
protected ILearningHubHttpClient LearningHubHttpClient => this.learningHubHttpClient;

/// <summary>
/// Gets the OpenApiHttpClient.
/// </summary>
protected IOpenApiHttpClient OpenApiHttpClient => this.openApiHttpClient;
}
}
Loading
Loading