Skip to content

Commit

Permalink
Add Path based variants to listCrashReportsInDirectory and listLogsIn…
Browse files Browse the repository at this point in the history
…Directory
  • Loading branch information
JulianVennen committed Sep 23, 2024
1 parent ad61b27 commit 28fdab0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Fix setting client in UploadLogResponse (Fix [#9](https://github.com/aternosorg/mclogs-java/issues/9))
- Add Path based variants to listCrashReportsInDirectory and listLogsInDirectory
27 changes: 22 additions & 5 deletions src/main/java/gs/mclo/api/MclogsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import gs.mclo.api.response.InsightsResponse;
import gs.mclo.api.response.UploadLogResponse;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
Expand Down Expand Up @@ -176,7 +175,7 @@ public CompletableFuture<UploadLogResponse> uploadLog(Log log) {
/**
* Upload a log to mclo.gs
*
* @param log the log to upload
* @param log content of the log to upload
* @return the response
*/
public CompletableFuture<UploadLogResponse> uploadLog(String log) {
Expand All @@ -186,7 +185,7 @@ public CompletableFuture<UploadLogResponse> uploadLog(String log) {
/**
* Upload a log to mclo.gs
*
* @param log the log to upload
* @param log path to the log to upload
* @return the response
*/
public CompletableFuture<UploadLogResponse> uploadLog(Path log) throws IOException {
Expand Down Expand Up @@ -226,13 +225,31 @@ public CompletableFuture<InsightsResponse> getInsights(String logId) {
return httpClient.sendAsync(request, Util.parseResponse(InsightsResponse.class, gson)).thenApply(HttpResponse::body);
}

/**
* list logs in a directory
* @param directory server/client directory
* @return log file names
*/
public String[] listLogsInDirectory(Path directory){
return Util.listFilesInDirectory(directory.resolve("logs"));
}

/**
* list logs in a directory
* @param directory server/client directory
* @return log file names
*/
public String[] listLogsInDirectory(String directory){
return Util.listFilesInDirectory(new File(directory, "logs"));
return listLogsInDirectory(Path.of(directory));
}

/**
* list crash reports in a directory
* @param directory server/client directory
* @return log file names
*/
public String[] listCrashReportsInDirectory(Path directory){
return Util.listFilesInDirectory(directory.resolve("crash-reports"));
}

/**
Expand All @@ -241,6 +258,6 @@ public String[] listLogsInDirectory(String directory){
* @return log file names
*/
public String[] listCrashReportsInDirectory(String directory){
return Util.listFilesInDirectory(new File(directory, "crash-reports"));
return listCrashReportsInDirectory(Path.of(directory));
}
}
11 changes: 8 additions & 3 deletions src/main/java/gs/mclo/api/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

public class Util {

public static String[] listFilesInDirectory(File file) {
if(!file.exists())
public static String[] listFilesInDirectory(File directory) {
if(!directory.exists())
return new String[0];
String[] files = file.list();
String[] files = directory.list();
if (files == null)
files = new String[0];

Expand All @@ -26,6 +27,10 @@ public static String[] listFilesInDirectory(File file) {
.toArray(String[]::new);
}

public static String[] listFilesInDirectory(Path directory) {
return listFilesInDirectory(directory.toFile());
}

public static <T> HttpResponse.BodyHandler<T> parseResponse(Class<T> clazz, Gson gson) {
return responseInfo -> HttpResponse.BodySubscribers.mapping(HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8),
body -> gson.fromJson(body, clazz));
Expand Down

0 comments on commit 28fdab0

Please sign in to comment.