Skip to content

Commit 2140c2a

Browse files
committed
Move OkHttp to HttpUtil (part 1)
1 parent f80758d commit 2140c2a

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.maxgamer.quickshop.util;
2+
3+
import okhttp3.Cache;
4+
import okhttp3.OkHttpClient;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
10+
public class HttpUtil {
11+
private final OkHttpClient client = new OkHttpClient.Builder()
12+
.cache(new Cache(getCacheFolder(),50L * 1024L * 1024L)).build();
13+
14+
public static HttpUtil instance(){
15+
return new HttpUtil();
16+
}
17+
18+
private File getCacheFolder(){
19+
try {
20+
File file = Files.createTempDirectory("quickshop_okhttp_tmp").toFile();
21+
file.mkdirs();
22+
return file;
23+
} catch (IOException e) {
24+
File file = new File(Util.getCacheFolder(),"okhttp_tmp");
25+
file.mkdirs();
26+
return file;
27+
}
28+
}
29+
30+
public OkHttpClient getClient() {
31+
return client;
32+
}
33+
}

src/main/java/org/maxgamer/quickshop/util/language/text/distributions/crowdin/CrowdinOTA.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import com.google.gson.JsonParser;
77
import com.google.gson.JsonPrimitive;
88
import lombok.*;
9-
import okhttp3.OkHttpClient;
109
import okhttp3.Request;
1110
import okhttp3.Response;
1211
import org.apache.commons.codec.digest.DigestUtils;
1312
import org.bukkit.configuration.file.YamlConfiguration;
1413
import org.jetbrains.annotations.NotNull;
1514
import org.jetbrains.annotations.Nullable;
1615
import org.maxgamer.quickshop.QuickShop;
16+
import org.maxgamer.quickshop.util.HttpUtil;
1717
import org.maxgamer.quickshop.util.JsonUtil;
1818
import org.maxgamer.quickshop.util.Util;
1919
import org.maxgamer.quickshop.util.language.text.distributions.Distribution;
@@ -36,14 +36,10 @@ public class CrowdinOTA implements Distribution {
3636
.recordStats()
3737
.build();
3838
private final QuickShop plugin;
39-
private final OkHttpClient client;
4039

4140
public CrowdinOTA(QuickShop plugin) {
4241
this.plugin = plugin;
4342
Util.getCacheFolder().mkdirs();
44-
this.client = new OkHttpClient.Builder()
45-
.cache(new okhttp3.Cache(new File(Util.getCacheFolder(), "okhttp"), 50L * 1024L * 1024L))
46-
.build();
4743

4844
}
4945
/**
@@ -67,7 +63,7 @@ public String getManifestJson() {
6763
if (requestCachePool.getIfPresent(url) != null) {
6864
return requestCachePool.getIfPresent(url);
6965
}
70-
try (Response response = client.newCall(new Request.Builder().get().url(url).build()).execute()) {
66+
try (Response response = HttpUtil.instance().getClient().newCall(new Request.Builder().get().url(url).build()).execute()) {
7167
val body = response.body();
7268
if (body == null) {
7369
return null;
@@ -176,7 +172,7 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
176172
if (forceFlush || data == null || localeTimestamp != manifest.getTimestamp()) {
177173
String url = CROWDIN_OTA_HOST + "content" + fileCrowdinPath.replace("%locale%", crowdinLocale);
178174
Util.debugLog("Reading data from remote server: " + url);
179-
try (Response response = client.newCall(new Request.Builder().get().url(url).build()).execute()) {
175+
try (Response response = HttpUtil.instance().getClient().newCall(new Request.Builder().get().url(url).build()).execute()) {
180176
val body = response.body();
181177
if (body == null) {
182178
throw new OTAException(response.code(), ""); // Returns empty string (failed to getting content)

0 commit comments

Comments
 (0)