http-api: Use observables for ItemClient

This commit is contained in:
Owain van Brakel
2019-07-19 04:51:40 +02:00
parent d2e99e23eb
commit 241fbd136c
2 changed files with 107 additions and 93 deletions

View File

@@ -26,6 +26,7 @@ package net.runelite.http.api.item;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import io.reactivex.Observable;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
@@ -112,7 +113,7 @@ public class ItemClient
}
}
public BufferedImage getIcon(int itemId) throws IOException
public Observable<BufferedImage> getIcon(int itemId)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("item")
@@ -126,6 +127,8 @@ public class ItemClient
.url(url)
.build();
return Observable.defer(() ->
{
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
if (!response.isSuccessful())
@@ -137,12 +140,13 @@ public class ItemClient
InputStream in = response.body().byteStream();
synchronized (ImageIO.class)
{
return ImageIO.read(in);
return Observable.just(ImageIO.read(in));
}
}
});
}
public SearchResult search(String itemName) throws IOException
public Observable<SearchResult> search(String itemName)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("item")
@@ -152,6 +156,8 @@ public class ItemClient
logger.debug("Built URI: {}", url);
return Observable.defer(() ->
{
Request request = new Request.Builder()
.url(url)
.build();
@@ -165,15 +171,16 @@ public class ItemClient
}
InputStream in = response.body().byteStream();
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), SearchResult.class);
return Observable.just(RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), SearchResult.class));
}
catch (JsonParseException ex)
{
throw new IOException(ex);
return Observable.error(ex);
}
});
}
public ItemPrice[] getPrices() throws IOException
public Observable<ItemPrice[]> getPrices()
{
HttpUrl.Builder urlBuilder = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("item")
@@ -183,6 +190,9 @@ public class ItemClient
logger.debug("Built URI: {}", url);
return Observable.defer(() ->
{
Request request = new Request.Builder()
.url(url)
.build();
@@ -196,15 +206,16 @@ public class ItemClient
}
InputStream in = response.body().byteStream();
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice[].class);
return Observable.just(RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), ItemPrice[].class));
}
catch (JsonParseException ex)
{
throw new IOException(ex);
return Observable.error(ex);
}
});
}
public Map<Integer, ItemStats> getStats() throws IOException
public Observable<Map<Integer, ItemStats>> getStats()
{
HttpUrl.Builder urlBuilder = RuneLiteAPI.getStaticBase().newBuilder()
.addPathSegment("item")
@@ -215,6 +226,9 @@ public class ItemClient
logger.debug("Built URI: {}", url);
return Observable.defer(() ->
{
Request request = new Request.Builder()
.url(url)
.build();
@@ -231,11 +245,12 @@ public class ItemClient
final Type typeToken = new TypeToken<Map<Integer, ItemStats>>()
{
}.getType();
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), typeToken);
return Observable.just(RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), typeToken));
}
catch (JsonParseException ex)
{
throw new IOException(ex);
return Observable.error(ex);
}
});
}
}

View File

@@ -30,9 +30,9 @@ import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.reactivex.schedulers.Schedulers;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
@@ -209,9 +209,11 @@ public class ItemManager
private void loadPrices()
{
try
itemClient.getPrices()
.subscribeOn(Schedulers.io())
.subscribe(
(prices) ->
{
ItemPrice[] prices = itemClient.getPrices();
if (prices != null)
{
ImmutableMap.Builder<Integer, ItemPrice> map = ImmutableMap.builderWithExpectedSize(prices.length);
@@ -223,29 +225,26 @@ public class ItemManager
}
log.debug("Loaded {} prices", itemPrices.size());
}
catch (IOException e)
{
log.warn("error loading prices!", e);
}
},
(e) -> log.warn("error loading prices!", e)
);
}
private void loadStats()
{
try
{
final Map<Integer, ItemStats> stats = itemClient.getStats();
itemClient.getStats()
.subscribeOn(Schedulers.io())
.subscribe(
(stats) -> {
if (stats != null)
{
itemStats = ImmutableMap.copyOf(stats);
}
log.debug("Loaded {} stats", itemStats.size());
}
catch (IOException e)
{
log.warn("error loading stats!", e);
}
},
(e) -> log.warn("error loading stats!", e)
);
}
private void onGameStateChanged(final GameStateChanged event)