feat(apiv2): stubs

This commit is contained in:
spiral 2021-09-29 22:30:20 -04:00
parent f785fa1204
commit 8a88b23021
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
10 changed files with 355 additions and 28 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ obj/
.vscode/
tags/
.DS_Store
mono_crash*
# Dependencies
node_modules/

View File

@ -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<PKShardInfo> 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;
}
}
}

View File

@ -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<ApiConfig>();
_db = svc.GetRequiredService<IDatabase>();
_repo = svc.GetRequiredService<ModelRepository>();
}
}
}

View File

@ -37,32 +37,4 @@ namespace PluralKit.API
return Ok(o);
}
}
public static class MetaJsonExt
{
public static JArray ToJSON(this IEnumerable<PKShardInfo> 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;
}
}
}

View File

@ -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<IActionResult> SystemGuildGet(string system, ulong guild_id)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPatch("systems/{system}/guilds/{guild_id}")]
public async Task<IActionResult> SystemGuildPatch(string system, ulong guild_id, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpGet("members/{member}/guilds/{guild_id}")]
public async Task<IActionResult> MemberGuildGet(string member, ulong guild_id)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPatch("members/{member}/guilds/{guild_id}")]
public async Task<IActionResult> MemberGuildPatch(string member, ulong guild_id, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
}
}

View File

@ -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<IActionResult> GetSystemMembers(string system)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPost("members")]
public async Task<IActionResult> MemberCreate([FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpGet("members/{member}")]
public async Task<IActionResult> MemberGet(string member)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPatch("members/{member}")]
public async Task<IActionResult> MemberPatch(string member, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpDelete("members/{member}")]
public async Task<IActionResult> MemberDelete(string member)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
}
}

View File

@ -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<ActionResult<JObject>> 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<IActionResult> MessageGet(ulong message_id)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
}
}

View File

@ -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<IActionResult> GetSystemSwitches(string system)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpGet("systems/{system}/fronters")]
public async Task<IActionResult> GetSystemFronters(string system)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPost("systems/{system}/switches")]
public async Task<IActionResult> SwitchCreate(string system, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpGet("systems/{system}/switches/{switch_id}")]
public async Task<IActionResult> SwitchGet(string system, string switch_id)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPatch("systems/{system}/switches/{switch_id}")]
public async Task<IActionResult> SwitchPatch(string system, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpDelete("systems/{system}/switches/{switch_id}")]
public async Task<IActionResult> SwitchDelete(string system, string switch_id)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
}
}

View File

@ -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<IActionResult> SystemGet(string system)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
[HttpPatch("{system}")]
public async Task<IActionResult> SystemPatch(string system, [FromBody] JObject data)
{
return new ObjectResult("Unimplemented")
{
StatusCode = 501
};
}
}
}

View File

@ -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"));