Migrate from Jackson to Gson for JSON handling and update build configuration for token-based authentication.
This commit is contained in:
+10
-5
@@ -16,7 +16,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
|
implementation 'com.google.code.gson:gson:2.10.1'
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
@@ -28,10 +28,15 @@ publishing {
|
|||||||
repositories {
|
repositories {
|
||||||
maven {
|
maven {
|
||||||
name = 'gitea'
|
name = 'gitea'
|
||||||
url = 'https://git.catlet.de/api/packages/DEIN_USER/maven'
|
url = 'https://git.catlet.info/api/packages/StateMc/maven'
|
||||||
credentials {
|
credentials(HttpHeaderCredentials) {
|
||||||
username = findProperty('giteaUser')
|
name = 'Authorization'
|
||||||
password = findProperty('giteaPassword')
|
value = 'Basic ' + Base64.encoder.encodeToString(
|
||||||
|
"${findProperty('giteaUser')}:${findProperty('giteaToken')}".bytes
|
||||||
|
)
|
||||||
|
}
|
||||||
|
authentication {
|
||||||
|
header(HttpHeaderAuthentication)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
giteaUser=
|
giteaUser=
|
||||||
giteaPassword=
|
giteaToken=
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package de.catmangames.jsonfetcher;
|
package de.catmangames.jsonfetcher;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.google.gson.Gson;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
@@ -13,9 +13,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
|
|
||||||
public class JsonFetcher {
|
public class JsonFetcher {
|
||||||
|
|
||||||
|
private final Gson gson = new Gson();
|
||||||
|
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
private final HttpClient client;
|
private final HttpClient client;
|
||||||
private final ObjectMapper mapper;
|
|
||||||
private final long cacheTtlMs;
|
private final long cacheTtlMs;
|
||||||
|
|
||||||
private final Map<String, long[]> cacheTimestamps = new ConcurrentHashMap<>();
|
private final Map<String, long[]> cacheTimestamps = new ConcurrentHashMap<>();
|
||||||
@@ -26,16 +27,15 @@ public class JsonFetcher {
|
|||||||
this.client = HttpClient.newBuilder()
|
this.client = HttpClient.newBuilder()
|
||||||
.connectTimeout(Duration.ofSeconds(b.timeoutSec))
|
.connectTimeout(Duration.ofSeconds(b.timeoutSec))
|
||||||
.build();
|
.build();
|
||||||
this.mapper = new ObjectMapper();
|
|
||||||
this.cacheTtlMs = b.cacheTtlMs;
|
this.cacheTtlMs = b.cacheTtlMs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T fetch(String path, Class<T> type) throws IOException {
|
public <T> T fetch(String path, Class<T> type) throws IOException {
|
||||||
return mapper.readValue(getRaw(path), type);
|
return gson.fromJson(getRaw(path), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T fetch(String path, TypeReference<T> type) throws IOException {
|
public <T> T fetch(String path, TypeToken<T> type) throws IOException {
|
||||||
return mapper.readValue(getRaw(path), type);
|
return gson.fromJson(getRaw(path), type.getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getRaw(String path) throws IOException {
|
private String getRaw(String path) throws IOException {
|
||||||
|
|||||||
Reference in New Issue
Block a user