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