All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.jimmydbe.GamesClient Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
package io.github.jimmydbe;

import io.github.jimmydbe.entities.*;
import io.github.jimmydbe.exceptions.GamesDbException;
import io.github.jimmydbe.exceptions.ParsingException;
import kong.unirest.GenericType;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;

import java.util.List;
import java.util.stream.Collectors;

class GamesClient extends Client {

    private final String fields = "players,publishers,genres,overview,last_updated,rating,platform,coop,youtube,os,processor,ram,hdd,video,sound";

    protected GamesClient(String apiKey) {
        super(apiKey);
    }

    private List convertGameToDto(AdvancedResponse data) {
        return data.getData().getGames().stream().map(game -> convert(game,data.getInclude())).collect(Collectors.toList());
    }

    private GameDto convert(Game game, IncludeResponse include) {

        GameDto dto = new GameDto(game);

        dto.setPlatformObject(include.getPlatform().getData().get(String.valueOf(game.getPlatform())));
        dto.setImages(include.getBoxart().getData().get(String.valueOf(game.getId())));

        return dto;
    }

    protected BaseImageResponse getGameImages(List id) throws ParsingException, GamesDbException {

        String ids = id.stream().map(n -> n.toString()).collect(Collectors.joining(","));

        final HttpResponse> response = Unirest.get(TheGamesDbClient.BASE_URL + "/v1/Games/Images")
                .queryString("apikey", getApiKey())
                .queryString("games_id", ids)
                .asObject(new GenericType>() {
                });

        checkForErrors(response);

        return response.getBody()
                .getData();
    }

    protected List getGameById(List id) throws ParsingException, GamesDbException {

        String ids = id.stream().map(n -> n.toString()).collect(Collectors.joining(","));

        final HttpResponse> response = Unirest.get(TheGamesDbClient.BASE_URL + "/v1/Games/ByGameID")
                .queryString("apikey", getApiKey())
                .queryString("id", ids)
                .queryString("fields", fields)
                .queryString("include", "boxart,platform")
                .asObject(new GenericType>() {
                });

        checkForErrors(response);

        return convertGameToDto(response.getBody());
    }

    protected List getGameByName(String name) throws ParsingException, GamesDbException {
        final HttpResponse> response = Unirest.get(TheGamesDbClient.BASE_URL + "/v1.1/Games/ByGameName")
                .queryString("apikey", getApiKey())
                .queryString("name", name)
                .queryString("fields", fields)
                .queryString("include", "boxart,platform")
                .asObject(new GenericType>() {
                });
        checkForErrors(response);
        return convertGameToDto(response.getBody());
    }

    protected List getGameByPlatform(List platformId) throws ParsingException, GamesDbException {

        String ids = platformId.stream().map(n -> n.toString()).collect(Collectors.joining(","));

        final HttpResponse> response = Unirest.get(TheGamesDbClient.BASE_URL + "/v1/Games/ByPlatformID")
                .queryString("apikey", getApiKey())
                .queryString("id", ids)
                .queryString("fields", fields)
                .queryString("include", "boxart,platform")
                .asObject(new GenericType>() {
                });

        checkForErrors(response);

        return convertGameToDto(response.getBody());
    }

    protected List getUpdateGame(int lastEditId) throws ParsingException, GamesDbException {
        final HttpResponse> response = Unirest.get(TheGamesDbClient.BASE_URL + "/v1/Games/Updates")
                .queryString("apikey", getApiKey())
                .queryString("last_edit_id", lastEditId)
                .asObject(new GenericType>() {
                });
        checkForErrors(response);

        return response.getBody()
                .getData()
                .getGames();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy