Skip to content

Commit 6a6a143

Browse files
committed
Added support for get requests in the ConnectionUtils
1 parent 9deeb72 commit 6a6a143

2 files changed

Lines changed: 180 additions & 44 deletions

File tree

src/main/java/io/github/intisy/utils/core/ConnectionUtils.java

Lines changed: 175 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@
2121
@SuppressWarnings("unused")
2222
public class ConnectionUtils {
2323
/**
24-
* Generates a curl command based on the provided HTTP connection, token, and payload.
24+
* Generates a curl command based on the provided HTTP connection, token, and
25+
* payload.
2526
*
2627
* @param connection The HTTP connection to be used.
27-
* @param token The authorization token to be included in the curl command. If null, no token will be included.
28+
* @param token The authorization token to be included in the curl command.
29+
* If null, no token will be included.
2830
* @param payload The payload to be sent in the curl command.
2931
* @return A curl command as a string.
3032
*/
3133
public static String curl(HttpURLConnection connection, String token, String payload) {
3234
StringBuilder curl = new StringBuilder();
3335
if (token != null)
34-
curl.append("curl -X ").append(connection.getRequestMethod()).append(" -H \"Authorization: Bearer ").append(token).append("\"");
35-
connection.getRequestProperties().forEach((key, value) -> curl.append(" -H \"").append(key).append(": ").append(value.get(0)).append("\""));
36+
curl.append("curl -X ").append(connection.getRequestMethod()).append(" -H \"Authorization: Bearer ")
37+
.append(token).append("\"");
38+
connection.getRequestProperties().forEach(
39+
(key, value) -> curl.append(" -H \"").append(key).append(": ").append(value.get(0)).append("\""));
3640
curl.append(" -d '").append(payload).append("' ").append(connection.getURL());
3741
System.out.println(connection.getRequestProperties());
3842
return curl.toString();
@@ -43,7 +47,8 @@ public static String curl(HttpURLConnection connection, String token, String pay
4347
*
4448
* @param connection The HTTP connection to be used.
4549
* @return The output from the error stream as a string.
46-
* @throws IOException If an I/O error occurs while reading from the error stream.
50+
* @throws IOException If an I/O error occurs while reading from the error
51+
* stream.
4752
*/
4853
public static String getOutput(HttpURLConnection connection) throws IOException {
4954
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
@@ -60,18 +65,21 @@ public static String getOutput(HttpURLConnection connection) throws IOException
6065
* Prints the output from the error stream of the provided HTTP connection.
6166
*
6267
* @param connection The HTTP connection to be used.
63-
* @throws IOException If an I/O error occurs while reading from the error stream.
68+
* @throws IOException If an I/O error occurs while reading from the error
69+
* stream.
6470
*/
6571
public static void printOutput(HttpURLConnection connection) throws IOException {
6672
System.out.println(getOutput(connection));
6773
}
6874

6975
/**
70-
* Sends a JSON payload through the output stream of the specified HTTP connection.
76+
* Sends a JSON payload through the output stream of the specified HTTP
77+
* connection.
7178
*
7279
* @param jsonPayload The JSON object to be sent as the request payload.
7380
* @param connection The HTTP connection to which the payload will be written.
74-
* @throws IOException If an I/O error occurs while writing to the output stream.
81+
* @throws IOException If an I/O error occurs while writing to the output
82+
* stream.
7583
*/
7684
public static void connectionPayload(JsonObject jsonPayload, HttpURLConnection connection) throws IOException {
7785
try (OutputStream os = connection.getOutputStream()) {
@@ -81,15 +89,31 @@ public static void connectionPayload(JsonObject jsonPayload, HttpURLConnection c
8189
}
8290

8391
/**
84-
* Sends an authorized POST request to the specified API URL with the provided authorization key and processes the response.
92+
* Sends an authorized POST request to the specified API URL with the provided
93+
* authorization key and processes the response.
8594
*
8695
* @param apiUrl The URL of the API to which the request will be sent.
8796
* @param key The authorization key to be included in the request header.
88-
* @return A JsonObject representing the server's response. Contains an additional "code" property indicating the HTTP response code.
97+
* @return A JsonObject representing the server's response. Contains an
98+
* additional "code" property indicating the HTTP response code.
8999
*/
90100
public static JsonObject authorizedRequest(String apiUrl, String key) {
101+
return authorizedRequest(apiUrl, key, RequestMethod.POST);
102+
}
103+
104+
/**
105+
* Sends an authorized request to the specified API URL with the provided
106+
* authorization key and method, and processes the response.
107+
*
108+
* @param apiUrl The URL of the API to which the request will be sent.
109+
* @param key The authorization key to be included in the request header.
110+
* @param method The HTTP method to use (e.g., GET, POST).
111+
* @return A JsonObject representing the server's response. Contains an
112+
* additional "code" property indicating the HTTP response code.
113+
*/
114+
public static JsonObject authorizedRequest(String apiUrl, String key, RequestMethod method) {
91115
try {
92-
HttpURLConnection connection = post(apiUrl);
116+
HttpURLConnection connection = connection(apiUrl, method);
93117
connection.setRequestProperty("Authorization", key);
94118
return handleResponse(connection);
95119
} catch (IOException e) {
@@ -100,10 +124,12 @@ public static JsonObject authorizedRequest(String apiUrl, String key) {
100124
/**
101125
* Handles an exception and returns a JSON object containing error details.
102126
*
103-
* @param e The exception to be handled. Provides details about the error that occurred.
127+
* @param e The exception to be handled. Provides details about the error that
128+
* occurred.
104129
* @return A JsonObject containing two properties:
105130
* - "message": A descriptive message about the error.
106-
* - "type": A fixed string "error" indicating the nature of the response.
131+
* - "type": A fixed string "error" indicating the nature of the
132+
* response.
107133
*/
108134
public static JsonObject handleException(Exception e) {
109135
JsonObject jsonObject = new JsonObject();
@@ -113,77 +139,159 @@ public static JsonObject handleException(Exception e) {
113139
}
114140

115141
/**
116-
* Sends an authorized HTTP request to the specified API URL with the given authorization key
142+
* Sends an authorized HTTP POST request to the specified API URL with the given
143+
* authorization key
117144
* and JSON payload, and processes the server's response.
118145
*
119146
* @param apiUrl The URL of the API to which the request will be sent.
120-
* @param key The authorization key to be included in the request header.
147+
* @param key The authorization key to be included in the request
148+
* header.
121149
* @param jsonPayload The JSON object to be sent as the request payload.
122-
* @return A JsonObject representing the server's response. Contains an additional "code" property
150+
* @return A JsonObject representing the server's response. Contains an
151+
* additional "code" property
123152
* indicating the HTTP response code.
124153
*/
125154
public static JsonObject authorizedRequest(String apiUrl, String key, JsonObject jsonPayload) {
155+
return authorizedRequest(apiUrl, key, jsonPayload, RequestMethod.POST);
156+
}
157+
158+
/**
159+
* Sends an authorized HTTP request to the specified API URL with the given
160+
* authorization key,
161+
* JSON payload, and method, and processes the server's response.
162+
*
163+
* @param apiUrl The URL of the API to which the request will be sent.
164+
* @param key The authorization key to be included in the request
165+
* header.
166+
* @param jsonPayload The JSON object to be sent as the request payload.
167+
* @param method The HTTP method to use (e.g., GET, POST).
168+
* @return A JsonObject representing the server's response. Contains an
169+
* additional "code" property
170+
* indicating the HTTP response code.
171+
*/
172+
public static JsonObject authorizedRequest(String apiUrl, String key, JsonObject jsonPayload,
173+
RequestMethod method) {
126174
Map<String, String> headers = new HashMap<>();
127175
headers.put("Authorization", key);
128-
return headerRequest(apiUrl, headers, jsonPayload);
176+
return headerRequest(apiUrl, headers, jsonPayload, method);
129177
}
130178

131179
/**
132-
* Sends a POST request to the specified API URL with given headers and JSON payload,
180+
* Sends a POST request to the specified API URL with given headers and JSON
181+
* payload,
133182
* and returns the server's response as a JSON object.
134-
* If an error occurs during the request, it handles the exception and returns
135-
* an error JSON object.
136183
*
137184
* @param apiUrl The URL of the API to which the request will be sent.
138-
* @param headers A map containing key-value pairs representing the request headers.
185+
* @param headers A map containing key-value pairs representing the request
186+
* headers.
139187
* @param jsonPayload The JSON object to be sent as the request payload.
140-
* @return A JsonObject representing the server's response. If an exception occurs,
188+
* @return A JsonObject representing the server's response. If an exception
189+
* occurs,
141190
* returns a JSON object containing error details.
142191
*/
143192
public static JsonObject headerRequest(String apiUrl, Map<String, String> headers, JsonObject jsonPayload) {
193+
return headerRequest(apiUrl, headers, jsonPayload, RequestMethod.POST);
194+
}
195+
196+
/**
197+
* Sends a request to the specified API URL with given headers, JSON payload,
198+
* and method,
199+
* and returns the server's response as a JSON object.
200+
*
201+
* @param apiUrl The URL of the API to which the request will be sent.
202+
* @param headers A map containing key-value pairs representing the request
203+
* headers.
204+
* @param jsonPayload The JSON object to be sent as the request payload.
205+
* @param method The HTTP method to use (e.g., GET, POST).
206+
* @return A JsonObject representing the server's response. If an exception
207+
* occurs,
208+
* returns a JSON object containing error details.
209+
*/
210+
public static JsonObject headerRequest(String apiUrl, Map<String, String> headers, JsonObject jsonPayload,
211+
RequestMethod method) {
144212
try {
145-
HttpURLConnection connection = post(apiUrl);
213+
HttpURLConnection connection = connection(apiUrl, method);
146214
headers.forEach(connection::setRequestProperty);
147-
connectionPayload(jsonPayload, connection);
215+
if (jsonPayload != null && method == RequestMethod.POST) {
216+
connectionPayload(jsonPayload, connection);
217+
}
148218
return handleResponse(connection);
149219
} catch (IOException e) {
150220
return handleException(e);
151221
}
152222
}
153223

154224
/**
155-
* Sends a JSON payload as a POST request to the specified API URL, processes the
225+
* Sends a JSON payload as a POST request to the specified API URL, processes
226+
* the
156227
* server's response, and handles potential exceptions.
157228
*
158229
* @param apiUrl The URL of the API to which the request will be sent.
159230
* @param jsonPayload The JSON object to be sent as the request payload.
160-
* @return A JsonObject representing the server's response. Contains an additional
231+
* @return A JsonObject representing the server's response. Contains an
232+
* additional
161233
* "code" property for the HTTP response code. If an exception occurs,
162234
* returns a JsonObject with error details.
163235
*/
164236
public static JsonObject request(String apiUrl, JsonObject jsonPayload) {
237+
return request(apiUrl, jsonPayload, RequestMethod.POST);
238+
}
239+
240+
/**
241+
* Sends a JSON payload as a request to the specified API URL with the specified
242+
* method,
243+
* processes the server's response, and handles potential exceptions.
244+
*
245+
* @param apiUrl The URL of the API to which the request will be sent.
246+
* @param jsonPayload The JSON object to be sent as the request payload.
247+
* @param method The HTTP method to use (e.g., GET, POST).
248+
* @return A JsonObject representing the server's response. Contains an
249+
* additional
250+
* "code" property for the HTTP response code. If an exception occurs,
251+
* returns a JsonObject with error details.
252+
*/
253+
public static JsonObject request(String apiUrl, JsonObject jsonPayload, RequestMethod method) {
165254
try {
166-
HttpURLConnection connection = post(apiUrl);
167-
connectionPayload(jsonPayload, connection);
255+
HttpURLConnection connection = connection(apiUrl, method);
256+
if (jsonPayload != null && method == RequestMethod.POST) {
257+
connectionPayload(jsonPayload, connection);
258+
}
168259
return handleResponse(connection);
169260
} catch (IOException e) {
170261
return handleException(e);
171262
}
172263
}
173264

174265
/**
175-
* Sends a POST request to the specified API URL, processes the server's response,
266+
* Sends a POST request to the specified API URL, processes the server's
267+
* response,
176268
* and handles any potential I/O exceptions that may occur during the request.
177269
*
178270
* @param apiUrl The URL of the API to which the POST request will be sent.
179271
* @return A JsonObject representing the server's response. If the request is
180-
* successful, the response includes a "code" property indicating the HTTP
181-
* response code and the parsed JSON payload. If an exception occurs, returns
272+
* successful, the response includes a "code" property indicating the
273+
* HTTP
274+
* response code and the parsed JSON payload. If an exception occurs,
275+
* returns
182276
* a JsonObject with error details, including a "message" and a "type".
183277
*/
184278
public static JsonObject request(String apiUrl) {
279+
return request(apiUrl, RequestMethod.POST);
280+
}
281+
282+
/**
283+
* Sends a request to the specified API URL with the specified method, processes
284+
* the
285+
* server's response, and handles any potential I/O exceptions that may occur
286+
* during the request.
287+
*
288+
* @param apiUrl The URL of the API to which the request will be sent.
289+
* @param method The HTTP method to use (e.g., GET, POST).
290+
* @return A JsonObject representing the server's response.
291+
*/
292+
public static JsonObject request(String apiUrl, RequestMethod method) {
185293
try {
186-
HttpURLConnection connection = post(apiUrl);
294+
HttpURLConnection connection = connection(apiUrl, method);
187295
return handleResponse(connection);
188296
} catch (IOException e) {
189297
return handleException(e);
@@ -192,19 +300,34 @@ public static JsonObject request(String apiUrl) {
192300

193301
/**
194302
* Sends a POST request to the specified API URL and returns the configured
195-
* HttpURLConnection object. This method sets the request method to POST,
196-
* the content type to "application/json", and enables output for the connection.
303+
* HttpURLConnection object.
197304
*
198305
* @param apiUrl The URL of the API to which the POST request will be sent.
199306
* @return An HttpURLConnection object configured for the POST request.
200307
* @throws IOException If an I/O error occurs during the connection setup.
201308
*/
202309
public static HttpURLConnection post(String apiUrl) throws IOException {
310+
return connection(apiUrl, RequestMethod.POST);
311+
}
312+
313+
/**
314+
* Sends a GET request to the specified API URL and returns the configured
315+
* HttpURLConnection object.
316+
*
317+
* @param apiUrl The URL of the API to which the GET request will be sent.
318+
* @return An HttpURLConnection object configured for the GET request.
319+
* @throws IOException If an I/O error occurs during the connection setup.
320+
*/
321+
public static HttpURLConnection get(String apiUrl) throws IOException {
322+
return connection(apiUrl, RequestMethod.GET);
323+
}
324+
325+
private static HttpURLConnection connection(String apiUrl, RequestMethod method) throws IOException {
203326
URL url = new URL(apiUrl);
204327
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
205-
connection.setRequestMethod("POST");
328+
connection.setRequestMethod(method.name());
206329
connection.setRequestProperty("Content-Type", "application/json");
207-
connection.setDoOutput(true);
330+
connection.setDoOutput(method == RequestMethod.POST);
208331
return connection;
209332
}
210333

@@ -214,10 +337,14 @@ public static HttpURLConnection post(String apiUrl) throws IOException {
214337
* the returned JSON object. If the response code is not 200 (HTTP OK), logs the
215338
* error details.
216339
*
217-
* @param connection The {@code HttpURLConnection} from which to read the response.
218-
* Must not be null and should already be configured and connected.
219-
* @return A {@code JsonObject} representing the response. Includes a "code" property
220-
* with the HTTP response code. If the response is in error, the response body
340+
* @param connection The {@code HttpURLConnection} from which to read the
341+
* response.
342+
* Must not be null and should already be configured and
343+
* connected.
344+
* @return A {@code JsonObject} representing the response. Includes a "code"
345+
* property
346+
* with the HTTP response code. If the response is in error, the
347+
* response body
221348
* is also included in the returned JSON object.
222349
* @throws IOException If an I/O error occurs while reading the response or
223350
* if the response cannot be parsed as a valid JSON object.
@@ -245,11 +372,15 @@ public static JsonObject handleResponse(HttpURLConnection connection) throws IOE
245372
}
246373

247374
/**
248-
* Reads all lines from the provided BufferedReader and returns them as a single string.
375+
* Reads all lines from the provided BufferedReader and returns them as a single
376+
* string.
249377
*
250-
* @param br The BufferedReader instance from which to read the lines. Must not be null.
251-
* @return A string containing the concatenated lines read from the BufferedReader.
252-
* @throws IOException If an I/O error occurs while reading from the BufferedReader.
378+
* @param br The BufferedReader instance from which to read the lines. Must not
379+
* be null.
380+
* @return A string containing the concatenated lines read from the
381+
* BufferedReader.
382+
* @throws IOException If an I/O error occurs while reading from the
383+
* BufferedReader.
253384
*/
254385
public static String read(BufferedReader br) throws IOException {
255386
StringBuilder response = new StringBuilder();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.intisy.utils.core;
2+
3+
public enum RequestMethod {
4+
GET, POST, PUT, DELETE
5+
}

0 commit comments

Comments
 (0)