Skip to content

Commit

Permalink
decompress services and service usage into separate services + classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jul 26, 2024
1 parent 687fcdc commit 6049876
Show file tree
Hide file tree
Showing 10 changed files with 470 additions and 264 deletions.
170 changes: 138 additions & 32 deletions src/main/java/io/github/lucasstarsz/fxdex/misc/ApiConversionTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,144 @@ public class ApiConversionTables {
"x", 10
);

public static final String National = "national";
public static final String Kanto = "kanto";
public static final String OriginalJohto = "original-johto";
public static final String Hoenn = "hoenn";
public static final String OriginalSinnoh = "original-sinnoh";
public static final String ExtendedSinnoh = "extended-sinnoh";
public static final String UpdatedJohto = "updated-johto";
public static final String OriginalUnova = "original-unova";
public static final String UpdatedUnova = "updated-unova";
public static final String ConquestGallery = "conquest-gallery";
public static final String KalosCentral = "kalos-central";
public static final String KalosCoastal = "kalos-coastal";
public static final String KalosMountain = "kalos-mountain";
public static final String UpdatedHoenn = "updated-hoenn";
public static final String OriginalAlola = "original-alola";
public static final String OriginalMelemele = "original-melemele";
public static final String OriginalAkala = "original-akala";
public static final String OriginalUlaula = "original-ulaula";
public static final String OriginalPoni = "original-poni";
public static final String UpdatedAlola = "updated-alola";
public static final String UpdatedMelemele = "updated-melemele";
public static final String UpdatedAkala = "updated-akala";
public static final String UpdatedUlaula = "updated-ulaula";
public static final String UpdatedPoni = "updated-poni";
public static final String LetsGoKanto = "letsgo-kanto";
public static final String Galar = "galar";
public static final String IsleOfArmor = "isle-of-armor";
public static final String CrownTundra = "crown-tundra";
public static final String Hisui = "hisui";
public static final String Paldea = "paldea";
public static final String Kitakami = "kitakami";
public static final String Blueberry = "blueberry";

public static final Map<String, String> PokedexNameMap = Map.ofEntries(
Map.entry("national", "National Dex"),
Map.entry("kanto", "Kanto (Red/Blue/Yellow/Green)"),
Map.entry("original-johto", "Johto (Gold/Silver/Crystal)"),
Map.entry("hoenn", "Hoenn (Ruby/Sapphire/Emerald)"),
Map.entry("original-sinnoh", "Sinnoh (Diamond/Pearl)"),
Map.entry("extended-sinnoh", "Sinnoh (Platinum)"),
Map.entry("updated-johto", "Johto (HeartGold/SoulSilver/Crystal)"),
Map.entry("original-unova", "Unova (Black/White)"),
Map.entry("updated-unova", "Unova (Black2/White2)"),
Map.entry("conquest-gallery", "Conquest (Pokemon Conquest)"),
Map.entry("kalos-central", "Central Kalos (X/Y)"),
Map.entry("kalos-coastal", "Coastal Kalos (X/Y)"),
Map.entry("kalos-mountain", "Mountain Kalos (X/Y)"),
Map.entry("updated-hoenn", "Hoenn (Omega Ruby/Alpha Sapphire)"),
Map.entry("original-alola", "Alola (Sun/Moon)"),
Map.entry("original-melemele", "Alola-Melemele (Sun/Moon)"),
Map.entry("original-akala", "Alola-Akala (Sun/Moon)"),
Map.entry("original-ulaula", "Alola-Ulaula (Sun/Moon)"),
Map.entry("original-poni", "Alola-Poni (Sun/Moon)"),
Map.entry("updated-alola", "Alola (Ultra Sun/Ultra Moon)"),
Map.entry("updated-melemele", "Alola-Melemele (Ultra Sun/Ultra Moon)"),
Map.entry("updated-akala", "Alola-Akala (Ultra Sun/Ultra Moon)"),
Map.entry("updated-ulaula", "Alola-Ulaula (Ultra Sun/Ultra Moon)"),
Map.entry("updated-poni", "Alola-Poni (Ultra Sun/Ultra Moon)"),
Map.entry("letsgo-kanto", "Let's Go - Kanto (Let's go Pikachu/Eevee)"),
Map.entry("galar", "Galar (Sword/Shield)"),
Map.entry("isle-of-armor", "Galar - Isle of Armor (Sword/Shield DLC)"),
Map.entry("crown-tundra", "Galar - Crown Tundra (Sword/Shield DLC)"),
Map.entry("hisui", "Hisui (Legends: Arceus)"),
Map.entry("paldea", "Paldea (Scarlet/Violet)"),
Map.entry("kitakami", "Paldea - Kitakami (Scarlet/Violet DLC)"),
Map.entry("blueberry", "Paldea - Blueberry (Scarlet/Violet DLC)")
Map.entry(National, "National Dex"),
Map.entry(Kanto, "Kanto (Red/Blue/Yellow/Green)"),
Map.entry(OriginalJohto, "Johto (Gold/Silver/Crystal)"),
Map.entry(Hoenn, "Hoenn (Ruby/Sapphire/Emerald)"),
Map.entry(OriginalSinnoh, "Sinnoh (Diamond/Pearl)"),
Map.entry(ExtendedSinnoh, "Sinnoh (Platinum)"),
Map.entry(UpdatedJohto, "Johto (HeartGold/SoulSilver/Crystal)"),
Map.entry(OriginalUnova, "Unova (Black/White)"),
Map.entry(UpdatedUnova, "Unova (Black2/White2)"),
Map.entry(ConquestGallery, "Conquest (Pokemon Conquest)"),
Map.entry(KalosCentral, "Central Kalos (X/Y)"),
Map.entry(KalosCoastal, "Coastal Kalos (X/Y)"),
Map.entry(KalosMountain, "Mountain Kalos (X/Y)"),
Map.entry(UpdatedHoenn, "Hoenn (Omega Ruby/Alpha Sapphire)"),
Map.entry(OriginalAlola, "Alola (Sun/Moon)"),
Map.entry(OriginalMelemele, "Alola-Melemele (Sun/Moon)"),
Map.entry(OriginalAkala, "Alola-Akala (Sun/Moon)"),
Map.entry(OriginalUlaula, "Alola-Ulaula (Sun/Moon)"),
Map.entry(OriginalPoni, "Alola-Poni (Sun/Moon)"),
Map.entry(UpdatedAlola, "Alola (Ultra Sun/Ultra Moon)"),
Map.entry(UpdatedMelemele, "Alola-Melemele (Ultra Sun/Ultra Moon)"),
Map.entry(UpdatedAkala, "Alola-Akala (Ultra Sun/Ultra Moon)"),
Map.entry(UpdatedUlaula, "Alola-Ulaula (Ultra Sun/Ultra Moon)"),
Map.entry(UpdatedPoni, "Alola-Poni (Ultra Sun/Ultra Moon)"),
Map.entry(LetsGoKanto, "Let's Go - Kanto (Let's go Pikachu/Eevee)"),
Map.entry(Galar, "Galar (Sword/Shield)"),
Map.entry(IsleOfArmor, "Galar - Isle of Armor (Sword/Shield DLC)"),
Map.entry(CrownTundra, "Galar - Crown Tundra (Sword/Shield DLC)"),
Map.entry(Hisui, "Hisui (Legends: Arceus)"),
Map.entry(Paldea, "Paldea (Scarlet/Violet)"),
Map.entry(Kitakami, "Paldea-Kitakami (Scarlet/Violet DLC)"),
Map.entry(Blueberry, "Paldea-Blueberry (Scarlet/Violet DLC)")
);

public static final Map<String, String> PokedexUiToApiNameMap = Map.ofEntries(
Map.entry("National Dex", National),
Map.entry("Kanto (Red/Blue/Yellow/Green)", Kanto),
Map.entry("Johto (Gold/Silver/Crystal)", OriginalJohto),
Map.entry("Hoenn (Ruby/Sapphire/Emerald)", Hoenn),
Map.entry("Sinnoh (Diamond/Pearl)", OriginalSinnoh),
Map.entry("Sinnoh (Platinum)", ExtendedSinnoh),
Map.entry("Johto (HeartGold/SoulSilver/Crystal)", UpdatedJohto),
Map.entry("Unova (Black/White)", OriginalUnova),
Map.entry("Unova (Black2/White2)", UpdatedUnova),
Map.entry("Conquest (Pokemon Conquest)", ConquestGallery),
Map.entry("Central Kalos (X/Y)", KalosCentral),
Map.entry("Coastal Kalos (X/Y)", KalosCoastal),
Map.entry("Mountain Kalos (X/Y)", KalosMountain),
Map.entry("Hoenn (Omega Ruby/Alpha Sapphire)", UpdatedHoenn),
Map.entry("Alola (Sun/Moon)", OriginalAlola),
Map.entry("Alola-Melemele (Sun/Moon)", OriginalMelemele),
Map.entry("Alola-Akala (Sun/Moon)", OriginalAkala),
Map.entry("Alola-Ulaula (Sun/Moon)", OriginalUlaula),
Map.entry("Alola-Poni (Sun/Moon)", OriginalPoni),
Map.entry("Alola (Ultra Sun/Ultra Moon)", UpdatedAlola),
Map.entry("Alola-Melemele (Ultra Sun/Ultra Moon)", UpdatedMelemele),
Map.entry("Alola-Akala (Ultra Sun/Ultra Moon)", UpdatedAkala),
Map.entry("Alola-Ulaula (Ultra Sun/Ultra Moon)", UpdatedUlaula),
Map.entry("Alola-Poni (Ultra Sun/Ultra Moon)", UpdatedPoni),
Map.entry("Let's Go - Kanto (Let's go Pikachu/Eevee)", LetsGoKanto),
Map.entry("Galar (Sword/Shield)", Galar),
Map.entry("Galar - Isle of Armor (Sword/Shield DLC)", IsleOfArmor),
Map.entry("Galar - Crown Tundra (Sword/Shield DLC)", CrownTundra),
Map.entry("Hisui (Legends: Arceus)", Hisui),
Map.entry("Paldea (Scarlet/Violet)", Paldea),
Map.entry("Paldea-Kitakami (Scarlet/Violet DLC)", Kitakami),
Map.entry("Paldea-Blueberry (Scarlet/Violet DLC)", Blueberry)
);

public static final Map<String, Integer> PokedexNameToIdMap = Map.ofEntries(
// PokeApi's pokedex list starts at index 1
Map.entry(National, 1),
Map.entry(Kanto, 2),
Map.entry(OriginalJohto, 3),
Map.entry(Hoenn, 4),
Map.entry(OriginalSinnoh, 5),
Map.entry(ExtendedSinnoh, 6),
Map.entry(UpdatedJohto, 7),
Map.entry(OriginalUnova, 8),
Map.entry(UpdatedUnova, 9),
// at index nine of the PokeAPI JSON, the dex number increases its offset by 1
// https://pokeapi.co/api/v2/pokedex/?offset=0&limit=32
Map.entry(ConquestGallery, 11),
Map.entry(KalosCentral, 12),
Map.entry(KalosCoastal, 13),
Map.entry(KalosMountain, 14),
Map.entry(UpdatedHoenn, 15),
Map.entry(OriginalAlola, 16),
Map.entry(OriginalMelemele, 17),
Map.entry(OriginalAkala, 18),
Map.entry(OriginalUlaula, 19),
Map.entry(OriginalPoni, 20),
Map.entry(UpdatedAlola, 21),
Map.entry(UpdatedMelemele, 22),
Map.entry(UpdatedAkala, 23),
Map.entry(UpdatedUlaula, 24),
Map.entry(UpdatedPoni, 25),
Map.entry(LetsGoKanto, 26),
Map.entry(Galar, 27),
Map.entry(IsleOfArmor, 28),
Map.entry(CrownTundra, 29),
Map.entry(Hisui, 30),
Map.entry(Paldea, 31),
Map.entry(Kitakami, 32),
Map.entry(Blueberry, 33)
);
}
6 changes: 2 additions & 4 deletions src/main/java/io/github/lucasstarsz/fxdex/misc/DexModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

import io.github.lucasstarsz.fxdex.service.DefaultHttpService;
import io.github.lucasstarsz.fxdex.service.DexService;
import io.github.lucasstarsz.fxdex.service.HttpService;
import io.github.lucasstarsz.fxdex.service.PokeApiDexService;
import io.github.lucasstarsz.fxdex.service.*;

public class DexModule extends AbstractModule {

@Override
protected void configure() {
bind(UiService.class).to(DefaultUIService.class);
bind(DexService.class).to(PokeApiDexService.class);
bind(HttpService.class).to(DefaultHttpService.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import com.google.inject.name.Named;

import static io.github.lucasstarsz.fxdex.misc.ApiLinks.AllDexesUrl;
import static io.github.lucasstarsz.fxdex.misc.ApiLinks.DexEntryUrl;
import static io.github.lucasstarsz.fxdex.misc.ApiLinks.DexUrl;

import io.github.lucasstarsz.fxdex.App;

public class DefaultHttpService implements HttpService {
Expand All @@ -52,26 +51,16 @@ public DefaultHttpService(@Named("dexThreadHandler") ExecutorService dexThreadHa
}

@Override
public HttpRequest getDefaultDexRequest() {
return dexesRequest;
public HttpClient getClient() {
return client;
}

@Override
public HttpRequest buildDexRequest(int dexId) throws URISyntaxException {
return HttpRequest.newBuilder(dexesRequest, (n, v) -> true)
.uri(new URI(DexUrl + dexId + "/"))
.build();
}

@Override
public HttpResponse<String> getString(HttpRequest request) throws IOException, InterruptedException {
return client.send(request, HttpResponse.BodyHandlers.ofString());
public HttpRequest getDefaultDexRequest() {
return HttpRequest.newBuilder(dexesRequest, (n, v) -> true).build();
}

@Override
public HttpRequest buildDexEntryRequest(String dexEntry) throws URISyntaxException {
return HttpRequest.newBuilder(dexesRequest, (n, v) -> true)
.uri(new URI(DexEntryUrl + dexEntry + "/"))
.build();
public HttpResponse<String> get(DexRequestOptions options) throws IOException, InterruptedException {
return client.send(options.buildGetRequest(this), HttpResponse.BodyHandlers.ofString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.lucasstarsz.fxdex.service;

public class DefaultUIService implements UiService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.lucasstarsz.fxdex.service;

import io.github.lucasstarsz.fxdex.misc.ApiLinks;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;

import static io.github.lucasstarsz.fxdex.misc.ApiConversionTables.PokedexNameToIdMap;

public class DexRequestOptions {
private final Property<URI> linkProperty;

public DexRequestOptions() throws URISyntaxException {
this(DexRequestType.Default, null);
}

public DexRequestOptions(DexRequestType requestType, String link) throws URISyntaxException {
linkProperty = new SimpleObjectProperty<>();

switch (requestType) {
case DexEntry -> setLinkAsDexEntry(link);
case DexList -> setLinkAsDexList(link);
default -> linkProperty.setValue(new URI(ApiLinks.AllDexesUrl));
}
}

public Property<URI> linkProperty() {
return new SimpleObjectProperty<>(linkProperty.getValue());
}

private void setLinkAsDexEntry(String dexEntry) throws URISyntaxException {
linkProperty.setValue(new URI(ApiLinks.DexEntryUrl + dexEntry.toLowerCase() + '/'));
}

private void setLinkAsDexList(String pokedex) throws URISyntaxException {
linkProperty.setValue(new URI(ApiLinks.DexUrl + PokedexNameToIdMap.get(pokedex.toLowerCase()) + '/'));
}

public HttpRequest buildGetRequest(HttpService httpService) {
return HttpRequest.newBuilder(httpService.getDefaultDexRequest(), (n, v) -> true)
.uri(linkProperty.getValue())
.GET()
.build();
}

public static DexRequestOptions defaultOptions() throws URISyntaxException {
return new DexRequestOptions();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.lucasstarsz.fxdex.service;

public enum DexRequestType {
Default,
DexEntry,
DexList
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public interface DexService {

void loadPokedexesForMenu(ListProperty<Label> currentDex, MenuButton dexMenu, StringProperty currentDexDisplayedProperty)
throws IOException, InterruptedException;
throws IOException, InterruptedException, URISyntaxException;

void loadDefaultPokedex(ListProperty<Label> currentDex, StringProperty currentDexDisplayedProperty)
throws IOException, InterruptedException, URISyntaxException;
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/io/github/lucasstarsz/fxdex/service/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@
package io.github.lucasstarsz.fxdex.service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
* Defines the interface for handling and simplifying the creation of HTTP requests.
*
* @author Andrew Dey
*/
public interface HttpService {

/** {@returns A standard instance of a PokeApi get request, configured to get information about pokedexes.} */
HttpRequest getDefaultDexRequest();

HttpRequest buildDexRequest(int dexId) throws URISyntaxException;

HttpResponse<String> getString(HttpRequest request) throws IOException, InterruptedException;

HttpRequest buildDexEntryRequest(String currentDexEntry) throws URISyntaxException;
/** {@returns The {@link HttpClient client} instance used for this service.} */
HttpClient getClient();

/**
* Creates a PokeApi pokedex get request, using the specified ID to signify which pokedex to get.
*
* @param options The {@link DexRequestOptions options} used to define the parameters of the dex request.
* @return A {@link HttpResponse response} from the server regarding the get request used.
* @throws IOException
* @throws InterruptedException
*/
default HttpResponse<String> get(DexRequestOptions options) throws IOException, InterruptedException {
return getClient().send(options.buildGetRequest(this), HttpResponse.BodyHandlers.ofString());
}
}
Loading

0 comments on commit 6049876

Please sign in to comment.