Search in sources :

Example 1 with WebParserUtils

use of me.duncte123.botcommons.web.WebParserUtils in project SkyBot by DuncteBot.

the class AirUtils method shortenUrl.

@Nonnull
public static PendingRequest<String> shortenUrl(String url, String googleKey, ObjectMapper mapper, String prefix) {
    final ObjectNode json = mapper.createObjectNode();
    json.set("dynamicLinkInfo", mapper.createObjectNode().put("domainUriPrefix", prefix).put("link", url));
    json.set("suffix", mapper.createObjectNode().put("option", // SHORT or UNGUESSABLE
    "SHORT"));
    try {
        return WebUtils.ins.postRequest("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + googleKey, JSONRequestBody.fromJackson(json)).setRateLimiter(RateLimiter.directLimiter()).build((r) -> {
            final ObjectNode response = toJSONObject(r, mapper);
            if (response == null) {
                return "Google did a fucky wucky and send invalid json";
            }
            return response.get("shortLink").asText();
        }, WebParserUtils::handleError);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        // Return a fake pending request to make sure that things don't break
        return new FakePendingRequest<>("JSON PARSING FAILED: " + e.getMessage());
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebParserUtils(me.duncte123.botcommons.web.WebParserUtils) Nonnull(javax.annotation.Nonnull)

Example 2 with WebParserUtils

use of me.duncte123.botcommons.web.WebParserUtils in project SkyBot by duncte123.

the class AirUtils method shortenUrl.

@Nonnull
public static PendingRequest<String> shortenUrl(String url, String googleKey, ObjectMapper mapper, String prefix) {
    final ObjectNode json = mapper.createObjectNode();
    json.set("dynamicLinkInfo", mapper.createObjectNode().put("domainUriPrefix", prefix).put("link", url));
    json.set("suffix", mapper.createObjectNode().put("option", // SHORT or UNGUESSABLE
    "SHORT"));
    try {
        return WebUtils.ins.postRequest("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + googleKey, JSONRequestBody.fromJackson(json)).build((r) -> {
            final ObjectNode response = toJSONObject(r, mapper);
            if (response == null) {
                return "Google did a fucky wucky and send invalid json";
            }
            return response.get("shortLink").asText();
        }, WebParserUtils::handleError);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        // Return a fake pending request to make sure that things don't break
        return new FakePendingRequest<>("JSON PARSING FAILED: " + e.getMessage());
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebParserUtils(me.duncte123.botcommons.web.WebParserUtils) Nonnull(javax.annotation.Nonnull)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Nonnull (javax.annotation.Nonnull)2 WebParserUtils (me.duncte123.botcommons.web.WebParserUtils)2