http-api: Use observables for world api

This commit is contained in:
Owain van Brakel
2019-07-19 05:00:39 +02:00
parent 60d734549f
commit 1cddf12a2c
3 changed files with 53 additions and 48 deletions

View File

@@ -61,7 +61,7 @@ public class WorldClient
if (!response.isSuccessful()) if (!response.isSuccessful())
{ {
logger.debug("Error looking up worlds: {}", response); logger.debug("Error looking up worlds: {}", response);
return null; return Observable.just(null);
} }
InputStream in = response.body().byteStream(); InputStream in = response.body().byteStream();
@@ -69,7 +69,7 @@ public class WorldClient
} }
catch (JsonParseException ex) catch (JsonParseException ex)
{ {
throw new IOException(ex); return Observable.error(ex);
} }
}); });
} }

View File

@@ -25,6 +25,7 @@
package net.runelite.client.plugins.defaultworld; package net.runelite.client.plugins.defaultworld;
import com.google.inject.Provides; import com.google.inject.Provides;
import io.reactivex.schedulers.Schedulers;
import java.io.IOException; import java.io.IOException;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@@ -32,6 +33,7 @@ import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.GameState; import net.runelite.api.GameState;
import net.runelite.api.events.GameStateChanged; import net.runelite.api.events.GameStateChanged;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.EventBus; import net.runelite.client.eventbus.EventBus;
import net.runelite.client.events.SessionOpen; import net.runelite.client.events.SessionOpen;
@@ -60,6 +62,9 @@ public class DefaultWorldPlugin extends Plugin
@Inject @Inject
private EventBus eventBus; private EventBus eventBus;
@Inject
private ClientThread clientThread;
private final WorldClient worldClient = new WorldClient(); private final WorldClient worldClient = new WorldClient();
private int worldCache; private int worldCache;
private boolean worldChangeRequired; private boolean worldChangeRequired;
@@ -122,10 +127,12 @@ public class DefaultWorldPlugin extends Plugin
return; return;
} }
try worldClient.lookupWorlds()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.from(clientThread))
.subscribe(
(worldResult) ->
{ {
final WorldResult worldResult = worldClient.lookupWorlds();
if (worldResult == null) if (worldResult == null)
{ {
return; return;
@@ -150,11 +157,9 @@ public class DefaultWorldPlugin extends Plugin
{ {
log.warn("World {} not found.", correctedWorld); log.warn("World {} not found.", correctedWorld);
} }
} },
catch (IOException e) (e) -> log.warn("Error looking up world {}. Error: {}", correctedWorld, e)
{ );
log.warn("Error looking up world {}. Error: {}", correctedWorld, e);
}
} }
private void applyWorld() private void applyWorld()

View File

@@ -29,6 +29,7 @@ import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ObjectArrays; import com.google.common.collect.ObjectArrays;
import com.google.inject.Provides; import com.google.inject.Provides;
import io.reactivex.schedulers.Schedulers;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.time.Duration; import java.time.Duration;
@@ -502,10 +503,11 @@ public class WorldHopperPlugin extends Plugin
{ {
log.debug("Fetching worlds"); log.debug("Fetching worlds");
try new WorldClient().lookupWorlds()
.subscribeOn(Schedulers.io())
.subscribe(
(worldResult) ->
{ {
WorldResult worldResult = new WorldClient().lookupWorlds();
if (worldResult != null) if (worldResult != null)
{ {
worldResult.getWorlds().sort(Comparator.comparingInt(World::getId)); worldResult.getWorlds().sort(Comparator.comparingInt(World::getId));
@@ -513,11 +515,9 @@ public class WorldHopperPlugin extends Plugin
this.lastFetch = Instant.now(); this.lastFetch = Instant.now();
updateList(); updateList();
} }
} },
catch (IOException ex) (ex) -> log.warn("Error looking up worlds", ex)
{ );
log.warn("Error looking up worlds", ex);
}
} }
/** /**