Skip to content

Commit 1be3db9

Browse files
v0.0.5 - Refactor & Improve customizability
1 parent 7b3f409 commit 1be3db9

File tree

10 files changed

+335
-122
lines changed

10 files changed

+335
-122
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,26 @@ The `SupportedHttpMethod` enum defines the supported HTTP methods. It includes t
2929
To use the `SimpleHttpWrapper` class, import the `com.github.lightlibs.simplehttpwrapper` package and call the desired static methods. For example:
3030

3131
```java
32+
import com.github.lightlibs.simplehttpwrapper.HttpResponse;
33+
import com.github.lightlibs.simplehttpwrapper.HttpWrapper;
3234
import com.github.lightlibs.simplehttpwrapper.SimpleHttpWrapper;
33-
import com.github.lightlibs.simplehttpwrapper.SimpleHttpResponse;
3435

3536
class Example {
36-
37+
3738
public static void main(String[] args) {
3839
// Perform a GET request
39-
SimpleHttpResponse response = SimpleHttpWrapper.get("https://example.com", null);
40+
HttpResponse response = HttpWrapper.get("https://example.com", null);
4041
int statusCode = response.getStatusCode();
41-
String data = response.getData();
42+
String data = response.getBody();
4243

4344
// Perform a POST request with headers and post data
4445
String[] headers = {"Content-Type: application/json"};
4546
String postData = "{ \"name\": \"John\", \"age\": 30 }";
46-
SimpleHttpResponse response = SimpleHttpWrapper.post("https://example.com", headers, postData);
47+
HttpResponse response = HttpWrapper.post("https://example.com", headers, postData);
4748
int statusCode = response.getStatusCode();
48-
String data = response.getData();
49+
String data = response.getBody();
4950
}
50-
51+
5152
}
5253
```
5354

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.lightlibs</groupId>
88
<artifactId>SimpleHttpWrapper</artifactId>
9-
<version>0.0.4</version>
9+
<version>0.0.5</version>
1010

1111
<build>
1212
<plugins>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
public class HttpHeader {
4+
5+
private final String key;
6+
private final String value;
7+
8+
public HttpHeader(String key, String value) {
9+
this.key = key;
10+
this.value = value;
11+
}
12+
13+
public String getKey() {
14+
return key;
15+
}
16+
17+
public String getValue() {
18+
return value;
19+
}
20+
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
public enum HttpMethod {
6+
7+
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
8+
// for details about each method.
9+
10+
GET("GET", false),
11+
HEAD("HEAD", false),
12+
POST("POST", true),
13+
PUT("PUT", true),
14+
DELETE("DELETE", false),
15+
CONNECT("CONNECT", false),
16+
OPTIONS("OPTIONS", false),
17+
TRACE("TRACE", false),
18+
PATCH("PATCH", true);
19+
20+
private final ProgrammaticHttpMethod method;
21+
22+
HttpMethod(String methodId, boolean hasBody) {
23+
this.method = new ProgrammaticHttpMethod(methodId, hasBody);
24+
}
25+
26+
public String getMethodId() {
27+
return method.getMethodId();
28+
}
29+
30+
public boolean hasBody() {
31+
return method.hasBody();
32+
}
33+
34+
public @NotNull ProgrammaticHttpMethod getMethod() {
35+
return method;
36+
}
37+
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
import java.util.List;
4+
5+
public class HttpResponse {
6+
7+
private final int statusCode;
8+
private final List<HttpHeader> headers;
9+
private final String body;
10+
11+
public HttpResponse(int statusCode, List<HttpHeader> headers, String body) {
12+
this.statusCode = statusCode;
13+
this.headers = headers;
14+
this.body = body;
15+
}
16+
17+
public int getStatusCode() {
18+
return statusCode;
19+
}
20+
21+
public List<HttpHeader> getHeaders() {
22+
return headers;
23+
}
24+
25+
public String getBody() {
26+
return body;
27+
}
28+
29+
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.io.*;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
@SuppressWarnings("unused")
15+
public class HttpWrapper {
16+
17+
18+
// GET
19+
public static HttpResponse get(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
20+
return request(HttpMethod.GET, urlStr, headers, null);
21+
}
22+
23+
public static HttpResponse getOrNull(@NotNull String urlStr, @Nullable String[] headers) {
24+
return requestOrNull(HttpMethod.GET, urlStr, headers, null);
25+
}
26+
27+
// HEAD
28+
public static HttpResponse head(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
29+
return request(HttpMethod.HEAD, urlStr, headers, null);
30+
}
31+
32+
public static HttpResponse headOrNull(@NotNull String urlStr, @Nullable String[] headers) {
33+
return requestOrNull(HttpMethod.HEAD, urlStr, headers, null);
34+
}
35+
36+
// POST
37+
public static HttpResponse post(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException {
38+
return request(HttpMethod.POST, urlStr, headers, body);
39+
}
40+
41+
public static HttpResponse postOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) {
42+
return requestOrNull(HttpMethod.POST, urlStr, headers, body);
43+
}
44+
45+
// PUT
46+
public static HttpResponse put(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException {
47+
return request(HttpMethod.PUT, urlStr, headers, body);
48+
}
49+
50+
public static HttpResponse putOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) {
51+
return requestOrNull(HttpMethod.PUT, urlStr, headers, body);
52+
}
53+
54+
// DELETE
55+
public static HttpResponse delete(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
56+
return request(HttpMethod.DELETE, urlStr, headers, null);
57+
}
58+
59+
public static HttpResponse deleteOrNull(@NotNull String urlStr, @Nullable String[] headers) {
60+
return requestOrNull(HttpMethod.DELETE, urlStr, headers, null);
61+
}
62+
63+
// CONNECT
64+
public static HttpResponse connect(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
65+
return request(HttpMethod.CONNECT, urlStr, headers, null);
66+
}
67+
68+
public static HttpResponse connectOrNull(@NotNull String urlStr, @Nullable String[] headers) {
69+
return requestOrNull(HttpMethod.CONNECT, urlStr, headers, null);
70+
}
71+
72+
// OPTIONS
73+
public static HttpResponse options(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
74+
return request(HttpMethod.OPTIONS, urlStr, headers, null);
75+
}
76+
77+
public static HttpResponse optionsOrNull(@NotNull String urlStr, @Nullable String[] headers) {
78+
return requestOrNull(HttpMethod.OPTIONS, urlStr, headers, null);
79+
}
80+
81+
// TRACE
82+
public static HttpResponse trace(@NotNull String urlStr, @Nullable String[] headers) throws IOException {
83+
return request(HttpMethod.TRACE, urlStr, headers, null);
84+
}
85+
86+
public static HttpResponse traceOrNull(@NotNull String urlStr, @Nullable String[] headers) {
87+
return requestOrNull(HttpMethod.TRACE, urlStr, headers, null);
88+
}
89+
90+
// PATCH
91+
public static HttpResponse patch(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException {
92+
return request(HttpMethod.PATCH, urlStr, headers, body);
93+
}
94+
95+
public static HttpResponse patchOrNull(@NotNull String urlStr, @Nullable String[] headers, @Nullable String body) {
96+
return requestOrNull(HttpMethod.PATCH, urlStr, headers, body);
97+
}
98+
99+
// -----
100+
// UTILS
101+
// -----
102+
103+
public static HttpResponse request(@NotNull HttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) throws IOException {
104+
return convertHeadersAndExecute(method.getMethod(), urlStr, headers, postData);
105+
}
106+
107+
public static HttpResponse request(@NotNull HttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String postData) throws IOException {
108+
return execute(method.getMethod(), urlStr, headers, postData);
109+
}
110+
111+
@SuppressWarnings("CallToPrintStackTrace")
112+
public static HttpResponse requestOrNull(@NotNull HttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String postData) {
113+
try {
114+
return convertHeadersAndExecute(method.getMethod(), urlStr, headers, postData);
115+
} catch (IOException e) {
116+
e.printStackTrace();
117+
return null;
118+
}
119+
}
120+
121+
@SuppressWarnings("CallToPrintStackTrace")
122+
public static HttpResponse requestOrNull(@NotNull HttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String postData) {
123+
try {
124+
return execute(method.getMethod(), urlStr, headers, postData);
125+
} catch (IOException e) {
126+
e.printStackTrace();
127+
return null;
128+
}
129+
}
130+
131+
/**
132+
* Use {@link #request(HttpMethod, String, String[], String)} instead.
133+
* Only use if you know what you are doing.
134+
*/
135+
@Deprecated
136+
public static HttpResponse customRequest(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String body) throws IOException {
137+
return convertHeadersAndExecute(method, urlStr, headers, body);
138+
}
139+
140+
@NotNull
141+
private static HttpResponse convertHeadersAndExecute(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable String[] headers, @Nullable String bodyOut) throws IOException {
142+
List<HttpHeader> convertedHeaders = convertHeaders(headers);
143+
return execute(method, urlStr, convertedHeaders, bodyOut);
144+
}
145+
146+
@NotNull
147+
private static HttpResponse execute(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String bodyOut) throws IOException {
148+
HttpURLConnection connection = writeHttpRequest(method, urlStr, headers, bodyOut);
149+
return readHttpResponse(connection);
150+
}
151+
152+
private static @NotNull List<HttpHeader> convertHeaders(@Nullable String[] headers) {
153+
List<HttpHeader> headersList = new ArrayList<>();
154+
if (headers != null) {
155+
for (String header : headers) {
156+
if (header == null) continue;
157+
158+
String[] parts = header.split(":", 2);
159+
if (parts.length != 2) continue;
160+
161+
headersList.add(new HttpHeader(parts[0], parts[1]));
162+
}
163+
}
164+
return headersList;
165+
}
166+
167+
private static @NotNull HttpURLConnection writeHttpRequest(@NotNull ProgrammaticHttpMethod method, String urlStr, @Nullable List<HttpHeader> headers, @Nullable String bodyOut) throws IOException {
168+
URL url = new URL(urlStr);
169+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
170+
connection.setRequestMethod(method.getMethodId());
171+
172+
if (headers != null) {
173+
for (HttpHeader header : headers) {
174+
if (header == null) continue;
175+
connection.setRequestProperty(header.getKey(), header.getValue());
176+
}
177+
}
178+
179+
if (method.hasBody() && bodyOut != null) {
180+
connection.setDoOutput(true);
181+
try (OutputStream os = connection.getOutputStream()) {
182+
byte[] postDataBytes = bodyOut.getBytes(StandardCharsets.UTF_8);
183+
os.write(postDataBytes, 0, postDataBytes.length);
184+
}
185+
}
186+
return connection;
187+
}
188+
189+
private static @NotNull HttpResponse readHttpResponse(HttpURLConnection connection) throws IOException {
190+
int responseCode = connection.getResponseCode();
191+
192+
Map<String, List<String>> headerFields = connection.getHeaderFields();
193+
List<HttpHeader> headers = new ArrayList<>();
194+
for (Map.Entry<String, List<String>> headerEntry : headerFields.entrySet()) {
195+
for (String value : headerEntry.getValue()) {
196+
headers.add(new HttpHeader(headerEntry.getKey(), value));
197+
}
198+
}
199+
200+
InputStream inputStream;
201+
if (responseCode >= 200 && responseCode < 300) inputStream = connection.getInputStream();
202+
else inputStream = connection.getErrorStream();
203+
204+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
205+
StringBuilder bodyIn = new StringBuilder();
206+
while (true) {
207+
String line = reader.readLine();
208+
if (line == null) break;
209+
bodyIn.append(line);
210+
}
211+
reader.close();
212+
connection.disconnect();
213+
214+
return new HttpResponse(responseCode, headers, bodyIn.toString());
215+
}
216+
217+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.lightlibs.simplehttpwrapper;
2+
3+
public class ProgrammaticHttpMethod {
4+
5+
private final String methodId;
6+
private final boolean hasBody;
7+
8+
public ProgrammaticHttpMethod(String methodId, boolean hasBody) {
9+
this.methodId = methodId;
10+
this.hasBody = hasBody;
11+
}
12+
13+
public String getMethodId() {
14+
return methodId;
15+
}
16+
17+
public boolean hasBody() {
18+
return hasBody;
19+
}
20+
21+
}

src/main/java/com/github/lightlibs/simplehttpwrapper/SimpleHttpResponse.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)