|
| 1 | +package xyz.srnyx.javautilities; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | + |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | + |
| 10 | +import java.io.BufferedReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStreamReader; |
| 13 | +import java.net.HttpURLConnection; |
| 14 | +import java.net.URI; |
| 15 | +import java.util.function.Function; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class for making HTTP requests |
| 21 | + */ |
| 22 | +public class HttpUtility { |
| 23 | + /** |
| 24 | + * Sends a GET request to the specified URL and returns the result of the specified function |
| 25 | + * |
| 26 | + * @param userAgent the user agent to use |
| 27 | + * @param url the URL to request from |
| 28 | + * @param function the function to apply to the {@link InputStreamReader} |
| 29 | + * |
| 30 | + * @param <T> the type of the result of the specified function |
| 31 | + * |
| 32 | + * @return the result of the specified function, or null if the request failed |
| 33 | + */ |
| 34 | + @Nullable |
| 35 | + public static <T> T get(@NotNull String userAgent, @NotNull String url, Function<InputStreamReader, T> function) { |
| 36 | + T result = null; |
| 37 | + HttpURLConnection connection = null; |
| 38 | + try { |
| 39 | + connection = (HttpURLConnection) URI.create(url).toURL().openConnection(); |
| 40 | + connection.setRequestMethod("GET"); |
| 41 | + connection.setRequestProperty("User-Agent", userAgent); |
| 42 | + if (connection.getResponseCode() == 404) return null; |
| 43 | + result = function.apply(new InputStreamReader(connection.getInputStream())); |
| 44 | + } catch (final IOException ignored) { |
| 45 | + // Ignored |
| 46 | + } |
| 47 | + if (connection != null) connection.disconnect(); |
| 48 | + return result; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sends a GET request to the specified URL and returns the result as a {@link String} |
| 53 | + * |
| 54 | + * @param userAgent the user agent to use |
| 55 | + * @param urlString the URL to request from |
| 56 | + * |
| 57 | + * @return the {@link String}, or null if the request failed |
| 58 | + */ |
| 59 | + @Nullable |
| 60 | + public static String getString(@NotNull String userAgent, @NotNull String urlString) { |
| 61 | + return get(userAgent, urlString, reader -> new BufferedReader(reader).lines().collect(Collectors.joining("\n"))); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sends a GET request to the specified URL and returns the result as a {@link JsonElement} |
| 66 | + * |
| 67 | + * @param userAgent the user agent to use when retrieving the {@link JsonElement} |
| 68 | + * @param urlString the URL to retrieve the {@link JsonElement} from |
| 69 | + * |
| 70 | + * @return the {@link JsonElement} retrieved from the specified URL |
| 71 | + */ |
| 72 | + @Nullable |
| 73 | + public static JsonElement getJson(@NotNull String userAgent, @NotNull String urlString) { |
| 74 | + return get(userAgent, urlString, reader -> new JsonParser().parse(reader)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Sends a POST request to the specified URL with the specified {@link JsonObject JSON data} |
| 79 | + * |
| 80 | + * @param userAgent the user agent to use |
| 81 | + * @param urlString the URL to send the POST request to |
| 82 | + * @param data the {@link JsonObject JSON data} to send with the POST request |
| 83 | + * |
| 84 | + * @return the response code of the request |
| 85 | + */ |
| 86 | + public static int postJson(@NotNull String userAgent, @NotNull String urlString, @NotNull JsonElement data) { |
| 87 | + int responseCode = -1; |
| 88 | + HttpURLConnection connection = null; |
| 89 | + try { |
| 90 | + connection = (HttpURLConnection) URI.create(urlString).toURL().openConnection(); |
| 91 | + connection.setRequestMethod("POST"); |
| 92 | + connection.setRequestProperty("User-Agent", userAgent); |
| 93 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 94 | + connection.setDoOutput(true); |
| 95 | + connection.getOutputStream().write(data.toString().getBytes()); |
| 96 | + responseCode = connection.getResponseCode(); |
| 97 | + } catch (final IOException ignored) { |
| 98 | + // Ignored |
| 99 | + } |
| 100 | + if (connection != null) connection.disconnect(); |
| 101 | + return responseCode; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Sends a PUT request to the specified URL with the specified {@link JsonElement JSON data} |
| 106 | + * |
| 107 | + * @param userAgent the user agent to use |
| 108 | + * @param urlString the URL to send the PUT request to |
| 109 | + * @param data the {@link JsonElement JSON data} to send with the PUT request |
| 110 | + * |
| 111 | + * @return the response code of the request |
| 112 | + */ |
| 113 | + public static int putJson(@NotNull String userAgent, @NotNull String urlString, @NotNull JsonElement data) { |
| 114 | + int responseCode = -1; |
| 115 | + HttpURLConnection connection = null; |
| 116 | + try { |
| 117 | + connection = (HttpURLConnection) URI.create(urlString).toURL().openConnection(); |
| 118 | + connection.setRequestMethod("PUT"); |
| 119 | + connection.setRequestProperty("User-Agent", userAgent); |
| 120 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 121 | + connection.setDoOutput(true); |
| 122 | + connection.getOutputStream().write(data.toString().getBytes()); |
| 123 | + responseCode = connection.getResponseCode(); |
| 124 | + } catch (final IOException ignored) { |
| 125 | + // Ignored |
| 126 | + } |
| 127 | + if (connection != null) connection.disconnect(); |
| 128 | + return responseCode; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Sends a DELETE request to the specified URL |
| 133 | + * |
| 134 | + * @param userAgent the user agent to use |
| 135 | + * @param urlString the URL to send the DELETE request to |
| 136 | + * |
| 137 | + * @return the response code of the request |
| 138 | + */ |
| 139 | + public static int delete(@NotNull String userAgent, @NotNull String urlString) { |
| 140 | + int responseCode = -1; |
| 141 | + HttpURLConnection connection = null; |
| 142 | + try { |
| 143 | + connection = (HttpURLConnection) URI.create(urlString).toURL().openConnection(); |
| 144 | + connection.setRequestMethod("DELETE"); |
| 145 | + connection.setRequestProperty("User-Agent", userAgent); |
| 146 | + responseCode = connection.getResponseCode(); |
| 147 | + } catch (final IOException ignored) { |
| 148 | + // Ignored |
| 149 | + } |
| 150 | + if (connection != null) connection.disconnect(); |
| 151 | + return responseCode; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Constructs a new {@link HttpUtility} instance (illegal) |
| 156 | + * |
| 157 | + * @throws UnsupportedOperationException if this class is instantiated |
| 158 | + */ |
| 159 | + private HttpUtility() { |
| 160 | + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); |
| 161 | + } |
| 162 | +} |
0 commit comments