Added RequestOptions to RestClient methods. Added guild summary paging.

This commit is contained in:
RogueException
2017-03-31 03:01:49 -03:00
parent 5aa92f8954
commit 5e94b97024
10 changed files with 201 additions and 156 deletions

View File

@@ -10,80 +10,104 @@ namespace Discord.Rest
internal static class ClientHelper
{
//Applications
public static async Task<RestApplication> GetApplicationInfoAsync(BaseDiscordClient client)
public static async Task<RestApplication> GetApplicationInfoAsync(BaseDiscordClient client, RequestOptions options)
{
var model = await client.ApiClient.GetMyApplicationAsync().ConfigureAwait(false);
var model = await client.ApiClient.GetMyApplicationAsync(options).ConfigureAwait(false);
return RestApplication.Create(client, model);
}
public static async Task<RestChannel> GetChannelAsync(BaseDiscordClient client,
ulong id)
ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetChannelAsync(id).ConfigureAwait(false);
var model = await client.ApiClient.GetChannelAsync(id, options).ConfigureAwait(false);
if (model != null)
return RestChannel.Create(client, model);
return null;
}
public static async Task<IReadOnlyCollection<IRestPrivateChannel>> GetPrivateChannelsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<IRestPrivateChannel>> GetPrivateChannelsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
return models.Select(x => RestChannel.CreatePrivate(client, x)).ToImmutableArray();
}
public static async Task<IReadOnlyCollection<RestDMChannel>> GetDMChannelsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<RestDMChannel>> GetDMChannelsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
return models
.Where(x => x.Type == ChannelType.DM)
.Select(x => RestDMChannel.Create(client, x)).ToImmutableArray();
}
public static async Task<IReadOnlyCollection<RestGroupChannel>> GetGroupChannelsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<RestGroupChannel>> GetGroupChannelsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
return models
.Where(x => x.Type == ChannelType.Group)
.Select(x => RestGroupChannel.Create(client, x)).ToImmutableArray();
}
public static async Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetMyConnectionsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetMyConnectionsAsync(options).ConfigureAwait(false);
return models.Select(x => RestConnection.Create(x)).ToImmutableArray();
}
public static async Task<RestInvite> GetInviteAsync(BaseDiscordClient client,
string inviteId)
string inviteId, RequestOptions options)
{
var model = await client.ApiClient.GetInviteAsync(inviteId).ConfigureAwait(false);
var model = await client.ApiClient.GetInviteAsync(inviteId, options).ConfigureAwait(false);
if (model != null)
return RestInvite.Create(client, null, null, model);
return null;
}
public static async Task<RestGuild> GetGuildAsync(BaseDiscordClient client,
ulong id)
ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetGuildAsync(id).ConfigureAwait(false);
var model = await client.ApiClient.GetGuildAsync(id, options).ConfigureAwait(false);
if (model != null)
return RestGuild.Create(client, model);
return null;
}
public static async Task<RestGuildEmbed?> GetGuildEmbedAsync(BaseDiscordClient client,
ulong id)
ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetGuildEmbedAsync(id).ConfigureAwait(false);
var model = await client.ApiClient.GetGuildEmbedAsync(id, options).ConfigureAwait(false);
if (model != null)
return RestGuildEmbed.Create(model);
return null;
}
public static async Task<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(BaseDiscordClient client)
public static IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(BaseDiscordClient client,
ulong? fromGuildId, int? limit, RequestOptions options)
{
var models = await client.ApiClient.GetMyGuildsAsync().ConfigureAwait(false);
return models.Select(x => RestUserGuild.Create(client, x)).ToImmutableArray();
return new PagedAsyncEnumerable<RestUserGuild>(
DiscordConfig.MaxUsersPerBatch,
async (info, ct) =>
{
var args = new GetGuildSummariesParams
{
Limit = info.PageSize
};
if (info.Position != null)
args.AfterGuildId = info.Position.Value;
var models = await client.ApiClient.GetMyGuildsAsync(args, options).ConfigureAwait(false);
return models
.Select(x => RestUserGuild.Create(client, x))
.ToImmutableArray();
},
nextPage: (info, lastPage) =>
{
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
return false;
info.Position = lastPage.Max(x => x.Id);
return true;
},
start: fromGuildId,
count: limit
);
}
public static async Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(BaseDiscordClient client, RequestOptions options)
{
var summaryModels = await client.ApiClient.GetMyGuildsAsync().ConfigureAwait(false);
var guilds = ImmutableArray.CreateBuilder<RestGuild>(summaryModels.Count);
var summaryModels = await GetGuildSummariesAsync(client, null, null, options).Flatten();
var guilds = ImmutableArray.CreateBuilder<RestGuild>();
foreach (var summaryModel in summaryModels)
{
var guildModel = await client.ApiClient.GetGuildAsync(summaryModel.Id).ConfigureAwait(false);
@@ -93,39 +117,39 @@ namespace Discord.Rest
return guilds.ToImmutable();
}
public static async Task<RestGuild> CreateGuildAsync(BaseDiscordClient client,
string name, IVoiceRegion region, Stream jpegIcon = null)
string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
{
var args = new CreateGuildParams(name, region.Id);
var model = await client.ApiClient.CreateGuildAsync(args).ConfigureAwait(false);
var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false);
return RestGuild.Create(client, model);
}
public static async Task<RestUser> GetUserAsync(BaseDiscordClient client,
ulong id)
ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetUserAsync(id).ConfigureAwait(false);
var model = await client.ApiClient.GetUserAsync(id, options).ConfigureAwait(false);
if (model != null)
return RestUser.Create(client, model);
return null;
}
public static async Task<RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
ulong guildId, ulong id)
ulong guildId, ulong id, RequestOptions options)
{
var model = await client.ApiClient.GetGuildMemberAsync(guildId, id).ConfigureAwait(false);
var model = await client.ApiClient.GetGuildMemberAsync(guildId, id, options).ConfigureAwait(false);
if (model != null)
return RestGuildUser.Create(client, new RestGuild(client, guildId), model);
return null;
}
public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client)
public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options)
{
var models = await client.ApiClient.GetVoiceRegionsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
return models.Select(x => RestVoiceRegion.Create(client, x)).ToImmutableArray();
}
public static async Task<RestVoiceRegion> GetVoiceRegionAsync(BaseDiscordClient client,
string id)
string id, RequestOptions options)
{
var models = await client.ApiClient.GetVoiceRegionsAsync().ConfigureAwait(false);
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
return models.Select(x => RestVoiceRegion.Create(client, x)).Where(x => x.Id == id).FirstOrDefault();
}
}