﻿using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure;
using Newtonsoft.Json;
using NLog;
using SFA.DAS.EmployerApprenticeshipsService.Domain;
using SFA.DAS.EmployerApprenticeshipsService.Domain.Interfaces;

namespace SFA.DAS.EmployerApprenticeshipsService.Infrastructure.Services
{
    public class CompaniesHouseEmployerVerificationService : IEmployerVerificationService
    {
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
        private readonly string _apiKey;

        public CompaniesHouseEmployerVerificationService(string apiKey)
        {
            _apiKey = apiKey;
        }

        public async Task<EmployerInformation> GetInformation(string id)
        {
            Logger.Info($"GetInformation({id})");

            var webClient = new WebClient();

            webClient.Headers.Add($"Authorization: Basic {_apiKey}");
            try
            {
                var result = await webClient.DownloadStringTaskAsync($"https://api.companieshouse.gov.uk/company/{id}");

                return JsonConvert.DeserializeObject<EmployerInformation>(result);
            }
            catch (WebException ex)
            {
                Logger.Error(ex, "There was a problem with the call to Companies House");
            }

            return null;
        }
    }
}
﻿using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;
using SFA.DAS.EmployerApprenticeshipsService.Domain;
using SFA.DAS.EmployerApprenticeshipsService.Domain.Configuration;
using SFA.DAS.EmployerApprenticeshipsService.Domain.Interfaces;

namespace SFA.DAS.EmployerApprenticeshipsService.Infrastructure.Services
{
    public class CompaniesHouseEmployerVerificationService : IEmployerVerificationService
    {
        private readonly EmployerApprenticeshipsServiceConfiguration _configuration;
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
        

        public CompaniesHouseEmployerVerificationService(EmployerApprenticeshipsServiceConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async Task<EmployerInformation> GetInformation(string id)
        {
            Logger.Info($"GetInformation({id})");

            var webClient = new WebClient();
            
            webClient.Headers.Add($"Authorization: Basic {_configuration.CompaniesHouse.ApiKey}");
            try
            {
                var result = await webClient.DownloadStringTaskAsync($"https://api.companieshouse.gov.uk/company/{id}");

                return JsonConvert.DeserializeObject<EmployerInformation>(result);
            }
            catch (WebException ex)
            {
                Logger.Error(ex, "There was a problem with the call to Companies House");
            }

            return null;
        }
    }
}
Modify the companies house verification service to use the config initalised as a constructor parameter
Modify the companies house verification service to use the config initalised as a constructor parameter
