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

View File

@@ -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<bool>(b => _clientService.Api.StopServer(new(b)),
StopCommand = ReactiveCommand.CreateFromTask<bool, IApiResponse>(b => _clientService.Api.StopServer(new(b)),
this.WhenAnyValue(x => x.Status)
.Select(b => b is ServerStatus.Running));
}
public ReactiveCommand<bool,Unit> StopCommand { get; set; }
public ReactiveCommand<Unit,Unit> StartCommand { get; set; }
public ReactiveCommand<bool, IApiResponse> StopCommand { get; set; }
public ReactiveCommand<Unit, IApiResponse> StartCommand { get; set; }
[Reactive]
public double SimSpeed { get; set; }

View File

@@ -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<Unit,Unit> SaveCommand { get; set; }
public ReactiveCommand<Unit, IApiResponse> SaveCommand { get; set; }
[Reactive]
public string Name { get; set; } = null!;