Pokeapi: faster creation of a pokêmon list

Asked

Viewed 420 times

0

I’m looking to create a program that offers information about Pokémon, using the Pokeapi, to gain access to each creature’s data.

I’m doing this program through the eclipse, with the help of Maven.

the PokemonController.java works like the controller, in it I want it to take the data of 500 Pokemon and save them in an array.

package br.com.pokemon.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import br.com.pokemon.command.findAll;
import br.com.pokemon.domain.pokemon;
import br.com.pokemon.domain.pokemonApi;
import br.com.pokemon.domain.pokemonSpecie;
import me.sargunvohra.lib.pokekotlin.client.PokeApi;
import me.sargunvohra.lib.pokekotlin.client.PokeApiClient;
import me.sargunvohra.lib.pokekotlin.model.NamedApiResource;
import me.sargunvohra.lib.pokekotlin.model.NamedApiResourceList;
import me.sargunvohra.lib.pokekotlin.model.Pokemon;
import me.sargunvohra.lib.pokekotlin.model.PokemonSpecies;
import me.sargunvohra.lib.pokekotlin.model.PokemonSpeciesVariety;
import me.sargunvohra.lib.pokekotlin.model.Type;

@Controller
public class PokemonController {

    @RequestMapping("/pokemon/image")
    public ModelAndView image() {
        ArrayList<Pokemon> pokemons = new ArrayList<Pokemon>();
        NamedApiResourceList na = pokemonApi.getApi().getPokemonList(0, 500);
        for(NamedApiResource n: na.getResults()) {
             Pokemon p = pokemonApi.getApi().getPokemon(n.getId());
             pokemons.add(p);
        }

        ModelAndView mav = new ModelAndView("pokemon/pokeimage");
        mav.addObject("pokemons", pokemons);
        return mav;

    }

}

the pokeimage.jsp would be the page that shows the information of these Pokemons, for now only the name and image of each one, however this process is time consuming, due to the opening of the api, and it takes + or - 5 minutes to load page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<table>
    <tr>
        <th>Image</th>
        <th>Name</th>
    </tr>
    <c:forEach items="${pokemons}" var="pokemon">
        <tr>
            <td><img alt="" src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png"></td>
            <td>${pokemon.name}</td>
        </tr>
    </c:forEach>
</table>

</body>
</html>

I was wondering if there is a way to be able to read the data of the Pokemon faster, I have seen sites that do this process more efficiently and effectively.

I created a pokemonApi.java to open pokeApi once and be used in all processes, I had the feeling that the program loads faster this way, but not in the desired way.

package br.com.pokemon.domain;

import me.sargunvohra.lib.pokekotlin.client.PokeApi;
import me.sargunvohra.lib.pokekotlin.client.PokeApiClient;

public class pokemonApi {

    private final static PokeApi pa = new PokeApiClient();

    public static PokeApi getApi() {
        return pa;
    }
}
  • Lucas, this question is very comprehensive. From what you can see in your code, your bottleneck is the requisitions. Instead of doing them all at once, you wait for each one to finish before doing the next one, and each of these requests can take up to a few seconds. Another thing you could do is also perform client-side requests, so it doesn’t have to wait for all the requests to be able to view the ones that are already ready, and it would smooth the processing on your server side as well. Ideally this API should bring all the information in a single request.

  • Something that would greatly improve your performance was to implement a cache for the already searched Pokemons, I believe that the information from Pokemon does not vary, so if you have already searched it once, you would not have to search again, taking the information from the cache

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.