using DataSharing.Share.Dtos.YiBin.Enterprise;
using System.Net.Http.Headers;
using XF.Domain.Exceptions;

namespace DataSharing.YiBin.Enterprise
{
    public static class EnterpriseHttpClientExtensions
    {
        public static HttpClient BuildHeader(this HttpClient httpClient, TokenTokenEnterpriseInfo tokenInfo)
        {
            if (tokenInfo == null || string.IsNullOrEmpty(tokenInfo.AccessToken))
                throw new UserFriendlyException("无效token");
            // httpClient.SetToken(tokenInfo.TokenType, tokenInfo.AccessToken);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Add("Blade-Auth", $"{tokenInfo.TokenType} {tokenInfo.AccessToken}");

            return httpClient;
        }

        public static HttpClient SetHttpClient(this HttpClient httpClient, string baseAddress, TokenTokenEnterpriseInfo tokenInfo) =>
            httpClient.SetBaseAddress(baseAddress).BuildHeader(tokenInfo);
    }
}
