public override async Task <bool> Attempt(string url) { try { var cache = ApiResponseCache <T> .Create(url); var requestInfo = new RequestInfo(ApiClient) { HttpMethod = "GET" }; Result = await requestInfo.TrySend <T>(); if (requestInfo.Error == null) { await cache.Update(requestInfo.ResponseText); return(true); } else { Error = requestInfo.Error; } } catch (Exception exception) { Error = exception; Log.For(this).Error(exception); } return(false); }
public override async Task <bool> Attempt(string url) { var cache = ApiResponseCache <T> .Create(url); if (!await cache.HasValidValue(CacheAge)) { Error = new Exception("No valid cache is available."); return(false); } Result = cache.Data; if (!Silent && FallBackEventPolicy == ApiFallBackEventPolicy.Raise) { await FallBackEvent.Raise(new FallBackEvent { Url = url, CacheAge = cache.Age, FriendlyMessage = $"Failed to get fresh results from {url.AsUri().Host}. Using the latest cache from {cache.Age.ToNaturalTime(1)} ago." }); } return(true); }
/// <summary> /// Deletes the cached Get API result for the specified API url. /// </summary> public Task DisposeCache <TResponse>(string getApiUrl) => ApiResponseCache <TResponse> .Create(getApiUrl).Delete();