use IApiResponse to handle errors easier

This commit is contained in:
zznty
2022-07-22 14:58:01 +07:00
parent 62dcd27a9d
commit cdd26c55a9
3 changed files with 28 additions and 19 deletions

View File

@@ -11,37 +11,37 @@ public interface IRemoteApi
{ {
#region Server #region Server
[Get("/server/status")] [Get("/server/status")]
Task<ServerStatusResponse> GetServerStatus(); Task<IApiResponse<ServerStatusResponse>> GetServerStatus();
[Post("/server/start")] [Post("/server/start")]
Task StartServer(); Task<IApiResponse> StartServer();
[Post("/server/stop")] [Post("/server/stop")]
Task StopServer([Body] StopServerRequest request); Task<IApiResponse> StopServer([Body] StopServerRequest request);
[Get("/server/settings")] [Get("/server/settings")]
Task<ServerSettings> GetServerSettings(); Task<IApiResponse<ServerSettings>> GetServerSettings();
[Post("/server/settings")] [Post("/server/settings")]
Task SetServerSettings([Body] ServerSettings request); Task<IApiResponse> SetServerSettings([Body] ServerSettings request);
#endregion #endregion
#region Chat #region Chat
[Post("/chat/message")] [Post("/chat/message")]
Task SendChatMessage([Body] ChatMessageRequest request); Task<IApiResponse> SendChatMessage([Body] ChatMessageRequest request);
[Post("/chat/command")] [Post("/chat/command")]
Task<Guid> InvokeChatCommand([Body] ChatCommandRequest request); Task<IApiResponse<Guid>> InvokeChatCommand([Body] ChatCommandRequest request);
#endregion #endregion
#region Worlds #region Worlds
[Get("/worlds")] [Get("/worlds")]
Task<IEnumerable<Guid>> GetWorlds(); Task<IApiResponse<IEnumerable<Guid>>> GetWorlds();
[Get("/worlds/selected")] [Get("/worlds/selected")]
Task<Guid> GetSelectedWorld(); Task<IApiResponse<Guid>> GetSelectedWorld();
[Get("/worlds/{id}")] [Get("/worlds/{id}")]
Task<WorldResponse> GetWorld(Guid id); Task<IApiResponse<WorldResponse>> GetWorld(Guid id);
[Post("/worlds/{id}/select")] [Post("/worlds/{id}/select")]
Task SelectWorld(Guid id); Task<IApiResponse<IApiResponse>> SelectWorld(Guid id);
#endregion #endregion
#region Settings #region Settings
[Get("/settings/{id}")] [Get("/settings/{id}")]
Task<SettingInfoResponse> GetSetting(Guid id); Task<IApiResponse<SettingInfoResponse>> GetSetting(Guid id);
#endregion #endregion
} }

View File

@@ -6,6 +6,7 @@ using System.Reactive.Linq;
using System.Text.Json; using System.Text.Json;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Refit;
using TorchRemote.Models.Responses; using TorchRemote.Models.Responses;
using TorchRemote.Services; using TorchRemote.Services;
namespace TorchRemote.ViewModels.Server; namespace TorchRemote.ViewModels.Server;
@@ -27,7 +28,7 @@ public class DashboardViewModel : ViewModelBase
.ObserveOn(RxApp.MainThreadScheduler) .ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(r => .Subscribe(r =>
{ {
var (simSpeed, online, uptime, status) = r; var (simSpeed, online, uptime, status) = r.Content!;
SimSpeed = simSpeed; SimSpeed = simSpeed;
Status = status; Status = status;
Uptime = uptime; Uptime = uptime;
@@ -53,12 +54,12 @@ public class DashboardViewModel : ViewModelBase
this.WhenAnyValue(x => x.Status) this.WhenAnyValue(x => x.Status)
.Select(b => b is ServerStatus.Stopped)); .Select(b => b is ServerStatus.Stopped));
StopCommand = ReactiveCommand.CreateFromTask<bool>(b => _clientService.Api.StopServer(new(b)), StopCommand = ReactiveCommand.CreateFromTask<bool, IApiResponse>(b => _clientService.Api.StopServer(new(b)),
this.WhenAnyValue(x => x.Status) this.WhenAnyValue(x => x.Status)
.Select(b => b is ServerStatus.Running)); .Select(b => b is ServerStatus.Running));
} }
public ReactiveCommand<bool,Unit> StopCommand { get; set; } public ReactiveCommand<bool, IApiResponse> StopCommand { get; set; }
public ReactiveCommand<Unit,Unit> StartCommand { get; set; } public ReactiveCommand<Unit, IApiResponse> StartCommand { get; set; }
[Reactive] [Reactive]
public double SimSpeed { get; set; } public double SimSpeed { get; set; }

View File

@@ -3,6 +3,7 @@ using System.Reactive;
using System.Reactive.Linq; using System.Reactive.Linq;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Refit;
using TorchRemote.Services; using TorchRemote.Services;
namespace TorchRemote.ViewModels.Server; namespace TorchRemote.ViewModels.Server;
@@ -14,6 +15,7 @@ public class ServerConfigViewModel : ViewModelBase
.ObserveOn(RxApp.MainThreadScheduler) .ObserveOn(RxApp.MainThreadScheduler)
.Select(_ => Observable.FromAsync(clientService.Api.GetServerSettings)) .Select(_ => Observable.FromAsync(clientService.Api.GetServerSettings))
.Concat() .Concat()
.Select(b => b.Content!)
.Subscribe(b => .Subscribe(b =>
{ {
Name = b.ServerName; Name = b.ServerName;
@@ -37,15 +39,21 @@ public class ServerConfigViewModel : ViewModelBase
.ObserveOn(RxApp.MainThreadScheduler) .ObserveOn(RxApp.MainThreadScheduler)
.Select(_ => Observable.FromAsync(clientService.Api.GetWorlds)) .Select(_ => Observable.FromAsync(clientService.Api.GetWorlds))
.Concat() .Concat()
.Select(b => b.Content!)
.SelectMany(ids => ids) .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(); .Concat();
Observable.FromEventPattern(clientService, nameof(clientService.Connected)) Observable.FromEventPattern(clientService, nameof(clientService.Connected))
.ObserveOn(RxApp.MainThreadScheduler) .ObserveOn(RxApp.MainThreadScheduler)
.Select(_ => Observable.FromAsync(clientService.Api.GetSelectedWorld)) .Select(_ => Observable.FromAsync(clientService.Api.GetSelectedWorld))
.Concat() .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() .Concat()
.BindTo(this, x => x.SelectedWorld); .BindTo(this, x => x.SelectedWorld);
@@ -54,7 +62,7 @@ public class ServerConfigViewModel : ViewModelBase
.Concat() .Concat()
.Subscribe(_ => { }); .Subscribe(_ => { });
} }
public ReactiveCommand<Unit,Unit> SaveCommand { get; set; } public ReactiveCommand<Unit, IApiResponse> SaveCommand { get; set; }
[Reactive] [Reactive]
public string Name { get; set; } = null!; public string Name { get; set; } = null!;