Updated repository manager for the new backend
This commit is contained in:
@@ -1,29 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Interfaces.PluginManagement;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Plugin;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
public class PluginRepository : IPluginRepository
|
||||
{
|
||||
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
|
||||
public HttpClient _httpClient;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration)
|
||||
{
|
||||
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.BaseAddress = new System.Uri(_pluginRepositoryConfiguration.BaseUrl);
|
||||
_httpClient.BaseAddress = new Uri(_pluginRepositoryConfiguration.BaseUrl);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<List<OnlinePlugin>> GetAllPlugins()
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation + "get-all-plugins");
|
||||
int operatingSystem = OS.GetOperatingSystemInt();
|
||||
bool includeNotApproved = false;
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -39,9 +49,13 @@ public class PluginRepository : IPluginRepository
|
||||
|
||||
public async Task<OnlinePlugin?> GetPluginById(int pluginId)
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
|
||||
$"get-plugin/{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)
|
||||
{
|
||||
@@ -57,9 +71,14 @@ public class PluginRepository : IPluginRepository
|
||||
|
||||
public async Task<OnlinePlugin?> GetPluginByName(string pluginName)
|
||||
{
|
||||
HttpResponseMessage response =
|
||||
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
|
||||
$"get-plugin-by-name/{pluginName}");
|
||||
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
|
||||
"get-plugin-by-name", new Dictionary<string, string>
|
||||
{
|
||||
{ "pluginName", pluginName },
|
||||
{ "operatingSystem", OS.GetOperatingSystemInt().ToString() },
|
||||
{ "includeNotApproved", "false" }
|
||||
});
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(url);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
@@ -75,7 +94,13 @@ public class PluginRepository : IPluginRepository
|
||||
|
||||
public async Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId)
|
||||
{
|
||||
HttpResponseMessage response = await _httpClient.GetAsync(_pluginRepositoryConfiguration.DependenciesRepositoryLocation + $"get-dependencies-for-plugin/{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)
|
||||
{
|
||||
Application.Log("Failed to get dependencies for plugin from the repository", LogType.Warning);
|
||||
@@ -87,4 +112,18 @@ public class PluginRepository : IPluginRepository
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
|
||||
{
|
||||
public static PluginRepositoryConfiguration Default => new ("http://localhost:5097/api/v1/", "plugins-repository/", "dependencies-repository/");
|
||||
public static PluginRepositoryConfiguration Default => new ("http://localhost:8080/api/v1/",
|
||||
"plugin/",
|
||||
"dependency/");
|
||||
|
||||
public string BaseUrl { get; }
|
||||
public string PluginRepositoryLocation { get; }
|
||||
|
||||
@@ -43,7 +43,7 @@ public sealed class PluginManager : IPluginManager
|
||||
|
||||
if (!onlinePlugins.Any())
|
||||
{
|
||||
Application.Log("Failed to get all plugins from the repository", LogType.Warning);
|
||||
Application.Log("Could not get any plugins from the repository", LogType.Warning);
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user