Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Using Twitch App API and Retrofit 2
Browse files Browse the repository at this point in the history
Minor visual changes
  • Loading branch information
skyecodes committed Jul 7, 2019
1 parent 22d5233 commit d7e2ada
Show file tree
Hide file tree
Showing 33 changed files with 667 additions and 525 deletions.
38 changes: 32 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.franckyi</groupId>
<artifactId>cmpdl</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -15,17 +15,43 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
<version>20180813</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.10.0</version>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
<version>1.12.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.github.franckyi.cmpdl.CMPDL</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
26 changes: 18 additions & 8 deletions src/main/java/com/github/franckyi/cmpdl/CMPDL.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<MainWindowController> mainWindow;
public static ContentControllerView<ModpackPaneController> modpackPane;
Expand All @@ -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;
Expand All @@ -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(() -> {
Expand Down
72 changes: 0 additions & 72 deletions src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java

This file was deleted.

127 changes: 127 additions & 0 deletions src/main/java/com/github/franckyi/cmpdl/api/CMPDLConverterFactory.java
Original file line number Diff line number Diff line change
@@ -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<ResponseBody, ?> 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<Object> list = new ArrayList<>();
if (IBean.class.isAssignableFrom(type0)) {
for (int i = 0; i < array.length(); i++) {
list.add(((IBean) type0.newInstance()).fromJson(array.getJSONObject(i)));
}
} else {
list.addAll(array.toList());
}
return list;
}

public static JSONArray fromList(List<?> list) throws Exception {
Type[] types = CMPDLConverterFactory.class.getDeclaredMethod("fromList", List.class).getGenericParameterTypes();
ParameterizedType type = (ParameterizedType) types[0];
return fromList(type, list);
}

public static JSONArray fromList(ParameterizedType type, List<?> list) {
Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];
return fromList(clazz, list);
}

public static JSONArray fromList(Class<?> clazz, List<?> list) {
JSONArray array = new JSONArray();
if (IBean.class.isAssignableFrom(clazz)) {
list.stream()
.map(IBean.class::cast)
.map(IBean::toJson)
.forEach(array::put);
} else {
array.put(list);
}
return array;
}

private class JSONBody extends RequestBody {
private final Object json;

public JSONBody(JSONArray json) {
this.json = json;
}

public JSONBody(JSONObject json) {
this.json = json;
}

@Override
public MediaType contentType() {
return MediaType.get("application/json");
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.writeUtf8(json.toString());
}
}
}
Loading

0 comments on commit d7e2ada

Please sign in to comment.