Skip to content

Commit

Permalink
SendGrid ServiceProvider Overload
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyertons committed Sep 23, 2024
1 parent 116a4ea commit 668d86b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,41 @@ public static IHealthChecksBuilder AddSendGrid(
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
var registrationName = name ?? NAME;
return builder.AddSendGrid(_ => apiKey, name, failureStatus, tags, timeout);
}

/// <summary>
/// Add a health check for SendGrid.
/// </summary>
/// <param name="builder">The <see cref="IHealthChecksBuilder"/>.</param>
/// <param name="apiKeyFactory">Factory to resolve the API key.</param>
/// <param name="name">The health check name. Optional. If <c>null</c> the type name 'sendgrid' will be used for the name.</param>
/// <param name="failureStatus">
/// The <see cref="HealthStatus"/> that should be reported when the health check fails. Optional. If <c>null</c> then
/// the default status of <see cref="HealthStatus.Unhealthy"/> will be reported.
/// </param>
/// <param name="tags">A list of tags that can be used to filter sets of health checks. Optional.</param>
/// <param name="timeout">An optional <see cref="TimeSpan"/> representing the timeout of the check.</param>
/// <returns>The specified <paramref name="builder"/>.</returns>
public static IHealthChecksBuilder AddSendGrid(
this IHealthChecksBuilder builder,
Func<IServiceProvider, string> apiKeyFactory,
string? name = default,
HealthStatus? failureStatus = default,
IEnumerable<string>? tags = default,
TimeSpan? timeout = default)
{
string registrationName = name ?? NAME;

builder.Services.AddHttpClient(registrationName);

return builder.Add(new HealthCheckRegistration(
registrationName,
sp => new SendGridHealthCheck(apiKey, sp.GetRequiredService<IHttpClientFactory>()),
sp =>
{
string apiKey = apiKeyFactory(sp);
return new SendGridHealthCheck(apiKey, sp.GetRequiredService<IHttpClientFactory>());
},
failureStatus,
tags,
timeout));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ public void add_health_check_when_properly_configured()
check.ShouldBeOfType<SendGridHealthCheck>();
}

private class SendGridOptions
{
public string ApiKey { get; set; } = default!;
}

[Fact]
public void add_health_check_from_DI_when_properly_configured()
{
var services = new ServiceCollection();

services.AddOptions<SendGridOptions>().Configure(options => options.ApiKey = "my_api_key");
services.AddHealthChecks()
.AddSendGrid(sp => sp.GetRequiredService<IOptions<SendGridOptions>>().Value.ApiKey);

using var serviceProvider = services.BuildServiceProvider();
var options = serviceProvider.GetRequiredService<IOptions<HealthCheckServiceOptions>>();

var registration = options.Value.Registrations.First();
var check = registration.Factory(serviceProvider);

registration.Name.ShouldBe("sendgrid");
check.ShouldBeOfType<SendGridHealthCheck>();
}

[Fact]
public void add_named_health_check_when_properly_configured()
{
Expand Down

0 comments on commit 668d86b

Please sign in to comment.