private async Task <bool> FetchIsOn(IFeatureEvaluationRequest request, CancellationToken token = default) { var httpRequest = request.ToHttpRequestMessage(); if (!token.IsCancellationRequested) { try { var response = await this._client.SendAsync(httpRequest, token); if (response.IsSuccessStatusCode) { return(await response.Content.ReadFromJsonAsync <bool>(this._jsonOptions, token)); } var content = await response.Content.ReadAsStringAsync(token); var e = new FailedToCommunicateWithEvaluationsException(response, content); throw e; } catch (Exception e) { this._logger.LogError(e, $"An error occurred while requesting if feature was On ${e.Message}."); throw; } } throw new OperationCanceledException("Cancellation was requested while attempting to request feature IsOn."); }
public async Task <bool> isOn(IFeatureEvaluationRequest request, Func <Task <bool> > isOnTask) { return(await this._cache.GetOrCreateAsync(request.GetCacheKey(), entry => { entry.SlidingExpiration = this._configuration.CacheTimeout; return isOnTask(); })); }
public async Task <bool> IsOn(IFeatureEvaluationRequest request, CancellationToken token = default) { if (!token.IsCancellationRequested) { return(await this._cache.isOn(request, () => this.FetchIsOn(request, token))); } throw new OperationCanceledException("Cancellation was requested while attempting to evaluate the cache for feature IsOn."); }
internal static IFeatureEvaluationRequest WithStrategies( this IFeatureEvaluationRequest request, params string[] strategies) { return(new FeatureEvaluationRequest { Name = request.Name, Path = request.Path, Strategies = strategies.Select(_ => new KeyValuePair <string, string>(_, _)) }); }
internal static HttpRequestMessage ToHttpRequestMessage( this IFeatureEvaluationRequest request) { var encoder = UrlEncoder.Default; var builder = new UriBuilder(); builder.Path = "features"; var query = HttpUtility.ParseQueryString(builder.Query); query["name"] = encoder.Encode(request.Name); query["path"] = encoder.Encode(request.Path); builder.Query = query.ToString(); var httpRequestMessage = new HttpRequestMessage( HttpMethod.Get, new Uri($"{builder.Path}?{builder.Query}", UriKind.Relative)); foreach (var(key, value) in request.Strategies) { httpRequestMessage.Headers.TryAddWithoutValidation(key, value); } return(httpRequestMessage); }
public Task <bool> isOn(IFeatureEvaluationRequest request, Func <Task <bool> > isOnTask) { return(Task.FromResult(this._expected)); }
public Task <bool> isOn(IFeatureEvaluationRequest request, Func <Task <bool> > isOnTask) { return(isOnTask()); }
private FeatureEvaluationRequestBuilder(IFeatureEvaluationRequest request) { this._request = request; }
internal FeatureEvaluationRequestBuilder() { this._request = new FeatureEvaluationRequest(); }
internal static string GetCacheKey(this IFeatureEvaluationRequest request) { var strategies = request.Strategies.OrderBy(_ => _.Key).Select(_ => _.Key + _.Value); return($"${request.Name}${request.Path}${string.Join("_", strategies)}"); }