Search in sources :

Example 6 with JSONConfig

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));
}
Also used : JsonElement(com.google.gson.JsonElement) JSONConfig(io.github.binaryoverload.JSONConfig) Pair(stream.flarebot.flarebot.util.Pair)

Example 7 with JSONConfig

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;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) JSONConfig(io.github.binaryoverload.JSONConfig)

Example 8 with JSONConfig

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;
}
Also used : Response(okhttp3.Response) JsonSyntaxException(com.google.gson.JsonSyntaxException) Request(okhttp3.Request) JSONConfig(io.github.binaryoverload.JSONConfig) IOException(java.io.IOException)

Aggregations

JSONConfig (io.github.binaryoverload.JSONConfig)8 Request (okhttp3.Request)5 Response (okhttp3.Response)4 ResultSet (com.datastax.driver.core.ResultSet)2 Row (com.datastax.driver.core.Row)2 JsonElement (com.google.gson.JsonElement)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Level (ch.qos.logback.classic.Level)1 PlayerManager (com.arsenarsen.lavaplayerbridge.PlayerManager)1 LibraryFactory (com.arsenarsen.lavaplayerbridge.libraries.LibraryFactory)1 UnknownBindingException (com.arsenarsen.lavaplayerbridge.libraries.UnknownBindingException)1 JDAMultiShard (com.arsenarsen.lavaplayerbridge.utils.JDAMultiShard)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 NativeAudioSendFactory (com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory)1 Sentry (io.sentry.Sentry)1 SentryClient (io.sentry.SentryClient)1