Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>in.bhargavrao</groupId>
<artifactId>natty</artifactId>
<version>v4.2.1</version>
<version>v4.2.2</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,61 @@
import org.jsoup.parser.Parser;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
* Created by bhargav.h on 10-Sep-16.
* AKA, TunaLib - All code is courtesy of Lord Tunaki
*/
public class JsonUtils {

public static JsonObject get(String url, String... data) throws IOException {
Connection.Response response = Jsoup.connect(url).data(data).method(Connection.Method.GET).ignoreContentType(true).ignoreHttpErrors(true).execute();
String json = response.body();
if (response.statusCode() != 200) {
throw new IOException("HTTP " + response.statusCode() + " fetching URL " + (url) + ". Body is: " + response.body());
}
JsonObject root = new JsonParser().parse(json).getAsJsonObject();
return root;
return execute(url, Connection.Method.GET, data);
}

public static JsonObject post(String url, String... data) throws IOException {
Connection.Response response = Jsoup.connect(url).data(data).method(Connection.Method.POST).ignoreContentType(true).ignoreHttpErrors(true).execute();
return execute(url, Connection.Method.POST, data);
}

private static JsonObject execute(String url, Connection.Method method, String... data) throws IOException {
String key = null;
String accessToken = null;
List<String> filtered = new ArrayList<>(data.length);

for (int i = 0; i + 1 < data.length; i += 2) {
String name = data[i];
String value = data[i + 1];
if ("access_token".equals(name)) {
accessToken = value;
} else if ("key".equals(name)) {
key = value;
} else {
filtered.add(name);
filtered.add(value);
}
}


Connection connection = Jsoup.connect(url)
.data(filtered.toArray(new String[0]))
.method(method)
.ignoreContentType(true)
.ignoreHttpErrors(true);

String bearer = accessToken != null ? accessToken : key;
if (bearer != null && !bearer.isEmpty()) {
connection = connection.header("Authorization", "Bearer " + bearer);
}

Connection.Response response = connection.execute();
String json = response.body();
if (response.statusCode() != 200) {
throw new IOException("HTTP " + response.statusCode() + " fetching URL " + (url) + ". Body is: " + response.body());
throw new IOException("HTTP " + response.statusCode() + " fetching URL " + url + ". Body is: " + response.body());
}
JsonObject root = new JsonParser().parse(json).getAsJsonObject();
return root;
return new JsonParser().parse(json).getAsJsonObject();
}

public static void handleBackoff(JsonObject root) {
if (root.has("backoff")) {
int backoff = root.get("backoff").getAsInt();
Expand Down