Skip to content

Commit

Permalink
changes to make the ApiClient abstract and then inherited so it can b…
Browse files Browse the repository at this point in the history
…e setup to use the correct api
  • Loading branch information
sscaife committed Sep 9, 2021
1 parent 591003d commit dd90c52
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class ManageApprenticeshipsOuterApiRegistry : Registry
public ManageApprenticeshipsOuterApiRegistry ()
{
For<ManageApprenticeshipsOuterApiConfiguration>().Use(c => c.GetInstance<EmployerFinanceConfiguration>().ManageApprenticeshipsOuterApiConfiguration).Singleton();
For<IApiClient>().Use<ApiClient>().Ctor<HttpClient>().Is(new HttpClient()).Singleton();
For<ICohortsService>().Use<CohortsService>().Ctor<HttpClient>().Is(new HttpClient()).Ctor<string>().Is(c => c.GetInstance<EmployerFinanceConfiguration>().EmployerCommitmentsBaseUrl).Singleton();
For<IApiClient>().Use<ManageApprenticeshipsApiClient>().Ctor<HttpClient>().Is(new HttpClient()).Singleton();
For<IApiClient>().Add<CommitmentsApiClient>().Named("CAPI").Ctor<HttpClient>().Is(new HttpClient()).Singleton();
For<ICohortsService>().Use<CohortsService>().Ctor<IApiClient>().IsNamedInstance("CAPI").Singleton();
}
}
}
18 changes: 6 additions & 12 deletions src/SFA.DAS.EmployerFinance/Infrastructure/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@

namespace SFA.DAS.EmployerFinance.Infrastructure
{
public class ApiClient : IApiClient
public abstract class ApiClient : IApiClient
{
private readonly HttpClient _httpClient;
private readonly ManageApprenticeshipsOuterApiConfiguration _config;

public ApiClient (
HttpClient httpClient,
ManageApprenticeshipsOuterApiConfiguration options)
protected HttpClient HttpClient => _httpClient;

protected ApiClient (
HttpClient httpClient)
{
_httpClient = httpClient;
_config = options;
_httpClient.BaseAddress = new Uri(_config.BaseUrl);
}

public async Task<TResponse> Get<TResponse>(IGetApiRequest request)
Expand All @@ -32,10 +30,6 @@ public async Task<TResponse> Get<TResponse>(IGetApiRequest request)
return JsonConvert.DeserializeObject<TResponse>(json);
}

private void AddHeaders()
{
_httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _config.Key);
_httpClient.DefaultRequestHeaders.Add("X-Version", "1");
}
protected abstract void AddHeaders();
}
}
23 changes: 23 additions & 0 deletions src/SFA.DAS.EmployerFinance/Infrastructure/CommitmentsApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using SFA.DAS.EmployerFinance.Configuration;

namespace SFA.DAS.EmployerFinance.Infrastructure
{
public class CommitmentsApiClient : ApiClient
{
public CommitmentsApiClient(HttpClient httpClient, EmployerFinanceConfiguration configuration) : base(httpClient)
{
httpClient.BaseAddress = new Uri(configuration.EmployerCommitmentsBaseUrl);
}

protected override void AddHeaders()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using SFA.DAS.EmployerFinance.Configuration;

namespace SFA.DAS.EmployerFinance.Infrastructure
{
public class ManageApprenticeshipsApiClient : ApiClient
{
private readonly ManageApprenticeshipsOuterApiConfiguration _config;

public ManageApprenticeshipsApiClient(HttpClient httpClient, ManageApprenticeshipsOuterApiConfiguration options) : base(httpClient)
{
_config = options;
httpClient.BaseAddress = new Uri(_config.BaseUrl);
}

protected override void AddHeaders()
{
HttpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _config.Key);
HttpClient.DefaultRequestHeaders.Add("X-Version", "1");
}
}
}
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;
using SFA.DAS.CommitmentsV2.Types;
using SFA.DAS.EmployerFinance.Interfaces.OuterApi;

namespace SFA.DAS.EmployerFinance.Infrastructure.OuterApiResponses
{
public class GetCohortsResponse
{
public List<CohortSummary> Cohorts { get; set; }
}
}
15 changes: 5 additions & 10 deletions src/SFA.DAS.EmployerFinance/Services/CohortsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@ namespace SFA.DAS.EmployerFinance.Services
{
public class CohortsService : ICohortsService
{
private readonly HttpClient _httpClient;
private readonly IApiClient _apiClient;

public CohortsService(HttpClient httpClient, string cohortsBaseUrl)
public CohortsService(IApiClient apiClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri(cohortsBaseUrl);
_apiClient = apiClient;
}

public async Task<int> GetCohortsCount(long accountId)
{
var cohortsCountResponse = await _httpClient.GetAsync(new GetCohortsRequest(accountId).GetUrl).ConfigureAwait(false);
var cohortsCountResponse = await _apiClient.Get<GetCohortsResponse>(new GetCohortsRequest(accountId)).ConfigureAwait(false);

cohortsCountResponse.EnsureSuccessStatusCode();
var json = await cohortsCountResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
var currentCohorts = JsonConvert.DeserializeObject<CurrentCohorts>(json);

return currentCohorts.Cohorts.Count;
return cohortsCountResponse.Cohorts.Count;
}
}
}

0 comments on commit dd90c52

Please sign in to comment.