http api/service: switch to okhttp

okhttp has websocket support and http/2 support
This commit is contained in:
Adam
2017-05-01 19:30:56 -04:00
parent 9e1ad9362f
commit 1ffb146474
11 changed files with 134 additions and 156 deletions

View File

@@ -36,9 +36,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>httpclient</artifactId> <artifactId>okhttp</artifactId>
<version>4.5.3</version> <version>3.7.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>

View File

@@ -27,6 +27,7 @@ package net.runelite.http.api;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import okhttp3.HttpUrl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -55,9 +56,9 @@ public class RuneliteAPI
} }
} }
public static String getApiBase() public static HttpUrl getApiBase()
{ {
return BASE + getVersion(); return HttpUrl.parse(BASE + getVersion());
} }
public static String getVersion() public static String getVersion()

View File

@@ -28,15 +28,12 @@ import com.google.gson.Gson;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import net.runelite.http.api.RuneliteAPI; import net.runelite.http.api.RuneliteAPI;
import org.apache.http.client.methods.CloseableHttpResponse; import okhttp3.HttpUrl;
import org.apache.http.client.methods.HttpGet; import okhttp3.OkHttpClient;
import org.apache.http.client.methods.HttpUriRequest; import okhttp3.Request;
import org.apache.http.client.utils.URIBuilder; import okhttp3.Response;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -44,26 +41,26 @@ public class HiscoreClient
{ {
private static final Logger logger = LoggerFactory.getLogger(HiscoreClient.class); private static final Logger logger = LoggerFactory.getLogger(HiscoreClient.class);
private static final String URL = RuneliteAPI.getApiBase() + "/hiscore"; private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson(); private final Gson gson = new Gson();
public HiscoreResult lookup(String username) throws IOException, URISyntaxException public HiscoreResult lookup(String username) throws IOException, URISyntaxException
{ {
URIBuilder builder = new URIBuilder(URL) HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
.addParameter("username", username); .addPathSegment("hiscore")
.addQueryParameter("username", username);
URI uri = builder.build(); HttpUrl url = builder.build();
logger.debug("Built URI: {}", uri); logger.debug("Built URI: {}", url);
HttpUriRequest request = new HttpGet(uri); Request request = new Request.Builder()
.url(url)
.build();
try (CloseableHttpClient client = HttpClients.createDefault(); Response response = client.newCall(request).execute();
CloseableHttpResponse response = client.execute(request))
{ InputStream in = response.body().byteStream();
InputStream in = response.getEntity().getContent(); return gson.fromJson(new InputStreamReader(in), HiscoreResult.class);
return gson.fromJson(new InputStreamReader(in), HiscoreResult.class);
}
} }
} }

View File

@@ -26,35 +26,27 @@ package net.runelite.http.api.xtea;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import net.runelite.http.api.RuneliteAPI; import net.runelite.http.api.RuneliteAPI;
import org.apache.http.client.methods.CloseableHttpResponse; import okhttp3.HttpUrl;
import org.apache.http.client.methods.HttpPost; import okhttp3.MediaType;
import org.apache.http.client.utils.URIBuilder; import okhttp3.OkHttpClient;
import org.apache.http.entity.StringEntity; import okhttp3.Request;
import org.apache.http.impl.client.CloseableHttpClient; import okhttp3.RequestBody;
import org.apache.http.impl.client.HttpClients; import okhttp3.Response;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class XteaClient public class XteaClient
{ {
private static final MediaType JSON = MediaType.parse("application/json");
private static final Logger logger = LoggerFactory.getLogger(XteaClient.class); private static final Logger logger = LoggerFactory.getLogger(XteaClient.class);
private static final String URL = RuneliteAPI.getApiBase() + "/xtea"; private final OkHttpClient client = new OkHttpClient();
private final Gson gson = new Gson(); private final Gson gson = new Gson();
public void submit(int revision, int region, int[] keys) throws URISyntaxException, UnsupportedEncodingException, IOException public Response submit(int revision, int region, int[] keys) throws IOException
{ {
URIBuilder builder = new URIBuilder(URL);
URI uri = builder.build();
logger.debug("Built URI: {}", uri);
XteaRequest xteaRequest = new XteaRequest(); XteaRequest xteaRequest = new XteaRequest();
xteaRequest.setRevision(revision); xteaRequest.setRevision(revision);
@@ -66,13 +58,18 @@ public class XteaClient
String json = gson.toJson(xteaRequest); String json = gson.toJson(xteaRequest);
HttpPost request = new HttpPost(uri); HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
request.setEntity(new StringEntity(json)); .addPathSegment("xtea");
try (CloseableHttpClient client = HttpClients.createDefault(); HttpUrl url = builder.build();
CloseableHttpResponse response = client.execute(request))
{ logger.debug("Built URI: {}", url);
logger.debug("Submitted XTEA key for region {}", region);
} Request request = new Request.Builder()
.post(RequestBody.create(JSON, json))
.url(url)
.build();
return client.newCall(request).execute();
} }
} }

View File

@@ -105,6 +105,12 @@
<version>1.7.12</version> <version>1.7.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>3.7.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -1,58 +0,0 @@
/*
* Copyright (c) 2017, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service;
import java.io.IOException;
import java.net.URI;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClient
{
public String get(URI uri) throws IOException
{
HttpGet request = new HttpGet(uri);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request))
{
return EntityUtils.toString(response.getEntity());
}
}
public byte[] getBytes(URI uri) throws IOException
{
HttpGet request = new HttpGet(uri);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request))
{
return EntityUtils.toByteArray(response.getEntity());
}
}
}

View File

@@ -28,25 +28,33 @@ import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
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 net.runelite.http.service.HttpClient; import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
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 org.apache.http.client.utils.URIBuilder;
public class HiscoreService public class HiscoreService
{ {
private static final String RUNESCAPE_HISCORE_SERVICE = "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws"; private static final HttpUrl RUNESCAPE_HISCORE_SERVICE = HttpUrl.parse("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws");
private HttpClient client = new HttpClient(); private final OkHttpClient client = new OkHttpClient();
private HttpUrl url = RUNESCAPE_HISCORE_SERVICE;
public HiscoreResult lookup(String username) throws IOException, URISyntaxException public HiscoreResult lookup(String username) throws IOException, URISyntaxException
{ {
URIBuilder builder = new URIBuilder(RUNESCAPE_HISCORE_SERVICE) HttpUrl.Builder builder = url.newBuilder()
.addParameter("player", username); .addQueryParameter("player", username);
String csv = client.get(builder.build()); Request request = new Request.Builder()
CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT); .url(builder.build())
.build();
Response response = client.newCall(request).execute();
CSVParser parser = CSVParser.parse(response.body().string(), CSVFormat.DEFAULT);
HiscoreResultBuilder hiscoreBuilder = new HiscoreResultBuilder(); HiscoreResultBuilder hiscoreBuilder = new HiscoreResultBuilder();
hiscoreBuilder.setPlayer(username); hiscoreBuilder.setPlayer(username);
@@ -72,13 +80,13 @@ public class HiscoreService
return hiscoreBuilder.build(); return hiscoreBuilder.build();
} }
public HttpClient getClient() public HttpUrl getUrl()
{ {
return client; return url;
} }
public void setClient(HttpClient client) public void setUrl(HttpUrl url)
{ {
this.client = client; this.url = url;
} }
} }

View File

@@ -25,27 +25,36 @@
package net.runelite.http.service.worlds; package net.runelite.http.service.worlds;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException; 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;
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 net.runelite.http.service.HttpClient; import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class WorldsService public class WorldsService
{ {
private static final String WORLD_URL = "http://www.runescape.com/g=oldscape/slr.ws?order=LPWM"; private static final HttpUrl WORLD_URL = HttpUrl.parse("http://www.runescape.com/g=oldscape/slr.ws?order=LPWM");
private HttpClient client = new HttpClient(); private final OkHttpClient client = new OkHttpClient();
private HttpUrl url = WORLD_URL;
public WorldResult listWorlds() throws IOException, URISyntaxException public WorldResult listWorlds() throws IOException, URISyntaxException
{ {
byte[] response = client.getBytes(new URI(WORLD_URL)); Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
byte[] b = response.body().bytes();
List<World> worlds = new ArrayList<>(); List<World> worlds = new ArrayList<>();
ByteBuffer buf = ByteBuffer.wrap(response); ByteBuffer buf = ByteBuffer.wrap(b);
int length = buf.getInt(); int length = buf.getInt();
buf.limit(length + 4); buf.limit(length + 4);
@@ -90,13 +99,13 @@ public class WorldsService
return sb.toString(); return sb.toString();
} }
public HttpClient getClient() public HttpUrl getUrl()
{ {
return client; return url;
} }
public void setClient(HttpClient client) public void setUrl(HttpUrl url)
{ {
this.client = client; this.url = url;
} }
} }

View File

@@ -24,14 +24,14 @@
*/ */
package net.runelite.http.service.hiscore; package net.runelite.http.service.hiscore;
import java.net.URI; import java.io.IOException;
import net.runelite.http.api.hiscore.HiscoreResult; import net.runelite.http.api.hiscore.HiscoreResult;
import net.runelite.http.service.HttpClient; import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Matchers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class HiscoreServiceTest public class HiscoreServiceTest
{ {
@@ -69,15 +69,27 @@ public class HiscoreServiceTest
+ "-1,-1\n" + "-1,-1\n"
+ "-1,-1"; + "-1,-1";
private final MockWebServer server = new MockWebServer();
@Before
public void before() throws IOException
{
server.enqueue(new MockResponse().setBody(RESPONSE));
server.start();
}
@After
public void after() throws IOException
{
server.shutdown();
}
@Test @Test
public void testLookup() throws Exception public void testLookup() throws Exception
{ {
HttpClient client = mock(HttpClient.class);
when(client.get(Matchers.any(URI.class)))
.thenReturn(RESPONSE);
HiscoreService hiscores = new HiscoreService(); HiscoreService hiscores = new HiscoreService();
hiscores.setClient(client); hiscores.setUrl(server.url("/"));
HiscoreResult result = hiscores.lookup("zezima"); HiscoreResult result = hiscores.lookup("zezima");

View File

@@ -26,37 +26,45 @@ package net.runelite.http.service.worlds;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import net.runelite.http.api.worlds.WorldResult; import net.runelite.http.api.worlds.WorldResult;
import net.runelite.http.service.HttpClient; import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okio.Buffer;
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 org.mockito.Matchers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import spark.utils.IOUtils; import spark.utils.IOUtils;
public class WorldsServiceTest public class WorldsServiceTest
{ {
private byte[] worldData; private final MockWebServer server = new MockWebServer();
@Before @Before
public void before() throws IOException public void before() throws IOException
{ {
InputStream in = WorldsServiceTest.class.getResourceAsStream("worldlist"); InputStream in = WorldsServiceTest.class.getResourceAsStream("worldlist");
worldData = IOUtils.toByteArray(in); byte[] worldData = IOUtils.toByteArray(in);
Buffer buffer = new Buffer();
buffer.write(worldData);
server.enqueue(new MockResponse().setBody(buffer));
server.start();
}
@After
public void after() throws IOException
{
server.shutdown();
} }
@Test @Test
public void testListWorlds() throws Exception public void testListWorlds() throws Exception
{ {
HttpClient client = mock(HttpClient.class);
when(client.getBytes(Matchers.any(URI.class)))
.thenReturn(worldData);
WorldsService worlds = new WorldsService(); WorldsService worlds = new WorldsService();
worlds.setClient(client); worlds.setUrl(server.url("/"));
WorldResult worldResult = worlds.listWorlds(); WorldResult worldResult = worlds.listWorlds();
Assert.assertEquals(82, worldResult.getWorlds().size()); Assert.assertEquals(82, worldResult.getWorlds().size());

View File

@@ -26,17 +26,15 @@ package net.runelite.client.plugins.xtea;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.client.RuneLite; import net.runelite.client.RuneLite;
import net.runelite.client.events.MapRegionChanged; import net.runelite.client.events.MapRegionChanged;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.http.api.xtea.XteaClient; import net.runelite.http.api.xtea.XteaClient;
import okhttp3.Response;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -94,7 +92,7 @@ public class Xtea extends Plugin
{ {
xteaClient.submit(revision, region, keys); xteaClient.submit(revision, region, keys);
} }
catch (URISyntaxException | IOException ex) catch (IOException ex)
{ {
logger.debug("unable to submit xtea keys", ex); logger.debug("unable to submit xtea keys", ex);
} }