http-service: set content-type to application/json in various responses
This commit is contained in:
@@ -86,8 +86,8 @@ public class Service implements SparkApplication
|
|||||||
|
|
||||||
get("/version", (request, response) -> RuneliteAPI.getVersion());
|
get("/version", (request, response) -> RuneliteAPI.getVersion());
|
||||||
get("/update-check", updateCheck::check, transformer);
|
get("/update-check", updateCheck::check, transformer);
|
||||||
get("/hiscore", (request, response) -> hiscores.lookup(request.queryParams("username")), transformer);
|
get("/hiscore", hiscores::lookup, transformer);
|
||||||
get("/worlds", (request, response) -> worlds.listWorlds(), transformer);
|
get("/worlds", worlds::listWorlds, transformer);
|
||||||
post("/xtea", xtea::submit);
|
post("/xtea", xtea::submit);
|
||||||
get("/xtea/:rev", xtea::get, transformer);
|
get("/xtea/:rev", xtea::get, transformer);
|
||||||
path("/account", () ->
|
path("/account", () ->
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ public class AccountService
|
|||||||
lr.setOauthUrl(authorizationUrl);
|
lr.setOauthUrl(authorizationUrl);
|
||||||
lr.setUid(uuid);
|
lr.setUid(uuid);
|
||||||
|
|
||||||
|
response.type("application/json");
|
||||||
return lr;
|
return lr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ public class ConfigService
|
|||||||
.executeAndFetch(ConfigEntry.class);
|
.executeAndFetch(ConfigEntry.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.type("application/json");
|
||||||
return new Configuration(config);
|
return new Configuration(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,17 +25,16 @@
|
|||||||
package net.runelite.http.service.hiscore;
|
package net.runelite.http.service.hiscore;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import net.runelite.http.api.RuneliteAPI;
|
import net.runelite.http.api.RuneliteAPI;
|
||||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||||
import net.runelite.http.api.hiscore.Skill;
|
import net.runelite.http.api.hiscore.Skill;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
import org.apache.commons.csv.CSVFormat;
|
import org.apache.commons.csv.CSVFormat;
|
||||||
import org.apache.commons.csv.CSVParser;
|
import org.apache.commons.csv.CSVParser;
|
||||||
import org.apache.commons.csv.CSVRecord;
|
import org.apache.commons.csv.CSVRecord;
|
||||||
|
import spark.Request;
|
||||||
|
import spark.Response;
|
||||||
|
|
||||||
public class HiscoreService
|
public class HiscoreService
|
||||||
{
|
{
|
||||||
@@ -43,20 +42,22 @@ public class HiscoreService
|
|||||||
|
|
||||||
private HttpUrl url = RUNESCAPE_HISCORE_SERVICE;
|
private HttpUrl url = RUNESCAPE_HISCORE_SERVICE;
|
||||||
|
|
||||||
public HiscoreResult lookup(String username) throws IOException, URISyntaxException
|
public HiscoreResult lookup(Request request, Response response) throws IOException
|
||||||
{
|
{
|
||||||
|
String username = request.queryParams("username");
|
||||||
|
|
||||||
HttpUrl hiscoreUrl = url.newBuilder()
|
HttpUrl hiscoreUrl = url.newBuilder()
|
||||||
.addQueryParameter("player", username)
|
.addQueryParameter("player", username)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
okhttp3.Request okrequest = new okhttp3.Request.Builder()
|
||||||
.url(hiscoreUrl)
|
.url(hiscoreUrl)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
okhttp3.Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
||||||
String responseStr;
|
String responseStr;
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
try (ResponseBody body = okresponse.body())
|
||||||
{
|
{
|
||||||
responseStr = body.string();
|
responseStr = body.string();
|
||||||
}
|
}
|
||||||
@@ -84,6 +85,7 @@ public class HiscoreService
|
|||||||
hiscoreBuilder.setNextSkill(skill);
|
hiscoreBuilder.setNextSkill(skill);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.type("application/json");
|
||||||
return hiscoreBuilder.build();
|
return hiscoreBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ public class ItemService
|
|||||||
ItemEntry item = get(itemId);
|
ItemEntry item = get(itemId);
|
||||||
if (item != null)
|
if (item != null)
|
||||||
{
|
{
|
||||||
|
response.type("application/json");
|
||||||
response.header(RUNELITE_ITEM_CACHE, "HIT");
|
response.header(RUNELITE_ITEM_CACHE, "HIT");
|
||||||
return item.toItem();
|
return item.toItem();
|
||||||
}
|
}
|
||||||
@@ -92,6 +93,7 @@ public class ItemService
|
|||||||
item = fetch(itemId);
|
item = fetch(itemId);
|
||||||
if (item != null)
|
if (item != null)
|
||||||
{
|
{
|
||||||
|
response.type("application/json");
|
||||||
response.header(RUNELITE_ITEM_CACHE, "MISS");
|
response.header(RUNELITE_ITEM_CACHE, "MISS");
|
||||||
return item.toItem();
|
return item.toItem();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ public class UpdateCheckService
|
|||||||
|
|
||||||
public Boolean check(Request request, Response response)
|
public Boolean check(Request request, Response response)
|
||||||
{
|
{
|
||||||
|
response.type("application/json");
|
||||||
return updateAvailable.get();
|
return updateAvailable.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +131,7 @@ public class UpdateCheckService
|
|||||||
int worldNumber = worlds.getWorlds().get(rand.nextInt(size)).getId();
|
int worldNumber = worlds.getWorlds().get(rand.nextInt(size)).getId();
|
||||||
return worldNumber - WORLD_OFFSET;
|
return worldNumber - WORLD_OFFSET;
|
||||||
}
|
}
|
||||||
catch (IOException | URISyntaxException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
logger.warn(null, ex);
|
logger.warn(null, ex);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
package net.runelite.http.service.worlds;
|
package net.runelite.http.service.worlds;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -33,9 +32,9 @@ import net.runelite.http.api.RuneliteAPI;
|
|||||||
import net.runelite.http.api.worlds.World;
|
import net.runelite.http.api.worlds.World;
|
||||||
import net.runelite.http.api.worlds.WorldResult;
|
import net.runelite.http.api.worlds.WorldResult;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import okhttp3.ResponseBody;
|
import okhttp3.ResponseBody;
|
||||||
|
import spark.Request;
|
||||||
|
import spark.Response;
|
||||||
|
|
||||||
public class WorldsService
|
public class WorldsService
|
||||||
{
|
{
|
||||||
@@ -43,16 +42,23 @@ public class WorldsService
|
|||||||
|
|
||||||
private HttpUrl url = WORLD_URL;
|
private HttpUrl url = WORLD_URL;
|
||||||
|
|
||||||
public WorldResult listWorlds() throws IOException, URISyntaxException
|
public WorldResult listWorlds(Request request, Response response) throws IOException
|
||||||
{
|
{
|
||||||
Request request = new Request.Builder()
|
WorldResult result = listWorlds();
|
||||||
|
response.type("application/json");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorldResult listWorlds() throws IOException
|
||||||
|
{
|
||||||
|
okhttp3.Request okrequest = new okhttp3.Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Response response = RuneliteAPI.CLIENT.newCall(request).execute();
|
okhttp3.Response okresponse = RuneliteAPI.CLIENT.newCall(okrequest).execute();
|
||||||
byte[] b;
|
byte[] b;
|
||||||
|
|
||||||
try (ResponseBody body = response.body())
|
try (ResponseBody body = okresponse.body())
|
||||||
{
|
{
|
||||||
b = body.bytes();
|
b = body.bytes();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,14 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Answers;
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.Matchers;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.sql2o.Sql2o;
|
import org.sql2o.Sql2o;
|
||||||
|
import spark.Request;
|
||||||
|
import spark.Response;
|
||||||
import spark.Spark;
|
import spark.Spark;
|
||||||
|
|
||||||
public class ServiceTest
|
public class ServiceTest
|
||||||
@@ -102,7 +105,7 @@ public class ServiceTest
|
|||||||
HiscoreResult result = new HiscoreResult();
|
HiscoreResult result = new HiscoreResult();
|
||||||
result.setAttack(new Skill(1, 99, 42));
|
result.setAttack(new Skill(1, 99, 42));
|
||||||
|
|
||||||
when(hiscoreService.lookup("zezima")).thenReturn(result);
|
when(hiscoreService.lookup(Matchers.any(Request.class), Matchers.any(Response.class))).thenReturn(result);
|
||||||
|
|
||||||
URL url = new URL(URL_BASE + "/hiscore?username=zezima");
|
URL url = new URL(URL_BASE + "/hiscore?username=zezima");
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ import org.junit.After;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import spark.Request;
|
||||||
|
import spark.Response;
|
||||||
|
|
||||||
public class HiscoreServiceTest
|
public class HiscoreServiceTest
|
||||||
{
|
{
|
||||||
@@ -91,7 +94,10 @@ public class HiscoreServiceTest
|
|||||||
HiscoreService hiscores = new HiscoreService();
|
HiscoreService hiscores = new HiscoreService();
|
||||||
hiscores.setUrl(server.url("/"));
|
hiscores.setUrl(server.url("/"));
|
||||||
|
|
||||||
HiscoreResult result = hiscores.lookup("zezima");
|
Request request = mock(Request.class);
|
||||||
|
Response response = mock(Response.class);
|
||||||
|
|
||||||
|
HiscoreResult result = hiscores.lookup(request, response);
|
||||||
|
|
||||||
Assert.assertEquals(50, result.getAttack().getLevel());
|
Assert.assertEquals(50, result.getAttack().getLevel());
|
||||||
Assert.assertEquals(159727L, result.getFishing().getExperience());
|
Assert.assertEquals(159727L, result.getFishing().getExperience());
|
||||||
|
|||||||
Reference in New Issue
Block a user