diff --git a/pom.xml b/pom.xml
index b9bf2f8..74cb421 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.github.franckyi
cmpdl
- 2.2.0
+ 2.3.0
1.8
1.8
@@ -15,17 +15,43 @@
org.json
json
- 20180130
+ 20180813
- com.squareup.okhttp3
- okhttp
- 3.10.0
+ com.squareup.retrofit2
+ retrofit
+ 2.6.0
org.jsoup
jsoup
- 1.11.2
+ 1.12.1
+
+
+
+ maven-assembly-plugin
+
+
+
+ com.github.franckyi.cmpdl.CMPDL
+
+
+
+ jar-with-dependencies
+
+
+
+
+ make-assembly
+ package
+
+ single
+
+
+
+
+
+
diff --git a/src/main/java/com/github/franckyi/cmpdl/CMPDL.java b/src/main/java/com/github/franckyi/cmpdl/CMPDL.java
index 68d0184..0581a3a 100644
--- a/src/main/java/com/github/franckyi/cmpdl/CMPDL.java
+++ b/src/main/java/com/github/franckyi/cmpdl/CMPDL.java
@@ -1,5 +1,7 @@
package com.github.franckyi.cmpdl;
+import com.github.franckyi.cmpdl.api.CMPDLConverterFactory;
+import com.github.franckyi.cmpdl.api.TwitchAppAPI;
import com.github.franckyi.cmpdl.controller.*;
import com.github.franckyi.cmpdl.core.ContentControllerView;
import com.github.franckyi.cmpdl.core.ControllerView;
@@ -9,6 +11,7 @@
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
+import retrofit2.Retrofit;
import java.awt.*;
import java.io.IOException;
@@ -20,16 +23,14 @@
public class CMPDL extends Application {
public static final String NAME = "CMPDL";
- public static final String VERSION = "2.2.0";
+ public static final String VERSION = "2.3.0";
public static final String AUTHOR = "Franckyi";
public static final String TITLE = String.format("%s v%s by %s", NAME, VERSION, AUTHOR);
- // As per the "Required reading" section on https://staging_cursemeta.dries007.net/docs
- public static final String USER_AGENT = String.format("%s/%s (%s)", NAME, AUTHOR, VERSION);
-
public static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();
public static Stage stage;
+ public static TwitchAppAPI api;
public static ControllerView mainWindow;
public static ContentControllerView modpackPane;
@@ -39,6 +40,15 @@ public class CMPDL extends Application {
public static ContentControllerView> currentContent;
+ public static void main(String[] args) {
+ api = new Retrofit.Builder()
+ .baseUrl("https://addons-ecs.forgesvc.net/api/v2/")
+ .addConverterFactory(CMPDLConverterFactory.create())
+ .build()
+ .create(TwitchAppAPI.class);
+ launch(args);
+ }
+
@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
@@ -53,15 +63,15 @@ public void start(Stage primaryStage) throws Exception {
stage.show();
}
- public static void main(String[] args) {
- launch(args);
- }
-
@Override
public void stop() {
EXECUTOR_SERVICE.shutdown();
}
+ public static TwitchAppAPI getAPI() {
+ return api;
+ }
+
public static void openBrowser(String url) {
if (Desktop.isDesktopSupported()) {
EXECUTOR_SERVICE.execute(() -> {
diff --git a/src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java b/src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java
deleted file mode 100644
index dc5413c..0000000
--- a/src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java
+++ /dev/null
@@ -1,72 +0,0 @@
-package com.github.franckyi.cmpdl;
-
-import com.github.franckyi.cmpdl.model.Project;
-import com.github.franckyi.cmpdl.model.ProjectFile;
-import com.github.franckyi.cmpdl.model.ProjectFilesList;
-import javafx.application.Platform;
-import javafx.scene.control.Alert;
-import javafx.scene.control.ButtonType;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Response;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import java.io.IOException;
-
-public class CurseMetaAPI {
-
- private static final OkHttpClient CLIENT = new OkHttpClient();
- // Docs: https://staging_cursemeta.dries007.net/docs
- private static final String URL = "https://staging_cursemeta.dries007.net/api/v3/direct";
-
- public static Project getProject(int projectId) {
- try {
- return new Project(new JSONObject(get("/addon", projectId)));
- } catch (JSONException e) {
- e.printStackTrace();
- Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
- return null;
- }
- }
-
- public static ProjectFilesList getProjectFiles(int projectId) {
- try {
- return new ProjectFilesList(new JSONArray(get("/addon", projectId, "/files")));
- } catch (JSONException e) {
- e.printStackTrace();
- Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
- return null;
- }
- }
-
- public static ProjectFile getProjectFile(int projectId, int fileId) {
- try {
- return new ProjectFile(new JSONObject(get("/addon", projectId, "/file", fileId)));
- } catch (JSONException e) {
- e.printStackTrace();
- Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project file (%d:%d)", projectId, fileId), ButtonType.OK).show());
- return null;
- }
- }
-
- private static String get(String path, Object... args) {
- StringBuilder sb = new StringBuilder(URL + path);
- for (Object o : args) {
- sb.append("/").append(o);
- }
- Request request = new Request.Builder().header("User-Agent", CMPDL.USER_AGENT).url(sb.toString()).get().build();
- try (Response response = CLIENT.newCall(request).execute()) {
- if (response.body() != null) {
- return response.body().string();
- } else {
- return "";
- }
- } catch (IOException e) {
- e.printStackTrace();
- return "";
- }
- }
-
-}
diff --git a/src/main/java/com/github/franckyi/cmpdl/api/CMPDLConverterFactory.java b/src/main/java/com/github/franckyi/cmpdl/api/CMPDLConverterFactory.java
new file mode 100644
index 0000000..84b8632
--- /dev/null
+++ b/src/main/java/com/github/franckyi/cmpdl/api/CMPDLConverterFactory.java
@@ -0,0 +1,127 @@
+package com.github.franckyi.cmpdl.api;
+
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
+import okhttp3.ResponseBody;
+import okio.BufferedSink;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import retrofit2.Converter;
+import retrofit2.Retrofit;
+
+import java.io.IOException;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CMPDLConverterFactory extends Converter.Factory {
+
+ private CMPDLConverterFactory() {
+ }
+
+ public static CMPDLConverterFactory create() {
+ return new CMPDLConverterFactory();
+ }
+
+ @Override
+ public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
+ return value -> {
+ try {
+ if (type instanceof Class) {
+ Class> clazz = (Class>) type;
+ if (clazz == Instant.class) {
+ return Instant.parse(value.string());
+ } else if (IBean.class.isAssignableFrom(clazz)) {
+ return ((IBean) clazz.newInstance()).fromJson(new JSONObject(value.string()));
+ }
+ } else if (type instanceof ParameterizedType) {
+ ParameterizedType type0 = (ParameterizedType) type;
+ if (List.class.isAssignableFrom((Class>) type0.getRawType())) {
+ return toList(type0, new JSONArray(value.string()));
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ };
+ }
+
+ @Override
+ public Converter, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
+ return value -> {
+ try {
+ if (value instanceof List) {
+ return new JSONBody(fromList(((ParameterizedType) type), (List) value));
+ } else if (value instanceof IBean) {
+ return new JSONBody(((IBean) value).toJson());
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
+ };
+ }
+
+ public static List toList(ParameterizedType type, JSONArray array) throws Exception {
+ Class> type0 = (Class>) getParameterUpperBound(0, type);
+ List