persistence/Persistence.Client/PersistenceClientFactory.cs

49 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Refit;
using Persistence.Client.Helpers;
namespace Persistence.Client
{
public static class PersistenceClientFactory
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
public static T GetClient<T>(HttpClient client)
{
return RestService.For<T>(client, RefitSettings);
}
public static T GetClient<T>(string baseUrl)
{
var client = new HttpClient();
client.BaseAddress = new Uri(baseUrl);
return RestService.For<T>(client, RefitSettings);
}
private static HttpClient GetAuthorizedClient()
{
var httpClient = new HttpClient();
var jwtToken = ApiTokenHelper.GetAdminUserToken();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
return httpClient;
}
}
}