From ff27783d3ef63bd680c27bcc74982ee1ccc3a0ca Mon Sep 17 00:00:00 2001 From: Jamie Sinn Date: Thu, 1 Aug 2024 14:26:59 -0400 Subject: [PATCH] fix: Use contentheaders for new versions of restsharp (#159) * Use contentheaders for new versions of restsharp * New Guid fix * Set event target to full url * set target to use resource path * use response uri * Set hostname in platformdata * Version bump * install json dep * Disable sdkconfig event for now * update openfeature --- .../DevCycle.SDK.Server.Cloud.Example.csproj | 3 ++- DevCycle.SDK.Server.Cloud.Example/Program.cs | 2 +- .../DevCycle.SDK.Server.Cloud.MSTests.csproj | 3 ++- .../DevCycle.SDK.Server.Cloud.csproj | 3 ++- .../DevCycle.SDK.Server.Common.csproj | 5 +++-- .../Model/Local/PlatformData.cs | 4 ++-- .../DevCycle.SDK.Server.Local.Benchmark.csproj | 1 + .../DevCycle.SDK.Server.Local.Example.csproj | 3 ++- DevCycle.SDK.Server.Local.Example/Program.cs | 2 +- .../DevCycle.SDK.Server.Local.MSTests.csproj | 3 ++- .../DevCycleTest.cs | 4 ++-- .../EnvironmentConfigManagerTest.cs | 5 +++-- DevCycle.SDK.Server.Local/Api/EventQueue.cs | 12 ++++++------ .../Api/LocalBucketing.cs | 2 +- .../ConfigManager/EnvironmentConfigManager.cs | 18 +++++++++--------- .../DevCycle.SDK.Server.Local.csproj | 5 +++-- 16 files changed, 42 insertions(+), 33 deletions(-) diff --git a/DevCycle.SDK.Server.Cloud.Example/DevCycle.SDK.Server.Cloud.Example.csproj b/DevCycle.SDK.Server.Cloud.Example/DevCycle.SDK.Server.Cloud.Example.csproj index 1eb986b9..abfc60b0 100644 --- a/DevCycle.SDK.Server.Cloud.Example/DevCycle.SDK.Server.Cloud.Example.csproj +++ b/DevCycle.SDK.Server.Cloud.Example/DevCycle.SDK.Server.Cloud.Example.csproj @@ -17,7 +17,8 @@ - + + diff --git a/DevCycle.SDK.Server.Cloud.Example/Program.cs b/DevCycle.SDK.Server.Cloud.Example/Program.cs index fba0beb1..fc0443e2 100644 --- a/DevCycle.SDK.Server.Cloud.Example/Program.cs +++ b/DevCycle.SDK.Server.Cloud.Example/Program.cs @@ -37,7 +37,7 @@ private static async Task Main(string[] args) .Set("nonSetValueBubbledCustomData4", new Value((object)null)) .Build(); - Api.Instance.SetProvider(api.GetOpenFeatureProvider()); + await Api.Instance.SetProviderAsync(api.GetOpenFeatureProvider()); FeatureClient oFeatureClient = Api.Instance.GetClient(); var allVariables = await api.AllVariables(DevCycleUser.FromEvaluationContext(ctx)); foreach (var readOnlyVariable in allVariables) diff --git a/DevCycle.SDK.Server.Cloud.MSTests/DevCycle.SDK.Server.Cloud.MSTests.csproj b/DevCycle.SDK.Server.Cloud.MSTests/DevCycle.SDK.Server.Cloud.MSTests.csproj index e2cca0be..0abf403b 100644 --- a/DevCycle.SDK.Server.Cloud.MSTests/DevCycle.SDK.Server.Cloud.MSTests.csproj +++ b/DevCycle.SDK.Server.Cloud.MSTests/DevCycle.SDK.Server.Cloud.MSTests.csproj @@ -16,8 +16,9 @@ - + + diff --git a/DevCycle.SDK.Server.Cloud/DevCycle.SDK.Server.Cloud.csproj b/DevCycle.SDK.Server.Cloud/DevCycle.SDK.Server.Cloud.csproj index a0f52280..1c73baa7 100644 --- a/DevCycle.SDK.Server.Cloud/DevCycle.SDK.Server.Cloud.csproj +++ b/DevCycle.SDK.Server.Cloud/DevCycle.SDK.Server.Cloud.csproj @@ -41,7 +41,8 @@ - + + diff --git a/DevCycle.SDK.Server.Common/DevCycle.SDK.Server.Common.csproj b/DevCycle.SDK.Server.Common/DevCycle.SDK.Server.Common.csproj index 95c7fe7b..9d83bad6 100644 --- a/DevCycle.SDK.Server.Common/DevCycle.SDK.Server.Common.csproj +++ b/DevCycle.SDK.Server.Common/DevCycle.SDK.Server.Common.csproj @@ -16,9 +16,10 @@ - + - + + diff --git a/DevCycle.SDK.Server.Common/Model/Local/PlatformData.cs b/DevCycle.SDK.Server.Common/Model/Local/PlatformData.cs index 040cea02..1df4ab51 100644 --- a/DevCycle.SDK.Server.Common/Model/Local/PlatformData.cs +++ b/DevCycle.SDK.Server.Common/Model/Local/PlatformData.cs @@ -1,3 +1,4 @@ +using System.Net; using Newtonsoft.Json; namespace DevCycle.SDK.Server.Common.Model.Local @@ -53,7 +54,6 @@ public virtual string ToJson() { return JsonConvert.SerializeObject(this, Formatting.Indented); } - public PlatformData() { @@ -61,7 +61,7 @@ public PlatformData() PlatformVersion = DefaultPlatformVersion; SdkType = DefaultSdkType.ToString().ToLower(); SdkVersion = DefaultSdkVersion; - HostName = null; + HostName = Dns.GetHostName(); } } } \ No newline at end of file diff --git a/DevCycle.SDK.Server.Local.Benchmark/DevCycle.SDK.Server.Local.Benchmark.csproj b/DevCycle.SDK.Server.Local.Benchmark/DevCycle.SDK.Server.Local.Benchmark.csproj index 01e2b5fc..c4b8d141 100644 --- a/DevCycle.SDK.Server.Local.Benchmark/DevCycle.SDK.Server.Local.Benchmark.csproj +++ b/DevCycle.SDK.Server.Local.Benchmark/DevCycle.SDK.Server.Local.Benchmark.csproj @@ -14,6 +14,7 @@ + diff --git a/DevCycle.SDK.Server.Local.Example/DevCycle.SDK.Server.Local.Example.csproj b/DevCycle.SDK.Server.Local.Example/DevCycle.SDK.Server.Local.Example.csproj index b6096c50..ec248ccb 100644 --- a/DevCycle.SDK.Server.Local.Example/DevCycle.SDK.Server.Local.Example.csproj +++ b/DevCycle.SDK.Server.Local.Example/DevCycle.SDK.Server.Local.Example.csproj @@ -17,7 +17,8 @@ - + + diff --git a/DevCycle.SDK.Server.Local.Example/Program.cs b/DevCycle.SDK.Server.Local.Example/Program.cs index c0608a53..66d8c512 100644 --- a/DevCycle.SDK.Server.Local.Example/Program.cs +++ b/DevCycle.SDK.Server.Local.Example/Program.cs @@ -92,7 +92,7 @@ private static async Task ClientInitialized(DevCycleUser user) .Set("nonSetValueBubbledCustomData4", new Value((object)null)) .Build(); - Api.Instance.SetProvider(api.GetOpenFeatureProvider()); + await Api.Instance.SetProviderAsync(api.GetOpenFeatureProvider()); FeatureClient oFeatureClient = Api.Instance.GetClient(); var allVariables = await api.AllVariables(DevCycleUser.FromEvaluationContext(ctx)); foreach (var readOnlyVariable in allVariables) diff --git a/DevCycle.SDK.Server.Local.MSTests/DevCycle.SDK.Server.Local.MSTests.csproj b/DevCycle.SDK.Server.Local.MSTests/DevCycle.SDK.Server.Local.MSTests.csproj index 2875e788..a6d5e8b8 100644 --- a/DevCycle.SDK.Server.Local.MSTests/DevCycle.SDK.Server.Local.MSTests.csproj +++ b/DevCycle.SDK.Server.Local.MSTests/DevCycle.SDK.Server.Local.MSTests.csproj @@ -17,8 +17,9 @@ all - + + diff --git a/DevCycle.SDK.Server.Local.MSTests/DevCycleTest.cs b/DevCycle.SDK.Server.Local.MSTests/DevCycleTest.cs index e21d09fa..c0551b40 100644 --- a/DevCycle.SDK.Server.Local.MSTests/DevCycleTest.cs +++ b/DevCycle.SDK.Server.Local.MSTests/DevCycleTest.cs @@ -338,7 +338,7 @@ public void OpenFeatureUserParsing() public async Task TestOpenFeatureInitialization() { var dvcClient = getTestClient(); - OpenFeature.Api.Instance.SetProvider(dvcClient.GetOpenFeatureProvider()); + await OpenFeature.Api.Instance.SetProviderAsync(dvcClient.GetOpenFeatureProvider()); FeatureClient client = OpenFeature.Api.Instance.GetClient(); var ctx = EvaluationContext.Builder().Set("user_id", "j_test").Build(); @@ -350,7 +350,7 @@ public async Task TestOpenFeatureInitialization() public async Task TestOpenFeatureJSON() { using DevCycleLocalClient api = getTestClient(); - OpenFeature.Api.Instance.SetProvider(api.GetOpenFeatureProvider()); + await OpenFeature.Api.Instance.SetProviderAsync(api.GetOpenFeatureProvider()); FeatureClient client = OpenFeature.Api.Instance.GetClient(); string key = "json"; diff --git a/DevCycle.SDK.Server.Local.MSTests/EnvironmentConfigManagerTest.cs b/DevCycle.SDK.Server.Local.MSTests/EnvironmentConfigManagerTest.cs index 7b919e47..e147177d 100644 --- a/DevCycle.SDK.Server.Local.MSTests/EnvironmentConfigManagerTest.cs +++ b/DevCycle.SDK.Server.Local.MSTests/EnvironmentConfigManagerTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Net; using System.Threading.Tasks; using DevCycle.SDK.Server.Local.Api; @@ -34,8 +35,8 @@ private Tuple g .Respond(statusCode, new List>() { - new("test etag", "test etag value"), - new("Last-Modified", "timestamp") + new("Etag", "test etag value"), + new("Last-Modified", DateTime.Now.AddHours(-1).ToString(CultureInfo.InvariantCulture)) }, "application/json", isError ? "" : config); diff --git a/DevCycle.SDK.Server.Local/Api/EventQueue.cs b/DevCycle.SDK.Server.Local/Api/EventQueue.cs index 0b2cf4be..6365f77e 100644 --- a/DevCycle.SDK.Server.Local/Api/EventQueue.cs +++ b/DevCycle.SDK.Server.Local/Api/EventQueue.cs @@ -172,27 +172,27 @@ public virtual void QueueSDKConfigEvent(RestRequest request, RestResponse respon new DevCycleUser(userId: $"{localBucketing.ClientUUID}@{Dns.GetHostName()}")); QueueEvent(popU, new DevCycleEvent( type: EventTypes.sdkConfig, - target: request.Resource, + target: response.ResponseUri?.ToString(), value: -1, metaData: new Dictionary { { "clientUUID", localBucketing.ClientUUID }, { "reqEtag", - request.Parameters.GetParameters().First(p => p.Name == "If-None-Match") + request.Parameters.GetParameters().FirstOrDefault(p => p.Name == "If-None-Match")?.Value as string }, { "reqLastModified", - request.Parameters.GetParameters().First(p => p.Name == "If-Modified-Since") + request.Parameters.GetParameters().FirstOrDefault(p => p.Name == "If-Modified-Since")?.Value as string }, { - "resEtag", response.Headers?.First(p => p.Name?.ToLower() == "etag") + "resEtag", response.Headers?.FirstOrDefault(p => p.Name?.ToLower() == "etag")?.Value as string }, { - "resLastModified", response.Headers?.First(p => p.Name?.ToLower() == "last-modified") + "resLastModified", response.ContentHeaders?.FirstOrDefault(p => p.Name?.ToLower() == "last-modified")?.Value as string }, { - "resRayId", response.Headers?.First(p => p.Name?.ToLower() == "cf-ray") + "resRayId", response.Headers?.FirstOrDefault(p => p.Name?.ToLower() == "cf-ray")?.Value as string }, { "resStatus", response.StatusCode diff --git a/DevCycle.SDK.Server.Local/Api/LocalBucketing.cs b/DevCycle.SDK.Server.Local/Api/LocalBucketing.cs index a3c4cf53..69f39728 100644 --- a/DevCycle.SDK.Server.Local/Api/LocalBucketing.cs +++ b/DevCycle.SDK.Server.Local/Api/LocalBucketing.cs @@ -115,7 +115,7 @@ public class LocalBucketing public LocalBucketing() { - ClientUUID = new Guid().ToString(); + ClientUUID = Guid.NewGuid().ToString(); WasmMutex.Wait(); random = new Random(); pinnedAddresses = new HashSet(); diff --git a/DevCycle.SDK.Server.Local/ConfigManager/EnvironmentConfigManager.cs b/DevCycle.SDK.Server.Local/ConfigManager/EnvironmentConfigManager.cs index 56b631ae..2c470322 100644 --- a/DevCycle.SDK.Server.Local/ConfigManager/EnvironmentConfigManager.cs +++ b/DevCycle.SDK.Server.Local/ConfigManager/EnvironmentConfigManager.cs @@ -38,8 +38,8 @@ public class EnvironmentConfigManager : IDisposable private bool PollingEnabled = true; - private string configEtag; - private string configLastModified; + private string configEtag = ""; + private string configLastModified = ""; public EnvironmentConfigManager( string sdkKey, @@ -186,11 +186,11 @@ private async Task FetchConfigAsyncWithTask() { try { - var lastModified = res.Headers?.FirstOrDefault(e => e.Name?.ToLower() == "last-modified"); - var etag = res.Headers?.FirstOrDefault(e => e.Name?.ToLower() == "etag"); - if (!string.IsNullOrEmpty(configLastModified) && lastModified != null && !string.IsNullOrEmpty((string)lastModified.Value) ) + var lastModified = res.ContentHeaders?.FirstOrDefault(e => e.Name?.ToLower() == "last-modified")?.Value as string; + var etag = res.Headers?.FirstOrDefault(e => e.Name?.ToLower() == "etag")?.Value as string; + if (!string.IsNullOrEmpty(configLastModified) && lastModified != null && !string.IsNullOrEmpty(lastModified) ) { - var parsedHeader = Convert.ToDateTime((string)lastModified.Value); + var parsedHeader = Convert.ToDateTime(lastModified); var storedHeader = Convert.ToDateTime(configLastModified); // negative means that the stored header is before the returned parsed header if (DateTime.Compare(storedHeader, parsedHeader) >= 0) @@ -201,10 +201,10 @@ private async Task FetchConfigAsyncWithTask() } localBucketing.StoreConfig(sdkKey, res.Content); - configEtag = (string)etag?.Value; - configLastModified = (string)lastModified?.Value; + configEtag = etag; + configLastModified = lastModified; logger.LogDebug("Config successfully initialized with etag: {ConfigEtag}, {lastmodified}", configEtag, configLastModified); - eventQueue?.QueueSDKConfigEvent(request, res); + //eventQueue?.QueueSDKConfigEvent(request, res); } catch (Exception e) { diff --git a/DevCycle.SDK.Server.Local/DevCycle.SDK.Server.Local.csproj b/DevCycle.SDK.Server.Local/DevCycle.SDK.Server.Local.csproj index 255001b8..4c8b7988 100644 --- a/DevCycle.SDK.Server.Local/DevCycle.SDK.Server.Local.csproj +++ b/DevCycle.SDK.Server.Local/DevCycle.SDK.Server.Local.csproj @@ -48,12 +48,13 @@ all - + + - +