Search in sources :

Example 6 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class WebpanelManager method sendRequest.

public void sendRequest(HttpRequestBuilder request) {
    try {
        request.addParameter("token", requestToken);
        RequestResponse response = request.sendRequest();
        if (response.getResponseCode() != 200) {
            Logger.debug(response.getResponseMessage());
            throw new Exception("Error while sending request to " + response.getEndpointUrl());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RequestResponse(de.foryasee.httprequest.RequestResponse)

Example 7 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class Bitly method shorten.

/**
 * Shortens a URL with bit.ly.
 *
 * @param longURL the URL to shorten.
 * @return the shortened URL.
 * @throws IllegalArgumentException if the long URI was invalid.
 * @throws HTTPException            if the bit.ly API returns a response code unlike 200 'OK'.
 * @throws RuntimeException         if the http request threw an unknown error.
 */
public static String shorten(String longURL) {
    HttpRequestBuilder request = new HttpRequestBuilder(API_URL, RequestType.GET);
    request.addParameter("access_token", Info.BITLY_TOKEN);
    request.addParameter("longUrl", longURL);
    request.addParameter("format", "json");
    RequestResponse result;
    try {
        result = request.sendRequest();
    } catch (Exception e) {
        // catch 'anonymous' exceptions
        throw new RuntimeException("An unknown exception occurred while fetching a bit.ly http request", e);
    }
    JSONObject response = new JSONObject(result.getResponseMessage());
    // check if uri was valid
    if (response.getString("status_txt").equals("INVALID_URI"))
        throw new IllegalArgumentException("'" + longURL + "' is not a valid URL.");
    else // ensure 'OK' status response
    if (response.getInt("status_code") == 400)
        throw new HTTPException(response.getInt("status_code"));
    // return shortened url
    return response.getJSONObject("data").getString("url");
}
Also used : HTTPException(javax.xml.ws.http.HTTPException) JSONObject(org.json.JSONObject) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse) HTTPException(javax.xml.ws.http.HTTPException)

Example 8 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class MojangUtil method fetchName.

private String fetchName(String playername) {
    HttpRequestBuilder request = new HttpRequestBuilder("https://api.mojang.com/users/profiles/minecraft/" + playername, RequestType.GET);
    RequestResponse response = null;
    try {
        response = request.sendRequest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (response.getResponseCode() != 200)
        return null;
    JSONObject json = null;
    try {
        json = (JSONObject) parser.parse(response.getResponseMessage());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return json.get("name").toString();
}
Also used : JSONObject(org.json.simple.JSONObject) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse) ParseException(org.json.simple.parser.ParseException) ParseException(org.json.simple.parser.ParseException)

Example 9 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class MojangUtil method fetchUUID.

public String fetchUUID(String playername) {
    HttpRequestBuilder request = new HttpRequestBuilder("https://api.mojang.com/users/profiles/minecraft/" + playername, RequestType.GET);
    RequestResponse response = null;
    try {
        response = request.sendRequest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (response.getResponseCode() != 200)
        return null;
    JSONObject json = null;
    try {
        json = (JSONObject) parser.parse(response.getResponseMessage());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return json.get("id").toString();
}
Also used : JSONObject(org.json.simple.JSONObject) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse) ParseException(org.json.simple.parser.ParseException) ParseException(org.json.simple.parser.ParseException)

Example 10 with RequestResponse

use of de.foryasee.httprequest.RequestResponse in project Rubicon by Rubicon-Bot.

the class MojangUtil method fetchStatus.

public JSONArray fetchStatus() {
    HttpRequestBuilder request = new HttpRequestBuilder("https://status.mojang.com/check", RequestType.GET);
    RequestResponse response = null;
    try {
        response = request.sendRequest();
    } catch (Exception e) {
        e.printStackTrace();
    }
    JSONArray res = null;
    try {
        res = ((JSONArray) parser.parse(response.getResponseMessage()));
    } catch (ParseException e) {
        Logger.error(e);
    }
    return res;
}
Also used : JSONArray(org.json.simple.JSONArray) HttpRequestBuilder(de.foryasee.httprequest.HttpRequestBuilder) RequestResponse(de.foryasee.httprequest.RequestResponse) ParseException(org.json.simple.parser.ParseException) ParseException(org.json.simple.parser.ParseException)

Aggregations

RequestResponse (de.foryasee.httprequest.RequestResponse)11 HttpRequestBuilder (de.foryasee.httprequest.HttpRequestBuilder)10 JSONObject (org.json.simple.JSONObject)7 JSONParser (org.json.simple.parser.JSONParser)5 ParseException (org.json.simple.parser.ParseException)4 JSONArray (org.json.simple.JSONArray)3 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)2 RequestHeader (de.foryasee.httprequest.RequestHeader)1 Date (java.util.Date)1 HTTPException (javax.xml.ws.http.HTTPException)1 JSONObject (org.json.JSONObject)1