/// <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)); }
/// <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)); }
/// <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); }
/// <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)); }
/// <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()); }
/// <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)); }