From cdd26c55a9807ff5550d159d9eeef3b05c1345b4 Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Fri, 22 Jul 2022 14:58:01 +0700 Subject: [PATCH] use IApiResponse to handle errors easier --- TorchRemote/Services/IRemoteApi.cs | 24 +++++++++---------- .../ViewModels/Server/DashboardViewModel.cs | 9 +++---- .../Server/ServerConfigViewModel.cs | 14 ++++++++--- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/TorchRemote/Services/IRemoteApi.cs b/TorchRemote/Services/IRemoteApi.cs index 38322db..2578852 100644 --- a/TorchRemote/Services/IRemoteApi.cs +++ b/TorchRemote/Services/IRemoteApi.cs @@ -11,37 +11,37 @@ public interface IRemoteApi { #region Server [Get("/server/status")] - Task GetServerStatus(); + Task> GetServerStatus(); [Post("/server/start")] - Task StartServer(); + Task StartServer(); [Post("/server/stop")] - Task StopServer([Body] StopServerRequest request); + Task StopServer([Body] StopServerRequest request); [Get("/server/settings")] - Task GetServerSettings(); + Task> GetServerSettings(); [Post("/server/settings")] - Task SetServerSettings([Body] ServerSettings request); + Task SetServerSettings([Body] ServerSettings request); #endregion #region Chat [Post("/chat/message")] - Task SendChatMessage([Body] ChatMessageRequest request); + Task SendChatMessage([Body] ChatMessageRequest request); [Post("/chat/command")] - Task InvokeChatCommand([Body] ChatCommandRequest request); + Task> InvokeChatCommand([Body] ChatCommandRequest request); #endregion #region Worlds [Get("/worlds")] - Task> GetWorlds(); + Task>> GetWorlds(); [Get("/worlds/selected")] - Task GetSelectedWorld(); + Task> GetSelectedWorld(); [Get("/worlds/{id}")] - Task GetWorld(Guid id); + Task> GetWorld(Guid id); [Post("/worlds/{id}/select")] - Task SelectWorld(Guid id); + Task> SelectWorld(Guid id); #endregion #region Settings [Get("/settings/{id}")] - Task GetSetting(Guid id); + Task> GetSetting(Guid id); #endregion } diff --git a/TorchRemote/ViewModels/Server/DashboardViewModel.cs b/TorchRemote/ViewModels/Server/DashboardViewModel.cs index d82b058..ad19692 100644 --- a/TorchRemote/ViewModels/Server/DashboardViewModel.cs +++ b/TorchRemote/ViewModels/Server/DashboardViewModel.cs @@ -6,6 +6,7 @@ using System.Reactive.Linq; using System.Text.Json; using ReactiveUI; using ReactiveUI.Fody.Helpers; +using Refit; using TorchRemote.Models.Responses; using TorchRemote.Services; namespace TorchRemote.ViewModels.Server; @@ -27,7 +28,7 @@ public class DashboardViewModel : ViewModelBase .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(r => { - var (simSpeed, online, uptime, status) = r; + var (simSpeed, online, uptime, status) = r.Content!; SimSpeed = simSpeed; Status = status; Uptime = uptime; @@ -53,12 +54,12 @@ public class DashboardViewModel : ViewModelBase this.WhenAnyValue(x => x.Status) .Select(b => b is ServerStatus.Stopped)); - StopCommand = ReactiveCommand.CreateFromTask(b => _clientService.Api.StopServer(new(b)), + StopCommand = ReactiveCommand.CreateFromTask(b => _clientService.Api.StopServer(new(b)), this.WhenAnyValue(x => x.Status) .Select(b => b is ServerStatus.Running)); } - public ReactiveCommand StopCommand { get; set; } - public ReactiveCommand StartCommand { get; set; } + public ReactiveCommand StopCommand { get; set; } + public ReactiveCommand StartCommand { get; set; } [Reactive] public double SimSpeed { get; set; } diff --git a/TorchRemote/ViewModels/Server/ServerConfigViewModel.cs b/TorchRemote/ViewModels/Server/ServerConfigViewModel.cs index ed00692..f27d323 100644 --- a/TorchRemote/ViewModels/Server/ServerConfigViewModel.cs +++ b/TorchRemote/ViewModels/Server/ServerConfigViewModel.cs @@ -3,6 +3,7 @@ using System.Reactive; using System.Reactive.Linq; using ReactiveUI; using ReactiveUI.Fody.Helpers; +using Refit; using TorchRemote.Services; namespace TorchRemote.ViewModels.Server; @@ -14,6 +15,7 @@ public class ServerConfigViewModel : ViewModelBase .ObserveOn(RxApp.MainThreadScheduler) .Select(_ => Observable.FromAsync(clientService.Api.GetServerSettings)) .Concat() + .Select(b => b.Content!) .Subscribe(b => { Name = b.ServerName; @@ -37,15 +39,21 @@ public class ServerConfigViewModel : ViewModelBase .ObserveOn(RxApp.MainThreadScheduler) .Select(_ => Observable.FromAsync(clientService.Api.GetWorlds)) .Concat() + .Select(b => b.Content!) .SelectMany(ids => ids) - .Select(id => Observable.FromAsync(() => clientService.Api.GetWorld(id)).Select(b => new World(id, b.Name, b.SizeKb))) + .Select(id => Observable.FromAsync(() => clientService.Api.GetWorld(id)) + .Select(b => b.Content!) + .Select(b => new World(id, b.Name, b.SizeKb))) .Concat(); Observable.FromEventPattern(clientService, nameof(clientService.Connected)) .ObserveOn(RxApp.MainThreadScheduler) .Select(_ => Observable.FromAsync(clientService.Api.GetSelectedWorld)) .Concat() - .Select(id => Observable.FromAsync(() => clientService.Api.GetWorld(id)).Select(b => new World(id, b.Name, b.SizeKb))) + .Select(b => b.Content!) + .Select(id => Observable.FromAsync(() => clientService.Api.GetWorld(id)) + .Select(b => b.Content!) + .Select(b => new World(id, b.Name, b.SizeKb))) .Concat() .BindTo(this, x => x.SelectedWorld); @@ -54,7 +62,7 @@ public class ServerConfigViewModel : ViewModelBase .Concat() .Subscribe(_ => { }); } - public ReactiveCommand SaveCommand { get; set; } + public ReactiveCommand SaveCommand { get; set; } [Reactive] public string Name { get; set; } = null!;