41 lines
No EOL
1.2 KiB
C#
41 lines
No EOL
1.2 KiB
C#
using AdOrbitSDK;
|
|
using StalwartSimpleLoginMiddleware.Constants;
|
|
|
|
namespace StalwartSimpleLoginMiddleware.Services;
|
|
|
|
public class StalwartClient() : IDisposable
|
|
{
|
|
private Client _userClient;
|
|
|
|
private static HttpClient HttpClient => new()
|
|
{
|
|
BaseAddress = new Uri(Environment.GetEnvironmentVariable(EnvironmentVariable.MailServerUri) + "/api/")
|
|
};
|
|
|
|
public async Task<Client> GetClient()
|
|
{
|
|
if (_userClient != null) return _userClient;
|
|
|
|
return GetClient(Environment.GetEnvironmentVariable(EnvironmentVariable.MailServerApiKey) ?? string.Empty);
|
|
}
|
|
|
|
private Client GetClient(string apiKey)
|
|
{
|
|
if (_userClient != null) return _userClient;
|
|
|
|
if (string.IsNullOrEmpty(HttpClient.BaseAddress?.AbsoluteUri))
|
|
throw new NullReferenceException($"{EnvironmentVariable.MailServerUri} is missing in environment.");
|
|
|
|
if (string.IsNullOrEmpty(apiKey))
|
|
throw new NullReferenceException($"{EnvironmentVariable.MailServerApiKey} is missing in environment.");
|
|
|
|
_userClient = new Client(HttpClient, apiKey);
|
|
return _userClient;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
HttpClient.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
} |