Linked Plugin List and Plugin Install endpoints between web and console
This commit is contained in:
55
DiscordBotWebUI/ServerCommunication/ApiHandler.cs
Normal file
55
DiscordBotWebUI/ServerCommunication/ApiHandler.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using DiscordBotWebUI.ServerCommunication.ApiSettings;
|
||||
|
||||
namespace DiscordBotWebUI.ServerCommunication;
|
||||
|
||||
public class ApiHandler
|
||||
{
|
||||
private IApiSettings ApiSettings { get; }
|
||||
public ApiHandler(IApiSettings apiSettings)
|
||||
{
|
||||
ApiSettings = apiSettings;
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> GetAsync(string endpoint)
|
||||
{
|
||||
using HttpClient client = new HttpClient();
|
||||
client.BaseAddress = new Uri($"{ApiSettings.BaseUrl}:{ApiSettings.BasePort}");
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
HttpResponseMessage response = await client.GetAsync(endpoint);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
return await JsonManager.ConvertFromJson<ApiResponse>(content);
|
||||
}
|
||||
return new ApiResponse("Failed to get response", false);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> PostAsync(string endpoint, Dictionary<string, string> jsonValues)
|
||||
{
|
||||
if (jsonValues.Count <= 0)
|
||||
{
|
||||
return new ApiResponse("No values to post", false);
|
||||
}
|
||||
|
||||
string jsonString = await JsonManager.ConvertToJsonString(jsonValues);
|
||||
return await PostAsync(endpoint, jsonString);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse> PostAsync(string endpoint, string json)
|
||||
{
|
||||
using HttpClient client = new HttpClient();
|
||||
client.BaseAddress = new Uri($"{ApiSettings.BaseUrl}:{ApiSettings.BasePort}");
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
HttpResponseMessage response = await client.PostAsync(endpoint, content);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
string responseContent = await response.Content.ReadAsStringAsync();
|
||||
return await JsonManager.ConvertFromJson<ApiResponse>(responseContent);
|
||||
}
|
||||
return new ApiResponse("Failed to get response", false);
|
||||
}
|
||||
|
||||
}
|
||||
16
DiscordBotWebUI/ServerCommunication/ApiResponse.cs
Normal file
16
DiscordBotWebUI/ServerCommunication/ApiResponse.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DiscordBotWebUI.ServerCommunication;
|
||||
|
||||
public class ApiResponse
|
||||
{
|
||||
public bool Success { get; }
|
||||
public string Message { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
public ApiResponse(string message, bool success)
|
||||
{
|
||||
Message = message;
|
||||
Success = success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace DiscordBotWebUI.ServerCommunication.ApiSettings;
|
||||
|
||||
public class ApiSettings : IApiSettings
|
||||
{
|
||||
public string BaseUrl { get; }
|
||||
public string BasePort { get; }
|
||||
|
||||
public ApiSettings(string baseUrl, string basePort)
|
||||
{
|
||||
BaseUrl = baseUrl;
|
||||
BasePort = basePort;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace DiscordBotWebUI.ServerCommunication.ApiSettings;
|
||||
|
||||
public interface IApiSettings
|
||||
{
|
||||
public string BaseUrl { get; }
|
||||
public string BasePort { get; }
|
||||
}
|
||||
31
DiscordBotWebUI/ServerCommunication/JsonManager.cs
Normal file
31
DiscordBotWebUI/ServerCommunication/JsonManager.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DiscordBotWebUI.ServerCommunication;
|
||||
|
||||
public class JsonManager
|
||||
{
|
||||
public static async Task<T?> ConvertFromJson<T>(string jsonString)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(jsonString))
|
||||
throw new ArgumentException("JSON string cannot be null or empty.", nameof(jsonString));
|
||||
|
||||
using MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
|
||||
try
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<T>(stream);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to deserialize JSON.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> ConvertToJsonString<T>(T data)
|
||||
{
|
||||
using MemoryStream stream = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(stream, data);
|
||||
return Encoding.UTF8.GetString(stream.ToArray());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user