Skip to content

Commit

Permalink
refactor: use single instance for HttpMessageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Apr 4, 2024
1 parent d4ee5e4 commit ca96110
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
14 changes: 5 additions & 9 deletions Vonage/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -82,7 +83,7 @@ public HttpClient Client
/// <summary>
/// Exposes an HttpMessageHandler.
/// </summary>
public HttpMessageHandler ClientHandler { get; set; }
public HttpMessageHandler ClientHandler { get; set; } = new HttpClientHandler();

/// <summary>
/// Retrieves the unique instance (Singleton).
Expand Down Expand Up @@ -148,19 +149,14 @@ public HttpClient Client
/// <returns>The Configuration.</returns>
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;
var execTimeSpanSemaphore = new TimeSpanSemaphore(1, TimeSpan.FromSeconds(delay));
return execTimeSpanSemaphore;
}

private ThrottlingMessageHandler GetThrottlingMessageHandler(TimeSpanSemaphore semaphore) =>
private HttpMessageHandler GetThrottlingMessageHandler(TimeSpanSemaphore semaphore) =>
this.ClientHandler != null
? new ThrottlingMessageHandler(semaphore, this.ClientHandler)
: new ThrottlingMessageHandler(semaphore);
Expand Down
44 changes: 17 additions & 27 deletions Vonage/VonageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public Credentials Credentials

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,
};
Expand All @@ -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));
}
}

0 comments on commit ca96110

Please sign in to comment.