diff --git a/Azaion.LoaderUI/AzaionApi.cs b/Azaion.LoaderUI/AzaionApi.cs index effc478..923782c 100644 --- a/Azaion.LoaderUI/AzaionApi.cs +++ b/Azaion.LoaderUI/AzaionApi.cs @@ -3,26 +3,28 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; namespace Azaion.LoaderUI; public interface IAzaionApi { - void Login(ApiCredentials credentials); + Task Validate(ApiCredentials credentials); Task GetLastInstallerName(string folder); Task<(string name, Stream stream)> DownloadInstaller(string folder); } -public class AzaionApi(HttpClient client) : IAzaionApi +public class AzaionApi(HttpClient client, ILogger logger) : IAzaionApi { private string _jwtToken = null!; - const string APP_JSON = "application/json"; + private const string APP_JSON = "application/json"; private ApiCredentials _credentials = null!; - public void Login(ApiCredentials credentials) + public async Task Validate(ApiCredentials credentials) { _credentials = credentials; + await Get("/resources/check"); } public async Task GetLastInstallerName(string folder) diff --git a/Azaion.LoaderUI/Login.xaml.cs b/Azaion.LoaderUI/Login.xaml.cs index fd5352d..48f1493 100644 --- a/Azaion.LoaderUI/Login.xaml.cs +++ b/Azaion.LoaderUI/Login.xaml.cs @@ -1,18 +1,13 @@ using System.Diagnostics; using System.IO; -using System.Text; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using Azaion.Common; -using MessagePack; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using NetMQ; -using NetMQ.Sockets; - namespace Azaion.LoaderUI; @@ -41,24 +36,23 @@ public partial class Login private async void LoginClick(object sender, RoutedEventArgs e) { - var creds = new ApiCredentials + var credentials = new ApiCredentials { Email = TbEmail.Text, Password = TbPassword.Password }; - if (string.IsNullOrWhiteSpace(creds.Email) || string.IsNullOrWhiteSpace(creds.Password)) + if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password)) return; try { SetControlsStatus(isLoading: true); - _azaionApi.Login(creds); - Validate(creds); + await _azaionApi.Validate(credentials); TbStatus.Foreground = Brushes.Black; var localVersion = Constants.GetLocalVersion(); var installerVersion = await GetInstallerVer() ?? localVersion; - var credsEncrypted = Security.Encrypt(creds); + var credsEncrypted = Security.Encrypt(credentials); if (installerVersion > localVersion) { @@ -97,69 +91,6 @@ public partial class Login } } - private void Validate(ApiCredentials creds) - { - var dealer = new DealerSocket(); - try - { - using var process = new Process(); - process.StartInfo = new ProcessStartInfo - { - FileName = Constants.EXTERNAL_LOADER_PATH, - Arguments = $"--port {ConstantsLoader.EXTERNAL_LOADER_PORT} --api {Constants.DEFAULT_API_URL}", - CreateNoWindow = true - }; - process.Start(); - dealer.Options.Identity = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString("N")); - dealer.Connect($"tcp://{Constants.DEFAULT_ZMQ_INFERENCE_HOST}:{ConstantsLoader.EXTERNAL_LOADER_PORT}"); - - var result = SendCommand(dealer, RemoteCommand.Create(CommandType.Login, creds)); - if (result.CommandType != CommandType.Ok) - throw new Exception(result.Message); - - result = SendCommand(dealer, RemoteCommand.Create(CommandType.CheckResource)); - if (result.CommandType != CommandType.Ok) - throw new Exception(result.Message); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - throw; - } - finally - { - SendCommand(dealer, RemoteCommand.Create(CommandType.Exit)); - dealer.Close(); - dealer.Dispose(); - } - } - - private RemoteCommand SendCommand(DealerSocket dealer, RemoteCommand command, int retryCount = 30, int retryDelayMs = 800) - { - try - { - dealer.SendFrame(MessagePackSerializer.Serialize(command)); - - var tryNum = 0; - while (tryNum++ < retryCount) - { - if (!dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(retryDelayMs), out var bytes)) - continue; - var res = MessagePackSerializer.Deserialize(bytes); - if (res.CommandType == CommandType.Error) - throw new Exception(res.Message); - return res; - } - - throw new Exception($"Sent {command} {retryCount} times, with wait time {retryDelayMs}ms for each call. No response from client."); - } - catch (Exception e) - { - _logger.LogError(e, e.Message); - throw; - } - } - private async Task GetInstallerVer() { TbStatus.Text = "Checking for the newer version...";