enqueue most post/put/delete requests

Instead of using the global SingleThreadedExecutor for some requests,
use enqueue method of okhttp.
This should hopefully help with some plugins, and more specifically the
ui from freezing while the online api is down.
This commit is contained in:
Jeremy Plsek
2018-05-28 12:54:36 +01:00
parent 794ead0007
commit 025ed157f3
8 changed files with 117 additions and 101 deletions

View File

@@ -30,6 +30,8 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.UUID;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
@@ -75,7 +77,7 @@ public class ConfigClient
}
}
public void set(String key, String value) throws IOException
public void set(String key, String value)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("config")
@@ -90,13 +92,24 @@ public class ConfigClient
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
logger.debug("Set configuration value '{}' to '{}'", key, value);
}
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("Unable to synchronize configuration item", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Synchronized configuration value '{}' to '{}'", key, value);
}
});
}
public void unset(String key) throws IOException
public void unset(String key)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("config")
@@ -111,9 +124,20 @@ public class ConfigClient
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
logger.debug("Unset configuration value '{}'", key);
}
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("Unable to unset configuration item", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Unset configuration value '{}'", key);
}
});
}
}

View File

@@ -26,6 +26,8 @@ package net.runelite.http.api.examine;
import java.io.IOException;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
@@ -40,22 +42,22 @@ public class ExamineClient
private static final MediaType TEXT = MediaType.parse("text");
public void submitObject(int id, String text) throws IOException
public void submitObject(int id, String text)
{
submit("object", id, text);
}
public void submitNpc(int id, String text) throws IOException
public void submitNpc(int id, String text)
{
submit("npc", id, text);
}
public void submitItem(int id, String text) throws IOException
public void submitItem(int id, String text)
{
submit("item", id, text);
}
private void submit(String type, int id, String text) throws IOException
private void submit(String type, int id, String text)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("examine")
@@ -70,10 +72,20 @@ public class ExamineClient
.post(RequestBody.create(TEXT, text))
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
logger.debug("Submitted examine info for {} {}: {}",
type, id, text);
}
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("Error submitting examine", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Submitted examine info for {} {}: {}", type, id, text);
}
});
}
}

View File

@@ -26,12 +26,19 @@ package net.runelite.http.api.xp;
import java.io.IOException;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class XpClient
{
public void update(String username) throws IOException
private static final Logger logger = LoggerFactory.getLogger(XpClient.class);
public void update(String username)
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("xp")
@@ -43,6 +50,20 @@ public class XpClient
.url(url)
.build();
RuneLiteAPI.CLIENT.newCall(request).execute().close();
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("Error submitting xp track", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Submitted xp track for {}", username);
}
});
}
}

View File

@@ -31,6 +31,8 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
@@ -45,7 +47,7 @@ public class XteaClient
private static final Logger logger = LoggerFactory.getLogger(XteaClient.class);
public Response submit(int revision, int region, int[] keys) throws IOException
public void submit(int revision, int region, int[] keys)
{
XteaRequest xteaRequest = new XteaRequest();
xteaRequest.setRevision(revision);
@@ -69,7 +71,30 @@ public class XteaClient
.url(url)
.build();
return RuneLiteAPI.CLIENT.newCall(request).execute();
RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("unable to submit xtea keys", e);
}
@Override
public void onResponse(Call call, Response response)
{
try
{
if (!response.isSuccessful())
{
logger.debug("unsuccessful xtea response");
}
}
finally
{
response.close();
}
}
});
}
public List<XteaKey> get() throws IOException