From bada677e67a35c938c7fd97a861d8bd064297fda Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Thu, 6 Oct 2022 20:56:45 +0700 Subject: [PATCH] refactor downloader full plugins list\ bump version --- TorchRemote.Models/Responses/PluginInfo.cs | 21 +++++++++---- .../Controllers/PluginDownloadsController.cs | 30 +++++++++---------- TorchRemote.Plugin/manifest.xml | 2 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/TorchRemote.Models/Responses/PluginInfo.cs b/TorchRemote.Models/Responses/PluginInfo.cs index 3355692..5e12c70 100644 --- a/TorchRemote.Models/Responses/PluginInfo.cs +++ b/TorchRemote.Models/Responses/PluginInfo.cs @@ -1,8 +1,19 @@ -namespace TorchRemote.Models.Responses; +using System.Text.Json.Serialization; + +namespace TorchRemote.Models.Responses; public record PluginInfo(Guid Id, string Name, string Version); +public record InstalledPluginInfo(Guid Id, string Name, string Version, string? SettingId) : PluginInfo(Id, Name, Version); -public record PluginItemInfo(Guid Id, string Name, string Version, string Author) : PluginInfo(Id, Name, Version); - -public record FullPluginItemInfo(Guid Id, string Name, string Description, string Version, string Author) : PluginItemInfo(Id, Name, Version, Author); -public record InstalledPluginInfo(Guid Id, string Name, string Version, string? SettingId) : PluginInfo(Id, Name, Version); \ No newline at end of file +public record PluginItemInfo( + [property: JsonPropertyName("guid")] Guid Id, + string Name, + string Author, + string Description, + int Downloads, + bool Archived, + bool Private, + string LatestVersion, + IReadOnlyList Versions, + string Icon +); \ No newline at end of file diff --git a/TorchRemote.Plugin/Controllers/PluginDownloadsController.cs b/TorchRemote.Plugin/Controllers/PluginDownloadsController.cs index fa42b36..17d10c8 100644 --- a/TorchRemote.Plugin/Controllers/PluginDownloadsController.cs +++ b/TorchRemote.Plugin/Controllers/PluginDownloadsController.cs @@ -1,4 +1,6 @@ -using EmbedIO; +using System.Net.Http; +using System.Text.Json; +using EmbedIO; using EmbedIO.Routing; using EmbedIO.WebApi; using Torch.API.WebAPI; @@ -7,27 +9,25 @@ using TorchRemote.Plugin.Utils; namespace TorchRemote.Plugin.Controllers; +public record PluginsResponse(IReadOnlyList Plugins); + public class PluginDownloadsController : WebApiController { private const string RootPath = "/plugins/downloads"; + private const string BaseAddress = "https://torchapi.com/"; [Route(HttpVerbs.Get, RootPath)] - public async Task> GetAsync() + public async Task> GetAsync() { - var response = await PluginQuery.Instance.QueryAll(); - return response.Plugins.Select(b => new PluginItemInfo(Guid.Parse(b.ID), b.Name, b.LatestVersion, b.Author)); - } - - [Route(HttpVerbs.Get, $"{RootPath}/{{id}}")] - public async Task GetFullAsync(Guid id) - { - var response = await PluginQuery.Instance.QueryOne(id); + using var client = new HttpClient() + { + BaseAddress = new(BaseAddress) + }; + using var stream = await client.GetStreamAsync("api/plugins"); - if (response is null) - throw HttpException.NotFound("Plugin not found", id); + var response = await JsonSerializer.DeserializeAsync(stream, Statics.SerializerOptions); - return new(Guid.Parse(response.ID), response.Name, response.Description, response.LatestVersion, - response.Author); + return response?.Plugins.Select(b => b with { Icon = BaseAddress + b.Icon }) ?? throw HttpException.InternalServerError("Torch site unavailable"); } [Route(HttpVerbs.Post, $"{RootPath}/{{id}}/install")] @@ -42,7 +42,7 @@ public class PluginDownloadsController : WebApiController throw HttpException.NotFound("Plugin not found", id); if (!await PluginQuery.Instance.DownloadPlugin(response)) - throw HttpException.InternalServerError(); + throw HttpException.InternalServerError("Torch site unavailable"); Statics.Torch.Config.Plugins.Add(id); } diff --git a/TorchRemote.Plugin/manifest.xml b/TorchRemote.Plugin/manifest.xml index 4efcad2..4075bf9 100644 --- a/TorchRemote.Plugin/manifest.xml +++ b/TorchRemote.Plugin/manifest.xml @@ -2,5 +2,5 @@ Torch Remote 284017F3-9682-4841-A544-EB04DB8CB9BA - v1.0.3 + v1.0.4 \ No newline at end of file