Skip to content

Commit

Permalink
Added Async to the Cafe Users Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jun 24, 2024
1 parent e997786 commit c8acd2e
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -25,12 +24,10 @@ public CompletableFuture<HashMap<String, Timestamp>> getAllUserDonationTimes() {
.setRoute("/beanCoin/donation_users")
.setAuthorization(apiKey)
.buildAsync()
.thenApply((optionalRequest) -> {
.thenApply((request) -> {
HashMap<String, Timestamp> 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);

Expand All @@ -46,11 +43,7 @@ public CompletableFuture<Optional<Timestamp>> 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<Boolean> addDonationUser(final String userID, final Timestamp timeUntilNextDonation) {
Expand All @@ -59,23 +52,15 @@ public CompletableFuture<Boolean> 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<Boolean> deleteDonationUser(final String userID) {
return RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.DELETE)
.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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Birthday> requestToBirthdayMap(Request request) {
Expand All @@ -32,45 +27,22 @@ private HashMap<String, Birthday> 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<HashMap<String, Birthday>> 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<Optional<Birthday>> 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<Boolean> updateUserBirthday(final String userID, final Birthday birthday) {
BirthdayMonth month = birthday.getMonth();
int day = birthday.getDay();
Expand All @@ -81,36 +53,18 @@ public CompletableFuture<Boolean> 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<Boolean> 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<Boolean> createUserBirthday(final String userID, final Birthday birthday) {
BirthdayMonth month = birthday.getMonth();
int day = birthday.getDay();
Expand All @@ -121,9 +75,8 @@ public CompletableFuture<Boolean> 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;
});
}

Expand All @@ -132,9 +85,8 @@ public CompletableFuture<Boolean> 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;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CafeUser> getAllCafeUsers()
throws AuthorizationException, ResponseException {
ArrayList<CafeUser> cafeUsers = new ArrayList<>();

Request request = RequestBuilder.create(RequestRoute.CAFEBOT, RequestType.GET)
public CompletableFuture<ArrayList<CafeUser>> 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<CafeUser> 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<CafeUser> 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<Boolean> 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");
}
}
}
Expand All @@ -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<Boolean> 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<Boolean> 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);
}
Expand Down
Loading

0 comments on commit c8acd2e

Please sign in to comment.