generated from fossapps/Micro.Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experiments): ability to force experiments
- Loading branch information
Showing
5 changed files
with
146 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Fossapps.FeatureManager | ||
{ | ||
public static class SetupClient | ||
{ | ||
public static void SetupFeatureClients<TUserDataImplementation>(this IServiceCollection collection, string endpoint, TimeSpan syncInterval) where TUserDataImplementation : class, IUserDataRepo | ||
{ | ||
var worker = new FeatureWorker(endpoint, syncInterval); | ||
worker.Init(); | ||
collection.AddScoped<IUserDataRepo, TUserDataImplementation>(); | ||
collection.AddSingleton<IFeatureWorker>(worker); | ||
collection.AddScoped<FeatureClient>(); | ||
} | ||
|
||
public static bool IsFeatureOn(this FeatureClient instance, string featId) | ||
{ | ||
return instance.GetVariant(featId) == 'B'; | ||
} | ||
} | ||
} |
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,51 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Fossapps.FeatureManager | ||
{ | ||
public interface IUserDataRepo | ||
{ | ||
public string GetUserId(); | ||
|
||
public IEnumerable<string> GetExperimentsForcedA() | ||
{ | ||
return new List<string>(); | ||
} | ||
|
||
public IEnumerable<string> GetExperimentsForcedB() | ||
{ | ||
return new List<string>(); | ||
} | ||
} | ||
public abstract class UserDataRepoBase : IUserDataRepo | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
public abstract string GetUserId(); | ||
|
||
protected UserDataRepoBase(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public IEnumerable<string> GetExperimentsForcedA() | ||
{ | ||
if (!_contextAccessor.HttpContext.Request.Headers.TryGetValue("X-Forced-Features-A", out var features)) | ||
{ | ||
return new List<string>(); | ||
} | ||
|
||
return features.ToString().Split(",").ToList(); | ||
} | ||
|
||
public IEnumerable<string> GetExperimentsForcedB() | ||
{ | ||
if (!_contextAccessor.HttpContext.Request.Headers.TryGetValue("X-Forced-Features-B", out var features)) | ||
{ | ||
return new List<string>(); | ||
} | ||
|
||
return features.ToString().Split(",").ToList(); | ||
} | ||
} | ||
} |