From ca9611042da7b85823f565941b9b3319ebc5a3a2 Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 4 Apr 2024 11:23:09 +0200 Subject: [PATCH] refactor: use single instance for HttpMessageHandler --- Vonage/Configuration.cs | 14 +++++-------- Vonage/VonageClient.cs | 44 ++++++++++++++++------------------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/Vonage/Configuration.cs b/Vonage/Configuration.cs index 2594ea8a8..22f2935cc 100644 --- a/Vonage/Configuration.cs +++ b/Vonage/Configuration.cs @@ -70,10 +70,11 @@ public HttpClient Client { get { - var client = RequestsPerSecond + var handler = RequestsPerSecond .Map(BuildSemaphore) .Map(this.GetThrottlingMessageHandler) - .Match(some => new HttpClient(some), this.BuildDefaultClient); + .IfNone(this.ClientHandler); + var client = new HttpClient(handler); this.RequestTimeout.IfSome(value => client.Timeout = value); return client; } @@ -82,7 +83,7 @@ public HttpClient Client /// /// Exposes an HttpMessageHandler. /// - public HttpMessageHandler ClientHandler { get; set; } + public HttpMessageHandler ClientHandler { get; set; } = new HttpClientHandler(); /// /// Retrieves the unique instance (Singleton). @@ -148,11 +149,6 @@ public Credentials BuildCredentials() => /// The Configuration. public static Configuration FromConfiguration(IConfiguration configuration) => new Configuration(configuration); - private HttpClient BuildDefaultClient() => - this.ClientHandler == null - ? new HttpClient() - : new HttpClient(this.ClientHandler); - private static TimeSpanSemaphore BuildSemaphore(double requestsPerSecond) { var delay = 1 / requestsPerSecond; @@ -160,7 +156,7 @@ private static TimeSpanSemaphore BuildSemaphore(double requestsPerSecond) return execTimeSpanSemaphore; } - private ThrottlingMessageHandler GetThrottlingMessageHandler(TimeSpanSemaphore semaphore) => + private HttpMessageHandler GetThrottlingMessageHandler(TimeSpanSemaphore semaphore) => this.ClientHandler != null ? new ThrottlingMessageHandler(semaphore, this.ClientHandler) : new ThrottlingMessageHandler(semaphore); diff --git a/Vonage/VonageClient.cs b/Vonage/VonageClient.cs index 6f06d6d6f..394d9d91b 100644 --- a/Vonage/VonageClient.cs +++ b/Vonage/VonageClient.cs @@ -140,9 +140,9 @@ private VonageHttpClientConfiguration BuildConfiguration(HttpClient client) => private Configuration GetConfiguration() => this.configuration.IfNone(Configuration.Instance); - private HttpClient InitializeHttpClient(Uri baseUri) + private HttpClient InitializeHttpClient(HttpMessageHandler handler, Uri baseUri) { - var client = new HttpClient(new HttpClientHandler()) + var client = new HttpClient(handler) { BaseAddress = baseUri, }; @@ -166,30 +166,20 @@ private void PropagateCredentials() this.SmsClient = new SmsClient(this.Credentials, this.GetConfiguration(), this.timeProvider); this.PricingClient = new PricingClient(this.Credentials, this.GetConfiguration(), this.timeProvider); this.MessagesClient = new MessagesClient(this.Credentials, this.GetConfiguration(), this.timeProvider); - this.VerifyV2Client = - new VerifyV2Client( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Nexmo))); - this.SubAccountsClient = new SubAccountsClient( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Nexmo)), - this.Credentials.ApiKey); - this.NumberInsightV2Client = new NumberInsightV2Client( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Nexmo))); - this.UsersClient = - new UsersClient( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Nexmo))); - this.ConversationsClient = - new ConversationsClient( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Nexmo))); - this.MeetingsClient = new MeetingsClient( - this.BuildConfiguration( - this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Get(VonageUrls.Region.EU))), - new FileSystem()); - this.ProactiveConnectClient = - new ProactiveConnectClient( - this.BuildConfiguration( - this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Get(VonageUrls.Region.EU)))); - this.VideoClient = - new VideoClient( - this.BuildConfiguration(this.InitializeHttpClient(this.GetConfiguration().VonageUrls.Video))); + var nexmoHttpClient = this.InitializeHttpClient( + this.GetConfiguration().ClientHandler ?? new HttpClientHandler(), this.GetConfiguration().VonageUrls.Nexmo); + var videoHttpClient = this.InitializeHttpClient( + this.GetConfiguration().ClientHandler ?? new HttpClientHandler(), this.GetConfiguration().VonageUrls.Video); + var euHttpClient = this.InitializeHttpClient(this.GetConfiguration().ClientHandler ?? new HttpClientHandler(), + this.GetConfiguration().VonageUrls.Get(VonageUrls.Region.EU)); + this.VerifyV2Client = new VerifyV2Client(this.BuildConfiguration(nexmoHttpClient)); + this.SubAccountsClient = + new SubAccountsClient(this.BuildConfiguration(nexmoHttpClient), this.Credentials.ApiKey); + this.NumberInsightV2Client = new NumberInsightV2Client(this.BuildConfiguration(nexmoHttpClient)); + this.UsersClient = new UsersClient(this.BuildConfiguration(nexmoHttpClient)); + this.ConversationsClient = new ConversationsClient(this.BuildConfiguration(nexmoHttpClient)); + this.MeetingsClient = new MeetingsClient(this.BuildConfiguration(euHttpClient), new FileSystem()); + this.ProactiveConnectClient = new ProactiveConnectClient(this.BuildConfiguration(euHttpClient)); + this.VideoClient = new VideoClient(this.BuildConfiguration(videoHttpClient)); } } \ No newline at end of file