[16.9k Lines] Fixed an error whilst fetching an unknown account in the API
This commit is contained in:
parent
ca4615c695
commit
cf1367bd4c
@ -36,7 +36,7 @@ public class API {
|
|||||||
.create();
|
.create();
|
||||||
private static final List<Class<? extends IModel>> models = new ArrayList<>();
|
private static final List<Class<? extends IModel>> models = new ArrayList<>();
|
||||||
private static final Map<Object, List<Method>> routes = new HashMap<>();
|
private static final Map<Object, List<Method>> routes = new HashMap<>();
|
||||||
private static final boolean requiresAuthentication = false;
|
private static final boolean requiresAuthentication = true;
|
||||||
|
|
||||||
// Rate Limiting
|
// Rate Limiting
|
||||||
private static final int rateLimit = 120; // The amount of requests a minute
|
private static final int rateLimit = 120; // The amount of requests a minute
|
||||||
@ -114,15 +114,21 @@ public class API {
|
|||||||
// if there is a problem checking the key or the key is invalid
|
// if there is a problem checking the key or the key is invalid
|
||||||
if (requiresAuthentication) {
|
if (requiresAuthentication) {
|
||||||
String apiKey = request.headers("key");
|
String apiKey = request.headers("key");
|
||||||
if (apiKey == null)
|
if (apiKey == null) {
|
||||||
|
response.status(401);
|
||||||
throw new APIException("Unauthorized");
|
throw new APIException("Unauthorized");
|
||||||
|
}
|
||||||
// Check if the provided API key is valid
|
// Check if the provided API key is valid
|
||||||
Optional<List<APIKey>> keys = apiKeyRepository.lookup(apiKey);
|
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");
|
throw new APIException("Unauthorized");
|
||||||
|
}
|
||||||
key = keys.get().get(0);
|
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");
|
throw new APIException("Unauthorized");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Checking if the request has the appropriate headers for the get route
|
// Checking if the request has the appropriate headers for the get route
|
||||||
if (restPath.headers().length > 0) {
|
if (restPath.headers().length > 0) {
|
||||||
@ -160,9 +166,11 @@ public class API {
|
|||||||
jsonObject.addProperty("error", message);
|
jsonObject.addProperty("error", message);
|
||||||
// If the caught exception is not an API exception, print the stacktrace
|
// If the caught exception is not an API exception, print the stacktrace
|
||||||
if (!(ex instanceof APIException)) {
|
if (!(ex instanceof APIException)) {
|
||||||
|
response.status(500);
|
||||||
System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:");
|
System.err.println("The route \"" + entry.getKey().getClass().getSimpleName() + "\" raised an exception:");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
} else if (response.status() == 200)
|
||||||
|
response.status(502);
|
||||||
}
|
}
|
||||||
return gson.toJson(jsonObject);
|
return gson.toJson(jsonObject);
|
||||||
});
|
});
|
||||||
|
@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class AccountRoute {
|
public class AccountRoute {
|
||||||
// Account model cache for players that were looked up via the account route
|
// Account model cache for players that were looked up via the account route
|
||||||
public static final Cache<UUID, AccountModel> CACHE = CacheBuilder.newBuilder()
|
public static final Cache<UUID, AccountModel> CACHE = CacheBuilder.newBuilder()
|
||||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private final AccountRepository accountRepository;
|
private final AccountRepository accountRepository;
|
||||||
@ -42,6 +42,8 @@ public class AccountRoute {
|
|||||||
AccountModel account = CACHE.getIfPresent(uuid);
|
AccountModel account = CACHE.getIfPresent(uuid);
|
||||||
if (account == null) {
|
if (account == null) {
|
||||||
account = accountRepository.getAccount(uuid);
|
account = accountRepository.getAccount(uuid);
|
||||||
|
if (account == null)
|
||||||
|
throw new APIException("Account not found");
|
||||||
account.setTimeCached(System.currentTimeMillis());
|
account.setTimeCached(System.currentTimeMillis());
|
||||||
CACHE.put(uuid, account);
|
CACHE.put(uuid, account);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package zone.themcgamer.core.deliveryMan;
|
|||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import zone.themcgamer.core.common.SkullTexture;
|
|
||||||
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
|
import zone.themcgamer.core.deliveryMan.rewardPackage.RewardPackage;
|
||||||
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.DailyRewardPackage;
|
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.DailyRewardPackage;
|
||||||
import zone.themcgamer.core.deliveryMan.rewardPackage.impl.MonthlyRewardPackage;
|
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 com.jagrosh.jdautilities.command.CommandEvent;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
|
||||||
import net.dv8tion.jda.api.entities.TextChannel;
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
import zone.themcgamer.discordbot.command.BaseCommand;
|
import zone.themcgamer.discordbot.command.BaseCommand;
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package zone.themcgamer.discordbot.command.impl;
|
|||||||
import com.jagrosh.jdautilities.command.CommandEvent;
|
import com.jagrosh.jdautilities.command.CommandEvent;
|
||||||
import net.dv8tion.jda.api.Permission;
|
import net.dv8tion.jda.api.Permission;
|
||||||
import net.dv8tion.jda.api.entities.Activity;
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
import zone.themcgamer.discordbot.BotConstants;
|
|
||||||
import zone.themcgamer.discordbot.MGZBot;
|
import zone.themcgamer.discordbot.MGZBot;
|
||||||
import zone.themcgamer.discordbot.command.BaseCommand;
|
import zone.themcgamer.discordbot.command.BaseCommand;
|
||||||
import zone.themcgamer.discordbot.guild.Guild;
|
import zone.themcgamer.discordbot.guild.Guild;
|
||||||
|
@ -13,4 +13,4 @@ public class SpawnCommand {
|
|||||||
public void onCommand(CommandProvider command) {
|
public void onCommand(CommandProvider command) {
|
||||||
command.getPlayer().teleport(hub.getSpawn());
|
command.getPlayer().teleport(hub.getSpawn());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +1,12 @@
|
|||||||
package zone.themcgamer.test;
|
package zone.themcgamer.test;
|
||||||
|
|
||||||
import lombok.SneakyThrows;
|
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.net.URI;
|
||||||
import java.util.Optional;
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Braydon
|
* @author Braydon
|
||||||
@ -16,15 +15,34 @@ import java.util.Optional;
|
|||||||
public class Test {
|
public class Test {
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new JedisController().start();
|
HttpClient client = HttpClient.newBuilder()
|
||||||
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
Optional<ServerGroupRepository> repository = RedisRepository.getRepository(ServerGroupRepository.class);
|
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||||
while (repository.isPresent()) {
|
.connectTimeout(Duration.ofSeconds(30L))
|
||||||
List<ServerGroup> cached = repository.get().getCached();
|
.build();
|
||||||
for (ServerGroup serverGroup : cached)
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
System.out.println(serverGroup.toString());
|
.GET()
|
||||||
System.out.println("total cached = " + cached.size());
|
.uri(URI.create("https://api.cnetwork.club/v1/status/Rainnny"))
|
||||||
Thread.sleep(1000L);
|
.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user