Skip to content

Commit f9914b8

Browse files
authored
Merge pull request #4 from JavaWebStack/dev
Release 1.0.1
2 parents 89a793b + f9c8f48 commit f9914b8

12 files changed

+1133
-151
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ JWS HTTP Client
55

66
![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/JavaWebStack/http-client/Maven%20Deploy/master)
77
![GitHub](https://img.shields.io/github/license/JavaWebStack/http-client)
8-
![Maven metadata URL](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Forg%2Fjavawebstack%2FHTTP-Client%2Fmaven-metadata.xml)
8+
![Maven metadata URL](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Forg%2Fjavawebstack%2Fhttp-client%2Fmaven-metadata.xml)
99
![GitHub contributors](https://img.shields.io/github/contributors/JavaWebStack/http-client)
1010
![Lines of code](https://img.shields.io/tokei/lines/github/JavaWebStack/http-client)
1111
![Discord](https://img.shields.io/discord/815612319378833408?color=%237289DA&label=discord)
@@ -22,6 +22,6 @@ You can find the current docs on our [website](https://docs.javawebstack.org/fra
2222
<dependency>
2323
<groupId>org.javawebstack</groupId>
2424
<artifactId>http-client</artifactId>
25-
<version>1.0.0</version>
25+
<version>1.0.1</version>
2626
</dependency>
2727
```

pom.xml

+16-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
<properties>
88
<maven.compiler.source>8</maven.compiler.source>
99
<maven.compiler.target>8</maven.compiler.target>
10-
<buildVersion>1.0.0-SNAPSHOT</buildVersion>
10+
<buildVersion>1.0.1-SNAPSHOT</buildVersion>
1111
</properties>
1212

1313
<groupId>org.javawebstack</groupId>
1414
<artifactId>http-client</artifactId>
1515
<version>${buildVersion}</version>
1616

1717
<name>http-client</name>
18-
<description>A simple to use and extendable http client wrapper around java's built in HttpURLConnection.</description>
18+
<description>A simple to use and extendable http client</description>
1919
<url>https://github.com/JavaWebStack/http-client</url>
2020

2121
<licenses>
@@ -32,6 +32,12 @@
3232
<organization>JavaWebStack</organization>
3333
<organizationUrl>https://javawebstack.org</organizationUrl>
3434
</developer>
35+
<developer>
36+
<name>Julian Gojani</name>
37+
<email>[email protected]</email>
38+
<organization>JavaWebStack</organization>
39+
<organizationUrl>https://javawebstack.org</organizationUrl>
40+
</developer>
3541
</developers>
3642

3743
<scm>
@@ -41,22 +47,23 @@
4147
</scm>
4248

4349
<dependencies>
44-
<dependency>
45-
<groupId>com.google.code.gson</groupId>
46-
<artifactId>gson</artifactId>
47-
<version>2.8.9</version>
48-
</dependency>
4950
<dependency>
5051
<groupId>org.javawebstack</groupId>
5152
<artifactId>abstract-data</artifactId>
52-
<version>1.0.0</version>
53+
<version>1.0.4</version>
5354
</dependency>
5455
<dependency>
5556
<groupId>org.junit.jupiter</groupId>
5657
<artifactId>junit-jupiter-engine</artifactId>
57-
<version>5.8.1</version>
58+
<version>5.9.0</version>
5859
<scope>test</scope>
5960
</dependency>
61+
<dependency>
62+
<groupId>org.apache.httpcomponents.client5</groupId>
63+
<artifactId>httpclient5</artifactId>
64+
<version>5.1.3</version>
65+
<optional>true</optional>
66+
</dependency>
6067
</dependencies>
6168

6269
<build>

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

+61-29
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@
22

33
import org.javawebstack.abstractdata.AbstractMapper;
44
import org.javawebstack.abstractdata.NamingPolicy;
5+
import org.javawebstack.httpclient.implementation.IHTTPRequestImplementation;
6+
import org.javawebstack.httpclient.implementation.JavaNetHTTPRequestImplementation;
57
import org.javawebstack.httpclient.interceptor.RequestInterceptor;
8+
import org.javawebstack.httpclient.websocket.WebSocket;
9+
import org.javawebstack.httpclient.websocket.WebSocketHandler;
610

11+
import java.io.IOException;
712
import java.net.HttpCookie;
813
import java.nio.charset.StandardCharsets;
914
import java.util.*;
15+
import java.util.function.Supplier;
1016

1117
public class HTTPClient {
1218

1319
private AbstractMapper abstractMapper = new AbstractMapper()
1420
.setNamingPolicy(NamingPolicy.SNAKE_CASE);
1521
private int timeout = 5000;
16-
private String baseUrl = "";
22+
private String baseUrl;
1723
private Map<String, String[]> defaultHeaders = new HashMap<>();
1824
private Map<String, String> defaultQuery = new HashMap<>();
1925
private List<HttpCookie> defaultCookies = new ArrayList<>();
2026

27+
private Supplier<? extends IHTTPRequestImplementation> httpImplementation = JavaNetHTTPRequestImplementation::new;
28+
2129
private RequestInterceptor beforeInterceptor;
2230
private RequestInterceptor afterInterceptor;
2331

@@ -31,45 +39,56 @@ public HTTPClient(String baseUrl) {
3139
this.baseUrl = baseUrl;
3240
}
3341

34-
public HTTPClient() { }
42+
public HTTPClient() {
43+
this("");
44+
}
3545

36-
public HTTPClient debug(){
46+
public HTTPClient debug() {
3747
this.debug = true;
3848
return this;
3949
}
4050

41-
public boolean isDebug(){
51+
public boolean isDebug() {
4252
return debug;
4353
}
4454

45-
public HTTPClient autoCookies(){
55+
public Supplier<? extends IHTTPRequestImplementation> getHttpImplementation() {
56+
return httpImplementation;
57+
}
58+
59+
public HTTPClient httpImplementation(Supplier<? extends IHTTPRequestImplementation> implementation) {
60+
this.httpImplementation = implementation;
61+
return this;
62+
}
63+
64+
public HTTPClient autoCookies() {
4665
autoCookies = true;
4766
return this;
4867
}
4968

50-
public boolean isAutoCookies(){
69+
public boolean isAutoCookies() {
5170
return autoCookies;
5271
}
5372

54-
public HTTPClient setSSLVerification(boolean sslVerification){
73+
public HTTPClient setSSLVerification(boolean sslVerification) {
5574
this.sslVerification = sslVerification;
5675
return this;
5776
}
5877

59-
public boolean isSSLVerification(){
78+
public boolean isSSLVerification() {
6079
return this.sslVerification;
6180
}
6281

63-
public HTTPClient abstractMapper(AbstractMapper mapper){
82+
public HTTPClient abstractMapper(AbstractMapper mapper) {
6483
this.abstractMapper = mapper;
6584
return this;
6685
}
6786

68-
public AbstractMapper getAbstractMapper(){
87+
public AbstractMapper getAbstractMapper() {
6988
return abstractMapper;
7089
}
7190

72-
public HTTPClient timeout(int timeout){
91+
public HTTPClient timeout(int timeout) {
7392
this.timeout = timeout;
7493
return this;
7594
}
@@ -78,35 +97,35 @@ public int getTimeout() {
7897
return timeout;
7998
}
8099

81-
public HTTPClient header(String key, String... values){
100+
public HTTPClient header(String key, String... values) {
82101
defaultHeaders.put(key, values);
83102
return this;
84103
}
85104

86-
public HTTPClient query(String key, String value){
105+
public HTTPClient query(String key, String value) {
87106
defaultQuery.put(key, value);
88107
return this;
89108
}
90109

91-
public HTTPClient cookie(HttpCookie cookie){
110+
public HTTPClient cookie(HttpCookie cookie) {
92111
removeCookie(cookie.getName());
93112
defaultCookies.add(cookie);
94113
return this;
95114
}
96115

97-
public HTTPClient removeCookie(String name){
98-
for(HttpCookie cookie : new HashSet<>(defaultCookies)){
116+
public HTTPClient removeCookie(String name) {
117+
for(HttpCookie cookie : new HashSet<>(defaultCookies)) {
99118
if(cookie.getName().equalsIgnoreCase(name))
100119
defaultCookies.remove(cookie);
101120
}
102121
return this;
103122
}
104123

105-
public List<HttpCookie> getDefaultCookies(){
124+
public List<HttpCookie> getDefaultCookies() {
106125
return defaultCookies;
107126
}
108127

109-
public HTTPClient setDefaultCookies(List<HttpCookie> cookies){
128+
public HTTPClient setDefaultCookies(List<HttpCookie> cookies) {
110129
this.defaultCookies = cookies;
111130
return this;
112131
}
@@ -124,16 +143,16 @@ public HTTPClient setDefaultQuery(Map<String, String> defaultQuery) {
124143
return this;
125144
}
126145

127-
public HTTPClient setDefaultHeaders(Map<String, String[]> defaultHeaders){
146+
public HTTPClient setDefaultHeaders(Map<String, String[]> defaultHeaders) {
128147
this.defaultHeaders = defaultHeaders;
129148
return this;
130149
}
131150

132-
public HTTPClient authorization(String type, String value){
151+
public HTTPClient authorization(String type, String value) {
133152
return header("Authorization", type + " " + value);
134153
}
135154

136-
public HTTPClient bearer(String token){
155+
public HTTPClient bearer(String token) {
137156
return authorization("Bearer", token);
138157
}
139158

@@ -152,15 +171,28 @@ public String getBaseUrl() {
152171
return baseUrl;
153172
}
154173

155-
public HTTPRequest request(String method, String path){
174+
public HTTPRequest request(String method, String path) {
156175
return new HTTPRequest(this, method, path);
157176
}
158177

159-
public HTTPRequest get(String path){
178+
public WebSocket webSocket(String path, WebSocketHandler handler) throws IOException {
179+
return webSocket(path, handler, null);
180+
}
181+
182+
public WebSocket webSocket(String path, WebSocketHandler handler, Map<String, String> additionalHeaders) throws IOException {
183+
HTTPClientSocket socket = new HTTPClientSocket(getBaseUrl() + ((path.startsWith("/") || path.startsWith("http://") || path.startsWith("https://")) ? "" : "/") + path, !isSSLVerification());
184+
if(additionalHeaders != null)
185+
additionalHeaders.forEach(socket::setRequestHeader);
186+
WebSocket webSocket = new WebSocket(socket, handler);
187+
new Thread(webSocket).start();
188+
return webSocket;
189+
}
190+
191+
public HTTPRequest get(String path) {
160192
return request("GET", path);
161193
}
162194

163-
public HTTPClient before(RequestInterceptor requestInterceptor){
195+
public HTTPClient before(RequestInterceptor requestInterceptor) {
164196
beforeInterceptor = requestInterceptor;
165197
return this;
166198
}
@@ -177,23 +209,23 @@ public RequestInterceptor getAfterInterceptor() {
177209
return afterInterceptor;
178210
}
179211

180-
public HTTPRequest post(String path){
212+
public HTTPRequest post(String path) {
181213
return request("POST", path);
182214
}
183215

184-
public HTTPRequest post(String path, Object body){
216+
public HTTPRequest post(String path, Object body) {
185217
return post(path).jsonBody(body);
186218
}
187219

188-
public HTTPRequest put(String path){
220+
public HTTPRequest put(String path) {
189221
return request("PUT", path);
190222
}
191223

192-
public HTTPRequest put(String path, Object body){
224+
public HTTPRequest put(String path, Object body) {
193225
return put(path).jsonBody(body);
194226
}
195227

196-
public HTTPRequest delete(String path){
228+
public HTTPRequest delete(String path) {
197229
return request("DELETE", path);
198230
}
199231

0 commit comments

Comments
 (0)