-
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-4835-EAS calls Provider Registrations API using MI credentials
- Loading branch information
1 parent
2cbb6f6
commit 4a5ef23
Showing
9 changed files
with
144 additions
and
58 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
39 changes: 11 additions & 28 deletions
39
...mployerAccounts/Commands/UnsubscribeProviderEmail/UnsubscribeProviderEmailQueryHandler.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 |
---|---|---|
@@ -1,42 +1,25 @@ | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.NLog.Logger; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Commands.UnsubscribeProviderEmail | ||
{ | ||
public class UnsubscribeProviderEmailQueryHandler : AsyncRequestHandler<UnsubscribeProviderEmailQuery> | ||
{ | ||
private readonly EmployerAccountsConfiguration _configuration; | ||
private readonly IHttpService _httpService; | ||
{ | ||
private readonly IProviderRegistrationApiClient _providerRegistrationApiClient; | ||
private readonly ILog _logger; | ||
|
||
public UnsubscribeProviderEmailQueryHandler( | ||
IHttpServiceFactory httpServiceFactory, | ||
EmployerAccountsConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
_httpService = httpServiceFactory.Create( | ||
configuration.ProviderRegistrationsApi.ClientId, | ||
configuration.ProviderRegistrationsApi.ClientSecret, | ||
configuration.ProviderRegistrationsApi.IdentifierUri, | ||
configuration.ProviderRegistrationsApi.Tenant | ||
); | ||
public UnsubscribeProviderEmailQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger) | ||
{ | ||
_providerRegistrationApiClient = providerRegistrationApiClient; | ||
_logger = logger; | ||
} | ||
|
||
protected override async Task HandleCore(UnsubscribeProviderEmailQuery message) | ||
{ | ||
var baseUrl = GetBaseUrl(); | ||
var url = $"{baseUrl}api/unsubscribe/{message.CorrelationId.ToString()}"; | ||
await _httpService.GetAsync(url, false); | ||
} | ||
|
||
private string GetBaseUrl() | ||
{ | ||
var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/") | ||
? _configuration.ProviderRegistrationsApi.BaseUrl | ||
: _configuration.ProviderRegistrationsApi.BaseUrl + "/"; | ||
|
||
return baseUrl; | ||
} | ||
await _providerRegistrationApiClient.Unsubscribe(message.CorrelationId.ToString()); | ||
_logger.Info($"Sent ProviderEmail to Unsubscribe {message.CorrelationId}"); | ||
} | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/SFA.DAS.EmployerAccounts/Configuration/ProviderRegistrationClientApiConfiguration.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 SFA.DAS.EmployerAccounts.Interfaces; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Configuration | ||
{ | ||
public class ProviderRegistrationClientApiConfiguration : IProviderRegistrationClientApiConfiguration | ||
{ | ||
public string BaseUrl { get; set; } | ||
public string IdentifierUri { get; set; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/SFA.DAS.EmployerAccounts/DependencyResolution/ProviderRegistrationApiClientRegistry.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,33 @@ | ||
using System.Net.Http; | ||
using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.EmployerAccounts.Services; | ||
using SFA.DAS.Http; | ||
using SFA.DAS.NLog.Logger.Web.MessageHandlers; | ||
using StructureMap; | ||
|
||
namespace SFA.DAS.EmployerAccounts.DependencyResolution | ||
{ | ||
public class ProviderRegistrationApiClientRegistry : Registry | ||
{ | ||
public ProviderRegistrationApiClientRegistry() | ||
{ | ||
For<ProviderRegistrationClientApiConfiguration>().Use(c => c.GetInstance<EmployerAccountsConfiguration>().ProviderRegistrationsApi); | ||
For<IProviderRegistrationClientApiConfiguration>().Use(c => c.GetInstance<ProviderRegistrationClientApiConfiguration>()); | ||
For<IProviderRegistrationApiClient>().Use<ProviderRegistrationApiClient>() | ||
.Ctor<HttpClient>().Is(c => CreateClient(c)); | ||
} | ||
|
||
private HttpClient CreateClient(IContext context) | ||
{ | ||
HttpClient httpClient = new HttpClientBuilder() | ||
.WithHandler(new RequestIdMessageRequestHandler()) | ||
.WithHandler(new SessionIdMessageRequestHandler()) | ||
.WithDefaultHeaders() | ||
.Build(); | ||
|
||
|
||
return httpClient; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationApiClient.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,11 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Interfaces | ||
{ | ||
public interface IProviderRegistrationApiClient | ||
{ | ||
Task Unsubscribe(string CorrelationId); | ||
|
||
Task<string> GetInvitations(string CorrelationId); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/SFA.DAS.EmployerAccounts/Interfaces/IProviderRegistrationClientApiConfiguration.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 @@ | ||
| ||
namespace SFA.DAS.EmployerAccounts.Interfaces | ||
{ | ||
public interface IProviderRegistrationClientApiConfiguration | ||
{ | ||
string BaseUrl { get; } | ||
string IdentifierUri { get; } | ||
} | ||
} |
38 changes: 10 additions & 28 deletions
38
...A.DAS.EmployerAccounts/Queries/GetProviderInvitation/GetProviderInvitationQueryHandler.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 |
---|---|---|
@@ -1,49 +1,31 @@ | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using SFA.DAS.EmployerAccounts.Configuration; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.EmployerAccounts.Models; | ||
using Newtonsoft.Json; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
using SFA.DAS.NLog.Logger; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Queries.GetProviderInvitation | ||
{ | ||
public class GetProviderInvitationQueryHandler : IAsyncRequestHandler<GetProviderInvitationQuery, GetProviderInvitationResponse> | ||
{ | ||
private readonly EmployerAccountsConfiguration _configuration; | ||
private readonly IHttpService _httpService; | ||
{ | ||
private readonly IProviderRegistrationApiClient _providerRegistrationApiClient; | ||
private readonly ILog _logger; | ||
|
||
public GetProviderInvitationQueryHandler( | ||
IHttpServiceFactory httpServiceFactory, | ||
EmployerAccountsConfiguration configuration) | ||
public GetProviderInvitationQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger) | ||
{ | ||
_configuration = configuration; | ||
_httpService = httpServiceFactory.Create( | ||
configuration.ProviderRegistrationsApi.ClientId, | ||
configuration.ProviderRegistrationsApi.ClientSecret, | ||
configuration.ProviderRegistrationsApi.IdentifierUri, | ||
configuration.ProviderRegistrationsApi.Tenant | ||
); | ||
_providerRegistrationApiClient = providerRegistrationApiClient; | ||
_logger = logger; | ||
} | ||
|
||
public async Task<GetProviderInvitationResponse> Handle(GetProviderInvitationQuery message) | ||
{ | ||
var baseUrl = GetBaseUrl(); | ||
var url = $"{baseUrl}api/invitations/{message.CorrelationId.ToString()}"; | ||
var json = await _httpService.GetAsync(url, false); | ||
|
||
var json = await _providerRegistrationApiClient.GetInvitations(message.CorrelationId.ToString()); | ||
_logger.Info($"Get Invitations for {message.CorrelationId}"); | ||
return new GetProviderInvitationResponse | ||
{ | ||
Result = json == null ? null : JsonConvert.DeserializeObject<ProviderInvitation>(json) | ||
}; | ||
} | ||
|
||
private string GetBaseUrl() | ||
{ | ||
var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/") | ||
? _configuration.ProviderRegistrationsApi.BaseUrl | ||
: _configuration.ProviderRegistrationsApi.BaseUrl + "/"; | ||
|
||
return baseUrl; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/SFA.DAS.EmployerAccounts/Services/ProviderRegistrationApiClient.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,57 @@ | ||
using System.Configuration; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Services.AppAuthentication; | ||
using SFA.DAS.Authentication.Extensions.Legacy; | ||
using SFA.DAS.EmployerAccounts.Interfaces; | ||
|
||
namespace SFA.DAS.EmployerAccounts.Services | ||
{ | ||
public class ProviderRegistrationApiClient : ApiClientBase, IProviderRegistrationApiClient | ||
{ | ||
private readonly string _apiBaseUrl; | ||
private readonly string _identifierUri; | ||
private readonly HttpClient _client; | ||
|
||
public ProviderRegistrationApiClient(HttpClient client, IProviderRegistrationClientApiConfiguration configuration) : base(client) | ||
{ | ||
_apiBaseUrl = configuration.BaseUrl.EndsWith("/") | ||
? configuration.BaseUrl | ||
: configuration.BaseUrl + "/"; | ||
|
||
_identifierUri = configuration.IdentifierUri; | ||
_client = client; | ||
} | ||
|
||
public async Task Unsubscribe(string CorrelationId) | ||
{ | ||
await AddAuthenticationHeader(); | ||
|
||
var url = $"{_apiBaseUrl}api/unsubscribe/{CorrelationId}"; | ||
await _client.GetAsync(url); | ||
} | ||
|
||
public async Task<string> GetInvitations(string CorrelationId) | ||
{ | ||
await AddAuthenticationHeader(); | ||
|
||
var url = $"{_apiBaseUrl}api/invitations/{CorrelationId}"; | ||
var response = await _client.GetAsync(url); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
return await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
} | ||
|
||
private async Task AddAuthenticationHeader() | ||
{ | ||
if (ConfigurationManager.AppSettings["EnvironmentName"].ToUpper() != "LOCAL") | ||
{ | ||
var azureServiceTokenProvider = new AzureServiceTokenProvider(); | ||
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(_identifierUri); | ||
|
||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); | ||
} | ||
} | ||
} | ||
} |