Redesigned the DiscordBotCore by splitting it into multiple projects. Created a WebUI and preparing to remove the DiscordBot application
This commit is contained in:
14
DiscordBotCore.PluginManagement/Helpers/IPluginRepository.cs
Normal file
14
DiscordBotCore.PluginManagement/Helpers/IPluginRepository.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using DiscordBotCore.PluginManagement.Models;
|
||||
|
||||
namespace DiscordBotCore.PluginManagement.Helpers;
|
||||
|
||||
public interface IPluginRepository
|
||||
{
|
||||
public Task<List<OnlinePlugin>> GetAllPlugins(int operatingSystem, bool includeNotApproved);
|
||||
|
||||
public Task<OnlinePlugin?> GetPluginById(int pluginId);
|
||||
public Task<OnlinePlugin?> GetPluginByName(string pluginName, int operatingSystem, bool includeNotApproved);
|
||||
|
||||
public Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace DiscordBotCore.PluginManagement.Helpers;
|
||||
|
||||
public interface IPluginRepositoryConfiguration
|
||||
{
|
||||
public string BaseUrl { get; }
|
||||
|
||||
public string PluginRepositoryLocation { get; }
|
||||
public string DependenciesRepositoryLocation { get; }
|
||||
}
|
||||
123
DiscordBotCore.PluginManagement/Helpers/PluginRepository.cs
Normal file
123
DiscordBotCore.PluginManagement/Helpers/PluginRepository.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Net.Mime;
|
||||
using DiscordBotCore.Logging;
|
||||
using DiscordBotCore.PluginManagement.Models;
|
||||
using DiscordBotCore.Utilities;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
|
||||
namespace DiscordBotCore.PluginManagement.Helpers;
|
||||
|
||||
public class PluginRepository : IPluginRepository
|
||||
{
|
||||
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration, ILogger logger)
|
||||
{
|
||||
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.BaseAddress = new Uri(_pluginRepositoryConfiguration.BaseUrl);
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<List<OnlinePlugin>> GetAllPlugins(int operatingSystem, bool includeNotApproved)
|
||||
{
|
||||
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
|
||||
"get-all-plugins", new Dictionary<string, string>
|
||||
{
|
||||
{ "operatingSystem", operatingSystem.ToString() },
|
||||
{ "includeNotApproved", includeNotApproved.ToString() }
|
||||
});
|
||||
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
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)
|
||||
{
|
||||
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
|
||||
"get-plugin", new Dictionary<string, string>
|
||||
{
|
||||
{ "pluginId", pluginId.ToString() },
|
||||
{ "includeNotApproved", "false" }
|
||||
});
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
|
||||
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public async Task<OnlinePlugin?> GetPluginByName(string pluginName, int operatingSystem, bool includeNotApproved)
|
||||
{
|
||||
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
|
||||
"get-plugin-by-name", new Dictionary<string, string>
|
||||
{
|
||||
{ "pluginName", pluginName },
|
||||
{ "operatingSystem", operatingSystem.ToString() },
|
||||
{ "includeNotApproved", includeNotApproved.ToString() }
|
||||
});
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Log($"Plugin {pluginName} not found");
|
||||
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)
|
||||
{
|
||||
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.DependenciesRepositoryLocation,
|
||||
"get-dependencies-for-plugin", new Dictionary<string, string>
|
||||
{
|
||||
{ "pluginId", pluginId.ToString() }
|
||||
});
|
||||
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.Log($"Failed to get dependencies for plugin with ID {pluginId}");
|
||||
return [];
|
||||
}
|
||||
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
List<OnlineDependencyInfo> dependencies = await JsonManager.ConvertFromJson<List<OnlineDependencyInfo>>(content);
|
||||
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
private string CreateUrlWithQueryParams(string baseUrl, string endpoint, Dictionary<string, string> queryParams)
|
||||
{
|
||||
QueryBuilder queryBuilder = new QueryBuilder();
|
||||
foreach (var(key,value) in queryParams)
|
||||
{
|
||||
queryBuilder.Add(key, value);
|
||||
}
|
||||
|
||||
string queryString = queryBuilder.ToQueryString().ToString();
|
||||
string url = baseUrl + endpoint + queryString;
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DiscordBotCore.PluginManagement.Helpers;
|
||||
|
||||
public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
|
||||
{
|
||||
public static PluginRepositoryConfiguration Default => new ("http://localhost:8080/api/v1/",
|
||||
"plugin/",
|
||||
"dependency/");
|
||||
|
||||
public string BaseUrl { get; }
|
||||
public string PluginRepositoryLocation { get; }
|
||||
public string DependenciesRepositoryLocation { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
public PluginRepositoryConfiguration(string baseUrl, string pluginRepositoryLocation, string dependenciesRepositoryLocation)
|
||||
{
|
||||
BaseUrl = baseUrl;
|
||||
PluginRepositoryLocation = pluginRepositoryLocation;
|
||||
DependenciesRepositoryLocation = dependenciesRepositoryLocation;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user