using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
using System.Net.Http;

namespace NetworkResurrector.Server.Wrapper.Services.Internal
{
    internal class HttpHeadersService
    {
        private readonly string[] _headersToForward = new string[] { HeaderNames.Authorization };
        private readonly IHttpContextAccessor _httpAccessor;

        public HttpHeadersService(IHttpContextAccessor httpAccessor)
        {
            _httpAccessor=httpAccessor;
        }

        public void SetHeadersFromContext(HttpClient httpClient)
        {
            foreach (var headerKey in _headersToForward)
            {
                var header = _httpAccessor.HttpContext.Request.Headers[headerKey];
                if (header == StringValues.Empty)
                    continue;

                httpClient.DefaultRequestHeaders.Add(headerKey, header.ToString());
            }
        }
    }
}
