-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from PostHog/haacked/better-configuration
Implement a more flexible configuration system
- Loading branch information
Showing
18 changed files
with
626 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/PostHog.AspNetCore/Config/IPostHogAspNetCoreConfigurationBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace PostHog.Config; | ||
|
||
public interface IPostHogAspNetCoreConfigurationBuilder : IPostHogConfigurationBuilder | ||
{ | ||
public IConfiguration Configuration { get; } | ||
} |
28 changes: 28 additions & 0 deletions
28
src/PostHog.AspNetCore/Config/PostHogConfigurationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using PostHog.Cache; | ||
using PostHog.Library; | ||
|
||
namespace PostHog.Config; | ||
using static Ensure; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IPostHogConfigurationBuilder"/>. | ||
/// </summary> | ||
public static class PostHogConfigurationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers ASP.NET Core specific implementations of PostHogClient services. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IPostHogConfigurationBuilder"/>.</param> | ||
/// <returns>The passed in <see cref="IPostHogConfigurationBuilder"/>.</returns> | ||
public static IPostHogConfigurationBuilder UseAspNetCore(this IPostHogConfigurationBuilder builder) | ||
{ | ||
NotNull(builder).Use(services => | ||
{ | ||
services.AddSingleton<IFeatureFlagCache, HttpContextFeatureFlagCache>(); | ||
services.AddHttpContextAccessor(); | ||
}); | ||
return builder; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace PostHog.Config; | ||
|
||
/// <summary> | ||
/// Base interface for configuring the PostHog client. | ||
/// </summary> | ||
public interface IPostHogConfigurationBuilder | ||
{ | ||
/// <summary> | ||
/// Calls the specified configuration action on the <see cref="IServiceCollection"/>. | ||
/// </summary> | ||
/// <param name="configurationAction">The configuration action to apply to <see cref="IServiceCollection"/>.</param> | ||
/// <returns>The <see cref="IPostHogConfigurationBuilder"/>.</returns> | ||
IPostHogConfigurationBuilder Use(Action<IServiceCollection> configurationAction); | ||
|
||
/// <summary> | ||
/// Allows the <see cref="HttpClient"/> used by <see cref="PostHogClient"/> to be additionally configured | ||
/// such as adding <see cref="HttpMessageHandler"/>s. | ||
/// </summary> | ||
/// <remarks> | ||
/// I was hoping I wouldn't have to special case this, but the AddHttpClient method creates and returns a new | ||
/// <see cref="DefaultHttpClientBuilder">DefaultHttpClientBuilder</see> which we need to store to be able to access. | ||
/// | ||
/// </remarks> | ||
/// <param name="configureHttpClient">An action used to configure the HttpClient via the <see cref="IHttpClientBuilder"/>.</param> | ||
/// <returns>The <see cref="IPostHogConfigurationBuilder"/>.</returns> | ||
IPostHogConfigurationBuilder ConfigureHttpClient(Action<IHttpClientBuilder> configureHttpClient); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using PostHog.Library; | ||
using static PostHog.Library.Ensure; | ||
|
||
namespace PostHog.Config; | ||
|
||
/// <summary> | ||
/// A builder for configuring the <see cref="PostHogClient"/>. | ||
/// </summary> | ||
public class PostHogConfigurationBuilder : IPostHogConfigurationBuilder | ||
{ | ||
readonly IServiceCollection _services; | ||
readonly IHttpClientBuilder _httpClientBuilder; | ||
|
||
public PostHogConfigurationBuilder(IServiceCollection services) | ||
{ | ||
_services = services; | ||
_services.AddLogging(); | ||
_httpClientBuilder = _services.AddHttpClient(nameof(PostHogClient)); // Registers the IHttpClientFactory. | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IPostHogConfigurationBuilder ConfigureHttpClient(Action<IHttpClientBuilder> configureHttpClient) | ||
{ | ||
NotNull(configureHttpClient)(_httpClientBuilder); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Builds the <see cref="IServiceCollection"/> with the configured services. This enables us to try and add | ||
/// default services after we've configured the <see cref="PostHogClient"/>. | ||
/// </summary> | ||
/// <returns></returns> | ||
public IServiceCollection Build() | ||
{ | ||
// Try to set up defaults if they haven't been set yet. | ||
_services.TryAddSingleton<IFeatureFlagCache>(_ => NullFeatureFlagCache.Instance); | ||
_services.TryAddSingleton<ITaskScheduler, TaskRunTaskScheduler>(); | ||
_services.TryAddSingleton<IPostHogClient, PostHogClient>(); | ||
_services.TryAddSingleton<TimeProvider>(_ => TimeProvider.System); | ||
|
||
return _services; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IPostHogConfigurationBuilder Use(Action<IServiceCollection>? configurationAction) | ||
{ | ||
configurationAction?.Invoke(_services); | ||
return this; | ||
} | ||
} |
Oops, something went wrong.