From 8a88b23021c39ffad60ca9e5410606b8d873616c Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 29 Sep 2021 22:30:20 -0400 Subject: [PATCH] feat(apiv2): stubs --- .gitignore | 1 + PluralKit.API/APIJsonExt.cs | 35 +++++++++ PluralKit.API/Controllers/PKControllerBase.cs | 33 ++++++++ .../Controllers/v1/MetaController.cs | 28 ------- .../Controllers/v2/GuildControllerV2.cs | 58 ++++++++++++++ .../Controllers/v2/MemberControllerV2.cs | 67 ++++++++++++++++ .../Controllers/v2/MiscControllerV2.cs | 40 ++++++++++ .../Controllers/v2/SwitchControllerV2.cs | 77 +++++++++++++++++++ .../Controllers/v2/SystemControllerV2.cs | 37 +++++++++ PluralKit.API/Startup.cs | 7 ++ 10 files changed, 355 insertions(+), 28 deletions(-) create mode 100644 PluralKit.API/APIJsonExt.cs create mode 100644 PluralKit.API/Controllers/PKControllerBase.cs create mode 100644 PluralKit.API/Controllers/v2/GuildControllerV2.cs create mode 100644 PluralKit.API/Controllers/v2/MemberControllerV2.cs create mode 100644 PluralKit.API/Controllers/v2/MiscControllerV2.cs create mode 100644 PluralKit.API/Controllers/v2/SwitchControllerV2.cs create mode 100644 PluralKit.API/Controllers/v2/SystemControllerV2.cs diff --git a/.gitignore b/.gitignore index 33fbce6f..5259ec66 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ obj/ .vscode/ tags/ .DS_Store +mono_crash* # Dependencies node_modules/ diff --git a/PluralKit.API/APIJsonExt.cs b/PluralKit.API/APIJsonExt.cs new file mode 100644 index 00000000..46744297 --- /dev/null +++ b/PluralKit.API/APIJsonExt.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + public static class APIJsonExt + { + public static JArray ToJSON(this IEnumerable shards) + { + var o = new JArray(); + + foreach (var shard in shards) + { + var s = new JObject(); + s.Add("id", shard.Id); + + if (shard.Status == PKShardInfo.ShardStatus.Down) + s.Add("status", "down"); + else + s.Add("status", "up"); + + s.Add("ping", shard.Ping); + s.Add("last_heartbeat", shard.LastHeartbeat.ToString()); + s.Add("last_connection", shard.LastConnection.ToString()); + + o.Add(s); + } + + return o; + } + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/PKControllerBase.cs b/PluralKit.API/Controllers/PKControllerBase.cs new file mode 100644 index 00000000..f28a3832 --- /dev/null +++ b/PluralKit.API/Controllers/PKControllerBase.cs @@ -0,0 +1,33 @@ +using System; +using System.Net; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; + +using NodaTime; + +using PluralKit.Core; + +namespace PluralKit.API +{ + public class PKControllerBase: ControllerBase + { + private readonly Guid _requestId = Guid.NewGuid(); + private readonly Regex _shortIdRegex = new Regex("^[a-z]{5}$"); + private readonly Regex _snowflakeRegex = new Regex("^[0-9]{17,19}$"); + + protected readonly ApiConfig _config; + protected readonly IDatabase _db; + protected readonly ModelRepository _repo; + + public PKControllerBase(IServiceProvider svc) + { + _config = svc.GetRequiredService(); + _db = svc.GetRequiredService(); + _repo = svc.GetRequiredService(); + } + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v1/MetaController.cs b/PluralKit.API/Controllers/v1/MetaController.cs index 098bd8a8..8bfae9e9 100644 --- a/PluralKit.API/Controllers/v1/MetaController.cs +++ b/PluralKit.API/Controllers/v1/MetaController.cs @@ -37,32 +37,4 @@ namespace PluralKit.API return Ok(o); } } - - public static class MetaJsonExt - { - public static JArray ToJSON(this IEnumerable shards) - { - var o = new JArray(); - - foreach (var shard in shards) - { - var s = new JObject(); - s.Add("id", shard.Id); - - if (shard.Status == PKShardInfo.ShardStatus.Down) - s.Add("status", "down"); - else - s.Add("status", "up"); - - s.Add("ping", shard.Ping); - s.Add("last_heartbeat", shard.LastHeartbeat.ToString()); - s.Add("last_connection", shard.LastConnection.ToString()); - - o.Add(s); - } - - return o; - } - - } } \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/GuildControllerV2.cs b/PluralKit.API/Controllers/v2/GuildControllerV2.cs new file mode 100644 index 00000000..ed0631cb --- /dev/null +++ b/PluralKit.API/Controllers/v2/GuildControllerV2.cs @@ -0,0 +1,58 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("2.0")] + [Route("v{version:apiVersion}")] + public class GuildControllerV2: PKControllerBase + { + public GuildControllerV2(IServiceProvider svc) : base(svc) { } + + + [HttpGet("systems/{system}/guilds/{guild_id}")] + public async Task SystemGuildGet(string system, ulong guild_id) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPatch("systems/{system}/guilds/{guild_id}")] + public async Task SystemGuildPatch(string system, ulong guild_id, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpGet("members/{member}/guilds/{guild_id}")] + public async Task MemberGuildGet(string member, ulong guild_id) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPatch("members/{member}/guilds/{guild_id}")] + public async Task MemberGuildPatch(string member, ulong guild_id, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/MemberControllerV2.cs b/PluralKit.API/Controllers/v2/MemberControllerV2.cs new file mode 100644 index 00000000..abb65a4c --- /dev/null +++ b/PluralKit.API/Controllers/v2/MemberControllerV2.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("2.0")] + [Route("v{version:apiVersion}")] + public class MemberControllerV2: PKControllerBase + { + public MemberControllerV2(IServiceProvider svc) : base(svc) { } + + + [HttpGet("systems/{system}/members")] + public async Task GetSystemMembers(string system) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPost("members")] + public async Task MemberCreate([FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpGet("members/{member}")] + public async Task MemberGet(string member) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPatch("members/{member}")] + public async Task MemberPatch(string member, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpDelete("members/{member}")] + public async Task MemberDelete(string member) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/MiscControllerV2.cs b/PluralKit.API/Controllers/v2/MiscControllerV2.cs new file mode 100644 index 00000000..acabf6ea --- /dev/null +++ b/PluralKit.API/Controllers/v2/MiscControllerV2.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("2.0")] + [Route("v{version:apiVersion}")] + public class MetaControllerV2: PKControllerBase + { + public MetaControllerV2(IServiceProvider svc) : base(svc) { } + + [HttpGet("meta")] + public async Task> Meta() + { + await using var conn = await _db.Obtain(); + var shards = await _repo.GetShards(conn); + + var o = new JObject(); + o.Add("shards", shards.ToJSON()); + + return Ok(o); + } + + [HttpGet("messages/{message_id}")] + public async Task MessageGet(ulong message_id) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/SwitchControllerV2.cs b/PluralKit.API/Controllers/v2/SwitchControllerV2.cs new file mode 100644 index 00000000..73e09bb2 --- /dev/null +++ b/PluralKit.API/Controllers/v2/SwitchControllerV2.cs @@ -0,0 +1,77 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("2.0")] + [Route("v{version:apiVersion}")] + public class SwitchControllerV2: PKControllerBase + { + public SwitchControllerV2(IServiceProvider svc) : base(svc) { } + + + [HttpGet("systems/{system}/switches")] + public async Task GetSystemSwitches(string system) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpGet("systems/{system}/fronters")] + public async Task GetSystemFronters(string system) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + + [HttpPost("systems/{system}/switches")] + public async Task SwitchCreate(string system, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + + [HttpGet("systems/{system}/switches/{switch_id}")] + public async Task SwitchGet(string system, string switch_id) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPatch("systems/{system}/switches/{switch_id}")] + public async Task SwitchPatch(string system, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpDelete("systems/{system}/switches/{switch_id}")] + public async Task SwitchDelete(string system, string switch_id) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + } +} \ No newline at end of file diff --git a/PluralKit.API/Controllers/v2/SystemControllerV2.cs b/PluralKit.API/Controllers/v2/SystemControllerV2.cs new file mode 100644 index 00000000..93e579c2 --- /dev/null +++ b/PluralKit.API/Controllers/v2/SystemControllerV2.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc; + +using Newtonsoft.Json.Linq; + +using PluralKit.Core; + +namespace PluralKit.API +{ + [ApiController] + [ApiVersion("2.0")] + [Route("v{version:apiVersion}/systems")] + public class SystemControllerV2: PKControllerBase + { + public SystemControllerV2(IServiceProvider svc) : base(svc) { } + + [HttpGet("{system}")] + public async Task SystemGet(string system) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + + [HttpPatch("{system}")] + public async Task SystemPatch(string system, [FromBody] JObject data) + { + return new ObjectResult("Unimplemented") + { + StatusCode = 501 + }; + } + } +} \ No newline at end of file diff --git a/PluralKit.API/Startup.cs b/PluralKit.API/Startup.cs index db755045..22dce95c 100644 --- a/PluralKit.API/Startup.cs +++ b/PluralKit.API/Startup.cs @@ -117,6 +117,13 @@ namespace PluralKit.API //app.UseHsts(); } + // add X-PluralKit-Version header + app.Use((ctx, next) => + { + ctx.Response.Headers.Add("X-PluralKit-Version", BuildInfoService.Version); + return next(); + }); + //app.UseHttpsRedirection(); app.UseCors(opts => opts.AllowAnyMethod().AllowAnyOrigin().WithHeaders("Content-Type", "Authorization"));