Skip to content

Commit 2511ed1

Browse files
committed
fix: configuration and http header fixes
1 parent 508bf52 commit 2511ed1

10 files changed

+56
-34
lines changed

DisCatSharp.Lavalink/LavalinkSession.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ internal async Task EstablishConnectionAsync()
469469
this._webSocket.ExceptionThrown += this.Lavalink_WebSocket_ExceptionThrown;
470470
this._webSocket.MessageReceived += this.Lavalink_WebSocket_MessageReceived;
471471

472-
this._webSocket.AddDefaultHeader("Authorization", this.Config.Password);
472+
this._webSocket.AddDefaultHeader(CommonHeaders.AUTHORIZATION, this.Config.Password);
473473
this._webSocket.AddDefaultHeader("Num-Shards", this.Discord.ShardCount.ToString(CultureInfo.InvariantCulture));
474474
this._webSocket.AddDefaultHeader("User-Id", this.Discord.CurrentUser.Id.ToString(CultureInfo.InvariantCulture));
475475
this._webSocket.AddDefaultHeader("Client-Name", $"DisCatSharp.Lavalink/{this.Discord.VersionString}");

DisCatSharp/Clients/BaseDiscordClient.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics.CodeAnalysis;
66
using System.IO;
77
using System.Linq;
8+
using System.Net;
89
using System.Net.Http;
910
using System.Reflection;
1011
using System.Threading.Tasks;
@@ -230,13 +231,23 @@ protected BaseDiscordClient(DiscordConfiguration config)
230231
this.InternalVoiceRegions = new();
231232
this.VoiceRegionsLazy = new(() => new ReadOnlyDictionary<string, DiscordVoiceRegion>(this.InternalVoiceRegions));
232233

233-
this.RestClient = new();
234-
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent());
235-
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this.Configuration.Locale);
234+
var httphandler = new HttpClientHandler
235+
{
236+
UseCookies = false,
237+
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
238+
UseProxy = this.Configuration.Proxy != null,
239+
Proxy = this.Configuration.Proxy
240+
};
241+
this.RestClient = new()
242+
{
243+
Timeout = this.Configuration.HttpTimeout
244+
};
245+
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
246+
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this.Configuration.Locale);
236247
if (!string.IsNullOrWhiteSpace(this.Configuration.Timezone))
237-
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this.Configuration.Timezone);
248+
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this.Configuration.Timezone);
238249
if (this.Configuration.Override is not null)
239-
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this.Configuration.Override);
250+
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this.Configuration.Override);
240251

241252
var a = typeof(DiscordClient).GetTypeInfo().Assembly;
242253

DisCatSharp/Clients/DiscordClient.WebSocket.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using DisCatSharp.Entities;
88
using DisCatSharp.Enums;
99
using DisCatSharp.EventArgs;
10+
using DisCatSharp.Net;
1011
using DisCatSharp.Net.Abstractions;
1112
using DisCatSharp.Net.WebSocket;
1213

@@ -169,6 +170,7 @@ internal async Task InternalConnectAsync()
169170
Volatile.Write(ref this._skippedHeartbeats, 0);
170171

171172
this.WebSocketClient = this.Configuration.WebSocketClientFactory(this.Configuration.Proxy, this.ServiceProvider);
173+
this.WebSocketClient.AddDefaultHeader(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
172174
this._payloadDecompressor = this.Configuration.GatewayCompressionLevel is not GatewayCompressionLevel.None
173175
? new PayloadDecompressor(this.Configuration.GatewayCompressionLevel)
174176
: null;

DisCatSharp/Clients/DiscordOAuth2Client.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public DiscordOAuth2Client(
140140
}
141141

142142
this.VersionHeader = $"DiscordBot (https://github.com/Aiko-IT-Systems/DisCatSharp, v{vs})";
143-
this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", this.VersionHeader);
143+
this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, this.VersionHeader);
144144

145145
this.OAuth2ClientErroredInternal = new("CLIENT_ERRORED", EventExecutionLimit, this.Goof);
146146

DisCatSharp/Clients/DiscordShardedClient.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,13 @@ private async Task<GatewayInfo> GetGatewayInfoAsync()
397397
Timeout = this._configuration.HttpTimeout
398398
};
399399

400-
http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent());
401-
http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", Utilities.GetFormattedToken(this._configuration));
402-
http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this._configuration.Locale);
400+
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
401+
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.AUTHORIZATION, Utilities.GetFormattedToken(this._configuration));
402+
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._configuration.Locale);
403403
if (!string.IsNullOrWhiteSpace(this._configuration.Timezone))
404-
http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this._configuration.Timezone);
404+
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._configuration.Timezone);
405405
if (this._configuration.Override != null)
406-
http.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this._configuration.Override);
406+
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._configuration.Override);
407407

408408
this.Logger.LogDebug(LoggerEvents.ShardRest, $"Obtaining gateway information from GET {Endpoints.GATEWAY}{Endpoints.BOT}...");
409409
var resp = await http.GetAsync(url).ConfigureAwait(false);

DisCatSharp/DiscordConfiguration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public DiscordConfiguration(DiscordConfiguration other)
8282
this.LoggerFactory = other.LoggerFactory;
8383
this.MobileStatus = other.MobileStatus;
8484
this.AutoRefreshChannelCache = other.AutoRefreshChannelCache;
85+
this.ApiChannel = other.ApiChannel;
8586
this.ApiVersion = other.ApiVersion;
8687
this.ServiceProvider = other.ServiceProvider;
8788
this.Override = other.Override;

DisCatSharp/Entities/Guild/DiscordGuild.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ public string GetWidgetImage(WidgetType bannerType = WidgetType.Shield)
19841984
WidgetType.Banner4 => "banner4",
19851985
_ => "shield"
19861986
};
1987-
return $"{Endpoints.BASE_URI}{Endpoints.GUILDS}/{this.Id}{Endpoints.WIDGET_PNG}?style={param}";
1987+
return $"{Utilities.GetApiBaseUri(this.Discord.Configuration)}{Endpoints.GUILDS}/{this.Id}{Endpoints.WIDGET_PNG}?style={param}";
19881988
}
19891989

19901990
/// <summary>

DisCatSharp/Net/Rest/CommonHeaders.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static class CommonHeaders
6363
/// <summary>
6464
/// Gets or sets the user agent header.
6565
/// </summary>
66-
public const string USER_AGENT = "UserAgent";
66+
public const string USER_AGENT = "User-Agent";
6767

6868
/// <summary>
6969
/// Gets or sets the authorization header.

DisCatSharp/Net/Rest/DiscordApiClient.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ internal DiscordApiClient(DiscordOAuth2Client client, IWebProxy proxy, TimeSpan
5656
{
5757
this.OAuth2Client = client;
5858
this.Discord = null!;
59-
this.Rest = new(proxy, timeout, useRelativeRateLimit, logger);
59+
this.Rest = new(new DiscordConfiguration() {
60+
Proxy = proxy,
61+
HttpTimeout = timeout,
62+
UseRelativeRatelimit = useRelativeRateLimit,
63+
ApiChannel = ApiChannel.Stable,
64+
ApiVersion = "10"
65+
}, logger);
6066
}
6167

6268
/// <summary>
@@ -70,7 +76,13 @@ internal DiscordApiClient(IWebProxy proxy, TimeSpan timeout, bool useRelativeRat
7076
{
7177
this.Discord = null!;
7278
this.OAuth2Client = null!;
73-
this.Rest = new(proxy, timeout, useRelativeRateLimit, logger);
79+
this.Rest = new(new DiscordConfiguration() {
80+
Proxy = proxy,
81+
HttpTimeout = timeout,
82+
UseRelativeRatelimit = useRelativeRateLimit,
83+
ApiChannel = ApiChannel.Stable,
84+
ApiVersion = "10"
85+
}, logger);
7486
}
7587

7688
/// <summary>
@@ -7089,7 +7101,7 @@ internal async Task<DiscordInteractionCallbackResponse> CreateInteractionRespons
70897101
attachments.Add(att);
70907102
fileId++;
70917103
}
7092-
7104+
70937105
if (pld.Data is not null)
70947106
pld.Data.Attachments = attachments;
70957107
}

DisCatSharp/Net/Rest/RestClient.cs

+13-17
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal sealed class RestClient : IDisposable
9191
/// </summary>
9292
/// <param name="client">The client.</param>
9393
internal RestClient(BaseDiscordClient client)
94-
: this(client.Configuration.Proxy, client.Configuration.HttpTimeout, client.Configuration.UseRelativeRatelimit, client.Logger)
94+
: this(client.Configuration, client.Logger)
9595
{
9696
this._discord = client;
9797

@@ -109,14 +109,10 @@ internal RestClient(BaseDiscordClient client)
109109
/// Initializes a new instance of the <see cref="RestClient" /> class.
110110
/// This is for meta-clients, such as the <see cref="DiscordWebhookClient" /> and <see cref="DiscordOAuth2Client" />.
111111
/// </summary>
112-
/// <param name="proxy">The proxy.</param>
113-
/// <param name="timeout">The timeout.</param>
114-
/// <param name="useRelativeRatelimit">Whether to use relative ratelimit.</param>
112+
/// <param name="configuration">The configuration.</param>
115113
/// <param name="logger">The logger.</param>
116114
internal RestClient(
117-
IWebProxy? proxy,
118-
TimeSpan timeout,
119-
bool useRelativeRatelimit,
115+
DiscordConfiguration configuration,
120116
ILogger logger
121117
)
122118
{
@@ -126,32 +122,32 @@ ILogger logger
126122
{
127123
UseCookies = false,
128124
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
129-
UseProxy = proxy != null,
130-
Proxy = proxy
125+
UseProxy = configuration.Proxy != null,
126+
Proxy = configuration.Proxy
131127
};
132128

133129
this.HttpClient = new(httphandler)
134130
{
135-
BaseAddress = new(Utilities.GetApiBaseUri(this._discord?.Configuration)),
136-
Timeout = timeout
131+
BaseAddress = new(Utilities.GetApiBaseUri(configuration)),
132+
Timeout = configuration.HttpTimeout
137133
};
138134

139135
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
140136
if (this._discord is { Configuration: not null })
141137
{
142-
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._discord.Configuration.Locale);
143-
if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Timezone))
144-
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._discord.Configuration.Timezone);
145-
if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Override))
146-
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._discord.Configuration.Override);
138+
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, configuration.Locale);
139+
if (!string.IsNullOrWhiteSpace(configuration.Timezone))
140+
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, configuration.Timezone);
141+
if (!string.IsNullOrWhiteSpace(configuration.Override))
142+
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, configuration.Override);
147143
}
148144

149145
this._routesToHashes = new();
150146
this._hashesToBuckets = new();
151147
this._requestQueue = new();
152148

153149
this._globalRateLimitEvent = new(true);
154-
this._useResetAfter = useRelativeRatelimit;
150+
this._useResetAfter = configuration.UseRelativeRatelimit;
155151
}
156152

157153
/// <summary>

0 commit comments

Comments
 (0)