[16.9k Lines] Fixed an error whilst fetching an unknown account in the API

This commit is contained in:
Rainnny7 2021-02-19 21:08:57 -05:00
parent ca4615c695
commit cf1367bd4c
7 changed files with 51 additions and 26 deletions

@ -36,7 +36,7 @@ public class API {
.create();
private static final List<Class<? extends IModel>> models = new ArrayList<>();
private static final Map<Object, List<Method>> routes = new HashMap<>();
private static final boolean requiresAuthentication = false;
private static final boolean requiresAuthentication = true;
// Rate Limiting
private static final int rateLimit = 120; // The amount of requests a minute
@ -114,16 +114,22 @@ public class API {
// if there is a problem checking the key or the key is invalid
if (requiresAuthentication) {
String apiKey = request.headers("key");
if (apiKey == null)
if (apiKey == null) {
response.status(401);
throw new APIException("Unauthorized");
}
// Check if the provided API key is valid
Optional<List<APIKey>> keys = apiKeyRepository.lookup(apiKey);
if (keys.isEmpty() || (keys.get().isEmpty()))
if (keys.isEmpty() || (keys.get().isEmpty())) {
response.status(401);
throw new APIException("Unauthorized");
}
key = keys.get().get(0);
if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel()))
if (restPath.accessLevel() == APIAccessLevel.DEV && (restPath.accessLevel() != key.getAccessLevel())) {
response.status(401);
throw new APIException("Unauthorized");
}
}
// Checking if the request has the appropriate headers for the get route
if (restPath.headers().length > 0) {
for (String header : restPath.headers()) {
@ -160,9 +166,11 @@ public class API {
jsonObject.addProperty("error", message);
// If the caught exception is not an API exception, print the stacktrace
if (!(ex instanceof APIException)) {
response.status(500);
System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:");
ex.printStackTrace();
}
} else if (response.status() == 200)
response.status(502);
}
return gson.toJson(jsonObject);
});

@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit;
public class AccountRoute {
// Account model cache for players that were looked up via the account route
public static final Cache<UUID, AccountModel> CACHE = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.expireAfterWrite(5, TimeUnit.MINUTES)
.build();
private final AccountRepository accountRepository;
@ -42,6 +42,8 @@ public class AccountRoute {
AccountModel account = CACHE.getIfPresent(uuid);
if (account == null) {
account = accountRepository.getAccount(uuid);
if (account == null)
throw new APIException("Account not found");
account.setTimeCached(System.currentTimeMillis());
CACHE.put(uuid, account);
}

@ -2,7 +2,6 @@ package zone.themcgamer.core.deliveryMan;
import lombok.AllArgsConstructor;
import lombok.Getter;
import zone.themcgamer.core.common.SkullTexture;
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.DailyRewardPackage;
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.MonthlyRewardPackage;

@ -2,7 +2,6 @@ package zone.themcgamer.discordbot.command.impl;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import zone.themcgamer.discordbot.command.BaseCommand;

@ -3,7 +3,6 @@ package zone.themcgamer.discordbot.command.impl;
import com.jagrosh.jdautilities.command.CommandEvent;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import zone.themcgamer.discordbot.BotConstants;
import zone.themcgamer.discordbot.MGZBot;
import zone.themcgamer.discordbot.command.BaseCommand;
import zone.themcgamer.discordbot.guild.Guild;

@ -1,13 +1,12 @@
package zone.themcgamer.test;
import lombok.SneakyThrows;
import zone.themcgamer.data.jedis.JedisController;
import zone.themcgamer.data.jedis.data.ServerGroup;
import zone.themcgamer.data.jedis.repository.RedisRepository;
import zone.themcgamer.data.jedis.repository.impl.ServerGroupRepository;
import java.util.List;
import java.util.Optional;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
/**
* @author Braydon
@ -16,15 +15,34 @@ import java.util.Optional;
public class Test {
@SneakyThrows
public static void main(String[] args) {
new JedisController().start();
Optional<ServerGroupRepository> repository = RedisRepository.getRepository(ServerGroupRepository.class);
while (repository.isPresent()) {
List<ServerGroup> cached = repository.get().getCached();
for (ServerGroup serverGroup : cached)
System.out.println(serverGroup.toString());
System.out.println("total cached = " + cached.size());
Thread.sleep(1000L);
}
HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(30L))
.build();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://api.cnetwork.club/v1/status/Rainnny"))
.timeout(Duration.ofMinutes(1L))
.header("Content-Type", "application/json")
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenAccept(response -> {
System.out.println("status = " + response.statusCode());
System.out.println("body = " + response.body());
});
// HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// System.out.println("status = " + response.statusCode());
// System.out.println("body = " + response.body());
//
// Optional<ServerGroupRepository> repository = RedisRepository.getRepository(ServerGroupRepository.class);
// while (repository.isPresent()) {
// List<ServerGroup> cached = repository.get().getCached();
// for (ServerGroup serverGroup : cached)
// System.out.println(serverGroup.toString());
// System.out.println("total cached = " + cached.size());
// Thread.sleep(1000L);
// }
Thread.sleep(2000L);
}
}