You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
1 year ago
|
using Refit;
|
||
1 year ago
|
using StabilityMatrix.Core.Services;
|
||
1 year ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Api;
|
||
1 year ago
|
|
||
|
public class A3WebApiManager : IA3WebApiManager
|
||
|
{
|
||
1 year ago
|
private IA3WebApi? client;
|
||
1 year ago
|
public IA3WebApi Client
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
// Return the existing client if it exists
|
||
1 year ago
|
if (client != null)
|
||
1 year ago
|
{
|
||
1 year ago
|
return client;
|
||
1 year ago
|
}
|
||
|
// Create a new client and store it otherwise
|
||
1 year ago
|
client = CreateClient();
|
||
|
return client;
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
private readonly ISettingsManager settingsManager;
|
||
1 year ago
|
private readonly IHttpClientFactory httpClientFactory;
|
||
1 year ago
|
public RefitSettings? RefitSettings { get; init; }
|
||
|
public string? BaseUrl { get; set; }
|
||
|
|
||
1 year ago
|
public A3WebApiManager(ISettingsManager settingsManager, IHttpClientFactory httpClientFactory)
|
||
1 year ago
|
{
|
||
|
this.settingsManager = settingsManager;
|
||
1 year ago
|
this.httpClientFactory = httpClientFactory;
|
||
1 year ago
|
}
|
||
|
|
||
|
public void ResetClient()
|
||
|
{
|
||
1 year ago
|
client = null;
|
||
1 year ago
|
}
|
||
|
|
||
|
private IA3WebApi CreateClient()
|
||
|
{
|
||
|
var settings = settingsManager.Settings;
|
||
|
|
||
|
// First check override
|
||
|
if (settings.WebApiHost != null)
|
||
|
{
|
||
|
BaseUrl = settings.WebApiHost;
|
||
|
|
||
|
if (settings.WebApiPort != null)
|
||
|
{
|
||
|
BaseUrl += $":{settings.WebApiPort}";
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Otherwise use default
|
||
|
BaseUrl = "http://localhost:7860";
|
||
|
}
|
||
|
|
||
1 year ago
|
var httpClient = httpClientFactory.CreateClient("A3Client");
|
||
|
httpClient.BaseAddress = new Uri(BaseUrl);
|
||
|
var api = RestService.For<IA3WebApi>(httpClient, RefitSettings);
|
||
|
return api;
|
||
1 year ago
|
}
|
||
|
}
|