Adds a simple http client

This is intended to standardize how we do these request in the core.
It doesn't do much but it will sufice for now to be used
in the new localization system.
This commit is contained in:
Christian Koop 2022-09-29 21:52:59 +02:00
parent 2860dffb83
commit 2e15ed5d28
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
7 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package com.songoda.core.http;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
public interface HttpClient {
@NotNull HttpResponse get(String url) throws IOException;
}

View File

@ -0,0 +1,11 @@
package com.songoda.core.http;
import java.io.IOException;
public interface HttpResponse {
int getResponseCode() throws IOException;
byte[] getBody() throws IOException;
String getBodyAsString() throws IOException;
}

View File

@ -0,0 +1,54 @@
package com.songoda.core.http;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
public class HttpResponseImpl implements HttpResponse, AutoCloseable {
protected final HttpURLConnection connection;
protected byte[] body;
HttpResponseImpl(HttpURLConnection connection) throws IOException {
this.connection = connection;
this.connection.connect();
}
public int getResponseCode() throws IOException {
int statusCode = this.connection.getResponseCode();
if (statusCode == -1) {
throw new IOException("HTTP Status Code is -1");
}
return statusCode;
}
public byte[] getBody() throws IOException {
if (this.body == null) {
try (InputStream in = this.connection.getInputStream();
InputStream err = this.connection.getErrorStream()) {
if (err != null) {
this.body = IOUtils.toByteArray(err);
} else {
this.body = IOUtils.toByteArray(in);
}
}
}
return this.body;
}
public String getBodyAsString() throws IOException {
return new String(getBody(), StandardCharsets.UTF_8);
}
@Override
public void close() throws Exception {
this.connection.disconnect();
}
}

View File

@ -0,0 +1,21 @@
package com.songoda.core.http;
import com.songoda.core.SongodaCore;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class SimpleHttpClient implements HttpClient {
public @NotNull HttpResponse get(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "SongodaCore/" + SongodaCore.getVersion() + " (+https://github.com/songoda/SongodaCore)");
return new HttpResponseImpl(connection);
}
}

View File

@ -0,0 +1,15 @@
package com.songoda.core.http;
import java.io.IOException;
public class UnexpectedHttpStatusException extends IOException {
public final int responseCode;
public final String url;
public UnexpectedHttpStatusException(int responseCode, String url) {
super("Got HTTP Status Code " + responseCode + ": " + url);
this.responseCode = responseCode;
this.url = url;
}
}

View File

@ -0,0 +1,22 @@
package com.songoda.core.http;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedList;
import java.util.List;
public class MockHttpClient implements HttpClient {
public HttpResponse returnValue;
public List<String> callsOnGet = new LinkedList<>();
public MockHttpClient(HttpResponse returnValue) {
this.returnValue = returnValue;
}
@Override
public @NotNull HttpResponse get(String url) {
this.callsOnGet.add(url);
return this.returnValue;
}
}

View File

@ -0,0 +1,25 @@
package com.songoda.core.http;
import java.nio.charset.StandardCharsets;
public class MockHttpResponse implements HttpResponse {
public int responseCode;
public byte[] body;
public MockHttpResponse(int responseCode, byte[] body) {
this.responseCode = responseCode;
this.body = body;
}
public int getResponseCode() {
return this.responseCode;
}
public byte[] getBody() {
return this.body;
}
public String getBodyAsString() {
return new String(getBody(), StandardCharsets.UTF_8);
}
}