From b745a3cd6d03a642a1b36da8340d6114c5086c60 Mon Sep 17 00:00:00 2001 From: emorell96 Date: Mon, 22 Mar 2021 00:21:41 -0700 Subject: [PATCH] Added two Extension methods "AddScopedDiscordRestClient" and "AddTransientDiscordRestClient" to allow for easier addition of scoped, and transient DiscordRestClients, it's left up to the user which one is more convenient to use for his/her application, and at the end of the day it doesn't risk running out of sockets since it uses IHttpClientFactory under the hood. This means that it moves the useProxy boolean parameter here, not sure how much that affects the rest of the library. def an addition to consider. And something that more accustomed users/contributors will be able to answer. The main advantage of having such a method in the future could be adding resiliency. Major resiliency by for example using Polly in a simple way see: https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-http-call-retries-exponential-backoff-polly for example. Of course this means that the user would have to use DI (which is anyways a good practice), but I left the other default ones so that it wouldn't bring any breaking changes. --- .../Extensions/ServiceCollectionExtensions.cs | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs diff --git a/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs b/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..8cf85e118 --- /dev/null +++ b/src/Discord.Net.Rest/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,69 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Http; +using System.Net; +using Discord.Net.Rest; +using Discord.Rest.Net; + +namespace Discord.Rest.Extensions +{ + /// + /// Here you will find all the extensions to be able to add a Discord Client to an IServiceCollection + /// + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddScopedDiscordRestClient(this IServiceCollection services, bool useProxy = false) + { + services.AddHttpClient("HttpFactoryRestClientProvider") + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + UseCookies = false, + UseProxy = useProxy, + }); + + + + services.AddTransient(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService())); + services.AddScoped(provider => + { + var config = new DiscordRestConfig + { + RestClientProvider = provider.GetRequiredService().Instance + }; + return new DiscordRestClient(config); + }); + + return services; + } + + public static IServiceCollection AddTransientDiscordRestClient(this IServiceCollection services, bool useProxy = false) //where should we put this useProxy options, I haven't fully understood where the original code takes this from. + { + services.AddHttpClient("HttpFactoryRestClientProvider") + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + UseCookies = false, + UseProxy = useProxy, + }); + + + + services.AddTransient(provider => new HttpFactoryRestClientProvider(provider.GetRequiredService())); + services.AddTransient(provider => + { + var config = new DiscordRestConfig + { + RestClientProvider = provider.GetRequiredService().Instance + }; + return new DiscordRestClient(config); + }); + + return services; + } + } +}