run dotnet format
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
@@ -52,7 +52,7 @@ namespace Myriad.Rest
|
||||
var waitPolicy = Policy
|
||||
.Handle<RatelimitBucketExhaustedException>()
|
||||
.WaitAndRetryAsync(3,
|
||||
(_, e, _) => ((RatelimitBucketExhaustedException) e).RetryAfter,
|
||||
(_, e, _) => ((RatelimitBucketExhaustedException)e).RetryAfter,
|
||||
(_, _, _, _) => Task.CompletedTask)
|
||||
.AsAsyncPolicy<HttpResponseMessage>();
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace Myriad.Rest
|
||||
return default;
|
||||
}
|
||||
|
||||
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),
|
||||
ratelimitParams, true);
|
||||
@@ -81,7 +81,7 @@ namespace Myriad.Rest
|
||||
}
|
||||
|
||||
public async Task<T?> Post<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
|
||||
where T: class
|
||||
where T : class
|
||||
{
|
||||
using var response = await Send(() =>
|
||||
{
|
||||
@@ -91,9 +91,9 @@ namespace Myriad.Rest
|
||||
}, ratelimitParams);
|
||||
return await ReadResponse<T>(response);
|
||||
}
|
||||
|
||||
|
||||
public async Task<T?> PostMultipart<T>(string path, (string endpointName, ulong major) ratelimitParams, object? payload, MultipartFile[]? files)
|
||||
where T: class
|
||||
where T : class
|
||||
{
|
||||
using var response = await Send(() =>
|
||||
{
|
||||
@@ -105,7 +105,7 @@ namespace Myriad.Rest
|
||||
}
|
||||
|
||||
public async Task<T?> Patch<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
|
||||
where T: class
|
||||
where T : class
|
||||
{
|
||||
using var response = await Send(() =>
|
||||
{
|
||||
@@ -117,7 +117,7 @@ namespace Myriad.Rest
|
||||
}
|
||||
|
||||
public async Task<T?> Put<T>(string path, (string endpointName, ulong major) ratelimitParams, object? body)
|
||||
where T: class
|
||||
where T : class
|
||||
{
|
||||
using var response = await Send(() =>
|
||||
{
|
||||
@@ -160,7 +160,7 @@ namespace Myriad.Rest
|
||||
request.Content = mfd;
|
||||
}
|
||||
|
||||
private async Task<T?> ReadResponse<T>(HttpResponseMessage response) where T: class
|
||||
private async Task<T?> ReadResponse<T>(HttpResponseMessage response) where T : class
|
||||
{
|
||||
if (response.StatusCode == HttpStatusCode.NoContent)
|
||||
return null;
|
||||
@@ -174,7 +174,7 @@ namespace Myriad.Rest
|
||||
return await _retryPolicy.ExecuteAsync(async _ =>
|
||||
{
|
||||
using var __ = LogContext.PushProperty("EndpointName", ratelimitParams.endpointName);
|
||||
|
||||
|
||||
var request = createRequest();
|
||||
_logger.Debug("Request: {RequestMethod} {RequestPath}",
|
||||
request.Method, request.RequestUri);
|
||||
@@ -189,7 +189,7 @@ 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, (int)response.StatusCode, response.ReasonPhrase, stopwatch.ElapsedMilliseconds);
|
||||
|
||||
await HandleApiError(response, ignoreNotFound);
|
||||
|
||||
@@ -211,7 +211,7 @@ namespace Myriad.Rest
|
||||
return;
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
var apiError = TryParseApiError(body);
|
||||
var apiError = TryParseApiError(body);
|
||||
if (apiError != null)
|
||||
_logger.Warning("Discord API error: {DiscordErrorCode} {DiscordErrorMessage}", apiError.Code, apiError.Message);
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Myriad.Rest
|
||||
|
||||
public Task<Guild?> GetGuild(ulong id) =>
|
||||
_client.Get<Guild>($"/guilds/{id}", ("GetGuild", id));
|
||||
|
||||
|
||||
public Task<Channel[]> GetGuildChannels(ulong id) =>
|
||||
_client.Get<Channel[]>($"/guilds/{id}/channels", ("GetGuildChannels", id))!;
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace Myriad.Rest
|
||||
|
||||
public Task<Webhook> CreateWebhook(ulong channelId, CreateWebhookRequest request) =>
|
||||
_client.Post<Webhook>($"/channels/{channelId}/webhooks", ("CreateWebhook", channelId), request)!;
|
||||
|
||||
|
||||
public Task<Webhook> GetWebhook(ulong webhookId) =>
|
||||
_client.Get<Webhook>($"/webhooks/{webhookId}/webhooks", ("GetWebhook", webhookId))!;
|
||||
|
||||
@@ -121,7 +121,7 @@ namespace Myriad.Rest
|
||||
var url = $"/webhooks/{webhookId}/{webhookToken}?wait=true";
|
||||
if (threadId != null)
|
||||
url += $"&thread_id={threadId}";
|
||||
|
||||
|
||||
return _client.PostMultipart<Message>(url,
|
||||
("ExecuteWebhook", webhookId), request, files)!;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Myriad.Rest
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
@@ -29,43 +29,49 @@ namespace Myriad.Rest.Exceptions
|
||||
|
||||
public class NotFoundException: DiscordRequestException
|
||||
{
|
||||
public NotFoundException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base(
|
||||
response, responseBody, apiError) { }
|
||||
public NotFoundException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base(
|
||||
response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class UnauthorizedException: DiscordRequestException
|
||||
{
|
||||
public UnauthorizedException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base(
|
||||
response, responseBody, apiError) { }
|
||||
public UnauthorizedException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base(
|
||||
response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class ForbiddenException: DiscordRequestException
|
||||
{
|
||||
public ForbiddenException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base(
|
||||
response, responseBody, apiError) { }
|
||||
public ForbiddenException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base(
|
||||
response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class ConflictException: DiscordRequestException
|
||||
{
|
||||
public ConflictException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base(
|
||||
response, responseBody, apiError) { }
|
||||
public ConflictException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base(
|
||||
response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class BadRequestException: DiscordRequestException
|
||||
{
|
||||
public BadRequestException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError): base(
|
||||
response, responseBody, apiError) { }
|
||||
public BadRequestException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base(
|
||||
response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class TooManyRequestsException: DiscordRequestException
|
||||
{
|
||||
public TooManyRequestsException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError):
|
||||
base(response, responseBody, apiError) { }
|
||||
public TooManyRequestsException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) :
|
||||
base(response, responseBody, apiError)
|
||||
{ }
|
||||
}
|
||||
|
||||
public class UnknownDiscordRequestException: DiscordRequestException
|
||||
{
|
||||
public UnknownDiscordRequestException(HttpResponseMessage response, string responseBody,
|
||||
DiscordApiError? apiError): base(response, responseBody, apiError) { }
|
||||
DiscordApiError? apiError) : base(response, responseBody, apiError) { }
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
using Myriad.Rest.Ratelimit;
|
||||
|
||||
@@ -6,12 +6,12 @@ namespace Myriad.Rest.Exceptions
|
||||
{
|
||||
public class RatelimitException: Exception
|
||||
{
|
||||
public RatelimitException(string? message): base(message) { }
|
||||
public RatelimitException(string? message) : base(message) { }
|
||||
}
|
||||
|
||||
public class RatelimitBucketExhaustedException: RatelimitException
|
||||
{
|
||||
public RatelimitBucketExhaustedException(Bucket bucket, TimeSpan retryAfter): base(
|
||||
public RatelimitBucketExhaustedException(Bucket bucket, TimeSpan retryAfter) : base(
|
||||
"Rate limit bucket exhausted, request blocked")
|
||||
{
|
||||
Bucket = bucket;
|
||||
@@ -24,6 +24,6 @@ namespace Myriad.Rest.Exceptions
|
||||
|
||||
public class GloballyRatelimitedException: RatelimitException
|
||||
{
|
||||
public GloballyRatelimitedException(): base("Global rate limit hit") { }
|
||||
public GloballyRatelimitedException() : base("Global rate limit hit") { }
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
using Serilog;
|
||||
@@ -54,7 +54,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
"{BucketKey}/{BucketMajor}: Bucket has [{BucketRemaining}/{BucketLimit} left], allowing through",
|
||||
Key, Major, Remaining, Limit);
|
||||
Remaining--;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
try
|
||||
{
|
||||
_semaphore.Wait();
|
||||
|
||||
|
||||
_logger.Verbose("{BucketKey}/{BucketMajor}: Received rate limit headers: {@RateLimitHeaders}",
|
||||
Key, Major, headers);
|
||||
|
||||
@@ -90,7 +90,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
}
|
||||
}
|
||||
|
||||
if (headers.Limit != null)
|
||||
if (headers.Limit != null)
|
||||
Limit = headers.Limit.Value;
|
||||
|
||||
if (headers.Remaining != null && !_hasReceivedHeaders)
|
||||
@@ -98,7 +98,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
var oldRemaining = Remaining;
|
||||
Remaining = Math.Min(headers.Remaining.Value, Remaining);
|
||||
|
||||
_logger.Debug("{BucketKey}/{BucketMajor}: Received first remaining of {HeaderRemaining}, previous local remaining is {LocalRemaining}, new local remaining is {Remaining}",
|
||||
_logger.Debug("{BucketKey}/{BucketMajor}: Received first remaining of {HeaderRemaining}, previous local remaining is {LocalRemaining}, new local remaining is {Remaining}",
|
||||
Key, Major, headers.Remaining.Value, oldRemaining, Remaining);
|
||||
_hasReceivedHeaders = true;
|
||||
}
|
||||
@@ -114,7 +114,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
try
|
||||
{
|
||||
_semaphore.Wait();
|
||||
|
||||
|
||||
// If we don't have any reset data, "snap" it to now
|
||||
// This happens before first request and at this point the reset is invalid anyway, so it's fine
|
||||
// but it ensures the stale timeout doesn't trigger early by using `default` value
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -72,7 +72,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
{
|
||||
if (now - bucket.LastUsed <= StaleBucketTimeout)
|
||||
continue;
|
||||
|
||||
|
||||
_logger.Debug("Pruning unused bucket {BucketKey}/{BucketMajor} (last used at {BucketLastUsed})",
|
||||
bucket.Key, bucket.Major, bucket.LastUsed);
|
||||
_buckets.TryRemove(key, out _);
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
@@ -13,7 +13,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
private const string ResetAfterHeader = "X-RateLimit-Reset-After";
|
||||
private const string BucketHeader = "X-RateLimit-Bucket";
|
||||
private const string GlobalHeader = "X-RateLimit-Global";
|
||||
|
||||
|
||||
public bool Global { get; private set; }
|
||||
public int? Limit { get; private set; }
|
||||
public int? Remaining { get; private set; }
|
||||
@@ -25,7 +25,7 @@ namespace Myriad.Rest.Ratelimit
|
||||
|
||||
public bool HasRatelimitInfo =>
|
||||
Limit != null && Remaining != null && Reset != null && ResetAfter != null && Bucket != null;
|
||||
|
||||
|
||||
public RatelimitHeaders() { }
|
||||
|
||||
public static RatelimitHeaders Parse(HttpResponseMessage response)
|
||||
@@ -41,12 +41,12 @@ namespace Myriad.Rest.Ratelimit
|
||||
|
||||
var resetTimestamp = TryGetDouble(response, ResetHeader);
|
||||
if (resetTimestamp != null)
|
||||
headers.Reset = DateTimeOffset.FromUnixTimeMilliseconds((long) (resetTimestamp.Value * 1000));
|
||||
headers.Reset = DateTimeOffset.FromUnixTimeMilliseconds((long)(resetTimestamp.Value * 1000));
|
||||
|
||||
var resetAfterSeconds = TryGetDouble(response, ResetAfterHeader);
|
||||
if (resetAfterSeconds != null)
|
||||
headers.ResetAfter = TimeSpan.FromSeconds(resetAfterSeconds.Value);
|
||||
|
||||
|
||||
var global = TryGetHeader(response, GlobalHeader);
|
||||
if (global != null && bool.TryParse(global, out var globalBool))
|
||||
headers.Global = globalBool;
|
||||
@@ -58,27 +58,27 @@ namespace Myriad.Rest.Ratelimit
|
||||
{
|
||||
if (!response.Headers.TryGetValues(headerName, out var values))
|
||||
return null;
|
||||
|
||||
|
||||
return values.FirstOrDefault();
|
||||
}
|
||||
|
||||
private static int? TryGetInt(HttpResponseMessage response, string headerName)
|
||||
{
|
||||
var valueString = TryGetHeader(response, headerName);
|
||||
|
||||
|
||||
if (!int.TryParse(valueString, NumberStyles.Integer, CultureInfo.InvariantCulture, out var value))
|
||||
return null;
|
||||
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
private static double? TryGetDouble(HttpResponseMessage response, string headerName)
|
||||
{
|
||||
var valueString = TryGetHeader(response, headerName);
|
||||
|
||||
|
||||
if (!double.TryParse(valueString, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
|
||||
return null;
|
||||
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
|
||||
using Myriad.Rest.Exceptions;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Serialization;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
|
||||
namespace Myriad.Rest.Types
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Myriad.Types;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
{
|
||||
public record CreateDmRequest(ulong RecipientId);
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
{
|
||||
public record CreateWebhookRequest(string Name);
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
using Myriad.Types;
|
||||
using Myriad.Types;
|
||||
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Types;
|
||||
using Myriad.Utils;
|
||||
@@ -8,14 +8,14 @@ namespace Myriad.Rest.Types.Requests
|
||||
public record MessageEditRequest
|
||||
{
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<string?> Content { get; init; }
|
||||
|
||||
public Optional<string?> Content { get; init; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<Embed?> Embed { get; init; }
|
||||
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<Message.MessageFlags> Flags { get; init; }
|
||||
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<AllowedMentions> AllowedMentions { get; init; }
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using Myriad.Types;
|
||||
using Myriad.Types;
|
||||
|
||||
namespace Myriad.Rest.Types.Requests
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
namespace Myriad.Rest.Types
|
||||
namespace Myriad.Rest.Types
|
||||
{
|
||||
public record ModifyGuildMemberRequest
|
||||
{
|
||||
|
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Utils;
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace Myriad.Rest.Types.Requests
|
||||
public record WebhookMessageEditRequest
|
||||
{
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<string?> Content { get; init; }
|
||||
|
||||
public Optional<string?> Content { get; init; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<AllowedMentions> AllowedMentions { get; init; }
|
||||
}
|
||||
|
Reference in New Issue
Block a user