예제 #1
0
        /// <summary>
        /// Returns the resource with the given ID, creating a cache entry if it doesn't exist.
        /// </summary>
        public override async Task <TResource> Upsert(UrlNavigation <TResource> res)
        {
            if (res == null)
            {
                return(null);
            }

            var namedRes = res as NamedApiResource <TResource>;

            var name  = namedRes.Name;
            var entry = await CacheSource.GetCacheEntry(name);

            if (entry == null)
            {
                var entryType = typeof(TResource).Name;
                Logger.LogInformation($"Caching {entryType} with name {name}...");

                var resource = await PokeApi.Get(res);

                return(await Create(resource));
            }
            else if (IsStale(entry))
            {
                // update cache entry if it's stale
                var entryType = typeof(TResource).Name;
                Logger.LogInformation($"Cached {entryType} with name {name} is stale - updating...");

                var resource = await PokeApi.Get(res);
                await Update(resource);

                return(resource);
            }

            return(entry.Resource);
        }
        /// <summary>
        /// Returns the resource with the given ID, creating a cache entry if it doesn't exist.
        /// </summary>
        public virtual async Task <TResource> Upsert(UrlNavigation <TResource> res)
        {
            if (res == null)
            {
                return(null);
            }

            var resource = await PokeApi.Get(res);

            return(await Upsert(resource.Id));
        }
예제 #3
0
        /// <summary>
        /// Upserts the evolution chain from the given navigation property and returns it.
        /// </summary>
        public override async Task <EvolutionChainEntry> Upsert(UrlNavigation <EvolutionChain> evolutionChain)
        {
            var chainRes = await CacheService.Upsert(evolutionChain);

            if (chainRes.IsEmpty())
            {
                // don't bother converting if the chain is empty
                return(null);
            }

            return(await base.Upsert(chainRes));
        }
예제 #4
0
        /// <summary>
        /// Wrapper for <see cref="PokeApiClient.GetResourceAsync{T}(UrlNavigation{T})"/> with
        /// exception logging.
        /// </summary>
        public async Task <T> Get <T>(UrlNavigation <T> nav) where T : ResourceBase
        {
            var call = $"Get<{typeof(T)}>(\"{nav.Url}\")";
            T   res;

            try
            {
                Logger.LogInformation($"{call} started...");

                res = await PokeApiClient.GetResourceAsync(nav);

                Logger.LogInformation($"{call} finished.");
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"{call} from UrlNavigation object failed.");
                throw;
            }

            return(res);
        }
예제 #5
0
        /// <summary>
        /// Creates a new entry for the given API resource and returns it.
        /// </summary>
        public virtual async Task <TEntry> Upsert(UrlNavigation <TSource> res)
        {
            var source = await CacheService.Upsert(res);

            return(await Upsert(source));
        }
예제 #6
0
        /// <summary>
        /// Returns a minimal copy of the given resource, caching the resource if needed.
        /// </summary>
        public override async Task <TResource> GetMinimal(UrlNavigation <TResource> res)
        {
            var resource = await Upsert(res);

            return(resource?.MinimiseNamed());
        }
        /// <summary>
        /// Returns a minimal copy of the given resource, caching the resource if needed.
        /// </summary>
        public virtual async Task <TResource> GetMinimal(UrlNavigation <TResource> res)
        {
            var resource = await Upsert(res);

            return(resource?.Minimise());
        }
예제 #8
0
 /// <summary>
 /// Resolves a single navigation property
 /// </summary>
 /// <typeparam name="T">Navigation type</typeparam>
 /// <param name="urlResource">The single navigation object to resolve</param>
 /// <param name="cancellationToken">Cancellation token for the request; not utilitized if data has been cached</param>
 /// <returns>A resolved object</returns>
 public async Task <T> GetResourceAsync <T>(UrlNavigation <T> urlResource, CancellationToken cancellationToken) where T : ResourceBase
 {
     return(await GetResourceByUrlAsync <T>(urlResource.Url, cancellationToken));
 }