How to pass along context with AppConfigDataClient when fetching multi-variant feature flag from AWS AppConfig? #3624
Unanswered
wani-guanxi
asked this question in
Q&A
Replies: 1 comment 1 reply
-
@wani-guanxi Good afternoon. Please share on how you are currently using
Taking clue from Amazon.Extensions.Configuration.SystemsManager high-level library:
Program.cs using Amazon.AppConfigData;
using Amazon.AppConfigData.Model;
using ConsoleTestNet8;
var amazonAppConfigDataClient = new AmazonAppConfigDataClient();
var startconfigurationSessionResponse = await amazonAppConfigDataClient.StartConfigurationSessionAsync(
new StartConfigurationSessionRequest()
{
ApplicationIdentifier = "<<APP-ID>>",
ConfigurationProfileIdentifier = "<<CONFIG-PROFILE-ID>>",
EnvironmentIdentifier = "<<ENV-ID>>"
}
);
var response = amazonAppConfigDataClient.GetLatestConfigurationAsync(new GetLatestConfigurationRequest() { ConfigurationToken = startconfigurationSessionResponse.InitialConfigurationToken }).Result;
var result = ParseConfig(response.ContentType, response.Configuration);
static IDictionary<string, string> ParseConfig(string contentType, Stream configuration)
{
// Content-Type has format "media-type; charset" or "media-type; boundary" (for multipart entities).
if (contentType != null)
{
contentType = contentType.Split(';')[0];
}
switch (contentType)
{
case "application/json":
return JsonConfigurationParser.Parse(configuration);
default:
throw new NotImplementedException($"Not implemented AppConfig type: {contentType}");
}
} JsonConfigurationParser.cs using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ConsoleTestNet8
{
public class JsonConfigurationParser
{
private JsonConfigurationParser() { }
private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> _context = new Stack<string>();
private string _currentPath;
public static IDictionary<string, string> Parse(Stream input)
{
using (var doc = JsonDocument.Parse(input))
{
var parser = new JsonConfigurationParser();
parser.VisitElement(doc.RootElement);
return parser._data;
}
}
public static IDictionary<string, string> Parse(string input)
{
using (var doc = JsonDocument.Parse(input))
{
var parser = new JsonConfigurationParser();
parser.VisitElement(doc.RootElement);
return parser._data;
}
}
private void VisitElement(JsonElement element)
{
switch (element.ValueKind)
{
case JsonValueKind.Undefined:
break;
case JsonValueKind.Object:
foreach (var property in element.EnumerateObject())
{
EnterContext(property.Name);
VisitElement(property.Value);
ExitContext();
}
break;
case JsonValueKind.Array:
VisitArray(element);
break;
case JsonValueKind.String:
case JsonValueKind.Number:
case JsonValueKind.True:
case JsonValueKind.False:
VisitPrimitive(element);
break;
case JsonValueKind.Null:
VisitNull();
break;
}
}
private void VisitArray(JsonElement array)
{
int index = 0;
foreach (var item in array.EnumerateArray())
{
EnterContext(index.ToString(CultureInfo.InvariantCulture));
VisitElement(item);
ExitContext();
index++;
}
}
private void VisitNull()
{
var key = _currentPath;
_data[key] = null;
}
private void VisitPrimitive(JsonElement data)
{
var key = _currentPath;
if (_data.ContainsKey(key))
{
throw new FormatException($"A duplicate key '{key}' was found.");
}
_data[key] = data.ToString();
}
private void EnterContext(string context)
{
_context.Push(context);
_currentPath = ConfigurationPath.Combine(_context.Reverse());
}
private void ExitContext()
{
_context.Pop();
_currentPath = ConfigurationPath.Combine(_context.Reverse());
}
}
} This successfully returns multi-variant feature flag and returns it as a dictionary. Thanks, |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am working on .NET implementation for retrieving AppConfig data using "AppConfigDataClient" and am having difficulty in understanding how to retrieve multi-variant flags. I don't see any way in the calls using the client to pass along context or any other information to AppConfig, and the returned value only consists of "default" variant but not other variants. Has anybody successfully done anything like that using AppConfigDataClient.
I am thinking of configuring client myself and test passing context in header as explained in this documentation https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-retrieving-feature-flags.html, but will have to rip off my current implementation to make that possible, so wanted to know if anybody have any experience / suggestions on how to do that.
Beta Was this translation helpful? Give feedback.
All reactions