Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
 /// <summary>
 /// Deletes the cached Get API result for the specified API url.
 /// </summary>
 public Task DisposeCache <TResponse>(string getApiUrl)
 => ApiResponseCache <TResponse> .Create(getApiUrl).Delete();