Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using NetCord.Gateway.LatencyTimers;
using NetCord.Gateway.ReconnectStrategies;
using NetCord.Gateway.WebSockets;
using NetCord.Rest;
using NetCord.Hosting.Rest;

namespace NetCord.Hosting.Gateway;

Expand Down Expand Up @@ -66,7 +66,7 @@ internal partial class Validator : IValidateOptions<GatewayClientOptions>
public Shard? Shard { get; set; }

/// <inheritdoc cref="GatewayClientConfiguration.RestClientConfiguration" />
public RestClientConfiguration? RestClientConfiguration { get; set; }
public RestClientOptions RestClientOptions { get; } = new();

internal GatewayClientConfiguration CreateConfiguration(IServiceProvider services)
{
Expand All @@ -84,7 +84,7 @@ internal GatewayClientConfiguration CreateConfiguration(IServiceProvider service
LargeThreshold,
Presence,
Shard,
RestClientConfiguration,
new GatewayMicrosoftExtensionsLogger(services));
RestClientOptions.CreateConfiguration(services),
new MicrosoftExtensionsGatewayLogger(services));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace NetCord.Hosting.Gateway;

internal class GatewayMicrosoftExtensionsLogger(IServiceProvider services) : IGatewayLogger, IRestLogger
internal class MicrosoftExtensionsGatewayLogger(IServiceProvider services) : IGatewayLogger, IRestLogger
{
private readonly ILogger<GatewayClient> _gatewayLogger = services.GetRequiredService<ILogger<GatewayClient>>();
private readonly ILogger<RestClient> _restLogger = services.GetRequiredService<ILogger<RestClient>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NetCord.Hosting.Gateway;

internal class ShardedGatewayMicrosoftExtensionsLogger(int shardId, IServiceProvider services) : IGatewayLogger
internal class MicrosoftExtensionsShardedGatewayLogger(int shardId, IServiceProvider services) : IGatewayLogger
{
private readonly ILogger<GatewayClient> _gatewayLogger = services.GetRequiredService<ILogger<GatewayClient>>();
private readonly EventId _eventId = new(shardId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ internal ShardedGatewayClientConfiguration CreateConfiguration(IServiceProvider
IGatewayLogger? CreateLogger(Shard? shard)
{
if (shard.HasValue)
return new ShardedGatewayMicrosoftExtensionsLogger(shard.GetValueOrDefault().Id, services);
return new MicrosoftExtensionsShardedGatewayLogger(shard.GetValueOrDefault().Id, services);

return new RestMicrosoftExtensionsLogger(services);
return new MicrosoftExtensionsRestLogger(services);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using NetCord.Logging;
using NetCord.Rest;

namespace NetCord.Hosting.Rest;

internal class RestMicrosoftExtensionsLogger(IServiceProvider services) : IRestLogger, IGatewayLogger
internal class MicrosoftExtensionsRestLogger(IServiceProvider services) : IRestLogger, IGatewayLogger
{
private readonly ILogger<RestClient> _logger = services.GetRequiredService<ILogger<RestClient>>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NetCord.Rest;

namespace NetCord.Hosting.Rest;

internal sealed class MicrosoftExtensionsRestRequestHandler(HttpClient httpClient) : IRestRequestHandler
{
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) => httpClient.SendAsync(request, cancellationToken);

public void AddDefaultHeader(string name, IEnumerable<string> values)
{
httpClient.DefaultRequestHeaders.Add(name, values);
}

public void Dispose()
{
httpClient.Dispose();
}
}
28 changes: 25 additions & 3 deletions Hosting/NetCord.Hosting/Rest/RestClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NetCord.Rest;
using System.Reflection;

using NetCord.Rest;
using NetCord.Rest.RateLimits;

namespace NetCord.Hosting.Rest;
Expand Down Expand Up @@ -30,10 +32,30 @@ internal RestClientConfiguration CreateConfiguration(IServiceProvider services)
{
Hostname = Hostname,
Version = Version,
RequestHandler = RequestHandler,
RequestHandler = RequestHandler ?? CreateDefaultRequestHandler(services),
DefaultRequestProperties = DefaultRequestProperties,
RateLimitManager = RateLimitManager,
Logger = new RestMicrosoftExtensionsLogger(services),
Logger = new MicrosoftExtensionsRestLogger(services),
};
}

private static MicrosoftExtensionsRestRequestHandler? CreateDefaultRequestHandler(IServiceProvider services)
{
var factoryType = Type.GetType("System.Net.Http.IHttpClientFactory,Microsoft.Extensions.Http");
if (factoryType is not null)
{
var method = factoryType.GetMethod("CreateClient", BindingFlags.Public | BindingFlags.Instance, [typeof(string)]);
if (method is null || method.ReturnType != typeof(HttpClient))
return null; // Wrong type

if (services.GetService(factoryType) is { } factory)
{
var client = (HttpClient)method.Invoke(factory, [nameof(RestClient)])!;

return new(client);
}
}

return null;
}
}
1 change: 1 addition & 0 deletions Tests/NetCord.Test.Hosting/NetCord.Test.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Tests/NetCord.Test.Hosting/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
var builder = Host.CreateApplicationBuilder(args);

builder.Services
.AddHttpClient()
.ConfigureDiscordGateway(o => o.Presence = new(UserStatusType.DoNotDisturb))
.ConfigureCommands<CommandContext>(o => o.Prefix = "!")
.ConfigureCommands(o => o.Prefix = ">")
Expand Down