internal async Task ApiSetPath( HttpContext context, FeatureFlagsSettings settings ) { if (context.Request.Method != HttpMethods.Post) { context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; return; } var featureFlagsSessionManager = context.RequestServices.GetRequiredService <FeatureFlagsSessionManager>(); string requestString; using (var streamReader = new StreamReader(context.Request.Body)) { requestString = await streamReader .ReadToEndAsync() .ConfigureAwait(false); } var setRequest = (FeatureRequest)JsonConvert.DeserializeObject( requestString, typeof(FeatureRequest) ); if (setRequest is null) { context.Response.StatusCode = StatusCodes.Status400BadRequest; return; } var featureType = settings.GetFeatureType(setRequest.Name); if (featureType is null) { context.Response.StatusCode = StatusCodes.Status404NotFound; return; } await featureFlagsSessionManager .SetNullableAsync(setRequest.Name, setRequest.Enabled) .ConfigureAwait(false); context.Response.StatusCode = StatusCodes.Status204NoContent; }
internal async Task ApiGetPath( HttpContext context, FeatureFlagsSettings settings ) { if (context.Request.Method != HttpMethods.Get) { context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; return; } var featureFlagsSessionManager = context.RequestServices.GetRequiredService <FeatureFlagsSessionManager>(); var featureName = context.Request.Query["feature"]; if (string.IsNullOrEmpty(featureName)) { context.Response.StatusCode = StatusCodes.Status400BadRequest; return; } var featureType = settings.GetFeatureType(featureName); if (featureType is null) { context.Response.StatusCode = StatusCodes.Status404NotFound; return; } var value = await featureFlagsSessionManager.GetAsync(featureName); var response = new FeatureResponse { Name = featureName, Description = featureType.GetDescription(), Enabled = value, }; var json = JsonConvert.SerializeObject(response); await context.Response.WriteAsync(json).ConfigureAwait(false); }