use of io.github.binaryoverload.JSONConfig in project FlareBot by FlareBot.
the class GeneralUtils method jsonContains.
/**
* Checks if paths exist in the given json
* <p>
* Key of the {@link Pair} is a list of the paths that exist in the JSON
* Value of the {@link Pair} is a list of the paths that don't exist in the JSON
*
* @param json The JSON to check <b>Mustn't be null</b>
* @param paths The paths to check <b>Mustn't be null or empty</b>
* @return
*/
public static Pair<List<String>, List<String>> jsonContains(String json, String... paths) {
Objects.requireNonNull(json);
Objects.requireNonNull(paths);
if (paths.length == 0)
throw new IllegalArgumentException("Paths cannot be empty!");
JsonElement jelem = FlareBot.GSON.fromJson(json, JsonElement.class);
JSONConfig config = new JSONConfig(jelem.getAsJsonObject());
List<String> contains = new ArrayList<>();
List<String> notContains = new ArrayList<>();
for (String path : paths) {
if (path == null)
continue;
if (config.getElement(path).isPresent())
contains.add(path);
else
notContains.add(path);
}
return new Pair<>(Collections.unmodifiableList(contains), Collections.unmodifiableList(notContains));
}
use of io.github.binaryoverload.JSONConfig in project FlareBot by FlareBot.
the class CurrencyConversionUtil method isValidCurrency.
public static boolean isValidCurrency(String currency, boolean nonCrypto) throws IOException {
if (!normalEndpointAvailable())
return isValidCurrency(currency) && !nonCrypto;
Response res = WebUtils.get(new Request.Builder().get().url(CurrencyApiRoutes.NormalApi.LATEST_ALL.getCompiledUrl()));
if (!res.isSuccessful() || res.body() == null) {
return isValidCrytoCurrency(currency) && !nonCrypto;
}
JSONConfig config = new JSONConfig(res.body().byteStream());
if (config.getSubConfig("rates").isPresent()) {
JSONConfig rates = config.getSubConfig("rates").get();
return rates.getKeys(false).contains(currency.toUpperCase()) || (isValidCrytoCurrency(currency) && !nonCrypto);
}
return false;
}
use of io.github.binaryoverload.JSONConfig in project FlareBot by FlareBot.
the class CurrencyConversionUtil method isValidCrytoCurrency.
public static Boolean isValidCrytoCurrency(String currency) throws IOException {
if (!cryptoEndpintAvailable())
return false;
Response res = WebUtils.get(new Request.Builder().get().url(CurrencyApiRoutes.CrytoApi.BASIC_TICKER.getCompiledUrl("usd", currency)));
if (!res.isSuccessful() || res.body() == null)
throw new IOException();
JSONConfig config;
try {
config = new JSONConfig(res.body().byteStream());
} catch (JsonSyntaxException ignored) {
return false;
}
if (config.getBoolean("success").isPresent()) {
return config.getBoolean("success").get();
}
return false;
}
Aggregations