Skip to content

Commit

Permalink
replaced unirest by okhttp (jar size now 50% smaller)
Browse files Browse the repository at this point in the history
  • Loading branch information
BloodWorkXGaming committed Jun 30, 2018
1 parent a330980 commit 92e6f5f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
25 changes: 24 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ dependencies {

compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'com.mashape.unirest', name: 'unirest-java', version: '1.4.9'
compile 'com.squareup.okhttp3:okhttp:3.10.0'

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.4'

// https://mvnrepository.com/artifact/org.fusesource.jansi/jansi
Expand Down Expand Up @@ -93,4 +94,26 @@ static String getGithubKey(){
Properties props = new Properties()
props.load(new FileInputStream(new File('secrets.properties')))
return props['GITHUB_TOKEN']
}

task depsize {
group = "help"
doLast {
def size = 0
def formatStr = "%,10.2f"
configurations.default.collect { it.length() / (1024 * 1024) }.each { size += it }

def out = new StringBuffer()
out << 'Total dependencies size:'.padRight(45)
out << "${String.format(formatStr, size)} Mb\n\n"

configurations
.default
.sort { -it.length() }
.each {
out << "${it.name}".padRight(45)
out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
}
println(out)
}
}
3 changes: 2 additions & 1 deletion serverstarter.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Auto genereated file, DO NOT EDIT!
{forgeInstalled: true, forgeVersion: 14.23.3.2682, mcVersion: 1.12.2, packInstalled: true,
packUrl: 'https://minecraft.curseforge.com/projects/all-the-mods-3/files/2556240'}
packUrl: 'https://minecraft.curseforge.com/projects/all-the-mods-3/files/2556240',
spongeBootstrapper: NONE}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.URISyntaxException;
Expand All @@ -36,18 +35,22 @@
import static atm.bloodworkxgaming.serverstarter.ServerStarter.LOGGER;

public class CursePackType implements IPackType {
private final OkHttpClient httpClient;
private ConfigFile configFile;
private String basePath;
private String forgeVersion;
private String mcVersion;
private File oldFiles;


public CursePackType(ConfigFile configFile) {
this.configFile = configFile;
basePath = configFile.install.baseInstallPath;
forgeVersion = configFile.install.forgeVersion;
mcVersion = configFile.install.mcVersion;
oldFiles = new File(basePath + "OLD_TO_DELETE/");

httpClient = new OkHttpClient();
}

@Override
Expand Down Expand Up @@ -80,9 +83,17 @@ public void installPack() {

} else if (configFile.install.formatSpecific.containsKey("packid") && configFile.install.formatSpecific.containsKey("fileid")) {
try {
HttpResponse<JsonNode> res = Unirest.get("/api/v2/direct/GetAddOnFile/" + configFile.install.formatSpecific.get("packid") + "/" + configFile.install.formatSpecific.get("fileid")).asJson();
LOGGER.info("PackID request response: " + res);
} catch (UnirestException e) {
Request request = new Request.Builder()
.url("/api/v2/direct/GetAddOnFile/" + configFile.install.formatSpecific.get("packid") + "/" + configFile.install.formatSpecific.get("fileid"))
.build();
Response response = httpClient.newCall(request).execute();
ResponseBody body = response.body();
if (body != null) {
LOGGER.info("PackID request response: " + body.string());
} else {
LOGGER.info("PackID request response returned with a null body");
}
} catch (IOException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -284,23 +295,26 @@ private void downloadMods(List<ModEntryRaw> mods) {
LOGGER.info("Download url is: " + url, true);

try {
HttpResponse<JsonNode> res = Unirest
.get(url)
Request request = new Request.Builder()
.url(url)
.header("User-Agent", "All the mods server installer.")
.header("Content-Type", "application/json")
.asJson();
.build();
Response res = httpClient.newCall(request).execute();

if (res.getStatus() != 200)
throw new UnirestException("Response was not OK");
if (!res.isSuccessful())
throw new IOException("Request to " + url + " was not successful.");
ResponseBody body = res.body();
if (body == null)
throw new IOException("Request to " + url + " returned a null body.");

JsonObject jsonRes = new JsonParser().parse(res.getBody().toString()).getAsJsonObject();
JsonObject jsonRes = new JsonParser().parse(body.string()).getAsJsonObject();
LOGGER.info("Response from manifest query: " + jsonRes, true);

urls.add(jsonRes
.getAsJsonObject()
.getAsJsonPrimitive("DownloadURL").getAsString());

} catch (UnirestException e) {
} catch (IOException e) {
LOGGER.error("Error while trying to get URL from cursemeta for mod " + mod.projectID, e);
}
});
Expand Down

0 comments on commit 92e6f5f

Please sign in to comment.