Updated API for plugins to work with database from remote. Added PluginRepository and removed Installation Scripts
This commit is contained in:
@@ -107,21 +107,4 @@ internal static class OnlineFunctions
|
||||
using var client = new HttpClient();
|
||||
return await client.GetStringAsync(url, cancellation);
|
||||
}
|
||||
|
||||
internal static async Task<bool> IsInternetConnected()
|
||||
{
|
||||
bool result = false;
|
||||
try
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
await client.GetStringAsync("files.wizzy-server.ro");
|
||||
result = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
89
DiscordBotCore/Online/Helpers/PluginRepository.cs
Normal file
89
DiscordBotCore/Online/Helpers/PluginRepository.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.PluginManagement;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Plugin;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
internal class PluginRepository : IPluginRepository
|
||||
{
|
||||
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
|
||||
private readonly HttpClient _httpClient;
|
||||
internal PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration)
|
||||
{
|
||||
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.BaseAddress = new System.Uri(_pluginRepositoryConfiguration.BaseUrl);
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<OnlinePlugin>> GetAllPlugins()
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation + "get-all-plugins");
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Application.Log("Failed to get all plugins from the repository", LogType.Warning);
|
||||
return [];
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
List<OnlinePlugin> plugins = await JsonManager.ConvertFromJson<List<OnlinePlugin>>(content);
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public async Task<OnlinePlugin?> GetPluginById(int pluginId)
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
|
||||
$"get-plugin/{pluginId}");
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Application.Log("Failed to get plugin from the repository", LogType.Warning);
|
||||
return null;
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public async Task<OnlinePlugin?> GetPluginByName(string pluginName)
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
|
||||
$"get-plugin-by-name/{pluginName}");
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Application.Log("Failed to get plugin from the repository", LogType.Warning);
|
||||
return null;
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public async Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId)
|
||||
{
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(_pluginRepositoryConfiguration.DependenciesRepositoryLocation + $"get-dependencies-for-plugin/{pluginId}");
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
Application.Log("Failed to get dependencies for plugin from the repository", LogType.Warning);
|
||||
return [];
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
List<OnlineDependencyInfo> dependencies = await JsonManager.ConvertFromJson<List<OnlineDependencyInfo>>(content);
|
||||
|
||||
return dependencies;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using DiscordBotCore.Interfaces.PluginManagement;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
|
||||
{
|
||||
public static PluginRepositoryConfiguration Default => new ("http://localhost:5097/api/v1/", "plugins-repository/", "dependencies-repository/");
|
||||
|
||||
public string BaseUrl { get; }
|
||||
public string PluginRepositoryLocation { get; }
|
||||
public string DependenciesRepositoryLocation { get; }
|
||||
|
||||
public PluginRepositoryConfiguration(string baseUrl, string pluginRepositoryLocation, string dependenciesRepositoryLocation)
|
||||
{
|
||||
BaseUrl = baseUrl;
|
||||
PluginRepositoryLocation = pluginRepositoryLocation;
|
||||
DependenciesRepositoryLocation = dependenciesRepositoryLocation;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using DiscordBotCore.Interfaces.Updater;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
public class PluginVersion: Version
|
||||
{
|
||||
[JsonConstructor]
|
||||
public PluginVersion(int major, int minor, int patch): base(major, minor, patch)
|
||||
{
|
||||
}
|
||||
public PluginVersion(string versionAsString): base(versionAsString)
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ToShortString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user