http api/service: switch to okhttp
okhttp has websocket support and http/2 support
This commit is contained in:
@@ -36,9 +36,9 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.5.3</version>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>3.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
|
||||
@@ -27,6 +27,7 @@ package net.runelite.http.api;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import okhttp3.HttpUrl;
|
||||
import org.slf4j.Logger;
|
||||
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()
|
||||
|
||||
@@ -28,15 +28,12 @@ import com.google.gson.Gson;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -44,26 +41,26 @@ public class HiscoreClient
|
||||
{
|
||||
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();
|
||||
|
||||
public HiscoreResult lookup(String username) throws IOException, URISyntaxException
|
||||
{
|
||||
URIBuilder builder = new URIBuilder(URL)
|
||||
.addParameter("username", username);
|
||||
HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
|
||||
.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();
|
||||
CloseableHttpResponse response = client.execute(request))
|
||||
{
|
||||
InputStream in = response.getEntity().getContent();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
InputStream in = response.body().byteStream();
|
||||
return gson.fromJson(new InputStreamReader(in), HiscoreResult.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,35 +26,27 @@ package net.runelite.http.api.xtea;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import net.runelite.http.api.RuneliteAPI;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class XteaClient
|
||||
{
|
||||
private static final MediaType JSON = MediaType.parse("application/json");
|
||||
|
||||
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();
|
||||
|
||||
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.setRevision(revision);
|
||||
|
||||
@@ -66,13 +58,18 @@ public class XteaClient
|
||||
|
||||
String json = gson.toJson(xteaRequest);
|
||||
|
||||
HttpPost request = new HttpPost(uri);
|
||||
request.setEntity(new StringEntity(json));
|
||||
HttpUrl.Builder builder = RuneliteAPI.getApiBase().newBuilder()
|
||||
.addPathSegment("xtea");
|
||||
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = client.execute(request))
|
||||
{
|
||||
logger.debug("Submitted XTEA key for region {}", region);
|
||||
}
|
||||
HttpUrl url = builder.build();
|
||||
|
||||
logger.debug("Built URI: {}", url);
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.post(RequestBody.create(JSON, json))
|
||||
.url(url)
|
||||
.build();
|
||||
|
||||
return client.newCall(request).execute();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +105,12 @@
|
||||
<version>1.7.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,25 +28,33 @@ import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||
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.CSVParser;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
|
||||
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
|
||||
{
|
||||
URIBuilder builder = new URIBuilder(RUNESCAPE_HISCORE_SERVICE)
|
||||
.addParameter("player", username);
|
||||
HttpUrl.Builder builder = url.newBuilder()
|
||||
.addQueryParameter("player", username);
|
||||
|
||||
String csv = client.get(builder.build());
|
||||
CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT);
|
||||
Request request = new Request.Builder()
|
||||
.url(builder.build())
|
||||
.build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
CSVParser parser = CSVParser.parse(response.body().string(), CSVFormat.DEFAULT);
|
||||
|
||||
HiscoreResultBuilder hiscoreBuilder = new HiscoreResultBuilder();
|
||||
hiscoreBuilder.setPlayer(username);
|
||||
@@ -72,13 +80,13 @@ public class HiscoreService
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,27 +25,36 @@
|
||||
package net.runelite.http.service.worlds;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.runelite.http.api.worlds.World;
|
||||
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
|
||||
{
|
||||
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
|
||||
{
|
||||
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<>();
|
||||
ByteBuffer buf = ByteBuffer.wrap(response);
|
||||
ByteBuffer buf = ByteBuffer.wrap(b);
|
||||
|
||||
int length = buf.getInt();
|
||||
buf.limit(length + 4);
|
||||
@@ -90,13 +99,13 @@ public class WorldsService
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,14 @@
|
||||
*/
|
||||
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.service.HttpClient;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Matchers;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class HiscoreServiceTest
|
||||
{
|
||||
@@ -69,15 +69,27 @@ public class HiscoreServiceTest
|
||||
+ "-1,-1\n"
|
||||
+ "-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
|
||||
public void testLookup() throws Exception
|
||||
{
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
when(client.get(Matchers.any(URI.class)))
|
||||
.thenReturn(RESPONSE);
|
||||
|
||||
HiscoreService hiscores = new HiscoreService();
|
||||
hiscores.setClient(client);
|
||||
hiscores.setUrl(server.url("/"));
|
||||
|
||||
HiscoreResult result = hiscores.lookup("zezima");
|
||||
|
||||
|
||||
@@ -26,37 +26,45 @@ package net.runelite.http.service.worlds;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
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.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Matchers;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import spark.utils.IOUtils;
|
||||
|
||||
public class WorldsServiceTest
|
||||
{
|
||||
private byte[] worldData;
|
||||
private final MockWebServer server = new MockWebServer();
|
||||
|
||||
@Before
|
||||
public void before() throws IOException
|
||||
{
|
||||
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
|
||||
public void testListWorlds() throws Exception
|
||||
{
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
when(client.getBytes(Matchers.any(URI.class)))
|
||||
.thenReturn(worldData);
|
||||
|
||||
WorldsService worlds = new WorldsService();
|
||||
worlds.setClient(client);
|
||||
worlds.setUrl(server.url("/"));
|
||||
|
||||
WorldResult worldResult = worlds.listWorlds();
|
||||
Assert.assertEquals(82, worldResult.getWorlds().size());
|
||||
|
||||
@@ -26,17 +26,15 @@ package net.runelite.client.plugins.xtea;
|
||||
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.logging.Level;
|
||||
import net.runelite.api.Client;
|
||||
import net.runelite.client.RuneLite;
|
||||
import net.runelite.client.events.MapRegionChanged;
|
||||
import net.runelite.client.plugins.Plugin;
|
||||
import net.runelite.client.ui.overlay.Overlay;
|
||||
import net.runelite.http.api.xtea.XteaClient;
|
||||
import okhttp3.Response;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -94,7 +92,7 @@ public class Xtea extends Plugin
|
||||
{
|
||||
xteaClient.submit(revision, region, keys);
|
||||
}
|
||||
catch (URISyntaxException | IOException ex)
|
||||
catch (IOException ex)
|
||||
{
|
||||
logger.debug("unable to submit xtea keys", ex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user