* Move REST requests to appropiate class * Add call to ClientHelper and expose to public API * Expose shard count request in public api * Expose method from interface * Update sharded client to utilize the new method * Method is already implemented in a base class * Refactor name to fit pattern for methods returning a `Task` * Adds missing ConfigureAwait * Corrects unnecessary whitespace * Removes unneeded whitespacepull/984/head
@@ -36,5 +36,7 @@ namespace Discord | |||||
Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null); | Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null); | ||||
Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null); | Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null); | ||||
Task<int> GetRecommendedShardCountAsync(RequestOptions options = null); | |||||
} | } | ||||
} | } |
@@ -1,4 +1,4 @@ | |||||
using Discord.Logging; | |||||
using Discord.Logging; | |||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Collections.Immutable; | using System.Collections.Immutable; | ||||
@@ -125,6 +125,10 @@ namespace Discord.Rest | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public void Dispose() => Dispose(true); | public void Dispose() => Dispose(true); | ||||
/// <inheritdoc /> | |||||
public Task<int> GetRecommendedShardCountAsync(RequestOptions options = null) | |||||
=> ClientHelper.GetRecommendShardCountAsync(this, options); | |||||
//IDiscordClient | //IDiscordClient | ||||
ConnectionState IDiscordClient.ConnectionState => ConnectionState.Disconnected; | ConnectionState IDiscordClient.ConnectionState => ConnectionState.Disconnected; | ||||
ISelfUser IDiscordClient.CurrentUser => CurrentUser; | ISelfUser IDiscordClient.CurrentUser => CurrentUser; | ||||
@@ -1,4 +1,4 @@ | |||||
using Discord.API.Rest; | |||||
using Discord.API.Rest; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Collections.Immutable; | using System.Collections.Immutable; | ||||
using System.IO; | using System.IO; | ||||
@@ -163,5 +163,11 @@ namespace Discord.Rest | |||||
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); | var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); | ||||
return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id); | return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id); | ||||
} | } | ||||
public static async Task<int> GetRecommendShardCountAsync(BaseDiscordClient client, RequestOptions options) | |||||
{ | |||||
var response = await client.ApiClient.GetBotGatewayAsync(options).ConfigureAwait(false); | |||||
return response.Shards; | |||||
} | |||||
} | } | ||||
} | } |
@@ -269,6 +269,18 @@ namespace Discord.API | |||||
await SendAsync("GET", () => "auth/login", new BucketIds(), options: options).ConfigureAwait(false); | await SendAsync("GET", () => "auth/login", new BucketIds(), options: options).ConfigureAwait(false); | ||||
} | } | ||||
//Gateway | |||||
public async Task<GetGatewayResponse> GetGatewayAsync(RequestOptions options = null) | |||||
{ | |||||
options = RequestOptions.CreateOrClone(options); | |||||
return await SendAsync<GetGatewayResponse>("GET", () => "gateway", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | |||||
public async Task<GetBotGatewayResponse> GetBotGatewayAsync(RequestOptions options = null) | |||||
{ | |||||
options = RequestOptions.CreateOrClone(options); | |||||
return await SendAsync<GetBotGatewayResponse>("GET", () => "gateway/bot", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | |||||
//Channels | //Channels | ||||
public async Task<Channel> GetChannelAsync(ulong channelId, RequestOptions options = null) | public async Task<Channel> GetChannelAsync(ulong channelId, RequestOptions options = null) | ||||
{ | { | ||||
@@ -1,4 +1,4 @@ | |||||
using System.Collections.Generic; | |||||
using System.Collections.Generic; | |||||
using System.Collections.Immutable; | using System.Collections.Immutable; | ||||
using System.IO; | using System.IO; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
@@ -1,4 +1,4 @@ | |||||
using System.Collections.Generic; | |||||
using System.Collections.Generic; | |||||
using System.IO; | using System.IO; | ||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using Discord.API; | using Discord.API; | ||||
@@ -1,4 +1,4 @@ | |||||
using Discord.API; | |||||
using Discord.API; | |||||
using Discord.Rest; | using Discord.Rest; | ||||
using System; | using System; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
@@ -75,8 +75,8 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
if (_automaticShards) | if (_automaticShards) | ||||
{ | { | ||||
var response = await ApiClient.GetBotGatewayAsync().ConfigureAwait(false); | |||||
_shardIds = Enumerable.Range(0, response.Shards).ToArray(); | |||||
var shardCount = await GetRecommendedShardCountAsync().ConfigureAwait(false); | |||||
_shardIds = Enumerable.Range(0, shardCount).ToArray(); | |||||
_totalShards = _shardIds.Length; | _totalShards = _shardIds.Length; | ||||
_shards = new DiscordSocketClient[_shardIds.Length]; | _shards = new DiscordSocketClient[_shardIds.Length]; | ||||
for (int i = 0; i < _shardIds.Length; i++) | for (int i = 0; i < _shardIds.Length; i++) | ||||
@@ -1,4 +1,4 @@ | |||||
#pragma warning disable CS1591 | |||||
#pragma warning disable CS1591 | |||||
using Discord.API.Gateway; | using Discord.API.Gateway; | ||||
using Discord.API.Rest; | using Discord.API.Rest; | ||||
using Discord.Net.Queue; | using Discord.Net.Queue; | ||||
@@ -207,18 +207,7 @@ namespace Discord.API | |||||
await RequestQueue.SendAsync(new WebSocketRequest(WebSocketClient, null, bytes, true, options)).ConfigureAwait(false); | await RequestQueue.SendAsync(new WebSocketRequest(WebSocketClient, null, bytes, true, options)).ConfigureAwait(false); | ||||
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); | await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); | ||||
} | } | ||||
//Gateway | |||||
public async Task<GetGatewayResponse> GetGatewayAsync(RequestOptions options = null) | |||||
{ | |||||
options = RequestOptions.CreateOrClone(options); | |||||
return await SendAsync<GetGatewayResponse>("GET", () => "gateway", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | |||||
public async Task<GetBotGatewayResponse> GetBotGatewayAsync(RequestOptions options = null) | |||||
{ | |||||
options = RequestOptions.CreateOrClone(options); | |||||
return await SendAsync<GetBotGatewayResponse>("GET", () => "gateway/bot", new BucketIds(), options: options).ConfigureAwait(false); | |||||
} | |||||
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null) | public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null) | ||||
{ | { | ||||
options = RequestOptions.CreateOrClone(options); | options = RequestOptions.CreateOrClone(options); | ||||