55import com .google .gson .JsonParser ;
66
77import org .jetbrains .annotations .NotNull ;
8+ import org .jetbrains .annotations .Nullable ;
89
910import java .io .BufferedReader ;
1011import java .io .IOException ;
1112import java .io .InputStreamReader ;
1213import java .net .HttpURLConnection ;
1314import java .net .URI ;
1415import java .util .Optional ;
16+ import java .util .function .Consumer ;
1517import java .util .function .Function ;
1618import java .util .stream .Collectors ;
1719
@@ -23,22 +25,24 @@ public class HttpUtility {
2325 /**
2426 * Sends a GET request to the specified URL and returns the result of the specified function
2527 *
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}
28+ * @param userAgent the user agent to use
29+ * @param url the URL to request from
30+ * @param function the function to apply to the {@link InputStreamReader}
31+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
2932 *
30- * @param <T> the type of the result of the specified function
33+ * @param <T> the type of the result of the specified function
3134 *
32- * @return the result of the specified function, or null if the request failed
35+ * @return the result of the specified function, or null if the request failed
3336 */
3437 @ NotNull
35- public static <T > Optional <T > get (@ NotNull String userAgent , @ NotNull String url , Function <InputStreamReader , T > function ) {
38+ public static <T > Optional <T > get (@ NotNull String userAgent , @ NotNull String url , @ NotNull Function <InputStreamReader , T > function , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
3639 T result = null ;
3740 HttpURLConnection connection = null ;
3841 try {
3942 connection = (HttpURLConnection ) URI .create (url ).toURL ().openConnection ();
4043 connection .setRequestMethod ("GET" );
4144 connection .setRequestProperty ("User-Agent" , userAgent );
45+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
4246 if (connection .getResponseCode () == 404 ) return Optional .empty ();
4347 result = function .apply (new InputStreamReader (connection .getInputStream ()));
4448 } catch (final IOException ignored ) {
@@ -51,39 +55,42 @@ public static <T> Optional<T> get(@NotNull String userAgent, @NotNull String url
5155 /**
5256 * Sends a GET request to the specified URL and returns the result as a {@link String}
5357 *
54- * @param userAgent the user agent to use
55- * @param urlString the URL to request from
58+ * @param userAgent the user agent to use
59+ * @param urlString the URL to request from
60+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
5661 *
57- * @return the {@link String}, or null if the request failed
62+ * @return the {@link String}, or null if the request failed
5863 */
5964 @ NotNull
60- public static Optional <String > getString (@ NotNull String userAgent , @ NotNull String urlString ) {
61- return get (userAgent , urlString , reader -> new BufferedReader (reader ).lines ().collect (Collectors .joining ("\n " )));
65+ public static Optional <String > getString (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
66+ return get (userAgent , urlString , reader -> new BufferedReader (reader ).lines ().collect (Collectors .joining ("\n " )), connectionConsumer );
6267 }
6368
6469 /**
6570 * Sends a GET request to the specified URL and returns the result as a {@link JsonElement}
6671 *
67- * @param userAgent the user agent to use when retrieving the {@link JsonElement}
68- * @param urlString the URL to retrieve the {@link JsonElement} from
72+ * @param userAgent the user agent to use when retrieving the {@link JsonElement}
73+ * @param urlString the URL to retrieve the {@link JsonElement} from
74+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
6975 *
70- * @return the {@link JsonElement} retrieved from the specified URL
76+ * @return the {@link JsonElement} retrieved from the specified URL
7177 */
7278 @ NotNull
73- public static Optional <JsonElement > getJson (@ NotNull String userAgent , @ NotNull String urlString ) {
74- return get (userAgent , urlString , reader -> new JsonParser ().parse (reader ));
79+ public static Optional <JsonElement > getJson (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
80+ return get (userAgent , urlString , reader -> new JsonParser ().parse (reader ), connectionConsumer );
7581 }
7682
7783 /**
7884 * Sends a POST request to the specified URL with the specified {@link JsonObject JSON data}
7985 *
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
86+ * @param userAgent the user agent to use
87+ * @param urlString the URL to send the POST request to
88+ * @param data the {@link JsonObject JSON data} to send with the POST request
89+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
8390 *
84- * @return the response code of the request
91+ * @return the response code of the request
8592 */
86- public static int postJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data ) {
93+ public static int postJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
8794 int responseCode = -1 ;
8895 HttpURLConnection connection = null ;
8996 try {
@@ -92,6 +99,7 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
9299 connection .setRequestProperty ("User-Agent" , userAgent );
93100 connection .setRequestProperty ("Content-Type" , "application/json" );
94101 connection .setDoOutput (true );
102+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
95103 connection .getOutputStream ().write (data .toString ().getBytes ());
96104 responseCode = connection .getResponseCode ();
97105 } catch (final IOException ignored ) {
@@ -104,13 +112,14 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
104112 /**
105113 * Sends a PUT request to the specified URL with the specified {@link JsonElement JSON data}
106114 *
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
115+ * @param userAgent the user agent to use
116+ * @param urlString the URL to send the PUT request to
117+ * @param data the {@link JsonElement JSON data} to send with the PUT request
118+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
110119 *
111- * @return the response code of the request
120+ * @return the response code of the request
112121 */
113- public static int putJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data ) {
122+ public static int putJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
114123 int responseCode = -1 ;
115124 HttpURLConnection connection = null ;
116125 try {
@@ -119,6 +128,7 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
119128 connection .setRequestProperty ("User-Agent" , userAgent );
120129 connection .setRequestProperty ("Content-Type" , "application/json" );
121130 connection .setDoOutput (true );
131+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
122132 connection .getOutputStream ().write (data .toString ().getBytes ());
123133 responseCode = connection .getResponseCode ();
124134 } catch (final IOException ignored ) {
@@ -131,13 +141,14 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
131141 /**
132142 * Sends a PATCH request to the specified URL with the specified {@link JsonElement JSON data}
133143 *
134- * @param userAgent the user agent to use
135- * @param urlString the URL to send the PATCH request to
136- * @param data the {@link JsonElement JSON data} to send with the PATCH request
144+ * @param userAgent the user agent to use
145+ * @param urlString the URL to send the PATCH request to
146+ * @param data the {@link JsonElement JSON data} to send with the PATCH request
147+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
137148 *
138- * @return the response code of the request
149+ * @return the response code of the request
139150 */
140- public static int patchJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data ) {
151+ public static int patchJson (@ NotNull String userAgent , @ NotNull String urlString , @ NotNull JsonElement data , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
141152 int responseCode = -1 ;
142153 HttpURLConnection connection = null ;
143154 try {
@@ -146,6 +157,7 @@ public static int patchJson(@NotNull String userAgent, @NotNull String urlString
146157 connection .setRequestProperty ("User-Agent" , userAgent );
147158 connection .setRequestProperty ("Content-Type" , "application/json" );
148159 connection .setDoOutput (true );
160+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
149161 connection .getOutputStream ().write (data .toString ().getBytes ());
150162 responseCode = connection .getResponseCode ();
151163 } catch (final IOException ignored ) {
@@ -158,18 +170,20 @@ public static int patchJson(@NotNull String userAgent, @NotNull String urlString
158170 /**
159171 * Sends a DELETE request to the specified URL
160172 *
161- * @param userAgent the user agent to use
162- * @param urlString the URL to send the DELETE request to
173+ * @param userAgent the user agent to use
174+ * @param urlString the URL to send the DELETE request to
175+ * @param connectionConsumer the consumer to apply to the {@link HttpURLConnection}
163176 *
164- * @return the response code of the request
177+ * @return the response code of the request
165178 */
166- public static int delete (@ NotNull String userAgent , @ NotNull String urlString ) {
179+ public static int delete (@ NotNull String userAgent , @ NotNull String urlString , @ Nullable Consumer < HttpURLConnection > connectionConsumer ) {
167180 int responseCode = -1 ;
168181 HttpURLConnection connection = null ;
169182 try {
170183 connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
171184 connection .setRequestMethod ("DELETE" );
172185 connection .setRequestProperty ("User-Agent" , userAgent );
186+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
173187 responseCode = connection .getResponseCode ();
174188 } catch (final IOException ignored ) {
175189 // Ignored
0 commit comments