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();
|
||||
return gson.fromJson(new InputStreamReader(in), HiscoreResult.class);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user