From bc25422492c13ac6b5ffac2056c91594e5a1f5ed Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 30 Jul 2020 21:29:41 -0500 Subject: [PATCH] Move Webhook out to main package and add send utility Signed-off-by: Etzelia --- pom.xml | 2 +- src/main/java/xyz/etztech/Javacord.java | 24 +++++++++++++++++++ .../java/xyz/etztech/{embed => }/Webhook.java | 4 ++-- src/test/java/Test.java | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) rename src/main/java/xyz/etztech/{embed => }/Webhook.java (93%) diff --git a/pom.xml b/pom.xml index 5c2e3f6..6261ac3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ xyz.etztech javacord jar - 0.1.0 + 0.2.0 diff --git a/src/main/java/xyz/etztech/Javacord.java b/src/main/java/xyz/etztech/Javacord.java index 77a0bce..51901d6 100644 --- a/src/main/java/xyz/etztech/Javacord.java +++ b/src/main/java/xyz/etztech/Javacord.java @@ -1,5 +1,11 @@ package xyz.etztech; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; + public class Javacord { public static String escapeFormat(String input) { @@ -13,4 +19,22 @@ public class Javacord { public static String escapeQuote(String input) { return input.replaceAll("\"", "\\\\\""); } + + public static void sendWebhook(String webhookURL, Webhook webhook) throws Exception { + URL url = new URL(webhookURL); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.setDoOutput(true); + + byte[] out = webhook.toString().getBytes(StandardCharsets.UTF_8); + int length = out.length; + http.setFixedLengthStreamingMode(length); + http.setRequestProperty("Content-Type", "application/json; utf-8"); + http.setRequestProperty("User-Agent", "Javacord Agent"); + + try (OutputStream os = http.getOutputStream()) { + os.write(out, 0, out.length); + } + } } diff --git a/src/main/java/xyz/etztech/embed/Webhook.java b/src/main/java/xyz/etztech/Webhook.java similarity index 93% rename from src/main/java/xyz/etztech/embed/Webhook.java rename to src/main/java/xyz/etztech/Webhook.java index 9da91ea..9863ce3 100644 --- a/src/main/java/xyz/etztech/embed/Webhook.java +++ b/src/main/java/xyz/etztech/Webhook.java @@ -1,6 +1,6 @@ -package xyz.etztech.embed; +package xyz.etztech; -import xyz.etztech.Javacord; +import xyz.etztech.embed.Embed; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/test/java/Test.java b/src/test/java/Test.java index acd0921..81b6e0d 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -1,7 +1,7 @@ import xyz.etztech.Javacord; import xyz.etztech.embed.Author; import xyz.etztech.embed.Embed; -import xyz.etztech.embed.Webhook; +import xyz.etztech.Webhook; import java.time.OffsetDateTime;