[Feature] SentRequest event (#2870)

This commit is contained in:
Mihail Gribkov
2024-03-04 21:36:11 +03:00
committed by GitHub
parent fa51f0a86f
commit fc2fc87708
2 changed files with 10 additions and 0 deletions

View File

@@ -20,6 +20,13 @@ namespace Discord.Rest
public event Func<Task> LoggedOut { add { _loggedOutEvent.Add(value); } remove { _loggedOutEvent.Remove(value); } } public event Func<Task> LoggedOut { add { _loggedOutEvent.Add(value); } remove { _loggedOutEvent.Remove(value); } }
private readonly AsyncEvent<Func<Task>> _loggedOutEvent = new AsyncEvent<Func<Task>>(); private readonly AsyncEvent<Func<Task>> _loggedOutEvent = new AsyncEvent<Func<Task>>();
internal readonly AsyncEvent<Func<string, string, double, Task>> _sentRequest = new();
/// <summary>
/// Fired when a REST request is sent to the API. First parameter is the HTTP method,
/// second is the endpoint, and third is the time taken to complete the request.
/// </summary>
public event Func<string, string, double, Task> SentRequest { add { _sentRequest.Add(value); } remove { _sentRequest.Remove(value); } }
internal readonly Logger _restLogger; internal readonly Logger _restLogger;
private readonly SemaphoreSlim _stateLock; private readonly SemaphoreSlim _stateLock;
private bool _isFirstLogin, _isDisposed; private bool _isFirstLogin, _isDisposed;
@@ -61,6 +68,7 @@ namespace Discord.Rest
await _restLogger.WarningAsync($"Rate limit triggered: {endpoint} Remaining: {info.Value.RetryAfter}s {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false); await _restLogger.WarningAsync($"Rate limit triggered: {endpoint} Remaining: {info.Value.RetryAfter}s {(id.IsHashBucket ? $"(Bucket: {id.BucketHash})" : "")}").ConfigureAwait(false);
}; };
ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false); ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false);
ApiClient.SentRequest += (method, endpoint, millis) => _sentRequest.InvokeAsync(method, endpoint, millis);
} }
public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true) public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true)

View File

@@ -431,6 +431,8 @@ namespace Discord.WebSocket
return Task.Delay(0); return Task.Delay(0);
}; };
client.SentRequest += (method, endpoint, millis) => _sentRequest.InvokeAsync(method, endpoint, millis);
client.Connected += () => _shardConnectedEvent.InvokeAsync(client); client.Connected += () => _shardConnectedEvent.InvokeAsync(client);
client.Disconnected += (exception) => _shardDisconnectedEvent.InvokeAsync(exception, client); client.Disconnected += (exception) => _shardDisconnectedEvent.InvokeAsync(exception, client);
client.Ready += () => _shardReadyEvent.InvokeAsync(client); client.Ready += () => _shardReadyEvent.InvokeAsync(client);