-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CON-2182 Conftent api for employer finance
- Loading branch information
1 parent
0a35f7a
commit 84d4b36
Showing
20 changed files
with
416 additions
and
4 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
18 changes: 18 additions & 0 deletions
18
src/SFA.DAS.EmployerFinance/Configuration/ContentClientApiConfiguration.cs
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,18 @@ | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Configuration | ||
{ | ||
public class ContentClientApiConfiguration : IContentClientApiConfiguration | ||
{ | ||
public string ApiBaseUrl { get; set; } | ||
public string Tenant { get; set; } | ||
public string ClientId { get; set; } | ||
public string ClientSecret { get; set; } | ||
public string IdentifierUri { 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
41 changes: 41 additions & 0 deletions
41
src/SFA.DAS.EmployerFinance/DependencyResolution/ContentApiClientRegistry.cs
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,41 @@ | ||
//using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerFinance.Configuration; | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
using SFA.DAS.EmployerFinance.Services; | ||
using SFA.DAS.Http; | ||
using SFA.DAS.Http.TokenGenerators; | ||
using SFA.DAS.NLog.Logger.Web.MessageHandlers; | ||
using StructureMap; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.DependencyResolution | ||
{ | ||
public class ContentApiClientRegistry : Registry | ||
{ | ||
public ContentApiClientRegistry() | ||
{ | ||
For<ContentClientApiConfiguration>().Use(c => c.GetInstance<EmployerFinanceConfiguration>().ContentApi); | ||
For<IContentClientApiConfiguration>().Use(c => c.GetInstance<ContentClientApiConfiguration>()); | ||
For<IClientContentApiClient>().Use<ClientContentApiClient>().Ctor<HttpClient>().Is(c => CreateClient(c)); | ||
} | ||
private HttpClient CreateClient(IContext context) | ||
{ | ||
var config = context.GetInstance<EmployerFinanceConfiguration>().ContentApi; | ||
|
||
HttpClient httpClient = new HttpClientBuilder() | ||
.WithBearerAuthorisationHeader(new AzureActiveDirectoryBearerTokenGenerator(config)) | ||
.WithHandler(new RequestIdMessageRequestHandler()) | ||
.WithHandler(new SessionIdMessageRequestHandler()) | ||
.WithDefaultHeaders() | ||
.Build(); | ||
|
||
return httpClient; | ||
} | ||
|
||
} | ||
} |
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,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Helpers | ||
{ | ||
// Borrowed from https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.Core/AsyncHelper.cs | ||
// [ASP.NET Identity] | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// MIT License | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
public static class AsyncHelper | ||
{ | ||
private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, | ||
TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); | ||
|
||
public static TResult RunSync<TResult>(Func<Task<TResult>> func) | ||
{ | ||
var cultureUi = CultureInfo.CurrentUICulture; | ||
var culture = CultureInfo.CurrentCulture; | ||
return _myTaskFactory.StartNew(() => | ||
{ | ||
Thread.CurrentThread.CurrentCulture = culture; | ||
Thread.CurrentThread.CurrentUICulture = cultureUi; | ||
return func(); | ||
}).Unwrap().GetAwaiter().GetResult(); | ||
} | ||
|
||
public static void RunSync(Func<Task> func) | ||
{ | ||
var cultureUi = CultureInfo.CurrentUICulture; | ||
var culture = CultureInfo.CurrentCulture; | ||
_myTaskFactory.StartNew(() => | ||
{ | ||
Thread.CurrentThread.CurrentCulture = culture; | ||
Thread.CurrentThread.CurrentUICulture = cultureUi; | ||
return func(); | ||
}).Unwrap().GetAwaiter().GetResult(); | ||
} | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/SFA.DAS.EmployerFinance/Interfaces/ICacheStorageService.cs
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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Interfaces | ||
{ | ||
public interface ICacheStorageService | ||
{ | ||
Task Save<T>(string key, T item, int expirationInMinutes); | ||
Task Delete(string key); | ||
bool TryGet(string key, out string value); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/SFA.DAS.EmployerFinance/Interfaces/IClientContentApiClient.cs
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,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Interfaces | ||
{ | ||
public interface IClientContentApiClient | ||
{ | ||
Task<string> Get(string type, string applicationId); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/SFA.DAS.EmployerFinance/Interfaces/IClientContentService.cs
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,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerFinance.Interfaces | ||
{ | ||
public interface IClientContentService | ||
{ | ||
Task<string> Get(string type, string applicationId); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/SFA.DAS.EmployerFinance/Interfaces/IContentClientApiConfiguration.cs
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,8 @@ | ||
using SFA.DAS.Http.Configuration; | ||
|
||
namespace SFA.DAS.EmployerFinance.Interfaces | ||
{ | ||
public interface IContentClientApiConfiguration : IAzureActiveDirectoryClientConfiguration | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/SFA.DAS.EmployerFinance/Queries/GetClientContent/GetClientContentRequest.cs
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,10 @@ | ||
using MediatR; | ||
|
||
namespace SFA.DAS.EmployerFinance.Queries.GetClientContent | ||
{ | ||
public class GetClientContentRequest : IAsyncRequest<GetClientContentResponse> | ||
{ | ||
public string ContentType { get; set; } | ||
public bool UseLegacyStyles { get; set; } | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/SFA.DAS.EmployerFinance/Queries/GetClientContent/GetClientContentRequestHandler.cs
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,87 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using SFA.DAS.Authentication.Extensions.Legacy; | ||
using SFA.DAS.EmployerFinance.Configuration; | ||
using SFA.DAS.EmployerFinance.Interfaces; | ||
//using SFA.DAS.EmployerAccounts.Configuration; | ||
//using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.NLog.Logger; | ||
using SFA.DAS.Validation; | ||
|
||
namespace SFA.DAS.EmployerFinance.Queries.GetClientContent | ||
{ | ||
public class GetClientContentRequestHandler : IAsyncRequestHandler<GetClientContentRequest, GetClientContentResponse> | ||
{ | ||
private readonly IValidator<GetClientContentRequest> _validator; | ||
private readonly ILog _logger; | ||
private readonly IClientContentService _service; | ||
private readonly ICacheStorageService _cacheStorageService; | ||
private readonly EmployerFinanceConfiguration _employerFinanceConfiguration; | ||
|
||
public GetClientContentRequestHandler( | ||
IValidator<GetClientContentRequest> validator, | ||
ILog logger, | ||
IClientContentService service, | ||
ICacheStorageService cacheStorageService, | ||
//EmployerAccountsConfiguration employerAccountsConfiguration) | ||
EmployerFinanceConfiguration employerFinanceConfiguration | ||
) | ||
{ | ||
_validator = validator; | ||
_logger = logger; | ||
_service = service; | ||
_cacheStorageService = cacheStorageService; | ||
_employerFinanceConfiguration = employerFinanceConfiguration; | ||
} | ||
|
||
public async Task<GetClientContentResponse> Handle(GetClientContentRequest message) | ||
{ | ||
var validationResult = _validator.Validate(message); | ||
|
||
if (!validationResult.IsValid()) | ||
{ | ||
throw new InvalidRequestException(validationResult.ValidationDictionary); | ||
} | ||
|
||
//var applicationId = message.UseLegacyStyles ? _employerFinanceConfiguration.ApplicationId + "-legacy" : _employerFinanceConfiguration.ApplicationId; | ||
|
||
//var applicationId = message.UseLegacyStyles ? "das-employeraccounts-web-legacy" : "das-employeraccounts-web"; | ||
var applicationId = message.UseLegacyStyles ? "das-employerfinance-web-legacy" : "das-employerfinance-web"; | ||
|
||
var cacheKey = $"{applicationId}_{message.ContentType}".ToLowerInvariant(); | ||
|
||
try | ||
{ | ||
if (_cacheStorageService.TryGet(cacheKey, out string cachedContentBanner)) | ||
{ | ||
return new GetClientContentResponse | ||
{ | ||
Content = cachedContentBanner | ||
}; | ||
} | ||
|
||
var contentBanner = await _service.Get(message.ContentType, applicationId); | ||
|
||
if (contentBanner != null) | ||
{ | ||
await _cacheStorageService.Save(cacheKey, contentBanner, 1); | ||
} | ||
return new GetClientContentResponse | ||
{ | ||
Content = contentBanner | ||
}; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.Error(ex, $"Failed to get Content for {cacheKey}"); | ||
|
||
return new GetClientContentResponse | ||
{ | ||
HasFailed = true | ||
}; | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/SFA.DAS.EmployerFinance/Queries/GetClientContent/GetClientContentRequestValidator.cs
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,25 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using SFA.DAS.Validation; | ||
|
||
namespace SFA.DAS.EmployerFinance.Queries.GetClientContent | ||
{ | ||
public class GetClientContentRequestValidator : IValidator<GetClientContentRequest> | ||
{ | ||
public ValidationResult Validate(GetClientContentRequest item) | ||
{ | ||
var validationResult = new ValidationResult(); | ||
|
||
if (string.IsNullOrEmpty(item.ContentType)) | ||
{ | ||
validationResult.AddError(nameof(item.ContentType), "Type has not been supplied"); | ||
} | ||
return validationResult; | ||
} | ||
|
||
public Task<ValidationResult> ValidateAsync(GetClientContentRequest item) | ||
{ | ||
return Task.FromResult(Validate(item)); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/SFA.DAS.EmployerFinance/Queries/GetClientContent/GetClientContentResponse.cs
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,8 @@ | ||
namespace SFA.DAS.EmployerFinance.Queries.GetClientContent | ||
{ | ||
public class GetClientContentResponse | ||
{ | ||
public string Content { get; set; } | ||
public bool HasFailed { get; set; } | ||
} | ||
} |
Oops, something went wrong.