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

@@ -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 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;
}
}

View File

@@ -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;
}
}

View File

@@ -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");

View File

@@ -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());