using IdentityModel.Client;
using System.Net.Http.Headers;
using XF.Domain.Exceptions;

namespace DataSharing.Province
{
    public static class HttpClientExtensions
    {
        public static HttpClient BuildHeader(this HttpClient httpClient, string token)
        {
            if (string.IsNullOrEmpty(token))
                throw new UserFriendlyException("无效token");
            httpClient.SetBearerToken(token);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            return httpClient;
        }

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

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