Skip to content

Commit fc2ff7a

Browse files
authored
Merge pull request #34 from Global-Tags/development
Release v1.2.4
2 parents 4f178a8 + 7c1d07c commit fc2ff7a

11 files changed

Lines changed: 389 additions & 208 deletions

File tree

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.3</version>
9+
<version>1.2.4</version>
1010

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

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

Lines changed: 141 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
import com.rappytv.globaltags.wrapper.enums.GlobalIcon;
66
import com.rappytv.globaltags.wrapper.enums.GlobalPosition;
77
import com.rappytv.globaltags.wrapper.enums.ReferralLeaderboardType;
8-
import com.rappytv.globaltags.wrapper.http.schemas.MessageSchema;
9-
import com.rappytv.globaltags.wrapper.http.schemas.PlayerInfoSchema;
10-
import com.rappytv.globaltags.wrapper.http.schemas.ReferralLeaderboardsSchema;
11-
import com.rappytv.globaltags.wrapper.http.schemas.VerificationCodeSchema;
8+
import com.rappytv.globaltags.wrapper.http.schemas.*;
129
import com.rappytv.globaltags.wrapper.model.*;
1310
import org.jetbrains.annotations.NotNull;
1411
import org.jetbrains.annotations.Nullable;
@@ -360,6 +357,28 @@ public void resetTag(UUID uuid, Consumer<ApiResponse<String>> consumer) {
360357
});
361358
}
362359

360+
/**
361+
* A request to get a player's watchlist status
362+
*
363+
* @param uuid The uuid you want to get the watchlist status of
364+
* @param consumer The action to be executed on response.
365+
*/
366+
public void getWatchlistStatus(UUID uuid, Consumer<ApiResponse<Boolean>> consumer) {
367+
new ApiRequest<>(
368+
this.api,
369+
"GET",
370+
Routes.watchlist(uuid),
371+
emptyBody,
372+
WatchlistSchema.class
373+
).sendRequestAsync((response) -> {
374+
if (!response.isSuccessful()) {
375+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
376+
return;
377+
}
378+
consumer.accept(new ApiResponse<>(true, response.getData().watched, null));
379+
});
380+
}
381+
363382
/**
364383
* A request to add a player to the watchlist
365384
*
@@ -382,6 +401,120 @@ public void updateWatchlistStatus(UUID uuid, boolean watched, Consumer<ApiRespon
382401
});
383402
}
384403

404+
/**
405+
* A request to get a player's API keys
406+
*
407+
* @param uuid The uuid you want to get the API keys of
408+
* @param consumer The action to be executed on response
409+
*/
410+
public void getApiKeys(UUID uuid, Consumer<ApiResponse<List<ApiKey>>> consumer) {
411+
new ApiRequest<>(
412+
this.api,
413+
"GET",
414+
Routes.apiKeys(uuid),
415+
emptyBody,
416+
ApiKey[].class
417+
).sendRequestAsync((response) -> {
418+
if (!response.isSuccessful()) {
419+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
420+
return;
421+
}
422+
consumer.accept(new ApiResponse<>(true, Arrays.asList(response.getData()), null));
423+
});
424+
}
425+
426+
/**
427+
* A request to get a player's API key
428+
*
429+
* @param uuid The uuid you want to get the API key of
430+
* @param name The name of the API key
431+
* @param consumer The action to be executed on response.
432+
*/
433+
public void getApiKey(UUID uuid, String name, Consumer<ApiResponse<ApiKey>> consumer) {
434+
new ApiRequest<>(
435+
this.api,
436+
"GET",
437+
Routes.apiKey(uuid, name),
438+
emptyBody,
439+
ApiKey.class
440+
).sendRequestAsync((response) -> {
441+
if (!response.isSuccessful()) {
442+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
443+
return;
444+
}
445+
consumer.accept(new ApiResponse<>(true, response.getData(), null));
446+
});
447+
}
448+
449+
/**
450+
* A request to create a player API key
451+
*
452+
* @param uuid The uuid you want to create the API key for
453+
* @param name The name of the API key
454+
* @param consumer The action to be executed on response.
455+
*/
456+
public void createApiKey(UUID uuid, String name, Consumer<ApiResponse<ApiKeyCreationSchema>> consumer) {
457+
new ApiRequest<>(
458+
this.api,
459+
"POST",
460+
Routes.apiKeys(uuid),
461+
Map.of("name", name),
462+
ApiKeyCreationSchema.class
463+
).sendRequestAsync((response) -> {
464+
if (!response.isSuccessful()) {
465+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
466+
return;
467+
}
468+
consumer.accept(new ApiResponse<>(true, response.getData(), null));
469+
});
470+
}
471+
472+
/**
473+
* A request to regenerate a player's API key
474+
*
475+
* @param uuid The uuid you want to regenerate the API key of
476+
* @param name The name of the API key
477+
* @param consumer The action to be executed on response.
478+
*/
479+
public void regenerateApiKey(UUID uuid, String name, Consumer<ApiResponse<ApiKeyRegenSchema>> consumer) {
480+
new ApiRequest<>(
481+
this.api,
482+
"PATCH",
483+
Routes.apiKey(uuid, name),
484+
emptyBody,
485+
ApiKeyRegenSchema.class
486+
).sendRequestAsync((response) -> {
487+
if (!response.isSuccessful()) {
488+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
489+
return;
490+
}
491+
consumer.accept(new ApiResponse<>(true, response.getData(), null));
492+
});
493+
}
494+
495+
/**
496+
* A request to delete a player's API key
497+
*
498+
* @param uuid The uuid you want to delete the API key of
499+
* @param name The name of the API key
500+
* @param consumer The action to be executed on response.
501+
*/
502+
public void deleteApiKey(UUID uuid, String name, Consumer<ApiResponse<String>> consumer) {
503+
new ApiRequest<>(
504+
this.api,
505+
"DELETE",
506+
Routes.apiKey(uuid, name),
507+
emptyBody,
508+
MessageSchema.class
509+
).sendRequestAsync((response) -> {
510+
if (!response.isSuccessful()) {
511+
consumer.accept(new ApiResponse<>(false, null, response.getError()));
512+
return;
513+
}
514+
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
515+
});
516+
}
517+
385518
/**
386519
* A request to mark a specific uuid as the inviter of {@link GlobalTagsAPI#getClientUUID()}
387520
*
@@ -582,16 +715,16 @@ public void unbanPlayer(UUID uuid, Consumer<ApiResponse<String>> consumer) {
582715
* A request to edit the ban of a specific uuid
583716
*
584717
* @param uuid The uuid you want to edit the ban of
585-
* @param suspension The new {@link PlayerInfo.Suspension} object
718+
* @param reason The new reason for the ban
719+
* @param appealable If the ban should be appealable or not
586720
* @param consumer The action to be executed on response.
587721
*/
588-
public void editBan(UUID uuid, PlayerInfo.Suspension suspension, Consumer<ApiResponse<String>> consumer) {
589-
Objects.requireNonNull(suspension.getReason(), "Reason must not be null");
722+
public void editBan(UUID uuid, @NotNull String reason, boolean appealable, Consumer<ApiResponse<String>> consumer) {
590723
new ApiRequest<>(
591724
this.api,
592725
"PATCH",
593726
Routes.bans(uuid),
594-
Map.of("reason", suspension.getReason(), "appealable", suspension.isAppealable()),
727+
Map.of("reason", reason, "appealable", appealable),
595728
MessageSchema.class
596729
).sendRequestAsync((response) -> {
597730
if(!response.isSuccessful()) {
@@ -864,63 +997,4 @@ public void deleteNote(UUID uuid, String noteId, Consumer<ApiResponse<String>> c
864997
consumer.accept(new ApiResponse<>(true, response.getData().message, null));
865998
});
866999
}
867-
868-
/**
869-
* An inline class containing response data for these requests
870-
* @param <T> The type of the data
871-
*/
872-
public static class ApiResponse<T> {
873-
874-
private final boolean successful;
875-
private final T data;
876-
private final String error;
877-
878-
/**
879-
* Constructs a new ApiResponse instance
880-
*
881-
* @param successful If the request was successful
882-
* @param data The data returned if available
883-
* @param error The error returned if available
884-
*/
885-
public ApiResponse(boolean successful, T data, String error) {
886-
this.successful = successful;
887-
this.data = data;
888-
this.error = error;
889-
}
890-
891-
/**
892-
* Checks if the request was successful
893-
*
894-
* @return If the request was successful
895-
*/
896-
public boolean isSuccessful() {
897-
return this.successful;
898-
}
899-
900-
/**
901-
* Gets the data returned if available
902-
*
903-
* @return the data if available
904-
*/
905-
public T getData() {
906-
return this.data;
907-
}
908-
909-
/**
910-
* Get the error returned if available
911-
* @return an error if available
912-
*/
913-
public String getError() {
914-
return this.error;
915-
}
916-
917-
@Override
918-
public String toString() {
919-
return "ApiResponse{" +
920-
"successful=" + this.successful +
921-
", data=" + this.data +
922-
", error='" + this.error + '\'' +
923-
'}';
924-
}
925-
}
9261000
}

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

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public ApiRequest(GlobalTagsAPI<?> api, String method, String path, Map<String,
7070
*
7171
* @param consumer A consumer which gets called when the API responds
7272
*/
73-
public void sendRequestAsync(Consumer<@NotNull ResponseBody<T>> consumer) {
73+
public void sendRequestAsync(Consumer<@NotNull ApiResponse<T>> consumer) {
7474
try {
7575
HttpRequest request = this.getBuilder()
7676
.uri(new URI(this.api.getUrls().getApiBase() + this.path))
@@ -81,21 +81,21 @@ public void sendRequestAsync(Consumer<@NotNull ResponseBody<T>> consumer) {
8181
boolean success = response.statusCode() >= 200 && response.statusCode() < 300;
8282
if(!success) {
8383
ErrorSchema body = gson.fromJson(response.body(), ErrorSchema.class);
84-
consumer.accept(new ResponseBody<>(false, null, body.error));
84+
consumer.accept(new ApiResponse<>(false, null, body.error));
8585
return;
8686
}
8787
T parsedBody = gson.fromJson(response.body(), this.responseType);
88-
consumer.accept(new ResponseBody<>(
88+
consumer.accept(new ApiResponse<>(
8989
true,
9090
parsedBody,
9191
null
9292
));
9393
}).exceptionally(throwable -> {
94-
consumer.accept(new ResponseBody<>(false, null, throwable.getLocalizedMessage()));
94+
consumer.accept(new ApiResponse<>(false, null, throwable.getLocalizedMessage()));
9595
return null;
9696
});
9797
} catch (Exception e) {
98-
consumer.accept(new ResponseBody<>(false, null, e.getLocalizedMessage()));
98+
consumer.accept(new ApiResponse<>(false, null, e.getLocalizedMessage()));
9999
}
100100
}
101101

@@ -121,55 +121,4 @@ private HttpRequest.BodyPublisher getBodyPublisher() {
121121
if (this.body == null || this.body.isEmpty()) return HttpRequest.BodyPublishers.noBody();
122122
return HttpRequest.BodyPublishers.ofString(gson.toJson(this.body));
123123
}
124-
125-
/**
126-
* A class which passes a lightweight API response to consumers.
127-
*
128-
* @param <T> The return type
129-
*/
130-
public static class ResponseBody<T> {
131-
132-
private final boolean successful;
133-
private final T data;
134-
private final String error;
135-
136-
/**
137-
* Constructs a new response body.
138-
*
139-
* @param successful If the request was successful
140-
* @param data The response data
141-
* @param error An error message
142-
*/
143-
public ResponseBody(boolean successful, T data, String error) {
144-
this.successful = successful;
145-
this.data = data;
146-
this.error = error;
147-
}
148-
149-
/**
150-
* Checks if the request was successful
151-
*
152-
* @return If the request was successful
153-
*/
154-
public boolean isSuccessful() {
155-
return this.successful;
156-
}
157-
158-
/**
159-
* Gets the data returned if available
160-
*
161-
* @return the data if available
162-
*/
163-
public T getData() {
164-
return this.data;
165-
}
166-
167-
/**
168-
* Get the error returned if available
169-
* @return an error if available
170-
*/
171-
public String getError() {
172-
return this.error;
173-
}
174-
}
175124
}

0 commit comments

Comments
 (0)