From c8acd2e8b29a5b2dbc4341bf88db8ed6c5de45f3 Mon Sep 17 00:00:00 2001 From: beanbeanjuice Date: Mon, 24 Jun 2024 04:50:12 -0400 Subject: [PATCH] Added Async to the Cafe Users Endpoint --- .../cafeapi/wrapper/CafeAPI.java | 2 +- .../users/DonationUsersEndpoint.java | 25 +--- .../birthdays/BirthdaysEndpoint.java | 64 ++------- .../endpoints/cafe/CafeUsersEndpoint.java | 129 +++++------------- .../codes/GeneratedCodesEndpoint.java | 10 +- .../endpoints/counting/CountingEndpoint.java | 10 +- .../endpoints/goodbyes/GoodbyesEndpoint.java | 10 +- .../endpoints/guilds/GuildsEndpoint.java | 10 +- .../interactions/InteractionsEndpoint.java | 24 ++-- .../pictures/InteractionPicturesEndpoint.java | 5 +- .../winstreaks/WinStreaksEndpoint.java | 10 +- .../endpoints/polls/PollsEndpoint.java | 8 +- .../endpoints/raffles/RafflesEndpoint.java | 8 +- .../endpoints/twitches/TwitchEndpoint.java | 8 +- .../endpoints/version/VersionsEndpoint.java | 4 +- .../voicebinds/VoiceChannelBindsEndpoint.java | 8 +- .../endpoints/welcomes/WelcomesEndpoint.java | 10 +- .../endpoints/words/WordsEndpoint.java | 6 +- .../wrapper/requests/RequestBuilder.java | 41 +----- .../cafeapi/wrapper/user/UsersEndpoint.java | 8 +- ...rTest.java => CafeUsersEndpointTests.java} | 67 +++++---- .../com/beanbeanjuice/GIFEndpointTest.java | 22 +-- 22 files changed, 174 insertions(+), 315 deletions(-) rename wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/{CafeUserTest.java => CafeUsersEndpointTests.java} (61%) diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java index e59154af..314460e8 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/CafeAPI.java @@ -132,7 +132,7 @@ private String getToken(String username, String password) { .setRoute("/user/login") .addParameter("username", username) .addParameter("password", password) - .build().orElseThrow(); + .build(); return request.getData().get("api_key").textValue(); } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java index 373d40a8..c3a80cf3 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/beancoins/users/DonationUsersEndpoint.java @@ -11,7 +11,6 @@ import java.util.HashMap; import java.util.Optional; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; /** * A class used to make {@link DonationUsersEndpoint} requests to the {@link CafeAPI CafeAPI}. @@ -25,12 +24,10 @@ public CompletableFuture> getAllUserDonationTimes() { .setRoute("/beanCoin/donation_users") .setAuthorization(apiKey) .buildAsync() - .thenApply((optionalRequest) -> { + .thenApply((request) -> { HashMap donationUsers = new HashMap<>(); - if (optionalRequest.isEmpty()) throw new CompletionException("Unable to get donation user times. Request is empty.", null); - - optionalRequest.get().getData().get("users").forEach((userNode) -> { + request.getData().get("users").forEach((userNode) -> { String userID = userNode.get("user_id").asText(); Timestamp timeUntilNextDonation = CafeGeneric.parseTimestampFromAPI(userNode.get("time_until_next_donation").asText()).orElse(null); @@ -46,11 +43,7 @@ public CompletableFuture> getUserDonationTime(final String u .setRoute("/beanCoin/donation_users/" + userID) .setAuthorization(apiKey) .buildAsync() - .thenApply((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Unable to get user donation time. Request is empty.", null); - - return CafeGeneric.parseTimestampFromAPI(optionalRequest.get().getData().get("user").get("time_until_next_donation").asText()); - }); + .thenApply((request) -> CafeGeneric.parseTimestampFromAPI(request.getData().get("user").get("time_until_next_donation").asText())); } public CompletableFuture addDonationUser(final String userID, final Timestamp timeUntilNextDonation) { @@ -59,11 +52,7 @@ public CompletableFuture addDonationUser(final String userID, final Tim .addParameter("time_stamp", timeUntilNextDonation.toString()) .setAuthorization(apiKey) .buildAsync() - .thenApply((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Unable to add donation user. Request is empty.", null); - - return optionalRequest.get().getStatusCode() == 201; - }); + .thenApply((request) -> request.getStatusCode() == 201); } public CompletableFuture deleteDonationUser(final String userID) { @@ -71,11 +60,7 @@ public CompletableFuture deleteDonationUser(final String userID) { .setRoute("/beanCoin/donation_users/" + userID) .setAuthorization(apiKey) .buildAsync() - .thenApply((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Unable to delete donation user. Request is empty.", null); - - return optionalRequest.get().getStatusCode() == 200; - }); + .thenApply((request) -> request.getStatusCode() == 200); } } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java index a1a27507..d12a1324 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/birthdays/BirthdaysEndpoint.java @@ -14,11 +14,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; -/** - * A class used for {@link Birthday} requests to the {@link CafeAPI CafeAPI}. - * - * @author beanbeanjuice - */ public class BirthdaysEndpoint extends CafeEndpoint { private HashMap requestToBirthdayMap(Request request) { @@ -32,45 +27,22 @@ private HashMap requestToBirthdayMap(Request request) { return birthdays; } - /** - * Retrieves all {@link Birthday} from the {@link CafeAPI CafeAPI}. - * @return A {@link HashMap} with keys of {@link String userID} and values of {@link Birthday}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - */ public CompletableFuture> getAllBirthdays() throws AuthorizationException, ResponseException { return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/birthdays") .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error getting all birthdays. Request is empty.", null); - return requestToBirthdayMap(optionalRequest.get()); - }); + .thenApplyAsync(this::requestToBirthdayMap); } - /** - * Retrieves the {@link Birthday} for a specified {@link String userID}. - * @param userID The specified {@link String userID}. - * @return The {@link Birthday} for the specified {@link String userID}. - */ public CompletableFuture> getUserBirthday(final String userID) { return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/birthdays/" + userID) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error getting user birthday. Request is empty.", null); - return parseBirthday(optionalRequest.get().getData().get("birthday")); - }); + .thenApplyAsync((request) -> parseBirthday(request.getData().get("birthday"))); } - /** - * Updates the {@link Birthday} for a specified {@link String userID}. - * @param userID The specified {@link String userID}. - * @param birthday The {@link Birthday} for the specified {@link String userID}. - * @return True, if the {@link Birthday} was successfully updated in the {@link CafeAPI CafeAPI}. - */ public CompletableFuture updateUserBirthday(final String userID, final Birthday birthday) { BirthdayMonth month = birthday.getMonth(); int day = birthday.getDay(); @@ -81,36 +53,18 @@ public CompletableFuture updateUserBirthday(final String userID, final .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error updating user birthday. Request is empty.", null); - return optionalRequest.get().getStatusCode() == 200; - }); + .thenApplyAsync((request) -> request.getStatusCode() == 200); } - /** - * Updates the {@link Boolean alreadyMentioned} state for a {@link Birthday}. - * @param userID The specified {@link String userID}. - * @param alreadyMentioned The new {@link Boolean alreadyMentioned} state. - * @return True, if the {@link Birthday} was successfully updated in the {@link CafeAPI CafeAPI}. - */ public CompletableFuture updateUserBirthdayMention(final String userID, final boolean alreadyMentioned) { return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.PATCH) .setRoute("/birthdays/" + userID + "/mention") .addParameter("already_mentioned", String.valueOf(alreadyMentioned)) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error updating user mention. Request is empty.", null); - return optionalRequest.get().getStatusCode() == 200; - }); + .thenApplyAsync((request) -> request.getStatusCode() == 200); } - /** - * Creates a {@link Birthday} for a specified {@link String userID}. - * @param userID The specified {@link String userID}. - * @param birthday The {@link Birthday} specified for the {@link String userID}. - * @return True, if the {@link Birthday} was successfully created. - */ public CompletableFuture createUserBirthday(final String userID, final Birthday birthday) { BirthdayMonth month = birthday.getMonth(); int day = birthday.getDay(); @@ -121,9 +75,8 @@ public CompletableFuture createUserBirthday(final String userID, final .addParameter("time_zone", birthday.getTimeZone().getID()) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error creating user birthday. Request is empty.", null); - return optionalRequest.get().getStatusCode() == 201; + .thenApplyAsync((request) -> { + return request.getStatusCode() == 201; }); } @@ -132,9 +85,8 @@ public CompletableFuture removeUserBirthday(final String userID) { .setRoute("/birthdays/" + userID) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isEmpty()) throw new CompletionException("Error removing user birthday. Request is empty.", null); - return optionalRequest.get().getStatusCode() == 200; + .thenApplyAsync((request) -> { + return request.getStatusCode() == 200; }); } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/cafe/CafeUsersEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/cafe/CafeUsersEndpoint.java index b7d065ad..bc1ea376 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/cafe/CafeUsersEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/cafe/CafeUsersEndpoint.java @@ -1,9 +1,7 @@ package com.beanbeanjuice.cafeapi.wrapper.endpoints.cafe; -import com.beanbeanjuice.cafeapi.wrapper.CafeAPI; import com.beanbeanjuice.cafeapi.wrapper.endpoints.CafeEndpoint; import com.beanbeanjuice.cafeapi.wrapper.generic.CafeGeneric; -import com.beanbeanjuice.cafeapi.wrapper.requests.Request; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestBuilder; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestRoute; import com.beanbeanjuice.cafeapi.wrapper.requests.RequestType; @@ -13,82 +11,45 @@ import java.sql.Timestamp; import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; -/** - * A class used for {@link CafeUser} requests to the {@link CafeAPI CafeAPI}. - * - * @author beanbeanjuice - */ public class CafeUsersEndpoint extends CafeEndpoint { - /** - * Retrieves all {@link CafeUser} from the {@link CafeAPI CafeAPI}. - * @return An {@link ArrayList} of {@link CafeUser}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - */ - public ArrayList getAllCafeUsers() - throws AuthorizationException, ResponseException { - ArrayList cafeUsers = new ArrayList<>(); - - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) + public CompletableFuture> getAllCafeUsers() { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/cafe/users") .setAuthorization(apiKey) - .build().orElseThrow(); - - request.getData().get("users").forEach((user) -> cafeUsers.add(parseCafeUser(user))); - - return cafeUsers; + .buildAsync() + .thenApply((request) -> { + ArrayList cafeUsers = new ArrayList<>(); + request.getData().get("users").forEach((user) -> cafeUsers.add(parseCafeUser(user))); + return cafeUsers; + }); } - /** - * Retrieves a specified {@link CafeUser}. - * @param userID The {@link String userID} of the {@link CafeUser}. - * @return The specified {@link CafeUser}. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws NotFoundException Thrown when the {@link CafeUser} does not exist for the specified {@link String userID}. - */ - public CafeUser getCafeUser(final String userID) - throws AuthorizationException, ResponseException, NotFoundException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) + public CompletableFuture getCafeUser(final String userID) { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/cafe/users/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); - - return parseCafeUser(request.getData().get("cafe_user")); + .buildAsync() + .thenApply((request) -> parseCafeUser(request.getData().get("cafe_user"))); } - /** - * Updates the {@link CafeUser} for the specified {@link String userID}. - * @param userID The specified {@link String userID}. - * @param type The specified {@link CafeType type}. - * @param value The {@link Object value} of the specified {@link CafeType type}. - * @return True, if the {@link CafeUser} was updated successfully. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws NotFoundException Thrown when a {@link CafeUser} does not exist for the specified {@link String userID}. - * @throws TeaPotException Thrown when the {@link Object value} entered is not compatible with the specified {@link CafeType type}. - * @throws UndefinedVariableException Thrown when a variable is undefined. - */ - public boolean updateCafeUser(final String userID, final CafeType type, @Nullable final Object value) - throws AuthorizationException, ResponseException, NotFoundException, TeaPotException, UndefinedVariableException { + public CompletableFuture updateCafeUser(final String userID, final CafeType type, @Nullable final Object value) + throws TeaPotException { if (!(type.equals(CafeType.LAST_SERVING_TIME) && value == null)) { switch (type) { case BEAN_COINS -> { - if (!(value instanceof Double)) - throw new TeaPotException("Type Specified Must be a Double"); + if (!(value instanceof Double)) throw new TeaPotException("Type Specified Must be a Double"); } case LAST_SERVING_TIME -> { - if (!(value instanceof Timestamp)) - throw new TeaPotException("Type Specified Must be a Timestamp"); + if (!(value instanceof Timestamp)) throw new TeaPotException("Type Specified Must be a Timestamp"); } case ORDERS_BOUGHT, ORDERS_RECEIVED -> { - if (!(value instanceof Integer)) - throw new TeaPotException("Type Specified Must be an Integer"); + if (!(value instanceof Integer)) throw new TeaPotException("Type Specified Must be an Integer"); } } } @@ -101,57 +62,33 @@ public boolean updateCafeUser(final String userID, final CafeType type, @Nullabl if (value == null) requestBuilder.addParameter("value", "null"); else requestBuilder.addParameter("value", value.toString()); - Request request = requestBuilder.build().orElseThrow(); - - return request.getStatusCode() == 200; + return requestBuilder + .buildAsync() + .thenApply((request) -> request.getStatusCode() == 200); } - /** - * Creates a new {@link CafeUser} for the specified {@link String userID}. - * @param userID The specified {@link String userID}. - * @return True, if the {@link CafeUser} was successfully created. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - * @throws ConflictException Thrown when the {@link CafeUser} already exists for the specified {@link String userID}. - */ - public boolean createCafeUser(final String userID) - throws AuthorizationException, ResponseException, ConflictException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) + public CompletableFuture createCafeUser(final String userID) { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/cafe/users/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getStatusCode() == 201; + .buildAsync() + .thenApply((request) -> request.getStatusCode() == 201); } - /** - * Deletes a specified {@link CafeUser}. - * @param userID The specified {@link String userID}. - * @return True, if the {@link CafeUser} was successfully deleted. - * @throws AuthorizationException Thrown when the {@link String apiKey} is invalid. - * @throws ResponseException Thrown when there is a generic server-side {@link CafeException}. - */ - public boolean deleteCafeUser(String userID) - throws AuthorizationException, ResponseException { - Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) + public CompletableFuture deleteCafeUser(final String userID) { + return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/cafe/users/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); - - return request.getStatusCode() == 200; + .buildAsync() + .thenApply((request) -> request.getStatusCode() == 200); } - /** - * Parses a {@link JsonNode} for its {@link CafeUser}. - * @param node The {@link JsonNode node} to parse. - * @return The parsed {@link CafeUser}. - */ - private CafeUser parseCafeUser(JsonNode node) { + private CafeUser parseCafeUser(final JsonNode node) { String userID = node.get("user_id").asText(); - Double beanCoins = node.get("bean_coins").asDouble(); + double beanCoins = node.get("bean_coins").asDouble(); Timestamp timestamp = CafeGeneric.parseTimestampFromAPI(node.get("last_serving_time").asText()).orElse(null); - Integer ordersBought = node.get("orders_bought").asInt(); - Integer ordersReceived = node.get("orders_received").asInt(); + int ordersBought = node.get("orders_bought").asInt(); + int ordersReceived = node.get("orders_received").asInt(); return new CafeUser(userID, beanCoins, timestamp, ordersBought, ordersReceived); } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/codes/GeneratedCodesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/codes/GeneratedCodesEndpoint.java index b10a7261..cb6f319f 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/codes/GeneratedCodesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/codes/GeneratedCodesEndpoint.java @@ -30,7 +30,7 @@ public HashMap getAllGeneratedCodes() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/codes") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("users").forEach((user) -> codes.put(user.get("user_id").asText(), user.get("generated_code").asText())); @@ -50,7 +50,7 @@ public String getUserGeneratedCode(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/codes/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getData().get("generated_code").asText(); } @@ -71,7 +71,7 @@ public boolean updateUserGeneratedCode(final String userID, final String newCode .setRoute("/codes/" + userID) .addParameter("generated_code", newCode) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -92,7 +92,7 @@ public Boolean createUserGeneratedCode(final String userID, final String newCode .setRoute("/codes/" + userID) .addParameter("generated_code", newCode) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -109,7 +109,7 @@ public Boolean deleteUserGeneratedCode(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/codes/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/counting/CountingEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/counting/CountingEndpoint.java index 25db3058..6bed385a 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/counting/CountingEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/counting/CountingEndpoint.java @@ -31,7 +31,7 @@ public HashMap getAllCountingInformation() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/counting/guilds") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("guilds").forEach((guild) -> { String guildID = guild.get("guild_id").asText(); @@ -55,7 +55,7 @@ public CountingInformation getGuildCountingInformation(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/counting/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return parseCountingInformation(request.getData().get("counting_information")); } @@ -82,7 +82,7 @@ public Boolean updateGuildCountingInformation(final String guildID, final int hi .addParameter("last_user_id", lastUserID) .addParameter("failure_role_id", failureRoleID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -121,7 +121,7 @@ public boolean createGuildCountingInformation(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/counting/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -138,7 +138,7 @@ public boolean deleteGuildCountingInformation(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/counting/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/goodbyes/GoodbyesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/goodbyes/GoodbyesEndpoint.java index a2ecd82f..6463ff43 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/goodbyes/GoodbyesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/goodbyes/GoodbyesEndpoint.java @@ -28,7 +28,7 @@ public ArrayList getAllGuildGoodbyes() throws AuthorizationExcepti Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/goodbyes") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("goodbyes").forEach((guildGoodbye) -> guildGoodbyes.add(parseGuildGoodbye(guildGoodbye))); @@ -48,7 +48,7 @@ public GuildGoodbye getGuildGoodbye(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/goodbyes/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); JsonNode guildGoodbye = request.getData().get("goodbye"); @@ -72,7 +72,7 @@ public Boolean updateGuildGoodbye(final GuildGoodbye guildGoodbye) .addParameter("image_url", guildGoodbye.getImageURL().orElse(null)) .addParameter("message", guildGoodbye.getMessage().orElse(null)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -95,7 +95,7 @@ public Boolean createGuildGoodbye(final GuildGoodbye guildGoodbye) .addParameter("image_url", guildGoodbye.getImageURL().orElse(null)) .addParameter("message", guildGoodbye.getMessage().orElse(null)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -112,7 +112,7 @@ public Boolean deleteGuildGoodbye(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/goodbyes/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/guilds/GuildsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/guilds/GuildsEndpoint.java index 1501f912..046b5b64 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/guilds/GuildsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/guilds/GuildsEndpoint.java @@ -32,7 +32,7 @@ public HashMap getAllGuildInformation() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/guilds") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); for (JsonNode guild : request.getData().get("guilds")) { String guildID = guild.get("guild_id").asText(); @@ -55,7 +55,7 @@ public GuildInformation getGuildInformation(String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return parseGuildInformation(request.getData().get("guild")); } @@ -73,7 +73,7 @@ public boolean createGuildInformation(String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -123,7 +123,7 @@ public boolean updateGuildInformation(String guildID, GuildInformationType type, .addParameter("type", type.getType()) .addParameter("value", value.toString()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -140,7 +140,7 @@ public boolean deleteGuildInformation(String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/guilds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/InteractionsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/InteractionsEndpoint.java index 26d29896..d953a0d3 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/InteractionsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/InteractionsEndpoint.java @@ -33,7 +33,7 @@ public HashMap getAllInteractionSenders() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/interactions/senders") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("interactions_sent").forEach((sender) -> { String userID = sender.get("user_id").asText(); @@ -56,7 +56,7 @@ public Interaction getUserInteractionsSent(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/interactions/senders/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return parseInteraction(request.getData().get("interactions_sent")); } @@ -77,7 +77,7 @@ public Integer getSpecificUserInteractionSentAmount(final String userID, final I .setRoute("/interactions/senders/" + userID) .addParameter("type", type.toString().toLowerCase()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getData().get(type.getType()).asInt(); } @@ -100,7 +100,7 @@ public Boolean updateSpecificUserInteractionSentAmount(final String userID, fina .addParameter("type", type.toString().toLowerCase()) .addParameter("value", String.valueOf(amount)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -118,7 +118,7 @@ public boolean createUserInteractionsSent(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/interactions/senders/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -135,7 +135,7 @@ public boolean deleteUserInteractionsSent(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/interactions/senders/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -157,7 +157,7 @@ public HashMap getAllInteractionReceivers() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/interactions/receivers") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("interactions_received").forEach((receiver) -> { String userID = receiver.get("user_id").asText(); @@ -180,7 +180,7 @@ public Interaction getUserInteractionsReceived(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/interactions/receivers/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return parseInteraction(request.getData().get("interactions_received")); } @@ -201,7 +201,7 @@ public Integer getSpecificUserInteractionReceivedAmount(final String userID, fin .setRoute("/interactions/receivers/" + userID) .addParameter("type", type.toString().toLowerCase()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getData().get(type.getType()).asInt(); } @@ -224,7 +224,7 @@ public Boolean updateSpecificUserInteractionReceivedAmount(final String userID, .addParameter("type", type.toString().toLowerCase()) .addParameter("value", String.valueOf(amount)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -242,7 +242,7 @@ public boolean createUserInteractionsReceived(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/interactions/receivers/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -259,7 +259,7 @@ public boolean deleteUserInteractionsReceived(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/interactions/receivers/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java index ecb7e6e0..4065a994 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/interactions/pictures/InteractionPicturesEndpoint.java @@ -50,10 +50,7 @@ public CompletableFuture> getRandomInteractionPicture(final Int .setRoute("/interaction_pictures/" + type) .setAuthorization(apiKey) .buildAsync() - .thenApplyAsync((optionalRequest) -> { - if (optionalRequest.isPresent()) return Optional.of(optionalRequest.get().getData().get("url").asText()); - throw new CompletionException("Unable to get a random interaction picture. Request is empty.", null); - }); + .thenApplyAsync((request) -> Optional.of(request.getData().get("url").asText())); } } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/minigames/winstreaks/WinStreaksEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/minigames/winstreaks/WinStreaksEndpoint.java index c5459aed..6ae4f6f3 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/minigames/winstreaks/WinStreaksEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/minigames/winstreaks/WinStreaksEndpoint.java @@ -30,7 +30,7 @@ public HashMap getAllWinStreaks() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/minigames/win_streaks") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("win_streaks").forEach((winStreak) -> { String userID = winStreak.get("user_id").asText(); @@ -55,7 +55,7 @@ public WinStreak getUserWinStreak(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/minigames/win_streaks/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); JsonNode winStreak = request.getData().get("win_streaks"); int ticTacToeWins = winStreak.get("tic_tac_toe").asInt(); @@ -82,7 +82,7 @@ public boolean updateUserWinStreak(final String userID, final MinigameType type, .addParameter("type", type.getType()) .addParameter("value", String.valueOf(winstreak)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -101,7 +101,7 @@ public boolean createUserWinStreak(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.POST) .setRoute("/minigames/win_streaks/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -118,7 +118,7 @@ public boolean deleteUserWinStreak(final String userID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/minigames/win_streaks/" + userID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/polls/PollsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/polls/PollsEndpoint.java index 38b1b3d8..e6c49216 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/polls/PollsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/polls/PollsEndpoint.java @@ -37,7 +37,7 @@ public HashMap> getAllPolls() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/polls") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("polls").forEach((poll) -> { String guildID = poll.get("guild_id").asText(); @@ -66,7 +66,7 @@ public ArrayList getGuildPolls(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/polls/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("polls").forEach((poll) -> { String messageID = poll.get("message_id").asText(); @@ -95,7 +95,7 @@ public boolean createPoll(final String guildID, final Poll poll) .addParameter("message_id", poll.getMessageID()) .addParameter("ending_time", poll.getEndingTime().toString()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -125,7 +125,7 @@ public boolean deletePoll(final String guildID, final String messageID) .setRoute("/polls/" + guildID) .addParameter("message_id", messageID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/raffles/RafflesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/raffles/RafflesEndpoint.java index f4907a66..debde6ff 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/raffles/RafflesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/raffles/RafflesEndpoint.java @@ -37,7 +37,7 @@ public HashMap> getAllRaffles() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/raffles") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("raffles").forEach((raffle) -> { String guildID = raffle.get("guild_id").asText(); @@ -67,7 +67,7 @@ public ArrayList getGuildRaffles(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/raffles/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("raffles").forEach((raffle) -> { String messageID = raffle.get("message_id").asText(); @@ -98,7 +98,7 @@ public boolean createRaffle(final String guildID, final Raffle raffle) .addParameter("ending_time", raffle.getEndingTime().toString()) .addParameter("winner_amount", String.valueOf(raffle.getWinnerAmount())) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -132,7 +132,7 @@ public boolean deleteRaffle(final String guildID, final String messageID) .setRoute("/raffles/" + guildID) .addParameter("message_id", messageID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/twitches/TwitchEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/twitches/TwitchEndpoint.java index 4e55fb77..2ce9b576 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/twitches/TwitchEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/twitches/TwitchEndpoint.java @@ -39,7 +39,7 @@ public HashMap> getAllTwitches() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/guilds/twitch") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("guilds_twitch").forEach((guildTwitch) -> { String guildID = guildTwitch.get("guild_id").asText(); @@ -65,7 +65,7 @@ public ArrayList getGuildTwitches(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/guilds/twitch/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); try { ObjectMapper mapper = new ObjectMapper(); @@ -95,7 +95,7 @@ public boolean addGuildTwitch(final String guildID, final String twitchChannelNa .setRoute("/guilds/twitch/" + guildID) .addParameter("twitch_channel", twitchChannelName) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -115,7 +115,7 @@ public boolean removeGuildTwitch(final String guildID, final String twitchChanne .setRoute("/guilds/twitch/" + guildID) .addParameter("twitch_channel", twitchChannelName) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/version/VersionsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/version/VersionsEndpoint.java index 45244e86..018a2bc7 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/version/VersionsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/version/VersionsEndpoint.java @@ -30,7 +30,7 @@ public String getCurrentCafeBotVersion() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/cafeBot") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getData().get("bot_information").get("version").asText(); } @@ -52,7 +52,7 @@ public boolean updateCurrentCafeBotVersion(final String versionNumber) .setRoute("/cafeBot") .addParameter("version", versionNumber) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/voicebinds/VoiceChannelBindsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/voicebinds/VoiceChannelBindsEndpoint.java index 35da6977..23f3d7b0 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/voicebinds/VoiceChannelBindsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/voicebinds/VoiceChannelBindsEndpoint.java @@ -33,7 +33,7 @@ public HashMap> getAllVoiceChannelBinds() Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/voice_binds") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); HashMap> voiceBinds = new HashMap<>(); @@ -62,7 +62,7 @@ public ArrayList getGuildVoiceChannelBinds(final String guildI Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/voice_binds/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); ArrayList voiceChannelBinds = new ArrayList<>(); @@ -93,7 +93,7 @@ public boolean addVoiceChannelBind(final String guildID, final VoiceChannelBind .addParameter("voice_channel_id", voiceChannelBind.getVoiceChannelID()) .addParameter("role_id", voiceChannelBind.getRoleID()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -114,7 +114,7 @@ public boolean deleteVoiceChannelBind(final String guildID, final VoiceChannelBi .addParameter("voice_channel_id", voiceChannelBind.getVoiceChannelID()) .addParameter("role_id", voiceChannelBind.getRoleID()) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/welcomes/WelcomesEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/welcomes/WelcomesEndpoint.java index 1ea50a39..b386052e 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/welcomes/WelcomesEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/welcomes/WelcomesEndpoint.java @@ -28,7 +28,7 @@ public ArrayList getAllGuildWelcomes() throws AuthorizationExcepti Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/welcomes") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData() .get("welcomes") @@ -50,7 +50,7 @@ public GuildWelcome getGuildWelcome(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/welcomes/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); JsonNode guildWelcome = request.getData().get("welcome"); @@ -74,7 +74,7 @@ public boolean updateGuildWelcome(final GuildWelcome guildWelcome) .addParameter("image_url", guildWelcome.getImageURL().orElse(null)) .addParameter("message", guildWelcome.getMessage().orElse(null)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } @@ -97,7 +97,7 @@ public boolean createGuildWelcome(final GuildWelcome guildWelcome) .addParameter("image_url", guildWelcome.getImageURL().orElse(null)) .addParameter("message", guildWelcome.getMessage().orElse(null)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -114,7 +114,7 @@ public boolean deleteGuildWelcome(final String guildID) Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE) .setRoute("/welcomes/" + guildID) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/words/WordsEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/words/WordsEndpoint.java index b0ed067f..856efae2 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/words/WordsEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/endpoints/words/WordsEndpoint.java @@ -28,7 +28,7 @@ public ArrayList getAllWords() throws ResponseException { Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/words") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); ArrayList wordList = new ArrayList<>(); request.getData() @@ -49,7 +49,7 @@ public Word getWord(final String word) throws NotFoundException, ResponseExcepti Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET) .setRoute("/words/" + word) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return new Word(word, request.getData().get("word").get("uses").asInt()); } @@ -70,7 +70,7 @@ public boolean updateWord(final String word, final int uses) .setRoute("/words/" + word) .addParameter("uses", String.valueOf(uses)) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java index fbc4547d..05b5693f 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/requests/RequestBuilder.java @@ -82,7 +82,7 @@ public RequestBuilder setAuthorization(final String apiKey) { } // TODO: Change this to private. - public Optional build() { + public Request build() { try { URIBuilder uriBuilder = new URIBuilder(apiURL + route); parameters.forEach(uriBuilder::setParameter); @@ -105,51 +105,18 @@ public Optional build() { case 500 -> throw new ResponseException(request); } - return Optional.of(request); + return request; } catch (URISyntaxException | ExecutionException | InterruptedException | IOException e) { Logger.getLogger(RequestBuilder.class.getName()).log(Level.WARNING, "Error queuing request: " + e.getMessage()); - return Optional.empty(); + throw new CompletionException(e); } -// try { -// httpClient = HttpClients.createDefault(); -// uriBuilder = new URIBuilder(apiURL + route); -// parameters.forEach((key, value) -> { -// uriBuilder.setParameter(key, value); -// }); -// authorization = new BasicHeader("Authorization", apiKey); -// -// switch (requestType) { -// case GET -> httpResponse = get(); -// case POST -> httpResponse = post(); -// case PATCH -> httpResponse = patch(); -// case DELETE -> httpResponse = delete(); -// } -// -// int statusCode = httpResponse.getStatusLine().getStatusCode(); -// HttpEntity httpEntity = httpResponse.getEntity(); -// try (InputStream inputStream = httpEntity.getContent()) { -// Request request = new Request(statusCode, new ObjectMapper().readTree(inputStream)); -// -// // Catching Status Codes -// if (request.getStatusCode() == 400) throw new UndefinedVariableException(request); -// if (request.getStatusCode() == 401) throw new AuthorizationException(request); -// if (request.getStatusCode() == 404) throw new NotFoundException(request); -// if (request.getStatusCode() == 409) throw new ConflictException(request); -// if (request.getStatusCode() == 418) throw new TeaPotException(request); -// if (request.getStatusCode() == 500) throw new ResponseException(request); -// -// return Optional.of(request); -// } -// } catch (URISyntaxException | IOException e) { -// return Optional.empty(); -// } } /** * Builds the {@link Request} asynchronously on a separate {@link Thread}. */ - public CompletableFuture> buildAsync() { + public CompletableFuture buildAsync() { ExecutorService exec = Executors.newSingleThreadExecutor(); return CompletableFuture.supplyAsync(this::build, exec); } diff --git a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/user/UsersEndpoint.java b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/user/UsersEndpoint.java index 412f1452..51dbf64c 100644 --- a/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/user/UsersEndpoint.java +++ b/wrappers/cafe-api-wrapper/src/main/java/com/beanbeanjuice/cafeapi/wrapper/user/UsersEndpoint.java @@ -28,7 +28,7 @@ public ArrayList getUsers() throws AuthorizationException, ResponseExcepti Request request = RequestBuilder.create(RequestRoute.CAFE, RequestType.GET) .setRoute("/users") .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); request.getData().get("users").forEach(user -> { int id = user.get("user_id").intValue(); @@ -51,7 +51,7 @@ public boolean signUp(final String username, final String password) { .setRoute("/user/signup") .addParameter("username", username) .addParameter("password", password) - .build().orElseThrow(); + .build(); return request.getStatusCode() == 201; } @@ -67,7 +67,7 @@ public User getUser(final String username) throws AuthorizationException, Respon Request request = RequestBuilder.create(RequestRoute.CAFE, RequestType.GET) .setRoute("/user/" + username) .setAuthorization(apiKey) - .build().orElseThrow(); + .build(); int ID = request.getData().get("user").get("user_id").intValue(); UserType userType = UserType.valueOf(request.getData().get("user").get("user_type").textValue()); @@ -82,7 +82,7 @@ public User getUser(final String username) throws AuthorizationException, Respon public boolean deleteUser(final String username) { Request request = RequestBuilder.create(RequestRoute.CAFE, RequestType.DELETE) .setRoute("/user/" + username) - .setAuthorization(apiKey).build().orElseThrow(); + .setAuthorization(apiKey).build(); return request.getStatusCode() == 200; } diff --git a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUserTest.java b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java similarity index 61% rename from wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUserTest.java rename to wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java index 876d91d5..29f26446 100644 --- a/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUserTest.java +++ b/wrappers/cafe-api-wrapper/src/test/java/com/beanbeanjuice/cafeapi/beta/CafeUsersEndpointTests.java @@ -13,71 +13,86 @@ import org.junit.jupiter.api.Test; import java.sql.Timestamp; +import java.util.concurrent.ExecutionException; -public class CafeUserTest { +public class CafeUsersEndpointTests { @Test @DisplayName("Cafe Users Endpoint Test") - public void testCafeUsersEndpoint() { + public void testCafeUsersEndpoint() throws ExecutionException, InterruptedException { CafeAPI cafeAPI = new CafeAPI("beanbeanjuice", System.getenv("API_PASSWORD"), RequestLocation.BETA); // Makes sure the user does not exist beforehand. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().deleteCafeUser("236654580300120064")); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().deleteCafeUser("236654580300120064").get()); // Makes sure the user does not exist when trying to get it. - Assertions.assertThrows(NotFoundException.class, () -> cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064")); + try { + cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get(); + Assertions.fail(); + } catch (Exception e) { + Assertions.assertInstanceOf(NotFoundException.class, e.getCause()); + } // Creates the user. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().createCafeUser("236654580300120064")); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().createCafeUser("236654580300120064").get()); // Makes sure the user cannot be created twice. - Assertions.assertThrows(ConflictException.class, () -> cafeAPI.getCafeUsersEndpoint().createCafeUser("236654580300120064")); + try { + cafeAPI.getCafeUsersEndpoint().createCafeUser("236654580300120064").get(); + Assertions.fail(); + } catch (Exception e) { + Assertions.assertInstanceOf(ConflictException.class, e.getCause()); + } // Makes sure the user exists in the array list. Assertions.assertTrue(() -> { - for (CafeUser cafeUser : cafeAPI.getCafeUsersEndpoint().getAllCafeUsers()) { - if (cafeUser.getUserID().equals("236654580300120064")) { - return true; + try { + for (CafeUser cafeUser : cafeAPI.getCafeUsersEndpoint().getAllCafeUsers().get()) { + if (cafeUser.getUserID().equals("236654580300120064")) { + return true; + } } + } catch (Exception e) { + Assertions.fail(); } return false; }); // Makes sure the user ID matches the one retrieved. - Assertions.assertEquals("236654580300120064", cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getUserID()); + Assertions.assertEquals("236654580300120064", cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getUserID()); // Makes sure all settings are default when first created. - Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getBeanCoins()); - Assertions.assertNull(cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getLastServingTime().orElse(null)); - Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getOrdersBought()); - Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getOrdersReceived()); + Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getBeanCoins()); + Assertions.assertNull(cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getLastServingTime().orElse(null)); + Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getOrdersBought()); + Assertions.assertEquals(0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getOrdersReceived()); // Makes sure the beanCoins can be changed. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.BEAN_COINS, 100.0)); - Assertions.assertEquals(100.0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getBeanCoins()); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.BEAN_COINS, 100.0).get()); + Assertions.assertEquals(100.0, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getBeanCoins()); // Makes sure the timestamp can be changed. Timestamp currentTimeStamp = CafeGeneric.parseTimestamp(new Timestamp(System.currentTimeMillis()).toString()).orElse(null); - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, currentTimeStamp)); - Assertions.assertEquals(currentTimeStamp, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getLastServingTime().orElse(null)); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, currentTimeStamp).get()); + Assertions.assertEquals(currentTimeStamp, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getLastServingTime().orElse(null)); // Makes sure the timestamp can be changed to null. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, null)); - Assertions.assertNull(cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getLastServingTime().orElse(null)); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, null).get()); + Assertions.assertNull(cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getLastServingTime().orElse(null)); // Makes sure the orders bought can be updated. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.ORDERS_BOUGHT, 10)); - Assertions.assertEquals(10, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getOrdersBought()); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.ORDERS_BOUGHT, 10).get()); + Assertions.assertEquals(10, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getOrdersBought()); // Makes sure the orders received can be updated. - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.ORDERS_RECEIVED, 15)); - Assertions.assertEquals(15, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").getOrdersReceived()); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.ORDERS_RECEIVED, 15).get()); + Assertions.assertEquals(15, cafeAPI.getCafeUsersEndpoint().getCafeUser("236654580300120064").get().getOrdersReceived()); // Makes sure a TeaPotException is thrown when trying to update using an invalid value. - Assertions.assertThrows(TeaPotException.class, () -> cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, 100)); + Assertions.assertThrows(TeaPotException.class, () -> cafeAPI.getCafeUsersEndpoint().updateCafeUser("236654580300120064", CafeType.LAST_SERVING_TIME, 100).get()); // Deletes a cafe user - Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().deleteCafeUser("236654580300120064")); + Assertions.assertTrue(cafeAPI.getCafeUsersEndpoint().deleteCafeUser("236654580300120064").get()); } } diff --git a/wrappers/kawaii-api-wrapper/src/test/java/com/beanbeanjuice/GIFEndpointTest.java b/wrappers/kawaii-api-wrapper/src/test/java/com/beanbeanjuice/GIFEndpointTest.java index 59e8f63b..ee25ff7c 100644 --- a/wrappers/kawaii-api-wrapper/src/test/java/com/beanbeanjuice/GIFEndpointTest.java +++ b/wrappers/kawaii-api-wrapper/src/test/java/com/beanbeanjuice/GIFEndpointTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Optional; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.function.Consumer; @@ -25,20 +26,25 @@ public void testGIFEndpoint() throws ExecutionException, InterruptedException { @Test @DisplayName("GIF Endpoint Asynchronous Test") - public void testGIFEndpointAsync() throws InterruptedException { + public void testGIFEndpointAsync() { KawaiiAPI kawaiiAPI = new KawaiiAPI("anonymous"); ArrayList links = new ArrayList<>(); Consumer> addToLinks = (stringOptional) -> stringOptional.ifPresent(links::add); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture1 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture2 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture3 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture4 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture5 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); + CompletableFuture hugFuture6 = kawaiiAPI.getGifEndpoint().getGIF("hug").thenAcceptAsync(addToLinks); - Thread.sleep(3000); + hugFuture1.join(); + hugFuture2.join(); + hugFuture3.join(); + hugFuture4.join(); + hugFuture5.join(); + hugFuture6.join(); Assertions.assertEquals(6, links.size()); }