Skip to content

Commit c230650

Browse files
Merge pull request #1236 from TechnologyEnhancedLearning/TD-5490
Merge Td 5490 branch to Messaging Service
2 parents c9bec6e + acacd84 commit c230650

File tree

152 files changed

+10872
-589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+10872
-589
lines changed

AdminUI/LearningHub.Nhs.AdminUI/Configuration/WebSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public class WebSettings
2727
/// </summary>
2828
public string LearningHubApiUrl { get; set; }
2929

30+
/// <summary>
31+
/// Gets or sets the OpenApiUrl.
32+
/// </summary>
33+
public string OpenApiUrl { get; set; }
34+
3035
/// <summary>
3136
/// Gets or sets the user api url.
3237
/// </summary>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace LearningHub.Nhs.AdminUI.Helpers
2+
{
3+
using System.Threading.Tasks;
4+
using LearningHub.Nhs.Models.Common;
5+
6+
/// <summary>
7+
/// Defines the <see cref="IOpenApiFacade" />.
8+
/// </summary>
9+
public interface IOpenApiFacade
10+
{
11+
/// <summary>
12+
/// The GetAsync.
13+
/// </summary>
14+
/// <typeparam name="T">The type.</typeparam>
15+
/// <param name="url">The url.</param>
16+
/// <returns>The <see cref="Task{T}"/>.</returns>
17+
Task<T> GetAsync<T>(string url)
18+
where T : class, new();
19+
20+
/// <summary>
21+
/// The PostAsync.
22+
/// </summary>
23+
/// <typeparam name="T">The type.</typeparam>
24+
/// <param name="url">The url.</param>
25+
/// <param name="body">The body.</param>
26+
/// <returns>The <see cref="Task"/>.</returns>
27+
Task PostAsync<T>(string url, T body)
28+
where T : class, new();
29+
30+
/// <summary>
31+
/// The PostAsync.
32+
/// </summary>
33+
/// <typeparam name="T">The type.</typeparam>
34+
/// <typeparam name="TBody">.</typeparam>
35+
/// <param name="url">The url.</param>
36+
/// <param name="body">The body.</param>
37+
/// <returns>The <see cref="Task{T}"/>.</returns>
38+
Task<ApiResponse> PostAsync<T, TBody>(string url, TBody body)
39+
where T : class, new()
40+
where TBody : class, new();
41+
42+
/// <summary>
43+
/// The PutAsync.
44+
/// </summary>
45+
/// <param name="url">The url.</param>
46+
/// <returns>The <see cref="Task"/>.</returns>
47+
Task<ApiResponse> PutAsync(string url);
48+
49+
/// <summary>
50+
/// The PutAsync.
51+
/// </summary>
52+
/// <typeparam name="T">.</typeparam>
53+
/// <param name="url">The url.</param>
54+
/// <param name="body">The body.</param>
55+
/// <returns>The <see cref="Task"/>.</returns>
56+
Task<ApiResponse> PutAsync<T>(string url, T body)
57+
where T : class, new();
58+
}
59+
}

AdminUI/LearningHub.Nhs.AdminUI/Helpers/LearningActivityHelper.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ public static string GetResourceTypeVerb(this MyLearningDetailedItemViewModel my
8585
case ResourceTypeEnum.Article:
8686
return "Read";
8787
case ResourceTypeEnum.Audio:
88-
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000);
88+
if ((myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000) > myLearningDetailedItemViewModel.ResourceDurationMilliseconds)
89+
{
90+
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ResourceDurationMilliseconds);
91+
}
92+
else
93+
{
94+
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000);
95+
}
96+
8997
case ResourceTypeEnum.Embedded:
9098
return string.Empty;
9199
case ResourceTypeEnum.Equipment:
@@ -113,7 +121,15 @@ public static string GetResourceTypeVerb(this MyLearningDetailedItemViewModel my
113121
}
114122

115123
case ResourceTypeEnum.Video:
116-
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000);
124+
if ((myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000) > myLearningDetailedItemViewModel.ResourceDurationMilliseconds)
125+
{
126+
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ResourceDurationMilliseconds);
127+
}
128+
else
129+
{
130+
return "Played " + GetDurationText(myLearningDetailedItemViewModel.ActivityDurationSeconds * 1000);
131+
}
132+
117133
case ResourceTypeEnum.WebLink:
118134
return "Visited";
119135
case ResourceTypeEnum.Html:
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
namespace LearningHub.Nhs.AdminUI.Helpers
2+
{
3+
using System;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using LearningHub.Nhs.AdminUI.Interfaces;
8+
using LearningHub.Nhs.Models.Common;
9+
using Newtonsoft.Json;
10+
11+
/// <summary>
12+
/// Defines the <see cref="OpenApiFacade" />.
13+
/// </summary>
14+
public class OpenApiFacade : IOpenApiFacade
15+
{
16+
/// <summary>
17+
/// Defines the _client.
18+
/// </summary>
19+
private readonly IOpenApiHttpClient client;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="OpenApiFacade"/> class.
23+
/// </summary>
24+
/// <param name="client">The client<see cref="IOpenApiHttpClient"/>.</param>
25+
public OpenApiFacade(IOpenApiHttpClient client)
26+
{
27+
this.client = client;
28+
}
29+
30+
/// <summary>
31+
/// The GetAsync.
32+
/// </summary>
33+
/// <typeparam name="T">.</typeparam>
34+
/// <param name="url">The url.</param>
35+
/// <returns>The <see cref="Task{T}"/>.</returns>
36+
public async Task<T> GetAsync<T>(string url)
37+
where T : class, new()
38+
{
39+
var client = await this.client.GetClientAsync();
40+
41+
var vm = new T();
42+
43+
var response = await client.GetAsync(url).ConfigureAwait(false);
44+
45+
if (response.IsSuccessStatusCode)
46+
{
47+
var result = response.Content.ReadAsStringAsync().Result;
48+
vm = JsonConvert.DeserializeObject<T>(result);
49+
50+
return vm;
51+
}
52+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
53+
||
54+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
55+
{
56+
throw new Exception("AccessDenied");
57+
}
58+
else
59+
{
60+
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
61+
}
62+
}
63+
64+
/// <summary>
65+
/// The PostAsync.
66+
/// </summary>
67+
/// <typeparam name="T">.</typeparam>
68+
/// <param name="url">The url.</param>
69+
/// <param name="body">The body.</param>
70+
/// <returns>The <see cref="Task"/>.</returns>
71+
public async Task PostAsync<T>(string url, T body)
72+
where T : class, new()
73+
{
74+
var client = await this.client.GetClientAsync();
75+
76+
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
77+
var response = await client.PostAsync(url, content).ConfigureAwait(false);
78+
79+
if (response.IsSuccessStatusCode)
80+
{
81+
return;
82+
}
83+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
84+
||
85+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
86+
{
87+
throw new Exception("AccessDenied");
88+
}
89+
else
90+
{
91+
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
92+
}
93+
}
94+
95+
/// <summary>
96+
/// The PostAsync.
97+
/// </summary>
98+
/// <typeparam name="T">The return type.</typeparam>
99+
/// <typeparam name="TBody">The type of body parameter.</typeparam>
100+
/// <param name="url">The url.</param>
101+
/// <param name="body">The body.</param>
102+
/// <returns>The <see cref="Task{T}"/>.</returns>
103+
public async Task<ApiResponse> PostAsync<T, TBody>(string url, TBody body)
104+
where TBody : class, new()
105+
where T : class, new()
106+
{
107+
var client = await this.client.GetClientAsync();
108+
109+
var vm = new T();
110+
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
111+
var response = await client.PostAsync(url, content).ConfigureAwait(false);
112+
113+
if (response.IsSuccessStatusCode)
114+
{
115+
var result = response.Content.ReadAsStringAsync().Result;
116+
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
117+
if (apiResponse.Success)
118+
{
119+
return apiResponse;
120+
}
121+
else
122+
{
123+
string details = string.Empty;
124+
if (apiResponse.ValidationResult != null)
125+
{
126+
if (apiResponse.ValidationResult.Details != null)
127+
{
128+
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
129+
}
130+
}
131+
132+
throw new Exception($"PostAsync ApiResponse returned False: {details}");
133+
}
134+
}
135+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
136+
||
137+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
138+
{
139+
throw new Exception("Access Denied");
140+
}
141+
else
142+
{
143+
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
144+
}
145+
}
146+
147+
/// <summary>
148+
/// The PutAsync.
149+
/// </summary>
150+
/// <param name="url">The url.</param>
151+
/// <returns>The <see cref="Task"/>.</returns>
152+
public async Task<ApiResponse> PutAsync(string url)
153+
{
154+
var client = await this.client.GetClientAsync();
155+
156+
var response = await client.PutAsync(url, null).ConfigureAwait(false);
157+
158+
if (response.IsSuccessStatusCode)
159+
{
160+
var result = response.Content.ReadAsStringAsync().Result;
161+
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
162+
if (apiResponse.Success)
163+
{
164+
return apiResponse;
165+
}
166+
else
167+
{
168+
string details = string.Empty;
169+
if (apiResponse.ValidationResult != null)
170+
{
171+
if (apiResponse.ValidationResult.Details != null)
172+
{
173+
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
174+
}
175+
}
176+
177+
throw new Exception($"PutAsync ApiResponse returned False: {details}");
178+
}
179+
}
180+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
181+
||
182+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
183+
{
184+
throw new Exception("Access Denied");
185+
}
186+
else
187+
{
188+
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
189+
}
190+
}
191+
192+
/// <summary>
193+
/// The PutAsync.
194+
/// </summary>
195+
/// <typeparam name="T">.</typeparam>
196+
/// <param name="url">The url.</param>
197+
/// <param name="body">The body.</param>
198+
/// <returns>The <see cref="Task"/>.</returns>
199+
public async Task<ApiResponse> PutAsync<T>(string url, T body)
200+
where T : class, new()
201+
{
202+
var client = await this.client.GetClientAsync();
203+
204+
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
205+
var response = await client.PutAsync(url, content).ConfigureAwait(false);
206+
207+
if (response.IsSuccessStatusCode)
208+
{
209+
var result = response.Content.ReadAsStringAsync().Result;
210+
var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(result);
211+
if (apiResponse.Success)
212+
{
213+
return apiResponse;
214+
}
215+
else
216+
{
217+
string details = string.Empty;
218+
if (apiResponse.ValidationResult != null)
219+
{
220+
if (apiResponse.ValidationResult.Details != null)
221+
{
222+
details = $"::ValidationResult: {string.Join(",", apiResponse.ValidationResult.Details)}";
223+
}
224+
}
225+
226+
throw new Exception($"PutAsync ApiResponse returned False: {details}");
227+
}
228+
}
229+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
230+
||
231+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
232+
{
233+
throw new Exception("Access Denied");
234+
}
235+
else
236+
{
237+
throw new Exception($"Exception HttpStatusCode={response.StatusCode}");
238+
}
239+
}
240+
}
241+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LearningHub.Nhs.AdminUI.Interfaces
2+
{
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
6+
/// <summary>
7+
/// The OpenApiHttpClient interface.
8+
/// </summary>
9+
public interface IOpenApiHttpClient
10+
{
11+
/// <summary>
12+
/// The get client.
13+
/// </summary>
14+
/// <returns>The <see cref="Task"/>.</returns>
15+
Task<HttpClient> GetClientAsync();
16+
}
17+
}

AdminUI/LearningHub.Nhs.AdminUI/ServiceCollectionExtension.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
8686
services.AddSingleton(configuration);
8787

8888
services.AddScoped<ILearningHubApiFacade, LearningHubApiFacade>();
89+
services.AddScoped<IOpenApiFacade, OpenApiFacade>();
8990
services.AddScoped<IResourceService, ResourceService>();
9091
services.AddScoped<IUserService, UserService>();
9192
services.AddScoped<IUserGroupService, UserGroupService>();
@@ -132,11 +133,19 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
132133
ServerCertificateCustomValidationCallback =
133134
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
134135
});
136+
services.AddHttpClient<IOpenApiHttpClient, OpenApiHttpClient>()
137+
.ConfigurePrimaryHttpMessageHandler(
138+
() => new HttpClientHandler
139+
{
140+
ServerCertificateCustomValidationCallback =
141+
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
142+
});
135143
}
136144
else
137145
{
138146
services.AddHttpClient<ILearningHubHttpClient, LearningHubHttpClient>();
139147
services.AddHttpClient<IUserApiHttpClient, UserApiHttpClient>();
148+
services.AddHttpClient<IOpenApiHttpClient, OpenApiHttpClient>();
140149
}
141150

142151
services.AddTransient<CookieEventHandler>();

0 commit comments

Comments
 (0)