Skip to content

Commit

Permalink
refactor: simplify client initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Apr 4, 2024
1 parent ca96110 commit 5779958
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
31 changes: 31 additions & 0 deletions Vonage/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,37 @@ public HttpClient Client
: default,
};

/// <summary>
/// Build an HttpClient for the Nexmo API.
/// </summary>
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForNexmo() => this.BuildHttpClient(this.VonageUrls.Nexmo);

private HttpClient BuildHttpClient(Uri baseUri)
{
var client = new HttpClient(this.ClientHandler)
{
BaseAddress = baseUri,
};
this.RequestTimeout.IfSome(value => client.Timeout = value);
client.DefaultRequestHeaders.Add("Accept", "application/json");
return client;
}

/// <summary>
/// Build an HttpClient for the Video API.
/// </summary>
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForVideo() => this.BuildHttpClient(this.VonageUrls.Video);

/// <summary>
/// Build an HttpClient for a specific region.
/// </summary>
/// <param name="region">The selected region.</param>
/// <returns>The HttpClient.</returns>
public HttpClient BuildHttpClientForRegion(VonageUrls.Region region) =>
this.BuildHttpClient(this.VonageUrls.Get(region));

/// <summary>
/// Builds a Configuration from an IConfiguration.
/// </summary>
Expand Down
63 changes: 25 additions & 38 deletions Vonage/VonageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,46 +140,33 @@ public Credentials Credentials

private Configuration GetConfiguration() => this.configuration.IfNone(Configuration.Instance);

private HttpClient InitializeHttpClient(HttpMessageHandler handler, Uri baseUri)
{
var client = new HttpClient(handler)
{
BaseAddress = baseUri,
};
this.GetConfiguration().RequestTimeout.IfSome(value => client.Timeout = value);
client.DefaultRequestHeaders.Add("Accept", "application/json");
return client;
}

private void PropagateCredentials()
{
this.AccountClient = new AccountClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.ApplicationClient = new ApplicationClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.VoiceClient = new VoiceClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.ConversionClient = new ConversionClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.NumbersClient = new NumbersClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
var currentConfiguration = this.GetConfiguration();
this.AccountClient = new AccountClient(this.Credentials, currentConfiguration, this.timeProvider);
this.ApplicationClient = new ApplicationClient(this.Credentials, currentConfiguration, this.timeProvider);
this.VoiceClient = new VoiceClient(this.Credentials, currentConfiguration, this.timeProvider);
this.ConversionClient = new ConversionClient(this.Credentials, currentConfiguration, this.timeProvider);
this.NumbersClient = new NumbersClient(this.Credentials, currentConfiguration, this.timeProvider);
this.NumberInsightClient =
new NumberInsightClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.VerifyClient = new VerifyClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.ShortCodesClient = new ShortCodesClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
this.RedactClient = new RedactClient(this.Credentials, this.GetConfiguration(), this.timeProvider);
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);
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));
new NumberInsightClient(this.Credentials, currentConfiguration, this.timeProvider);
this.VerifyClient = new VerifyClient(this.Credentials, currentConfiguration, this.timeProvider);
this.ShortCodesClient = new ShortCodesClient(this.Credentials, currentConfiguration, this.timeProvider);
this.RedactClient = new RedactClient(this.Credentials, currentConfiguration, this.timeProvider);
this.SmsClient = new SmsClient(this.Credentials, currentConfiguration, this.timeProvider);
this.PricingClient = new PricingClient(this.Credentials, currentConfiguration, this.timeProvider);
this.MessagesClient = new MessagesClient(this.Credentials, currentConfiguration, this.timeProvider);
var nexmoConfiguration = this.BuildConfiguration(currentConfiguration.BuildHttpClientForNexmo());
var videoConfiguration = this.BuildConfiguration(currentConfiguration.BuildHttpClientForVideo());
var euConfiguration =
this.BuildConfiguration(currentConfiguration.BuildHttpClientForRegion(VonageUrls.Region.EU));
this.VerifyV2Client = new VerifyV2Client(nexmoConfiguration);
this.SubAccountsClient = new SubAccountsClient(nexmoConfiguration, this.Credentials.ApiKey);
this.NumberInsightV2Client = new NumberInsightV2Client(nexmoConfiguration);
this.UsersClient = new UsersClient(nexmoConfiguration);
this.ConversationsClient = new ConversationsClient(nexmoConfiguration);
this.MeetingsClient = new MeetingsClient(euConfiguration, new FileSystem());
this.ProactiveConnectClient = new ProactiveConnectClient(euConfiguration);
this.VideoClient = new VideoClient(videoConfiguration);
}
}

0 comments on commit 5779958

Please sign in to comment.