Skip to content

Commit 8d31f54

Browse files
authored
Merge pull request #44 from Global-Tags/feat/upload-icon
feat: Support uploading custom icons
2 parents 495d649 + daa56c5 commit 8d31f54

17 files changed

Lines changed: 372 additions & 81 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.rappytv.globaltags</groupId>
88
<artifactId>GlobalTagsJava</artifactId>
9-
<version>1.2.6</version>
9+
<version>1.2.7-SNAPSHOT</version>
1010

1111
<name>GlobalTagsJava</name>
1212
<description>A wrapper for the GlobalTagsAPI</description>

src/main/java/com/rappytv/globaltags/wrapper/GlobalTagsAPI.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
public abstract class GlobalTagsAPI<T> {
1818

19+
/*+
20+
* Default urls
21+
*/
1922
private final Urls urls = new Urls();
2023
/**
2124
* Default cache options
@@ -229,7 +232,8 @@ public String getRoleIcon(String role) {
229232
@NotNull
230233
public String getCustomIcon(UUID uuid, String hash) {
231234
return String.format(
232-
"https://api.globaltags.xyz/players/%s/icon/%s",
235+
"%s/players/%s/icon/%s",
236+
this.getApiBase(),
233237
uuid,
234238
hash
235239
);

src/main/java/com/rappytv/globaltags/wrapper/http/ApiHandler.java

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.jetbrains.annotations.NotNull;
1111
import org.jetbrains.annotations.Nullable;
1212

13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.nio.file.Path;
1316
import java.util.*;
1417
import java.util.function.Consumer;
1518

@@ -226,7 +229,7 @@ public void setTag(@NotNull UUID uuid, @NotNull String tag, @NotNull Consumer<Ap
226229
return;
227230
}
228231
this.api.getCache().renew(uuid, (info) ->
229-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
232+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
230233
);
231234
});
232235
}
@@ -266,7 +269,7 @@ public void setPosition(@NotNull UUID uuid, @NotNull GlobalPosition position, @N
266269
return;
267270
}
268271
this.api.getCache().renew(uuid, (info) ->
269-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
272+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
270273
);
271274
});
272275
}
@@ -306,7 +309,47 @@ public void setIcon(@NotNull UUID uuid, @NotNull GlobalIcon icon, @NotNull Consu
306309
return;
307310
}
308311
this.api.getCache().renew(uuid, (info) ->
309-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
312+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
313+
);
314+
});
315+
}
316+
317+
/**
318+
* A request to upload a custom icon for a specific uuid
319+
*
320+
* @param uuid The uuid you want to upload the icon for
321+
* @param file The image file you want to upload
322+
* @param consumer The action to be executed on response.
323+
*/
324+
public void uploadIcon(@NotNull UUID uuid, @NotNull File file, @NotNull Consumer<ApiResponse<IconUploadSchema>> consumer) throws IOException {
325+
Objects.requireNonNull(file);
326+
this.uploadIcon(uuid, file.toPath(), consumer);
327+
}
328+
329+
/**
330+
* A request to upload a custom icon for a specific uuid
331+
*
332+
* @param uuid The uuid you want to upload the icon for
333+
* @param path The image file path you want to upload
334+
* @param consumer The action to be executed on response.
335+
*/
336+
public void uploadIcon(@NotNull UUID uuid, @NotNull Path path, @NotNull Consumer<ApiResponse<IconUploadSchema>> consumer) throws IOException {
337+
Objects.requireNonNull(uuid);
338+
Objects.requireNonNull(path);
339+
Objects.requireNonNull(consumer);
340+
new ApiRequest<>(
341+
this.api,
342+
"POST",
343+
Routes.uploadIcon(uuid),
344+
MultipartData.newBuilder().addFile("image", path, "image/png").build(),
345+
IconUploadSchema.class
346+
).sendRequestAsync((response) -> {
347+
if (!response.isSuccessful()) {
348+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
349+
return;
350+
}
351+
this.api.getCache().renew(uuid, (info) ->
352+
consumer.accept(new ApiResponse<>(true, response.getData(), null))
310353
);
311354
});
312355
}
@@ -344,7 +387,7 @@ public void setRoleIconVisibility(@NotNull UUID uuid, boolean visible, @NotNull
344387
return;
345388
}
346389
this.api.getCache().renew(uuid, (info) ->
347-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
390+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
348391
);
349392
});
350393
}
@@ -380,7 +423,7 @@ public void resetTag(@NotNull UUID uuid, @NotNull Consumer<ApiResponse<String>>
380423
return;
381424
}
382425
this.api.getCache().renew(uuid, (info) ->
383-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
426+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
384427
);
385428
});
386429
}
@@ -429,7 +472,7 @@ public void updateWatchlistStatus(@NotNull UUID uuid, boolean watched, @NotNull
429472
consumer.accept(new ApiResponse<>(false, null, response.getError()));
430473
return;
431474
}
432-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
475+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
433476
});
434477
}
435478

@@ -557,7 +600,7 @@ public void deleteApiKey(@NotNull UUID uuid, @NotNull String name, @NotNull Cons
557600
consumer.accept(new ApiResponse<>(false, null, response.getError()));
558601
return;
559602
}
560-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
603+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
561604
});
562605
}
563606

@@ -731,7 +774,7 @@ public void deleteGiftCode(@NotNull String code, @NotNull Consumer<ApiResponse<S
731774
consumer.accept(new ApiResponse<>(false, null, response.getError()));
732775
return;
733776
}
734-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
777+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
735778
});
736779
}
737780

@@ -755,7 +798,7 @@ public void referPlayer(@NotNull UUID uuid, @NotNull Consumer<ApiResponse<String
755798
consumer.accept(new ApiResponse<>(false, null, response.getError()));
756799
return;
757800
}
758-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
801+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
759802
});
760803
}
761804

@@ -781,7 +824,7 @@ public void reportPlayer(@NotNull UUID uuid, @NotNull String reason, @NotNull Co
781824
consumer.accept(new ApiResponse<>(false, null, response.getError()));
782825
return;
783826
}
784-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
827+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
785828
});
786829
}
787830

@@ -918,7 +961,7 @@ public void banPlayer(@NotNull UUID uuid, @NotNull String reason, @Nullable Bool
918961
return;
919962
}
920963
this.api.getCache().renew(uuid, (info) ->
921-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
964+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
922965
);
923966
});
924967
}
@@ -944,7 +987,7 @@ public void unbanPlayer(@NotNull UUID uuid, @NotNull Consumer<ApiResponse<String
944987
return;
945988
}
946989
this.api.getCache().renew(uuid, (info) ->
947-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
990+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
948991
);
949992
});
950993
}
@@ -973,7 +1016,7 @@ public void editBan(@NotNull UUID uuid, @NotNull String reason, boolean appealab
9731016
return;
9741017
}
9751018
this.api.getCache().renew(uuid, (info) ->
976-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
1019+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
9771020
);
9781021
});
9791022
}
@@ -998,7 +1041,7 @@ public void appealBan(@NotNull String reason, @NotNull Consumer<ApiResponse<Stri
9981041
consumer.accept(new ApiResponse<>(false, null, response.getError()));
9991042
return;
10001043
}
1001-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1044+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
10021045
});
10031046
}
10041047

@@ -1043,7 +1086,7 @@ public void unlinkDiscord(@NotNull Consumer<ApiResponse<String>> consumer) {
10431086
return;
10441087
}
10451088
this.api.getCache().renewSelf((info) ->
1046-
consumer.accept(new ApiResponse<>(true, response.getData().message, null))
1089+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null))
10471090
);
10481091
});
10491092
}
@@ -1068,7 +1111,7 @@ public void linkEmail(@NotNull String email, @NotNull Consumer<ApiResponse<Strin
10681111
consumer.accept(new ApiResponse<>(false, null, response.getError()));
10691112
return;
10701113
}
1071-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1114+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
10721115
});
10731116
}
10741117

@@ -1090,7 +1133,7 @@ public void unlinkEmail(@NotNull Consumer<ApiResponse<String>> consumer) {
10901133
consumer.accept(new ApiResponse<>(false, null, response.getError()));
10911134
return;
10921135
}
1093-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1136+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
10941137
});
10951138
}
10961139

@@ -1114,7 +1157,7 @@ public void verifyEmail(@NotNull String code, @NotNull Consumer<ApiResponse<Stri
11141157
consumer.accept(new ApiResponse<>(false, null, response.getError()));
11151158
return;
11161159
}
1117-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1160+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
11181161
});
11191162
}
11201163

@@ -1185,7 +1228,7 @@ public void createNote(@NotNull UUID uuid, @NotNull String note, @NotNull Consum
11851228
consumer.accept(new ApiResponse<>(false, null, response.getError()));
11861229
return;
11871230
}
1188-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1231+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
11891232
});
11901233
}
11911234

@@ -1259,7 +1302,7 @@ public void deleteNote(@NotNull UUID uuid, @NotNull String noteId, @NotNull Cons
12591302
consumer.accept(new ApiResponse<>(false, null, response.getError()));
12601303
return;
12611304
}
1262-
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
1305+
consumer.accept(new ApiResponse<>(true, response.getData().getMessage(), null));
12631306
});
12641307
}
12651308
}

src/main/java/com/rappytv/globaltags/wrapper/http/ApiRequest.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
55
import com.rappytv.globaltags.wrapper.GlobalTagsAPI;
6+
import com.rappytv.globaltags.wrapper.enums.GlobalIcon;
7+
import com.rappytv.globaltags.wrapper.enums.GlobalPermission;
8+
import com.rappytv.globaltags.wrapper.enums.GlobalPosition;
69
import com.rappytv.globaltags.wrapper.http.schemas.ErrorSchema;
7-
import com.rappytv.globaltags.wrapper.model.adapters.DateTypeAdapter;
8-
import com.rappytv.globaltags.wrapper.model.adapters.UUIDTypeAdapter;
10+
import com.rappytv.globaltags.wrapper.model.adapters.*;
911
import org.jetbrains.annotations.NotNull;
1012

1113
import java.net.URI;
@@ -27,13 +29,17 @@ public class ApiRequest<T> {
2729
private static final HttpClient client = HttpClient.newHttpClient();
2830
private static final Gson gson = new GsonBuilder()
2931
.registerTypeAdapter(Date.class, new DateTypeAdapter())
32+
.registerTypeAdapter(GlobalIcon.class, new GlobalIconTypeAdapter())
33+
.registerTypeAdapter(GlobalPermission.class, new GlobalPermissionTypeAdapter())
34+
.registerTypeAdapter(GlobalPosition.class, new GlobalPositionTypeAdapter())
3035
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
3136
.create();
3237

3338
private final String method;
3439
private final String path;
3540
private final GlobalTagsAPI<?> api;
36-
private final Map<String, Object> body;
41+
private final HttpRequest.BodyPublisher body;
42+
private final String contentType;
3743
private final Class<T> responseType;
3844

3945
/**
@@ -45,11 +51,16 @@ public class ApiRequest<T> {
4551
* @param responseType The class type for parsing the response
4652
*/
4753
public ApiRequest(GlobalTagsAPI<?> api, String method, String path, Class<T> responseType) {
48-
this(api, method, path, null, responseType);
54+
this.api = api;
55+
this.method = method;
56+
this.path = path;
57+
this.body = HttpRequest.BodyPublishers.noBody();
58+
this.contentType = null;
59+
this.responseType = responseType;
4960
}
5061

5162
/**
52-
* Builds a new request.
63+
* Builds a new request with a json body.
5364
*
5465
* @param api An API instance
5566
* @param method The method
@@ -61,7 +72,26 @@ public ApiRequest(GlobalTagsAPI<?> api, String method, String path, Map<String,
6172
this.api = api;
6273
this.method = method;
6374
this.path = path;
64-
this.body = body;
75+
this.body = HttpRequest.BodyPublishers.ofString(gson.toJson(body));
76+
this.contentType = "application/json";
77+
this.responseType = responseType;
78+
}
79+
80+
/**
81+
* Builds a new request with a formdata body.
82+
*
83+
* @param api An API instance
84+
* @param method The method
85+
* @param path The request path, use {@link Routes}
86+
* @param body The request data
87+
* @param responseType The class type for parsing the response
88+
*/
89+
public ApiRequest(GlobalTagsAPI<?> api, String method, String path, MultipartData body, Class<T> responseType) {
90+
this.api = api;
91+
this.method = method;
92+
this.path = path;
93+
this.body = body.getBodyPublisher();
94+
this.contentType = body.getContentType();
6595
this.responseType = responseType;
6696
}
6797

@@ -72,12 +102,15 @@ public ApiRequest(GlobalTagsAPI<?> api, String method, String path, Map<String,
72102
*/
73103
public void sendRequestAsync(Consumer<@NotNull ApiResponse<T>> consumer) {
74104
try {
75-
HttpRequest request = this.getBuilder()
105+
HttpRequest.Builder builder = this.getBuilder()
76106
.uri(new URI(this.api.getUrls().getApiBase() + this.path))
77-
.method(this.method, this.getBodyPublisher())
78-
.build();
107+
.method(this.method, this.body);
79108

80-
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(response -> {
109+
if(this.contentType != null) {
110+
builder.header("Content-Type", this.contentType);
111+
}
112+
113+
client.sendAsync(builder.build(), HttpResponse.BodyHandlers.ofString()).thenAccept(response -> {
81114
boolean success = response.statusCode() >= 200 && response.statusCode() < 300;
82115
if(!success) {
83116
ErrorSchema body = gson.fromJson(response.body(), ErrorSchema.class);
@@ -106,19 +139,8 @@ public void sendRequestAsync(Consumer<@NotNull ApiResponse<T>> consumer) {
106139
*/
107140
private HttpRequest.Builder getBuilder() {
108141
return HttpRequest.newBuilder()
109-
.header("Content-Type", "application/json")
110142
.header("Authorization", this.api.getAuthorizationHeader())
111143
.header("X-Language", this.api.getLanguageCode())
112144
.header("X-Agent", this.api.getAgent().toString());
113145
}
114-
115-
/**
116-
* Get the {@link HttpRequest.BodyPublisher} from the data parameter.
117-
*
118-
* @return The {@link HttpRequest.BodyPublisher} from the data parameter
119-
*/
120-
private HttpRequest.BodyPublisher getBodyPublisher() {
121-
if (this.body == null || this.body.isEmpty()) return HttpRequest.BodyPublishers.noBody();
122-
return HttpRequest.BodyPublishers.ofString(gson.toJson(this.body));
123-
}
124146
}

0 commit comments

Comments
 (0)