Browse Source

Remove token overrides from REST.

Leaving that as a Webhook package only feature.
pull/843/head
Alex Gravely 8 years ago
parent
commit
681aba42cf
6 changed files with 46 additions and 50 deletions
  1. +1
    -1
      src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
  2. +1
    -1
      src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs
  3. +20
    -32
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  4. +17
    -11
      src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs
  5. +4
    -5
      src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs
  6. +3
    -0
      src/Discord.Net.Webhook/DiscordWebhookClient.cs

+ 1
- 1
src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs View File

@@ -29,6 +29,6 @@ namespace Discord
IUser Creator { get; }

/// <summary> Modifies this webhook. </summary>
Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null);
Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null);
}
}

+ 1
- 1
src/Discord.Net.Rest/API/Rest/CreateWebhookMessageParams.cs View File

@@ -9,7 +9,7 @@ namespace Discord.API.Rest
[JsonProperty("content")]
public string Content { get; }
[JsonProperty("wait")]
public bool ReturnCreatedMessage { get; set; }
public bool ReturnCreatedMessage { get; set; } = true;

[JsonProperty("nonce")]
public Optional<string> Nonce { get; set; }


+ 20
- 32
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -473,13 +473,11 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Message>("POST", () => $"channels/{channelId}/messages", args, ids, clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
}
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, string webhookToken = null, RequestOptions options = null)
public async Task<Message> CreateWebhookMessageAsync(ulong webhookId, CreateWebhookMessageParams args, RequestOptions options = null)
{
if (AuthTokenType != TokenType.Webhook && string.IsNullOrWhiteSpace(webhookToken))
if (AuthTokenType != TokenType.Webhook)
throw new InvalidOperationException($"This operation may only be called with a {nameof(TokenType.Webhook)} token.");

webhookToken = webhookToken ?? AuthToken;

Preconditions.NotNull(args, nameof(args));
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
if (!args.Embeds.IsSpecified || args.Embeds.Value == null || args.Embeds.Value.Length == 0)
@@ -490,9 +488,9 @@ namespace Discord.API
options = RequestOptions.CreateOrClone(options);

if (args.ReturnCreatedMessage)
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
return await SendJsonAsync<Message>("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);

await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{webhookToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
await SendJsonAsync("POST", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), clientBucket: ClientBucketType.SendEdit, options: options).ConfigureAwait(false);
return null;
}
public async Task<Message> UploadFileAsync(ulong channelId, UploadFileParams args, RequestOptions options = null)
@@ -1170,51 +1168,41 @@ namespace Discord.API

return await SendJsonAsync<Webhook>("POST", () => $"channels/{channelId}/webhooks", args, new BucketIds(), options: options);
}
public async Task<Webhook> GetWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null)
public async Task<Webhook> GetWebhookAsync(ulong webhookId, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
options = RequestOptions.CreateOrClone(options);

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

try
{
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false);
if (AuthTokenType == TokenType.Webhook)
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false);
else
return await SendAsync<Webhook>("GET", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false);
}
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.NotFound) { return null; }
}
public async Task<Webhook> ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, string webhookToken = null, RequestOptions options = null)
public async Task<Webhook> ModifyWebhookAsync(ulong webhookId, ModifyWebhookParams args, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
Preconditions.NotNull(args, nameof(args));
Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name));
options = RequestOptions.CreateOrClone(options);

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}{webhookToken}", args, new BucketIds(), options: options).ConfigureAwait(false);
if (AuthTokenType == TokenType.Webhook)
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}/{AuthToken}", args, new BucketIds(), options: options).ConfigureAwait(false);
else
return await SendJsonAsync<Webhook>("PATCH", () => $"webhooks/{webhookId}", args, new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task DeleteWebhookAsync(ulong webhookId, string webhookToken = null, RequestOptions options = null)
public async Task DeleteWebhookAsync(ulong webhookId, RequestOptions options = null)
{
Preconditions.NotEqual(webhookId, 0, nameof(webhookId));
options = RequestOptions.CreateOrClone(options);
options.IgnoreState = true;

if (!string.IsNullOrWhiteSpace(webhookToken))
{
webhookToken = "/" + webhookToken;
options.IgnoreState = true;
}

await SendAsync("DELETE", () => $"webhooks/{webhookId}{webhookToken}", new BucketIds(), options: options).ConfigureAwait(false);
if (AuthTokenType == TokenType.Webhook)
await SendAsync("DELETE", () => $"webhooks/{webhookId}/{AuthToken}", new BucketIds(), options: options).ConfigureAwait(false);
else
await SendAsync("DELETE", () => $"webhooks/{webhookId}", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<Webhook>> GetGuildWebhooksAsync(ulong guildId, RequestOptions options = null)
{


+ 17
- 11
src/Discord.Net.Rest/Entities/Webhooks/RestWebhook.cs View File

@@ -8,8 +8,8 @@ namespace Discord.Rest
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestWebhook : RestEntity<ulong>, IWebhook, IUpdateable
{
internal IGuild Guild { get; }
internal ITextChannel Channel { get; }
internal IGuild Guild { get; private set; }
internal ITextChannel Channel { get; private set; }
public string Token { get; private set; }
public string Name { get; private set; }
public string AvatarId { get; private set; }
@@ -57,21 +57,29 @@ namespace Discord.Rest

public async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetWebhookAsync(Id, Token, options).ConfigureAwait(false);
var model = await Discord.ApiClient.GetWebhookAsync(Id, options).ConfigureAwait(false);
Update(model);
}

public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);

public async Task ModifyAsync(Action<WebhookProperties> func, string webhookToken = null, RequestOptions options = null)
public async Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null)
{
var model = await WebhookHelper.ModifyAsync(this, Discord, func, webhookToken, options).ConfigureAwait(false);
var model = await WebhookHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
Update(model);
}

public Task DeleteAsync(string webhookToken = null, RequestOptions options = null)
=> WebhookHelper.DeleteAsync(this, Discord, webhookToken, options);
public Task DeleteAsync(RequestOptions options = null)
=> WebhookHelper.DeleteAsync(this, Discord, options);

public async Task<RestTextChannel> GetChannelAsync(RequestOptions options = null)
{
Channel = await ClientHelper.GetChannelAsync(Discord, ChannelId, options) as RestTextChannel;
Guild = Channel.Guild;

return Channel as RestTextChannel;
}

public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
@@ -81,9 +89,7 @@ namespace Discord.Rest
=> Guild ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
ITextChannel IWebhook.Channel
=> Channel ?? throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
Task IWebhook.ModifyAsync(Action<WebhookProperties> func, string webhookToken, RequestOptions options)
=> ModifyAsync(func, webhookToken, options);
Task IDeletable.DeleteAsync(RequestOptions options)
=> DeleteAsync(Token, options);
Task IWebhook.ModifyAsync(Action<WebhookProperties> func, RequestOptions options)
=> ModifyAsync(func, options);
}
}

+ 4
- 5
src/Discord.Net.Rest/Entities/Webhooks/WebhookHelper.cs View File

@@ -9,7 +9,7 @@ namespace Discord.Rest
internal static class WebhookHelper
{
public static async Task<Model> ModifyAsync(IWebhook webhook, BaseDiscordClient client,
Action<WebhookProperties> func, string webhookToken, RequestOptions options)
Action<WebhookProperties> func, RequestOptions options)
{
var args = new WebhookProperties();
func(args);
@@ -22,12 +22,11 @@ namespace Discord.Rest
if (!apiArgs.Avatar.IsSpecified && webhook.AvatarId != null)
apiArgs.Avatar = new ImageModel(webhook.AvatarId);

return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, webhookToken, options).ConfigureAwait(false);
return await client.ApiClient.ModifyWebhookAsync(webhook.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, string webhookToken,
RequestOptions options)
public static async Task DeleteAsync(IWebhook webhook, BaseDiscordClient client, RequestOptions options)
{
await client.ApiClient.DeleteWebhookAsync(webhook.Id, webhookToken, options).ConfigureAwait(false);
await client.ApiClient.DeleteWebhookAsync(webhook.Id, options).ConfigureAwait(false);
}

}


+ 3
- 0
src/Discord.Net.Webhook/DiscordWebhookClient.cs View File

@@ -19,6 +19,9 @@ namespace Discord.Webhook
internal API.DiscordRestApiClient ApiClient { get; }
internal LogManager LogManager { get; }

/// <summary> Creates a new Webhook discord client. </summary>
public DiscordWebhookClient(IWebhook webhook)
: this(webhook.Id, webhook.Token) { }
/// <summary> Creates a new Webhook discord client. </summary>
public DiscordWebhookClient(ulong webhookId, string webhookToken)
: this(webhookId, webhookToken, new DiscordRestConfig()) { }


Loading…
Cancel
Save