Skip to content

Commit

Permalink
Switch DCS to use Get Request (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
sroyal-statsig authored Nov 25, 2024
1 parent 85ff9c2 commit 91cd1df
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 28 deletions.
12 changes: 2 additions & 10 deletions dotnet-statsig-tests/Common/StatsigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public async void TestServerInitialize()
{
_server.ResetLogEntries();
_server.Given(
Request.Create().WithPath("/v1/download_config_specs").UsingPost()
Request.Create().WithPath("/v1/download_config_specs/secret-fake-key.json").UsingGet()
).RespondWith(
Response.Create().WithStatusCode(200).WithBodyAsJson(
new Dictionary<string, object>
Expand Down Expand Up @@ -307,19 +307,11 @@ await StatsigServer.Initialize

var requestBody = _server.LogEntries.ElementAt(0).RequestMessage.Body;
var requestHeaders = _server.LogEntries.ElementAt(0).RequestMessage.Headers;
var requestDict = JObject.Parse(requestBody);

JToken m;
requestDict.TryGetValue("statsigMetadata", out m);
var metadata = m.ToObject<Dictionary<string, string>>();

Assert.True(requestHeaders["STATSIG-API-KEY"].ToString().Equals("secret-fake-key"));
Assert.True(requestHeaders["STATSIG-SDK-VERSION"].ToString().Equals(ExpectedSdkVersion));
Assert.True(requestHeaders["STATSIG-SDK-TYPE"].ToString().Equals("dotnet-server"));

Assert.True(metadata["sdkType"].Equals("dotnet-server"));
Assert.True(metadata["sdkVersion"].Equals(ExpectedSdkVersion));

var gate = await StatsigServer.CheckGate(user, "test_gate");
Assert.True(gate);
var exp = await StatsigServer.GetExperiment(user, "test_config");
Expand All @@ -340,7 +332,7 @@ await StatsigServer.Initialize
// Verify log event requets for exposures and custom logs
requestBody = _server.LogEntries.ElementAt(2).RequestMessage.Body;
requestHeaders = _server.LogEntries.ElementAt(2).RequestMessage.Headers;
requestDict = JObject.Parse(requestBody);
var requestDict = JObject.Parse(requestBody);

Assert.True(requestHeaders["STATSIG-API-KEY"].ToString().Equals("secret-fake-key"));

Expand Down
4 changes: 2 additions & 2 deletions dotnet-statsig-tests/Server/ConcurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Task IAsyncLifetime.InitializeAsync()
_baseURL = _server.Urls[0];
_server.ResetLogEntries();
_server.Given(
Request.Create().WithPath("/v1/download_config_specs").UsingPost()
Request.Create().WithPath("/v1/download_config_specs/secret-server-key.json").UsingGet()
).RespondWith(this);
_server.Given(
Request.Create().WithPath("/v1/log_event").UsingPost()
Expand Down Expand Up @@ -142,7 +142,7 @@ private async Task RunChecks(int taskId, int delay, int times)
Assert.True(await StatsigServer.CheckGate(user, "always_on_gate"));

// check id list gate for a user that should be in the id list
Assert.True(await StatsigServer.CheckGate(new StatsigUser { UserID = "regular_user_id", customIDs = {{"unique_id", $"{i}_{taskId}"}}}, "on_for_id_list"));
Assert.True(await StatsigServer.CheckGate(new StatsigUser { UserID = "regular_user_id", customIDs = { { "unique_id", $"{i}_{taskId}" } } }, "on_for_id_list"));

StatsigServer.LogEvent(user, "test_event_2", 1, new Dictionary<string, string>() { { "Key", "Value" } });

Expand Down
4 changes: 2 additions & 2 deletions dotnet-statsig-tests/Server/DedupeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DedupeTest : IAsyncLifetime, IResponseProvider
private const int NUM_EVENTS = 8;
private const int NUM_LOOOPS = 20;
private const int NUM_THREADS = 10;

private WireMockServer _server;
private string _baseUrl;
private int _flushedEventCount;
Expand All @@ -32,7 +32,7 @@ Task IAsyncLifetime.InitializeAsync()
_baseUrl = _server.Urls[0];
_server.ResetLogEntries();
_server.Given(
Request.Create().WithPath("/v1/download_config_specs").UsingPost()
Request.Create().WithPath("/v1/download_config_specs/secret-server-key.json").UsingGet()
).RespondWith(this);
_server.Given(
Request.Create().WithPath("/v1/log_event").UsingPost()
Expand Down
2 changes: 1 addition & 1 deletion dotnet-statsig-tests/Server/GetFeatureGateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Task IAsyncLifetime.InitializeAsync()
baseURL = _server.Urls[0];
_server.ResetLogEntries();
_server.Given(
Request.Create().WithPath("/v1/download_config_specs").UsingPost()
Request.Create().WithPath("/v1/download_config_specs/secret-server-key.json").UsingGet()
).RespondWith(this);
_server.Given(
Request.Create().WithPath("/v1/log_event").UsingPost()
Expand Down
2 changes: 1 addition & 1 deletion dotnet-statsig-tests/Server/GetListsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task InitializeAsync()
_server = WireMockServer.Start();
_server.ResetLogEntries();
_server.Given(Request.Create()
.WithPath("/v1/download_config_specs").UsingPost()
.WithPath("/v1/download_config_specs/secret-key.json").UsingGet()
).RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(SpecStoreResponseData.downloadConfigSpecResponse)
Expand Down
2 changes: 1 addition & 1 deletion dotnet-statsig-tests/Server/LayerExposureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Task IAsyncLifetime.InitializeAsync()
_server = WireMockServer.Start();
_server.ResetLogEntries();
_server.Given(
Request.Create().WithPath("/v1/download_config_specs").UsingPost()
Request.Create().WithPath("/v1/download_config_specs/secret-server-key.json").UsingGet()
).RespondWith(this);
_server.Given(
Request.Create().WithPath("/v1/log_event").UsingPost()
Expand Down
13 changes: 7 additions & 6 deletions dotnet-statsig/src/Statsig/Network/RequestDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ string sessionID
int timeoutInMs = 0,
IReadOnlyDictionary<string, string>? additionalHeaders = null)
{
var (result, status) = await FetchAsString(endpoint, body, retries, backoff, timeoutInMs, additionalHeaders).ConfigureAwait(false);
var (result, status) = await FetchAsString(endpoint, body, 0, retries, backoff, timeoutInMs, additionalHeaders).ConfigureAwait(false);
return JsonConvert.DeserializeObject<IReadOnlyDictionary<string, JToken>>(result ?? "");
}

Expand All @@ -74,7 +74,7 @@ public async Task<InitializeResult> FetchStatus(
IReadOnlyDictionary<string, string>? additionalHeaders = null,
bool zipped = false)
{
var (result, status) = await FetchAsString(endpoint, body, retries, backoff, timeoutInMs, additionalHeaders, zipped).ConfigureAwait(false);
var (result, status) = await FetchAsString(endpoint, body, 0, retries, backoff, timeoutInMs, additionalHeaders, zipped).ConfigureAwait(false);
return status;
}

Expand Down Expand Up @@ -109,6 +109,7 @@ public static void CopyTo(Stream src, Stream dest)
public async Task<(string?, InitializeResult)> FetchAsString(
string endpoint,
string body,
long sinceTime = 0,
int retries = 0,
int backoff = 1,
int timeoutInMs = 0,
Expand All @@ -125,21 +126,21 @@ public static void CopyTo(Stream src, Stream dest)
var url = ApiBaseUrl.EndsWith("/") ? ApiBaseUrl + endpoint : ApiBaseUrl + "/" + endpoint;
if (endpoint.Equals("download_config_specs"))
{
url = CDNBaseUrl.EndsWith("/") ? CDNBaseUrl + endpoint : CDNBaseUrl + "/" + endpoint;
url = (CDNBaseUrl.EndsWith("/") ? CDNBaseUrl + endpoint : CDNBaseUrl + "/" + endpoint) + "/" + Key + ".json?sinceTime=" + sinceTime;
}
var client = new HttpClient(new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip
});
using var request = new HttpRequestMessage(HttpMethod.Post, url);
using var request = new HttpRequestMessage(endpoint.Equals("download_config_specs") ? HttpMethod.Get : HttpMethod.Post, url);
if (zipped)
{
var zippedBody = Zip(body);
request.Content = new ByteArrayContent(zippedBody);
request.Content.Headers.Add("Content-Encoding", "gzip");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
else
else if (!string.IsNullOrWhiteSpace(body))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
Expand Down Expand Up @@ -234,7 +235,7 @@ public static void CopyTo(Stream src, Stream dest)
bool zipped = false)
{
await Task.Delay(backoff * 1000).ConfigureAwait(false);
return await FetchAsString(endpoint, body, retries - 1, backoff * BackoffMultiplier, timeoutInMs, additionalHeaders, zipped).ConfigureAwait(false);
return await FetchAsString(endpoint, body, 0, retries - 1, backoff * BackoffMultiplier, timeoutInMs, additionalHeaders, zipped).ConfigureAwait(false);
}
}
}
7 changes: 2 additions & 5 deletions dotnet-statsig/src/Statsig/Server/Evaluation/SpecStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,8 @@ private async Task<InitializeResult> SyncValuesFromNetwork()
{
var (response, status) = await _requestDispatcher.FetchAsString(
"download_config_specs",
JsonConvert.SerializeObject(new Dictionary<string, object>
{
["sinceTime"] = LastSyncTime,
["statsigMetadata"] = SDKDetails.GetServerSDKDetails().StatsigMetadata
})
"",
LastSyncTime
).ConfigureAwait(false);

var hasUpdates = ParseResponse(response);
Expand Down

0 comments on commit 91cd1df

Please sign in to comment.