Added Socket support

This commit is contained in:
2024-12-18 23:58:13 +02:00
parent 424bf2196f
commit c79c792c43
14 changed files with 251 additions and 11 deletions

View File

@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using DiscordBotCore.API.Endpoints;
using DiscordBotCore.API.Endpoints.PluginManagement;
using DiscordBotCore.API.Endpoints.SettingsManagement;
using DiscordBotCore.Interfaces.API;
using DiscordBotCore.Others;
using Microsoft.AspNetCore.Builder;
namespace DiscordBotCore.API.Endpoints;
public class ApiManager
{
private bool IsRunning { get; set; }
private List<IEndpoint> ApiEndpoints { get; }
public ApiManager()
{
ApiEndpoints = new List<IEndpoint>();
}
internal void AddBaseEndpoints()
{
AddEndpoint(new HomeEndpoint());
AddEndpoint(new PluginListEndpoint());
AddEndpoint(new PluginListInstalledEndpoint());
AddEndpoint(new PluginInstallEndpoint());
AddEndpoint(new PluginInstallGetProgressEndpoint());
AddEndpoint(new SettingsChangeEndpoint());
AddEndpoint(new SettingsGetEndpoint());
}
public Result AddEndpoint(IEndpoint endpoint)
{
if (ApiEndpoints.Contains(endpoint) || ApiEndpoints.Exists(x => x.Path == endpoint.Path))
{
return Result.Failure("Endpoint already exists");
}
ApiEndpoints.Add(endpoint);
return Result.Success();
}
public void RemoveEndpoint(string endpointPath)
{
this.ApiEndpoints.RemoveAll(endpoint => endpoint.Path == endpointPath);
}
public bool EndpointExists(string endpointPath)
{
return this.ApiEndpoints.Exists(endpoint => endpoint.Path == endpointPath);
}
public async void InitializeApi()
{
if (IsRunning)
return;
IsRunning = true;
var builder = WebApplication.CreateBuilder();
var app = builder.Build();
app.UseRouting();
EndpointManager manager = new EndpointManager(app);
foreach(IEndpoint endpoint in this.ApiEndpoints)
{
manager.MapEndpoint(endpoint);
}
await app.RunAsync();
}
}

View File

@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using DiscordBotCore.Others;
namespace DiscordBotCore.API.Endpoints;
public class ApiResponse
{
public string Message { get; set; }
public bool Success { get; set; }
private ApiResponse(string message, bool success)
{
Message = message;
Success = success;
}
public static ApiResponse From(string message, bool success)
{
return new ApiResponse(message, success);
}
public static ApiResponse Fail(string message)
{
return new ApiResponse(message, false);
}
public static ApiResponse Ok()
{
return new ApiResponse(string.Empty, true);
}
public async Task<string> ToJson() => await JsonManager.ConvertToJsonString(this);
}

View File

@@ -0,0 +1,80 @@
using System.IO;
using System.Text;
using DiscordBotCore.Interfaces.API;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace DiscordBotCore.API.Endpoints;
internal sealed class EndpointManager
{
private WebApplication _appBuilder;
internal EndpointManager(WebApplication appBuilder)
{
_appBuilder = appBuilder;
}
internal void MapEndpoint(IEndpoint endpoint)
{
switch (endpoint.HttpMethod)
{
case EndpointType.Get:
_appBuilder.MapGet(endpoint.Path, async context =>
{
//convert the context to a string
string jsonRequest = string.Empty;
if (context.Request.Body.CanRead)
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
jsonRequest = await reader.ReadToEndAsync();
}
var response = await endpoint.HandleRequest(jsonRequest);
await context.Response.WriteAsync(await response.ToJson());
});
break;
case EndpointType.Put:
_appBuilder.MapPut(endpoint.Path, async context =>
{
string jsonRequest = string.Empty;
if (context.Request.Body.CanRead)
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
jsonRequest = await reader.ReadToEndAsync();
}
var response = await endpoint.HandleRequest(jsonRequest);
await context.Response.WriteAsync(await response.ToJson());
});
break;
case EndpointType.Post:
_appBuilder.MapPost(endpoint.Path, async context =>
{
string jsonRequest = string.Empty;
if (context.Request.Body.CanRead)
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
jsonRequest = await reader.ReadToEndAsync();
}
var response = await endpoint.HandleRequest(jsonRequest);
await context.Response.WriteAsync(await response.ToJson());
});
break;
case EndpointType.Delete:
_appBuilder.MapDelete(endpoint.Path, async context =>
{
string jsonRequest = string.Empty;
if (context.Request.Body.CanRead)
{
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
jsonRequest = await reader.ReadToEndAsync();
}
var response = await endpoint.HandleRequest(jsonRequest);
await context.Response.WriteAsync(await response.ToJson());
});
break;
}
}
}