public async Task ClearResourceListCacheOfSpecificType() { // assemble PokeApiClient sut = CreateSut(); mockHttp.Expect("*machine") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeApiResourceList <Machine>())); mockHttp.Expect("*berry") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.Expect("*machine") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeApiResourceList <Machine>())); // act await sut.GetApiResourcePageAsync <Machine>(); await sut.GetNamedResourcePageAsync <Berry>(); sut.ClearResourceListCache <Machine>(); await sut.GetApiResourcePageAsync <Machine>(); await sut.GetNamedResourcePageAsync <Berry>(); // assert mockHttp.VerifyNoOutstandingExpectation(); }
public async Task ClearCacheWipesAllCachedData() { // assemble PokeApiClient sut = CreateSut(); Berry berry = new Berry { Name = "test", Id = 1 }; mockHttp.Expect($"*berry/{berry.Id}*") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.Expect("*berry") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.Expect($"*berry/{berry.Id}*") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.Expect("*berry") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); // act await sut.GetResourceAsync <Berry>(berry.Id); await sut.GetNamedResourcePageAsync <Berry>(); sut.ClearCache(); await sut.GetResourceAsync <Berry>(berry.Id); await sut.GetNamedResourcePageAsync <Berry>(); // assert mockHttp.VerifyNoOutstandingExpectation(); }
/// <summary> /// Sets the list of valid pokemon names. /// </summary> private async Task SetPokemonList() { var clientResult = await pokeClient.GetNamedResourcePageAsync <Pokemon>(-1, 0, default); lPokemon = clientResult.Results; }
// method to load PokemonList from the internet over API private async Task LoadFromApi() { // MAX limit currently: 807 int maxPokemons = 807; NamedApiResourceList <Pokemon> allPokemons = await pokeClient.GetNamedResourcePageAsync <Pokemon>(maxPokemons, 0); for (int i = 1; i <= allPokemons.Results.Count; i++) { PokemonSpecies pokemonNameLang = await pokeClient.GetResourceAsync <PokemonSpecies>(i); for (int j = 0; j < pokemonNameLang.Names.Count; j++) { if (pokemonNameLang.Names[j].Language.Name == Language) { PokeList.Add(new PokemonModel { Id = i, PokeNameOriginal = allPokemons.Results[i - 1].Name, PokeName = pokemonNameLang.Names[j].Name, PokeUrl = allPokemons.Results[i - 1].Url }); SearchPokeList.Add(new PokemonModel { Id = i, PokeNameOriginal = allPokemons.Results[i - 1].Name, PokeName = pokemonNameLang.Names[j].Name, PokeUrl = allPokemons.Results[i - 1].Url }); } } // Brings loading status to front-end in ProgressBar await _events.PublishOnUIThreadAsync(new LoadingBarEvent { LoadingCount = i }, CancellationToken.None); } }
private async void carregamentoInicial() { NamedApiResourceList <Pokemon> listaFull = await pokeClient.GetNamedResourcePageAsync <Pokemon>(25, page); //RootObject lista = await PokeApi.listaPokemons(); lstPokemons.ItemsSource = listaFull.Results; }
public async Task GetNamedApiResourcePage_WithSamePaginationParams_CacheHit() { // assemble PokeApiClient sut = CreateSut(); mockHttp.Expect("*") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.When("*") .Throw(new Exception("Cache miss")); // act await sut.GetNamedResourcePageAsync <Berry>(); await sut.GetNamedResourcePageAsync <Berry>(); // assert mockHttp.VerifyNoOutstandingExpectation(); }
public async Task GetLocationPagedResourceIntegrationTest() { // assemble PokeApiClient client = new PokeApiClient(); // act NamedApiResourceList <Location> page = await client.GetNamedResourcePageAsync <Location>(); // assert Assert.True(page.Results.Any()); }
public async Task GetEncounterConditionValuePagedResourceIntegrationTest() { // assemble PokeApiClient client = new PokeApiClient(); // act NamedApiResourceList <EncounterConditionValue> page = await client.GetNamedResourcePageAsync <EncounterConditionValue>(); // assert Assert.True(page.Results.Any()); }
public async Task GetNamedApiResourcePage_WithDifferentPaginationParams_CacheMiss() { // assemble (int limit, int offset) = (30, 2); PokeApiClient sut = CreateSut(); mockHttp.Expect("*") .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); mockHttp.Expect("*") .WithExactQueryString(ToPairs(limit, offset)) .Respond("application/json", JsonConvert.SerializeObject(CreateFakeNamedResourceList <Berry>())); // act await sut.GetNamedResourcePageAsync <Berry>(); await sut.GetNamedResourcePageAsync <Berry>(limit, offset); // assert mockHttp.VerifyNoOutstandingExpectation(); }
public async Task GetNamedResourcePageLimitOffsetAsyncCancellation() { // assemble NamedApiResourceList <Berry> berryPage = new NamedApiResourceList <Berry>(); MockHttpMessageHandler mockHttp = new MockHttpMessageHandler(); mockHttp.Expect("*berry?limit=50&offset=2").Respond("application/json", JsonConvert.SerializeObject(berryPage)); PokeApiClient client = new PokeApiClient(mockHttp); CancellationToken cancellationToken = new CancellationToken(true); // act / assert await Assert.ThrowsAsync <OperationCanceledException>(async() => { await client.GetNamedResourcePageAsync <Berry>(50, 2, cancellationToken); }); }
public async Task GetNamedResourcePageLimitOffsetAsync() { // assemble NamedApiResourceList <Berry> berryPage = new NamedApiResourceList <Berry>(); MockHttpMessageHandler mockHttp = new MockHttpMessageHandler(); mockHttp.Expect("*berry?limit=50&offset=2").Respond("application/json", JsonConvert.SerializeObject(berryPage)); PokeApiClient client = new PokeApiClient(mockHttp); // act NamedApiResourceList <Berry> page = await client.GetNamedResourcePageAsync <Berry>(50, 2); // assert mockHttp.VerifyNoOutstandingExpectation(); }
/// <summary> /// Create and send a full list of Pokemon. /// </summary> /// <param name="type">If playing in hard mode, a type will be specified. Defaults to -1 (easy mode).</param> /// <returns>List of Pokemon</returns> public async Task <IEnumerable <(Pokemon, PokemonSpecies)> > CreateList(int type = -1) { List <NamedApiResource <Pokemon> > allPokemonList; // If in hard mode, all pokemon should be of the specified type if (type != -1) { var typed = await pokeClient.GetResourceAsync <PokeApiNet.Type>(type); allPokemonList = typed.Pokemon.Select(pokemons => pokemons.Pokemon).ToList(); } // Otherwise, populate using all Pokemon else { allPokemonList = (await pokeClient.GetNamedResourcePageAsync <Pokemon>(Int32.MaxValue, 0)).Results; } var usedPokemon = PickPokemon(allPokemonList.Count, 25); var stopwatch = new Stopwatch(); stopwatch.Start(); var pokemonTasks = usedPokemon.Select(async(i) => { var newPoke = await pokeClient.GetResourceAsync <Pokemon>(allPokemonList[i]); while (newPoke.Sprites.FrontDefault is null) { int newIndex; do { newIndex = random.Next(0, allPokemonList.Count - 1); }while (usedPokemon.Contains(newIndex)); newPoke = await pokeClient.GetResourceAsync <Pokemon>(allPokemonList[newIndex]); } var newPokeSpecies = await pokeClient.GetResourceAsync <PokemonSpecies>(newPoke.Species); logger.LogInformation($"Pokemon '{newPoke.Name}' added to board."); return(newPoke, newPokeSpecies); }); var pokemon = await Task.WhenAll(pokemonTasks); stopwatch.Stop(); logger.LogInformation($"Fetching took {stopwatch.Elapsed}"); return(pokemon); }
/// <summary> /// Wrapper for <see cref="PokeApiClient.GetNamedResourcePageAsync{T}()"/> with exception logging. /// </summary> public async Task <NamedApiResourceList <T> > GetNamedPage <T>(int limit, int offset) where T : NamedApiResource { var call = $"GetPage<{typeof(T)}>(limit={limit}, offset={offset})"; NamedApiResourceList <T> resList; try { Logger.LogInformation($"{call} started..."); resList = await PokeApiClient.GetNamedResourcePageAsync <T>(limit, offset); Logger.LogInformation($"{call} finished."); } catch (Exception e) { Logger.LogError(e, $"{call} failed."); throw; } return(resList); }
public async Task <ActionResult <List <PokemonDataModel> > > GetPokemonList(int pageIndex) { List <PokemonDataModel> cacheValue; if (!_cache.TryGetValue <List <PokemonDataModel> >(pageIndex, out cacheValue)) { //Get Data if not in cache. //Create List to store Pokemon Data. List <PokemonDataModel> pokemonList = new List <PokemonDataModel>(); //Data Settings per page. int limit = 20; int offset = 20 * pageIndex; if (pageIndex > 50 || pageIndex < 0) { return(BadRequest()); } //Get Page Data. var pokemonPageData = await pokeClient.GetNamedResourcePageAsync <Pokemon>(limit, offset); if (pokemonPageData == null) { return(NotFound()); } //Processing for each one of the pokemon in the page data retrieved. foreach (var result in pokemonPageData.Results) { //Create Data Structures for one pokemon. PokemonDataModel pokemonModel = new PokemonDataModel { Types = new List <string>(), Abilities = new List <string>() }; // Get Opertiaon to obtain the specific pokemon data. var fetchedPokemonData = await pokeClient.GetResourceAsync <Pokemon>(result.Name); if (fetchedPokemonData == null) { return(NotFound()); } //Fill the Data Model pokemonModel.Name = fetchedPokemonData.Name; pokemonModel.Weight = fetchedPokemonData.Weight; pokemonModel.ImageUrl = fetchedPokemonData.Sprites.FrontDefault; pokemonModel.Types.AddRange(fetchedPokemonData.Types.Select(element => element.Type.Name)); pokemonModel.Abilities.AddRange(fetchedPokemonData.Abilities.Select(element => element.Ability.Name)); //Add the pokemon data to the list. pokemonList.Add(pokemonModel); } cacheValue = pokemonList; //Cache expiration in 3 minutes var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(3)); // Save values in cache for a given key. _cache.Set <List <PokemonDataModel> >(pageIndex, cacheValue, cacheEntryOptions); } return(Ok(cacheValue)); }