Creat initial interaction command framework

This commit is contained in:
Daan Boerlage 2021-09-20 01:31:24 +02:00
parent 60547140ea
commit 65bb7f6cac
Signed by: daan
GPG key ID: FCE070E1E4956606
11 changed files with 409 additions and 15 deletions

View file

@ -1,6 +1,9 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
@ -22,7 +25,7 @@ namespace Geekbot.Core
return client;
}
public static async Task<T> Get<T>(Uri location, HttpClient httpClient = null, bool disposeClient = true)
public static async Task<TResponse> Get<TResponse>(Uri location, HttpClient httpClient = null, bool disposeClient = true)
{
httpClient ??= CreateDefaultClient();
httpClient.BaseAddress = location;
@ -36,7 +39,83 @@ namespace Geekbot.Core
httpClient.Dispose();
}
return JsonConvert.DeserializeObject<T>(stringResponse);
return JsonConvert.DeserializeObject<TResponse>(stringResponse);
}
public static async Task<TResponse> Post<TResponse>(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true)
{
httpClient ??= CreateDefaultClient();
httpClient.BaseAddress = location;
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }),
Encoding.UTF8,
"application/json"
);
var response = await httpClient.PostAsync(location.PathAndQuery, content);
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
if (disposeClient)
{
httpClient.Dispose();
}
return JsonConvert.DeserializeObject<TResponse>(stringResponse);
}
public static async Task Post(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true)
{
httpClient ??= CreateDefaultClient();
httpClient.BaseAddress = location;
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }),
Encoding.UTF8,
"application/json"
);
var response = await httpClient.PostAsync(location, content);
response.EnsureSuccessStatusCode();
if (disposeClient)
{
httpClient.Dispose();
}
}
public static async Task Patch(Uri location, object data, HttpClient httpClient = null, bool disposeClient = true)
{
httpClient ??= CreateDefaultClient();
httpClient.BaseAddress = location;
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }),
Encoding.UTF8,
"application/json"
);
var response = await httpClient.PatchAsync(location, content);
response.EnsureSuccessStatusCode();
if (disposeClient)
{
httpClient.Dispose();
}
}
public static async Task Delete(Uri location, HttpClient httpClient = null, bool disposeClient = true)
{
httpClient ??= CreateDefaultClient();
httpClient.BaseAddress = location;
var response = await httpClient.DeleteAsync(location);
response.EnsureSuccessStatusCode();
if (disposeClient)
{
httpClient.Dispose();
}
}
}
}

View file

@ -0,0 +1,9 @@
namespace Geekbot.Core.Interactions
{
public interface IInteractionBase
{
void BeforeExecute();
void AfterExecute();
void OnException();
}
}

View file

@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Geekbot.Core.Interactions.ApplicationCommand;
using Geekbot.Core.Interactions.Request;
using Geekbot.Core.Interactions.Response;
namespace Geekbot.Core.Interactions
{
public interface IInteractionCommandManager
{
Dictionary<string, Command> CommandsInfo { get; init; }
Task<InteractionResponse> RunCommand(Interaction interaction);
}
}

View file

@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Geekbot.Core.Interactions.ApplicationCommand;
using Geekbot.Core.Interactions.Request;
using Geekbot.Core.Interactions.Response;
namespace Geekbot.Core.Interactions
{
public abstract class InteractionBase : IInteractionBase
{
protected virtual void BeforeExecute()
{
}
protected virtual void AfterExecute()
{
}
protected virtual void OnException()
{
}
public abstract Command GetCommandInfo();
public abstract Task<InteractionResponse> Exec(InteractionData interaction);
void IInteractionBase.BeforeExecute() => this.BeforeExecute();
void IInteractionBase.AfterExecute() => this.AfterExecute();
void IInteractionBase.OnException() => this.OnException();
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading.Tasks;
using Geekbot.Core.GlobalSettings;
using Geekbot.Core.Interactions.ApplicationCommand;
using Geekbot.Core.Interactions.Request;
using Geekbot.Core.Interactions.Response;
using Geekbot.Core.Logger;
namespace Geekbot.Core.Interactions
{
public class InteractionCommandManager : IInteractionCommandManager
{
private readonly Dictionary<string, Type> _commands = new();
public Dictionary<string, Command> CommandsInfo { get; init; }
public InteractionCommandManager()
{
var interactions = Assembly.GetCallingAssembly()
.GetTypes()
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InteractionBase)))
.ToList();
CommandsInfo = new Dictionary<string, Command>();
foreach (var interactionType in interactions)
{
var instance = (InteractionBase)Activator.CreateInstance(interactionType);
var commandInfo = instance.GetCommandInfo();
_commands.Add(commandInfo.Name, interactionType);
CommandsInfo.Add(commandInfo.Name, commandInfo);
}
}
public async Task<InteractionResponse> RunCommand(Interaction interaction)
{
var type = _commands[interaction.Data.Name];
var command = (InteractionBase)Activator.CreateInstance(type);
return await command.Exec(interaction.Data);
}
}
}

View file

@ -17,6 +17,7 @@ namespace Geekbot.Core.Logger
Api,
Migration,
HighscoreManager,
Interaction,
Other
}
}