Skip to content

Commit fb8e408

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/org/javawebstack/httpclient/HTTPClient.java
2 parents e555ed4 + 32cdf29 commit fb8e408

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<p align="center"><img src="https://raw.githubusercontent.com/JavaWebStack/docs/master/src/assets/img/icon.svg" width="100">
2+
<br><br>
3+
JavaWebStack HTTP-Client
4+
</p>
5+
6+
7+
8+
## A little tour
9+
Full documentations: [HTTP-Client JWS-Docs](https://javawebstack.org/docs/http-client)
10+
11+
```java
12+
HTTPClient client = new HTTPClient();
13+
14+
// Sending a simple GET-Request and printing the response-text
15+
System.out.println(client.get("https://example.javawebstack.org/api/test").string());
16+
17+
// Setting a base-url
18+
client.setBaseUrl("https://example.javawebstack.org"); // or new HTTPClient(baseUrl)
19+
20+
// Sending a POST-Request with a JSON-Body
21+
Map<String, Object> data = new HashMap<>();
22+
data.put("username", "password");
23+
24+
HTTPResponse res = client.post("/api/post")
25+
.jsonBody(data).json(User.class);
26+
```

src/main/java/org/javawebstack/httpclient/HTTPClient.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.gson.FieldNamingPolicy;
44
import com.google.gson.Gson;
55
import com.google.gson.GsonBuilder;
6+
import org.javawebstack.httpclient.interfaces.BeforeRequest;
7+
import org.javawebstack.httpclient.interfaces.ResponseTransformer;
68

79
import java.util.HashMap;
810
import java.util.Map;
@@ -17,6 +19,15 @@ public class HTTPClient {
1719
private int timeout = 5000;
1820
private String baseUrl = "";
1921
private Map<String, String> defaultHeaders = new HashMap<>();
22+
private ResponseTransformer transformer;
23+
24+
private BeforeRequest beforeInterceptor;
25+
26+
public HTTPClient(String baseUrl) {
27+
this.baseUrl = baseUrl;
28+
}
29+
30+
public HTTPClient() { }
2031
private Map<String, String> defaultQuery = new HashMap<>();
2132

2233
public HTTPClient gson(Gson gson){
@@ -80,6 +91,11 @@ public HTTPClient setBaseUrl(String baseUrl) {
8091
return this;
8192
}
8293

94+
public HTTPClient transformer(ResponseTransformer responseTransformer){
95+
transformer = responseTransformer;
96+
return this;
97+
}
98+
8399
public String getBaseUrl() {
84100
return baseUrl;
85101
}
@@ -92,6 +108,15 @@ public HTTPRequest get(String path){
92108
return request("GET", path);
93109
}
94110

111+
public HTTPClient before(BeforeRequest requestInterceptor){
112+
beforeInterceptor = requestInterceptor;
113+
return this;
114+
}
115+
116+
public BeforeRequest getBeforeInterceptor() {
117+
return beforeInterceptor;
118+
}
119+
95120
public HTTPRequest post(String path){
96121
return request("POST", path);
97122
}
@@ -112,4 +137,7 @@ public HTTPRequest delete(String path){
112137
return request("DELETE", path);
113138
}
114139

140+
public ResponseTransformer getTransformer() {
141+
return transformer;
142+
}
115143
}

src/main/java/org/javawebstack/httpclient/HTTPRequest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.javawebstack.httpclient;
22

3+
import org.javawebstack.httpclient.interfaces.ResponseTransformer;
34
import org.javawebstack.querystring.QueryString;
45

56
import java.io.ByteArrayOutputStream;
@@ -95,7 +96,7 @@ public int status(){
9596
}
9697

9798
public byte[] bytes(){
98-
if (requestBody == null)
99+
if (responseBody == null)
99100
execute();
100101

101102
return responseBody;
@@ -119,6 +120,17 @@ public String header(String key){
119120
return responseHeaders.get(key);
120121
}
121122

123+
public <T> T transform(ResponseTransformer responseTransformer, Class<T> type){
124+
return (T) responseTransformer.transform(this);
125+
}
126+
127+
/*
128+
* Requires a transformer in the HttpClient
129+
* */
130+
public <T> T transform(Class<T> type){
131+
return (T) client.getTransformer().transform(this);
132+
}
133+
122134
public HTTPRequest execute(){
123135
HttpURLConnection conn = null;
124136
try{
@@ -131,6 +143,11 @@ public HTTPRequest execute(){
131143
for(String k : requestHeaders.keySet()){
132144
conn.setRequestProperty(k, requestHeaders.get(k));
133145
}
146+
147+
if (client.getBeforeInterceptor() != null)
148+
client.getBeforeInterceptor().doBefore(this);
149+
150+
134151
if(requestBody!=null){
135152
conn.setDoOutput(true);
136153
OutputStream os = conn.getOutputStream();
@@ -172,4 +189,7 @@ private static byte[] readAll(InputStream is) throws IOException {
172189
return baos.toByteArray();
173190
}
174191

192+
public String toString(){
193+
return string();
194+
}
175195
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.javawebstack.httpclient.interfaces;
2+
3+
import org.javawebstack.httpclient.HTTPRequest;
4+
5+
public interface BeforeRequest {
6+
void doBefore(HTTPRequest request);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.javawebstack.httpclient.interfaces;
2+
3+
import org.javawebstack.httpclient.HTTPRequest;
4+
5+
public interface ResponseTransformer {
6+
Object transform(HTTPRequest request);
7+
}

0 commit comments

Comments
 (0)