diff --git a/Myriad/Rest/BaseRestClient.cs b/Myriad/Rest/BaseRestClient.cs index 8e8e6f80..4013cc22 100644 --- a/Myriad/Rest/BaseRestClient.cs +++ b/Myriad/Rest/BaseRestClient.cs @@ -23,17 +23,17 @@ namespace Myriad.Rest { public class BaseRestClient: IAsyncDisposable { - private const string ApiBaseUrl = "https://discord.com/api/v9"; - private readonly Version _httpVersion = new(2, 0); private readonly JsonSerializerOptions _jsonSerializerOptions; private readonly ILogger _logger; private readonly Ratelimiter _ratelimiter; private readonly AsyncPolicy _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(); + _baseUrl = baseUrl; if (!token.StartsWith("Bot ")) token = "Bot " + token; @@ -72,7 +72,7 @@ namespace Myriad.Rest public async Task Get(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); // GET-only special case: 404s are nulls and not exceptions @@ -87,7 +87,7 @@ namespace Myriad.Rest { using var response = await Send(() => { - var request = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + path); + var request = new HttpRequestMessage(HttpMethod.Post, _baseUrl + path); SetRequestJsonBody(request, body); return request; }, ratelimitParams); @@ -99,7 +99,7 @@ namespace Myriad.Rest { 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); return request; }, ratelimitParams); @@ -111,7 +111,7 @@ namespace Myriad.Rest { using var response = await Send(() => { - var request = new HttpRequestMessage(HttpMethod.Patch, ApiBaseUrl + path); + var request = new HttpRequestMessage(HttpMethod.Patch, _baseUrl + path); SetRequestJsonBody(request, body); return request; }, ratelimitParams); @@ -123,7 +123,7 @@ namespace Myriad.Rest { using var response = await Send(() => { - var request = new HttpRequestMessage(HttpMethod.Put, ApiBaseUrl + path); + var request = new HttpRequestMessage(HttpMethod.Put, _baseUrl + path); SetRequestJsonBody(request, body); return request; }, ratelimitParams); @@ -132,7 +132,7 @@ namespace Myriad.Rest 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) @@ -179,7 +179,7 @@ namespace Myriad.Rest var request = createRequest(); _logger.Debug("Request: {RequestMethod} {RequestPath}", - request.Method, request.RequestUri); + request.Method, request.RequestUri?.ToString().Substring(_baseUrl.Length)); request.Version = _httpVersion; request.VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher; @@ -204,7 +204,8 @@ namespace Myriad.Rest _logger.Debug( "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); diff --git a/Myriad/Rest/DiscordApiClient.cs b/Myriad/Rest/DiscordApiClient.cs index a7de74a3..3082574b 100644 --- a/Myriad/Rest/DiscordApiClient.cs +++ b/Myriad/Rest/DiscordApiClient.cs @@ -13,11 +13,12 @@ namespace Myriad.Rest public class DiscordApiClient { 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; - 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; } diff --git a/PluralKit.Bot/BotConfig.cs b/PluralKit.Bot/BotConfig.cs index aeb94f5f..5f2b4613 100644 --- a/PluralKit.Bot/BotConfig.cs +++ b/PluralKit.Bot/BotConfig.cs @@ -20,6 +20,8 @@ namespace PluralKit.Bot public string? GatewayQueueUrl { get; set; } + public string? DiscordBaseUrl { get; set; } + public record ClusterSettings { public string NodeName { get; set; } diff --git a/PluralKit.Bot/Modules.cs b/PluralKit.Bot/Modules.cs index 95eb07a9..7c54ef4a 100644 --- a/PluralKit.Bot/Modules.cs +++ b/PluralKit.Bot/Modules.cs @@ -48,7 +48,8 @@ namespace PluralKit.Bot { var client = new Myriad.Rest.DiscordApiClient( c.Resolve().Token, - c.Resolve() + c.Resolve(), + c.Resolve().DiscordBaseUrl ); client.OnResponseEvent += ((_, ev) =>