5
5
import com .google .gson .JsonParser ;
6
6
7
7
import org .jetbrains .annotations .NotNull ;
8
+ import org .jetbrains .annotations .Nullable ;
8
9
9
10
import java .io .BufferedReader ;
10
11
import java .io .IOException ;
11
12
import java .io .InputStreamReader ;
12
13
import java .net .HttpURLConnection ;
13
14
import java .net .URI ;
14
15
import java .util .Optional ;
16
+ import java .util .function .Consumer ;
15
17
import java .util .function .Function ;
16
18
import java .util .stream .Collectors ;
17
19
@@ -23,22 +25,24 @@ public class HttpUtility {
23
25
/**
24
26
* Sends a GET request to the specified URL and returns the result of the specified function
25
27
*
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}
29
32
*
30
- * @param <T> the type of the result of the specified function
33
+ * @param <T> the type of the result of the specified function
31
34
*
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
33
36
*/
34
37
@ 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 ) {
36
39
T result = null ;
37
40
HttpURLConnection connection = null ;
38
41
try {
39
42
connection = (HttpURLConnection ) URI .create (url ).toURL ().openConnection ();
40
43
connection .setRequestMethod ("GET" );
41
44
connection .setRequestProperty ("User-Agent" , userAgent );
45
+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
42
46
if (connection .getResponseCode () == 404 ) return Optional .empty ();
43
47
result = function .apply (new InputStreamReader (connection .getInputStream ()));
44
48
} catch (final IOException ignored ) {
@@ -51,39 +55,42 @@ public static <T> Optional<T> get(@NotNull String userAgent, @NotNull String url
51
55
/**
52
56
* Sends a GET request to the specified URL and returns the result as a {@link String}
53
57
*
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}
56
61
*
57
- * @return the {@link String}, or null if the request failed
62
+ * @return the {@link String}, or null if the request failed
58
63
*/
59
64
@ 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 );
62
67
}
63
68
64
69
/**
65
70
* Sends a GET request to the specified URL and returns the result as a {@link JsonElement}
66
71
*
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}
69
75
*
70
- * @return the {@link JsonElement} retrieved from the specified URL
76
+ * @return the {@link JsonElement} retrieved from the specified URL
71
77
*/
72
78
@ 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 );
75
81
}
76
82
77
83
/**
78
84
* Sends a POST request to the specified URL with the specified {@link JsonObject JSON data}
79
85
*
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}
83
90
*
84
- * @return the response code of the request
91
+ * @return the response code of the request
85
92
*/
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 ) {
87
94
int responseCode = -1 ;
88
95
HttpURLConnection connection = null ;
89
96
try {
@@ -92,6 +99,7 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
92
99
connection .setRequestProperty ("User-Agent" , userAgent );
93
100
connection .setRequestProperty ("Content-Type" , "application/json" );
94
101
connection .setDoOutput (true );
102
+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
95
103
connection .getOutputStream ().write (data .toString ().getBytes ());
96
104
responseCode = connection .getResponseCode ();
97
105
} catch (final IOException ignored ) {
@@ -104,13 +112,14 @@ public static int postJson(@NotNull String userAgent, @NotNull String urlString,
104
112
/**
105
113
* Sends a PUT request to the specified URL with the specified {@link JsonElement JSON data}
106
114
*
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}
110
119
*
111
- * @return the response code of the request
120
+ * @return the response code of the request
112
121
*/
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 ) {
114
123
int responseCode = -1 ;
115
124
HttpURLConnection connection = null ;
116
125
try {
@@ -119,6 +128,7 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
119
128
connection .setRequestProperty ("User-Agent" , userAgent );
120
129
connection .setRequestProperty ("Content-Type" , "application/json" );
121
130
connection .setDoOutput (true );
131
+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
122
132
connection .getOutputStream ().write (data .toString ().getBytes ());
123
133
responseCode = connection .getResponseCode ();
124
134
} catch (final IOException ignored ) {
@@ -131,13 +141,14 @@ public static int putJson(@NotNull String userAgent, @NotNull String urlString,
131
141
/**
132
142
* Sends a PATCH request to the specified URL with the specified {@link JsonElement JSON data}
133
143
*
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}
137
148
*
138
- * @return the response code of the request
149
+ * @return the response code of the request
139
150
*/
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 ) {
141
152
int responseCode = -1 ;
142
153
HttpURLConnection connection = null ;
143
154
try {
@@ -146,6 +157,7 @@ public static int patchJson(@NotNull String userAgent, @NotNull String urlString
146
157
connection .setRequestProperty ("User-Agent" , userAgent );
147
158
connection .setRequestProperty ("Content-Type" , "application/json" );
148
159
connection .setDoOutput (true );
160
+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
149
161
connection .getOutputStream ().write (data .toString ().getBytes ());
150
162
responseCode = connection .getResponseCode ();
151
163
} catch (final IOException ignored ) {
@@ -158,18 +170,20 @@ public static int patchJson(@NotNull String userAgent, @NotNull String urlString
158
170
/**
159
171
* Sends a DELETE request to the specified URL
160
172
*
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}
163
176
*
164
- * @return the response code of the request
177
+ * @return the response code of the request
165
178
*/
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 ) {
167
180
int responseCode = -1 ;
168
181
HttpURLConnection connection = null ;
169
182
try {
170
183
connection = (HttpURLConnection ) URI .create (urlString ).toURL ().openConnection ();
171
184
connection .setRequestMethod ("DELETE" );
172
185
connection .setRequestProperty ("User-Agent" , userAgent );
186
+ if (connectionConsumer != null ) connectionConsumer .accept (connection );
173
187
responseCode = connection .getResponseCode ();
174
188
} catch (final IOException ignored ) {
175
189
// Ignored
0 commit comments