Skip to content

Commit

Permalink
Add HTTPRequestUtils with GET and String -> Json in Jsonutils
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim19375 committed Feb 20, 2021
1 parent 8d0ba12 commit 43212e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'java'
}

Expand All @@ -7,10 +8,16 @@ sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
compileJava.options.encoding 'UTF-8'

group 'me.dkim19375'
version '2.4.1'
version '2.5.0'

shadowJar {
relocate 'okio', 'me.dkim19375.dkim19375core.okio'
relocate 'com.squareup.okhttp', 'me.dkim19375.dkim19375core.okhttp'
}

repositories {
mavenCentral()
mavenLocal()
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/' }
Expand All @@ -24,4 +31,5 @@ dependencies {
compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.4'
compileOnly 'com.github.MilkBowl:VaultAPI:1.7'
compileOnly 'org.jetbrains:annotations:16.0.2'
implementation 'com.squareup.okhttp:okhttp:2.7.5'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.dkim19375.dkim19375core.external;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;

public class HTTPRequestUtils {
private HTTPRequestUtils() {}

private static final OkHttpClient client = new OkHttpClient();

public static String sendGET(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
return sendGET(new URL(url), headers, params);
}

public static String sendGET(URL url, Map<String, String> headers, Map<String, String> params) throws IOException {
final HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url.toString())).newBuilder();
for (Map.Entry<String, String> key : params.entrySet()) {
urlBuilder.addQueryParameter(key.getKey(), key.getValue());
}
final String newUrl = urlBuilder.build().toString();
final Request.Builder builder = new Request.Builder().url(newUrl).get();

for (Map.Entry<String, String> key : headers.entrySet()) {
builder.addHeader(key.getKey(), key.getValue());
}
final Response response = client.newCall(builder.build()).execute();
return response.body() == null ? null : response.body().string();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ public static void getJson(final @NotNull Consumer<JsonObject> consumer, final @
consumer.accept(jsonObject);
});
}

public static JsonObject getFromString(final String stringJson) {
final JsonParser parse = new JsonParser();
final JsonElement json = parse.parse(stringJson);
return json.getAsJsonObject();
}
}

0 comments on commit 43212e1

Please sign in to comment.