Added !layout

Signed-off-by: James Munson <jmunson@openpoll.io>
This commit is contained in:
James Munson
2019-06-24 05:30:26 -07:00
parent cd77f9b513
commit 9ee4320482
4 changed files with 283 additions and 0 deletions

View File

@@ -27,6 +27,8 @@ package net.runelite.http.api.chat;
import com.google.gson.JsonParseException;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.io.InputStreamReader;
import net.runelite.http.api.RuneLiteAPI;
import okhttp3.HttpUrl;
@@ -36,6 +38,10 @@ import okhttp3.Response;
public class ChatClient
{
private static final Predicate<String> LAYOUT_VALIDATOR = Pattern
.compile("\\[[A-Z]+]:(\\s*\\w+\\s*(\\([A-Za-z]+\\))?,?)+")
.asPredicate();
public boolean submitKc(String username, String boss, int kc) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
@@ -258,4 +264,64 @@ public class ChatClient
return Integer.parseInt(response.body().string());
}
}
public boolean submitLayout(String username, String layout) throws IOException
{
if (!testLayout(layout))
{
throw new IOException("Layout " + layout + " is not valid!");
}
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
.addPathSegment("layout")
.addQueryParameter("name", username)
.addQueryParameter("layout", layout)
.build();
Request request = new Request.Builder()
.post(RequestBody.create(null, new byte[0]))
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
return response.isSuccessful();
}
}
public String getLayout(String username) throws IOException
{
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("chat")
.addPathSegment("layout")
.addQueryParameter("name", username)
.build();
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
{
if (!response.isSuccessful())
{
throw new IOException("Unable to look up layout!");
}
final String layout = response.body().string();
if (!testLayout(layout))
{
throw new IOException("Layout " + layout + " is not valid!");
}
return layout;
}
}
public boolean testLayout(String layout)
{
return LAYOUT_VALIDATOR.test(layout);
}
}