Skip to content

Commit 43212e1

Browse files
committed
Add HTTPRequestUtils with GET and String -> Json in Jsonutils
1 parent 8d0ba12 commit 43212e1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
plugins {
2+
id 'com.github.johnrengelman.shadow' version '6.1.0'
23
id 'java'
34
}
45

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

910
group 'me.dkim19375'
10-
version '2.4.1'
11+
version '2.5.0'
12+
13+
shadowJar {
14+
relocate 'okio', 'me.dkim19375.dkim19375core.okio'
15+
relocate 'com.squareup.okhttp', 'me.dkim19375.dkim19375core.okhttp'
16+
}
1117

1218
repositories {
1319
mavenCentral()
20+
mavenLocal()
1421
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
1522
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
1623
maven { url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/' }
@@ -24,4 +31,5 @@ dependencies {
2431
compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.4'
2532
compileOnly 'com.github.MilkBowl:VaultAPI:1.7'
2633
compileOnly 'org.jetbrains:annotations:16.0.2'
34+
implementation 'com.squareup.okhttp:okhttp:2.7.5'
2735
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.dkim19375.dkim19375core.external;
2+
3+
import com.squareup.okhttp.HttpUrl;
4+
import com.squareup.okhttp.OkHttpClient;
5+
import com.squareup.okhttp.Request;
6+
import com.squareup.okhttp.Response;
7+
8+
import java.io.IOException;
9+
import java.net.URL;
10+
import java.util.Map;
11+
import java.util.Objects;
12+
13+
public class HTTPRequestUtils {
14+
private HTTPRequestUtils() {}
15+
16+
private static final OkHttpClient client = new OkHttpClient();
17+
18+
public static String sendGET(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
19+
return sendGET(new URL(url), headers, params);
20+
}
21+
22+
public static String sendGET(URL url, Map<String, String> headers, Map<String, String> params) throws IOException {
23+
final HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url.toString())).newBuilder();
24+
for (Map.Entry<String, String> key : params.entrySet()) {
25+
urlBuilder.addQueryParameter(key.getKey(), key.getValue());
26+
}
27+
final String newUrl = urlBuilder.build().toString();
28+
final Request.Builder builder = new Request.Builder().url(newUrl).get();
29+
30+
for (Map.Entry<String, String> key : headers.entrySet()) {
31+
builder.addHeader(key.getKey(), key.getValue());
32+
}
33+
final Response response = client.newCall(builder.build()).execute();
34+
return response.body() == null ? null : response.body().string();
35+
}
36+
}

src/main/java/me/dkim19375/dkim19375core/external/JsonUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ public static void getJson(final @NotNull Consumer<JsonObject> consumer, final @
5555
consumer.accept(jsonObject);
5656
});
5757
}
58+
59+
public static JsonObject getFromString(final String stringJson) {
60+
final JsonParser parse = new JsonParser();
61+
final JsonElement json = parse.parse(stringJson);
62+
return json.getAsJsonObject();
63+
}
5864
}

0 commit comments

Comments
 (0)