Skip to content

Commit 13bbad7

Browse files
refactor(audience): extract named constants for HTTP header, URL helper, and field length
Replaces the x-immutable-publishable-key literal, the inline BaseUrl(...) + MessagesPath URL construction, and the repeated 256 MaxFieldLength literal in MessageBuilder with named Constants helpers. Pure refactor of code already on main - no behaviour change.
1 parent 4bc6baa commit 13bbad7

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/Packages/Audience/Runtime/Core/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ internal static class Constants
1414
internal const int DefaultFlushSize = 20;
1515
internal const int MaxBatchSize = 100;
1616
internal const int StaleEventDays = 30;
17+
internal const int MaxFieldLength = 256; // Backend schema limit.
1718

1819
internal const string LibraryName = "com.immutable.audience";
1920
internal const string Surface = "unity";
2021
internal const string ConsentSource = "UnitySDK";
2122

23+
internal const string PublishableKeyHeader = "x-immutable-publishable-key";
24+
25+
internal static string MessagesUrl(string publishableKey) => BaseUrl(publishableKey) + MessagesPath;
26+
2227
internal static string BaseUrl(string publishableKey) =>
2328
publishableKey != null && publishableKey.StartsWith(TestKeyPrefix)
2429
? SandboxBaseUrl

src/Packages/Audience/Runtime/Events/MessageBuilder.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ internal static Dictionary<string, object> Track(
1313
Dictionary<string, object> properties = null)
1414
{
1515
var msg = BuildBase("track", packageVersion);
16-
msg["eventName"] = Truncate(eventName, 256);
16+
msg["eventName"] = Truncate(eventName, Constants.MaxFieldLength);
1717

1818
if (!string.IsNullOrEmpty(anonymousId))
19-
msg["anonymousId"] = Truncate(anonymousId, 256);
19+
msg["anonymousId"] = Truncate(anonymousId, Constants.MaxFieldLength);
2020

2121
if (!string.IsNullOrEmpty(userId))
22-
msg["userId"] = Truncate(userId, 256);
22+
msg["userId"] = Truncate(userId, Constants.MaxFieldLength);
2323

2424
if (properties != null && properties.Count > 0)
2525
msg["properties"] = properties;
@@ -37,13 +37,13 @@ internal static Dictionary<string, object> Identify(
3737
var msg = BuildBase("identify", packageVersion);
3838

3939
if (!string.IsNullOrEmpty(anonymousId))
40-
msg["anonymousId"] = Truncate(anonymousId, 256);
40+
msg["anonymousId"] = Truncate(anonymousId, Constants.MaxFieldLength);
4141

4242
if (!string.IsNullOrEmpty(userId))
43-
msg["userId"] = Truncate(userId, 256);
43+
msg["userId"] = Truncate(userId, Constants.MaxFieldLength);
4444

4545
if (!string.IsNullOrEmpty(identityType))
46-
msg["identityType"] = Truncate(identityType, 256);
46+
msg["identityType"] = Truncate(identityType, Constants.MaxFieldLength);
4747

4848
if (traits != null && traits.Count > 0)
4949
msg["traits"] = traits;
@@ -59,10 +59,10 @@ internal static Dictionary<string, object> Alias(
5959
string packageVersion)
6060
{
6161
var msg = BuildBase("alias", packageVersion);
62-
msg["fromId"] = Truncate(fromId, 256);
63-
msg["fromType"] = Truncate(fromType, 256);
64-
msg["toId"] = Truncate(toId, 256);
65-
msg["toType"] = Truncate(toType, 256);
62+
msg["fromId"] = Truncate(fromId, Constants.MaxFieldLength);
63+
msg["fromType"] = Truncate(fromType, Constants.MaxFieldLength);
64+
msg["toId"] = Truncate(toId, Constants.MaxFieldLength);
65+
msg["toType"] = Truncate(toType, Constants.MaxFieldLength);
6666
return msg;
6767
}
6868

@@ -76,7 +76,7 @@ private static Dictionary<string, object> BuildBase(string type, string packageV
7676
["context"] = new Dictionary<string, object>
7777
{
7878
["library"] = Constants.LibraryName,
79-
["libraryVersion"] = Truncate(packageVersion, 256)
79+
["libraryVersion"] = Truncate(packageVersion, Constants.MaxFieldLength)
8080
},
8181
["surface"] = Constants.Surface
8282
};

src/Packages/Audience/Runtime/Transport/HttpTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal HttpTransport(
3838
{
3939
_store = store ?? throw new ArgumentNullException(nameof(store));
4040
_publishableKey = publishableKey ?? throw new ArgumentNullException(nameof(publishableKey));
41-
_url = Constants.BaseUrl(publishableKey) + Constants.MessagesPath;
41+
_url = Constants.MessagesUrl(publishableKey);
4242
_onError = onError;
4343
_client = handler != null ? new HttpClient(handler) : new HttpClient();
4444
_client.Timeout = TimeSpan.FromSeconds(30);
@@ -78,7 +78,7 @@ internal async Task<bool> SendBatchAsync(CancellationToken ct = default)
7878
try
7979
{
8080
using var request = new HttpRequestMessage(HttpMethod.Post, _url);
81-
request.Headers.Add("x-immutable-publishable-key", _publishableKey);
81+
request.Headers.Add(Constants.PublishableKeyHeader, _publishableKey);
8282
#if IMMUTABLE_AUDIENCE_GZIP
8383
var compressed = Gzip.Compress(payload);
8484
request.Content = new ByteArrayContent(compressed);

0 commit comments

Comments
 (0)