feat: specify custom Discord base URL

for use with twilight_http_proxy or similar
This commit is contained in:
spiral 2021-11-02 05:36:53 -04:00
parent 14b0e98791
commit b430446171
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
4 changed files with 19 additions and 14 deletions

View File

@ -23,17 +23,17 @@ namespace Myriad.Rest
{ {
public class BaseRestClient: IAsyncDisposable public class BaseRestClient: IAsyncDisposable
{ {
private const string ApiBaseUrl = "https://discord.com/api/v9";
private readonly Version _httpVersion = new(2, 0); private readonly Version _httpVersion = new(2, 0);
private readonly JsonSerializerOptions _jsonSerializerOptions; private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Ratelimiter _ratelimiter; private readonly Ratelimiter _ratelimiter;
private readonly AsyncPolicy<HttpResponseMessage> _retryPolicy; private readonly AsyncPolicy<HttpResponseMessage> _retryPolicy;
private readonly string _baseUrl;
public BaseRestClient(string userAgent, string token, ILogger logger) public BaseRestClient(string userAgent, string token, ILogger logger, string baseUrl)
{ {
_logger = logger.ForContext<BaseRestClient>(); _logger = logger.ForContext<BaseRestClient>();
_baseUrl = baseUrl;
if (!token.StartsWith("Bot ")) if (!token.StartsWith("Bot "))
token = "Bot " + token; token = "Bot " + token;
@ -72,7 +72,7 @@ namespace Myriad.Rest
public async Task<T?> Get<T>(string path, (string endpointName, ulong major) ratelimitParams) where T : class public async Task<T?> Get<T>(string path, (string endpointName, ulong major) ratelimitParams) where T : class
{ {
using var response = await Send(() => new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + path), using var response = await Send(() => new HttpRequestMessage(HttpMethod.Get, _baseUrl + path),
ratelimitParams, true); ratelimitParams, true);
// GET-only special case: 404s are nulls and not exceptions // GET-only special case: 404s are nulls and not exceptions
@ -87,7 +87,7 @@ namespace Myriad.Rest
{ {
using var response = await Send(() => using var response = await Send(() =>
{ {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path); var request = new HttpRequestMessage(HttpMethod.Post, _baseUrl + path);
SetRequestJsonBody(request, body); SetRequestJsonBody(request, body);
return request; return request;
}, ratelimitParams); }, ratelimitParams);
@ -99,7 +99,7 @@ namespace Myriad.Rest
{ {
using var response = await Send(() => using var response = await Send(() =>
{ {
var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path); var request = new HttpRequestMessage(HttpMethod.Post, _baseUrl + path);
SetRequestFormDataBody(request, payload, files); SetRequestFormDataBody(request, payload, files);
return request; return request;
}, ratelimitParams); }, ratelimitParams);
@ -111,7 +111,7 @@ namespace Myriad.Rest
{ {
using var response = await Send(() => using var response = await Send(() =>
{ {
var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path); var request = new HttpRequestMessage(HttpMethod.Patch, _baseUrl + path);
SetRequestJsonBody(request, body); SetRequestJsonBody(request, body);
return request; return request;
}, ratelimitParams); }, ratelimitParams);
@ -123,7 +123,7 @@ namespace Myriad.Rest
{ {
using var response = await Send(() => using var response = await Send(() =>
{ {
var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path); var request = new HttpRequestMessage(HttpMethod.Put, _baseUrl + path);
SetRequestJsonBody(request, body); SetRequestJsonBody(request, body);
return request; return request;
}, ratelimitParams); }, ratelimitParams);
@ -132,7 +132,7 @@ namespace Myriad.Rest
public async Task Delete(string path, (string endpointName, ulong major) ratelimitParams) public async Task Delete(string path, (string endpointName, ulong major) ratelimitParams)
{ {
using var _ = await Send(() => new HttpRequestMessage(HttpMethod.Delete, ApiBaseUrl + path), ratelimitParams); using var _ = await Send(() => new HttpRequestMessage(HttpMethod.Delete, _baseUrl + path), ratelimitParams);
} }
private void SetRequestJsonBody(HttpRequestMessage request, object? body) private void SetRequestJsonBody(HttpRequestMessage request, object? body)
@ -179,7 +179,7 @@ namespace Myriad.Rest
var request = createRequest(); var request = createRequest();
_logger.Debug("Request: {RequestMethod} {RequestPath}", _logger.Debug("Request: {RequestMethod} {RequestPath}",
request.Method, request.RequestUri); request.Method, request.RequestUri?.ToString().Substring(_baseUrl.Length));
request.Version = _httpVersion; request.Version = _httpVersion;
request.VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; request.VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
@ -204,7 +204,8 @@ namespace Myriad.Rest
_logger.Debug( _logger.Debug(
"Response: {RequestMethod} {RequestPath} -> {StatusCode} {ReasonPhrase} (in {ResponseDurationMs} ms)", "Response: {RequestMethod} {RequestPath} -> {StatusCode} {ReasonPhrase} (in {ResponseDurationMs} ms)",
request.Method, request.RequestUri, (int)response.StatusCode, response.ReasonPhrase, stopwatch.ElapsedMilliseconds); request.Method, request.RequestUri?.ToString().Substring(_baseUrl.Length), (int)response.StatusCode,
response.ReasonPhrase, stopwatch.ElapsedMilliseconds);
await HandleApiError(response, ignoreNotFound); await HandleApiError(response, ignoreNotFound);

View File

@ -13,11 +13,12 @@ namespace Myriad.Rest
public class DiscordApiClient public class DiscordApiClient
{ {
public const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)"; public const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)";
private const string DefaultApiBaseUrl = "https://discord.com/api/v9";
private readonly BaseRestClient _client; private readonly BaseRestClient _client;
public DiscordApiClient(string token, ILogger logger) public DiscordApiClient(string token, ILogger logger, string? baseUrl = null)
{ {
_client = new BaseRestClient(UserAgent, token, logger); _client = new BaseRestClient(UserAgent, token, logger, baseUrl ?? DefaultApiBaseUrl);
_client.OnResponseEvent += OnResponseEvent; _client.OnResponseEvent += OnResponseEvent;
} }

View File

@ -20,6 +20,8 @@ namespace PluralKit.Bot
public string? GatewayQueueUrl { get; set; } public string? GatewayQueueUrl { get; set; }
public string? DiscordBaseUrl { get; set; }
public record ClusterSettings public record ClusterSettings
{ {
public string NodeName { get; set; } public string NodeName { get; set; }

View File

@ -48,7 +48,8 @@ namespace PluralKit.Bot
{ {
var client = new Myriad.Rest.DiscordApiClient( var client = new Myriad.Rest.DiscordApiClient(
c.Resolve<BotConfig>().Token, c.Resolve<BotConfig>().Token,
c.Resolve<ILogger>() c.Resolve<ILogger>(),
c.Resolve<BotConfig>().DiscordBaseUrl
); );
client.OnResponseEvent += ((_, ev) => client.OnResponseEvent += ((_, ev) =>