From 4cc1732aee0717ca139507a39f722d4377f4d37d Mon Sep 17 00:00:00 2001 From: Chris Hannon Date: Wed, 24 Jan 2024 10:51:40 -0700 Subject: [PATCH] Made changes from PR feedback --- GrowthBook.Tests/ApiTests/ApiUnitTest.cs | 1 - .../ApiTests/LoadFeaturesTests.cs | 32 ------------------- GrowthBook/Api/ConfiguredClients.cs | 12 +++++++ GrowthBook/Api/FeatureRefreshWorker.cs | 17 +++++++--- GrowthBook/Api/HttpClientFactory.cs | 6 ---- GrowthBook/AssemblyInfo.cs | 6 ---- GrowthBook/GrowthBook.csproj | 7 ++-- 7 files changed, 29 insertions(+), 52 deletions(-) delete mode 100644 GrowthBook.Tests/ApiTests/LoadFeaturesTests.cs create mode 100644 GrowthBook/Api/ConfiguredClients.cs delete mode 100644 GrowthBook/AssemblyInfo.cs diff --git a/GrowthBook.Tests/ApiTests/ApiUnitTest.cs b/GrowthBook.Tests/ApiTests/ApiUnitTest.cs index e0df947..fb20eca 100644 --- a/GrowthBook.Tests/ApiTests/ApiUnitTest.cs +++ b/GrowthBook.Tests/ApiTests/ApiUnitTest.cs @@ -17,7 +17,6 @@ public abstract class ApiUnitTest : UnitTest protected readonly ILogger _logger; protected readonly Mock _cache; - protected readonly FeatureRefreshWorker _worker; protected readonly Feature _firstFeature; protected readonly Feature _secondFeature; protected readonly Dictionary _availableFeatures; diff --git a/GrowthBook.Tests/ApiTests/LoadFeaturesTests.cs b/GrowthBook.Tests/ApiTests/LoadFeaturesTests.cs deleted file mode 100644 index d4b6f28..0000000 --- a/GrowthBook.Tests/ApiTests/LoadFeaturesTests.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Xunit; - -namespace GrowthBook.Tests.ApiTests; - -public class LoadFeaturesTests -{ - private const string ApiKey = ""; - - [Fact(Skip = "This is here as a shortcut for the purpose of debugging this SDK against the actual API and should not be used otherwise")] - public async Task LoadFromApi() - { - var context = new Context - { - ClientKey = ApiKey, - DefaultLogLevel = LogLevel.Debug - }; - - var gb = new GrowthBook(context); - await gb.LoadFeatures(); - - // This is effectively an API test harness and so it should wait indefinitely - // so the test doesn't exit mid-debug session. - - await Task.Delay(-1); - } -} diff --git a/GrowthBook/Api/ConfiguredClients.cs b/GrowthBook/Api/ConfiguredClients.cs new file mode 100644 index 0000000..975a564 --- /dev/null +++ b/GrowthBook/Api/ConfiguredClients.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GrowthBook.Api +{ + public static class ConfiguredClients + { + public const string DefaultApiClient = "growthbook-default-api-client"; + public const string ServerSentEventsApiClient = "growthbook-sse-api-client"; + } +} diff --git a/GrowthBook/Api/FeatureRefreshWorker.cs b/GrowthBook/Api/FeatureRefreshWorker.cs index bd3e110..56f2cbd 100644 --- a/GrowthBook/Api/FeatureRefreshWorker.cs +++ b/GrowthBook/Api/FeatureRefreshWorker.cs @@ -62,7 +62,7 @@ public async Task> RefreshCacheFromApi(Cancellation { _logger.LogInformation($"Making an HTTP request to the default Features API endpoint '{_featuresApiEndpoint}'"); - var httpClient = _httpClientFactory.CreateClient(HttpClientFactory.ConfiguredClients.DefaultApiClient); + var httpClient = _httpClientFactory.CreateClient(ConfiguredClients.DefaultApiClient); var response = await httpClient.GetAsync(_featuresApiEndpoint, cancellationToken ?? _refreshWorkerCancellation.Token); if (!response.IsSuccessStatusCode) @@ -128,7 +128,7 @@ private Task ListenForServerSentEvents() { _logger.LogInformation($"Making an HTTP request to server sent events endpoint '{_serverSentEventsApiEndpoint}'"); - var httpClient = _httpClientFactory.CreateClient(HttpClientFactory.ConfiguredClients.ServerSentEventsApiClient); + var httpClient = _httpClientFactory.CreateClient(ConfiguredClients.ServerSentEventsApiClient); var stream = await httpClient.GetStreamAsync(_serverSentEventsApiEndpoint); using (var reader = new StreamReader(stream)) @@ -137,15 +137,22 @@ private Task ListenForServerSentEvents() { var json = reader.ReadLine(); - // Server sent events have a few potential different bits of information - // that may be sent along with the actual JSON data we care about. For now, - // we're just dropping those extra pieces and solely focusing on grabbing the JSON. + // All server sent events will have the format ":" and each message + // is a single line in the stream. Right now, the only message that we care about + // has a key of "data" and value of the JSON data sent from the server, so we're going + // to ignore everything that's doesn't contain a "data" key. if (json?.StartsWith("data:") != true) { + // No actual JSON data is present, ignore this message. + continue; } + // Strip off the key and the colon so we can try to deserialize the JSON data. Keep in mind + // that the data key might be sent with no actual data present, so we're also checking up front + // to see whether we can just drop this as well or if it actually needs processing. + json = json.Substring(5).Trim(); if (string.IsNullOrWhiteSpace(json)) diff --git a/GrowthBook/Api/HttpClientFactory.cs b/GrowthBook/Api/HttpClientFactory.cs index 9168688..4964d36 100644 --- a/GrowthBook/Api/HttpClientFactory.cs +++ b/GrowthBook/Api/HttpClientFactory.cs @@ -7,12 +7,6 @@ namespace GrowthBook.Api { public class HttpClientFactory : IHttpClientFactory { - public static class ConfiguredClients - { - public const string DefaultApiClient = "growthbook-default-api-client"; - public const string ServerSentEventsApiClient = "growthbook-sse-api-client"; - } - private readonly int _requestTimeoutInSeconds; public HttpClientFactory(int requestTimeoutInSeconds = 60) => _requestTimeoutInSeconds = requestTimeoutInSeconds; diff --git a/GrowthBook/AssemblyInfo.cs b/GrowthBook/AssemblyInfo.cs deleted file mode 100644 index 43411de..0000000 --- a/GrowthBook/AssemblyInfo.cs +++ /dev/null @@ -1,6 +0,0 @@ -using System.Runtime.CompilerServices; - -// We're including this here so we can make things more testable without -// exposing unnecessary implementation details to SDK consumers. - -[assembly: InternalsVisibleTo("GrowthBook.Tests")] diff --git a/GrowthBook/GrowthBook.csproj b/GrowthBook/GrowthBook.csproj index 6cb7b95..4e18218 100644 --- a/GrowthBook/GrowthBook.csproj +++ b/GrowthBook/GrowthBook.csproj @@ -2,7 +2,7 @@ netstandard2.0 - Ben Whatley,Norhaven + Ben Whatley Powerful feature flagging and A/B testing for C# apps using GrowthBook. GrowthBook LICENSE @@ -10,7 +10,7 @@ growthbook-c-sharp https://github.com/growthbook/growthbook-csharp.git git - GrowthBook,A/B test,feature toggle + GrowthBook,A/B test,feature toggle,flag 1.0.0 GrowthBook C# SDK https://www.growthbook.io/ @@ -31,4 +31,7 @@ + + +