Add GET /Api/Plugins endpoint,

In regards to https://github.com/JustArchiNET/ASF-ui/issues/1015
This commit is contained in:
JustArchi
2020-06-24 20:54:41 +02:00
parent d191820317
commit 69936d55b6
5 changed files with 54 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ using System.Linq;
using System.Threading.Tasks;
using ArchiSteamFarm.Json;
using ArchiSteamFarm.Plugins;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SteamKit2;
@@ -38,6 +39,10 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin {
// This will keep your code compact, efficient and less dependent. You can always add additional interfaces when you'll need them, this example project will inherit quite a bit of them to show you potential usage
// ReSharper disable once UnusedType.Global - this is example plugin class that isn't used in our main code
internal sealed class ExamplePlugin : IASF, IBot, IBotCommand, IBotConnection, IBotFriendRequest, IBotMessage, IBotModules, IBotTradeOffer {
// Plugins can expose custom properties for our GET /Api/Plugins API call, simply annotate them with [JsonProperty] (or keep public)
[JsonProperty]
public readonly bool CustomIsEnabledField = true;
// This is used for identification purposes, typically you want to use a friendly name of your plugin here, such as the name of your main class
// Please note that this property can have direct dependencies only on structures that were initialized by the constructor, as it's possible to be called before OnLoaded() takes place
public string Name => nameof(ExamplePlugin);

View File

@@ -30,6 +30,7 @@ using System.Threading.Tasks;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Plugins;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SteamKit2;
@@ -44,10 +45,13 @@ namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper {
private static GlobalCache GlobalCache;
[JsonProperty]
private static bool IsEnabled;
[JsonProperty]
public override string Name => nameof(SteamTokenDumperPlugin);
[JsonProperty]
public override Version Version => typeof(SteamTokenDumperPlugin).Assembly.GetName().Version ?? throw new ArgumentNullException(nameof(Version));
public Task<uint> GetPreferredChangeNumberToStartFrom() => Task.FromResult(IsEnabled ? GlobalCache?.LastChangeNumber ?? 0 : 0);

View File

@@ -0,0 +1,39 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2020 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Collections.Generic;
using System.Net;
using ArchiSteamFarm.IPC.Responses;
using ArchiSteamFarm.Plugins;
using Microsoft.AspNetCore.Mvc;
namespace ArchiSteamFarm.IPC.Controllers.Api {
[Route("Api/Plugins")]
public sealed class PluginsController : ArchiController {
[HttpGet]
[ProducesResponseType(typeof(GenericResponse<IReadOnlyCollection<IPlugin>>), (int) HttpStatusCode.OK)]
public ActionResult<GenericResponse<IReadOnlyCollection<IPlugin>>> PluginsGet() {
IReadOnlyCollection<IPlugin> activePlugins = PluginsCore.ActivePlugins ?? (IReadOnlyCollection<IPlugin>) new IPlugin[0];
return Ok(new GenericResponse<IReadOnlyCollection<IPlugin>>(activePlugins));
}
}
}

View File

@@ -21,6 +21,7 @@
using System;
using JetBrains.Annotations;
using Newtonsoft.Json;
namespace ArchiSteamFarm.Plugins {
[PublicAPI]
@@ -29,6 +30,7 @@ namespace ArchiSteamFarm.Plugins {
/// ASF will use this property as general plugin identifier for the user.
/// </summary>
/// <returns>String that will be used as the name of this plugin.</returns>
[JsonProperty]
[NotNull]
string Name { get; }
@@ -37,6 +39,7 @@ namespace ArchiSteamFarm.Plugins {
/// You have a freedom in deciding what versioning you want to use, this is for identification purposes only.
/// </summary>
/// <returns>Version that will be shown to the user when plugin is loaded.</returns>
[JsonProperty]
[NotNull]
Version Version { get; }

View File

@@ -39,10 +39,10 @@ namespace ArchiSteamFarm.Plugins {
internal static class PluginsCore {
internal static bool HasCustomPluginsLoaded => HasActivePluginsLoaded && ActivePlugins.Any(plugin => !(plugin is OfficialPlugin officialPlugin) || !officialPlugin.HasSameVersion());
private static bool HasActivePluginsLoaded => ActivePlugins?.Count > 0;
[ImportMany]
private static ImmutableHashSet<IPlugin> ActivePlugins { get; set; }
internal static ImmutableHashSet<IPlugin> ActivePlugins { get; private set; }
private static bool HasActivePluginsLoaded => ActivePlugins?.Count > 0;
[ItemNotNull]
internal static async Task<StringComparer> GetBotsComparer() {