Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ JWS HTTP Client

![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/JavaWebStack/http-client/Maven%20Deploy/master)
![GitHub](https://img.shields.io/github/license/JavaWebStack/http-client)
![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)
![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)
![GitHub contributors](https://img.shields.io/github/contributors/JavaWebStack/http-client)
![Lines of code](https://img.shields.io/tokei/lines/github/JavaWebStack/http-client)
![Discord](https://img.shields.io/discord/815612319378833408?color=%237289DA&label=discord)
Expand Down
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<version>${buildVersion}</version>

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

<licenses>
Expand All @@ -41,11 +41,6 @@
</scm>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>abstract-data</artifactId>
Expand All @@ -54,9 +49,15 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
90 changes: 61 additions & 29 deletions src/main/java/org/javawebstack/httpclient/HTTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

import org.javawebstack.abstractdata.AbstractMapper;
import org.javawebstack.abstractdata.NamingPolicy;
import org.javawebstack.httpclient.implementation.IHTTPRequestImplementation;
import org.javawebstack.httpclient.implementation.JavaNetHTTPRequestImplementation;
import org.javawebstack.httpclient.interceptor.RequestInterceptor;
import org.javawebstack.httpclient.websocket.WebSocket;
import org.javawebstack.httpclient.websocket.WebSocketHandler;

import java.io.IOException;
import java.net.HttpCookie;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Supplier;

public class HTTPClient {

private AbstractMapper abstractMapper = new AbstractMapper()
.setNamingPolicy(NamingPolicy.SNAKE_CASE);
private int timeout = 5000;
private String baseUrl = "";
private String baseUrl;
private Map<String, String[]> defaultHeaders = new HashMap<>();
private Map<String, String> defaultQuery = new HashMap<>();
private List<HttpCookie> defaultCookies = new ArrayList<>();

private Supplier<? extends IHTTPRequestImplementation> httpImplementation = JavaNetHTTPRequestImplementation::new;

private RequestInterceptor beforeInterceptor;
private RequestInterceptor afterInterceptor;

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

public HTTPClient() { }
public HTTPClient() {
this("");
}

public HTTPClient debug(){
public HTTPClient debug() {
this.debug = true;
return this;
}

public boolean isDebug(){
public boolean isDebug() {
return debug;
}

public HTTPClient autoCookies(){
public Supplier<? extends IHTTPRequestImplementation> getHttpImplementation() {
return httpImplementation;
}

public HTTPClient httpImplementation(Supplier<? extends IHTTPRequestImplementation> implementation) {
this.httpImplementation = implementation;
return this;
}

public HTTPClient autoCookies() {
autoCookies = true;
return this;
}

public boolean isAutoCookies(){
public boolean isAutoCookies() {
return autoCookies;
}

public HTTPClient setSSLVerification(boolean sslVerification){
public HTTPClient setSSLVerification(boolean sslVerification) {
this.sslVerification = sslVerification;
return this;
}

public boolean isSSLVerification(){
public boolean isSSLVerification() {
return this.sslVerification;
}

public HTTPClient abstractMapper(AbstractMapper mapper){
public HTTPClient abstractMapper(AbstractMapper mapper) {
this.abstractMapper = mapper;
return this;
}

public AbstractMapper getAbstractMapper(){
public AbstractMapper getAbstractMapper() {
return abstractMapper;
}

public HTTPClient timeout(int timeout){
public HTTPClient timeout(int timeout) {
this.timeout = timeout;
return this;
}
Expand All @@ -78,35 +97,35 @@ public int getTimeout() {
return timeout;
}

public HTTPClient header(String key, String... values){
public HTTPClient header(String key, String... values) {
defaultHeaders.put(key, values);
return this;
}

public HTTPClient query(String key, String value){
public HTTPClient query(String key, String value) {
defaultQuery.put(key, value);
return this;
}

public HTTPClient cookie(HttpCookie cookie){
public HTTPClient cookie(HttpCookie cookie) {
removeCookie(cookie.getName());
defaultCookies.add(cookie);
return this;
}

public HTTPClient removeCookie(String name){
for(HttpCookie cookie : new HashSet<>(defaultCookies)){
public HTTPClient removeCookie(String name) {
for(HttpCookie cookie : new HashSet<>(defaultCookies)) {
if(cookie.getName().equalsIgnoreCase(name))
defaultCookies.remove(cookie);
}
return this;
}

public List<HttpCookie> getDefaultCookies(){
public List<HttpCookie> getDefaultCookies() {
return defaultCookies;
}

public HTTPClient setDefaultCookies(List<HttpCookie> cookies){
public HTTPClient setDefaultCookies(List<HttpCookie> cookies) {
this.defaultCookies = cookies;
return this;
}
Expand All @@ -124,16 +143,16 @@ public HTTPClient setDefaultQuery(Map<String, String> defaultQuery) {
return this;
}

public HTTPClient setDefaultHeaders(Map<String, String[]> defaultHeaders){
public HTTPClient setDefaultHeaders(Map<String, String[]> defaultHeaders) {
this.defaultHeaders = defaultHeaders;
return this;
}

public HTTPClient authorization(String type, String value){
public HTTPClient authorization(String type, String value) {
return header("Authorization", type + " " + value);
}

public HTTPClient bearer(String token){
public HTTPClient bearer(String token) {
return authorization("Bearer", token);
}

Expand All @@ -152,15 +171,28 @@ public String getBaseUrl() {
return baseUrl;
}

public HTTPRequest request(String method, String path){
public HTTPRequest request(String method, String path) {
return new HTTPRequest(this, method, path);
}

public HTTPRequest get(String path){
public WebSocket webSocket(String path, WebSocketHandler handler) throws IOException {
return webSocket(path, handler, null);
}

public WebSocket webSocket(String path, WebSocketHandler handler, Map<String, String> additionalHeaders) throws IOException {
HTTPClientSocket socket = new HTTPClientSocket(getBaseUrl() + ((path.startsWith("/") || path.startsWith("http://") || path.startsWith("https://")) ? "" : "/") + path, !isSSLVerification());
if(additionalHeaders != null)
additionalHeaders.forEach(socket::setRequestHeader);
WebSocket webSocket = new WebSocket(socket, handler);
new Thread(webSocket).start();
return webSocket;
}

public HTTPRequest get(String path) {
return request("GET", path);
}

public HTTPClient before(RequestInterceptor requestInterceptor){
public HTTPClient before(RequestInterceptor requestInterceptor) {
beforeInterceptor = requestInterceptor;
return this;
}
Expand All @@ -177,23 +209,23 @@ public RequestInterceptor getAfterInterceptor() {
return afterInterceptor;
}

public HTTPRequest post(String path){
public HTTPRequest post(String path) {
return request("POST", path);
}

public HTTPRequest post(String path, Object body){
public HTTPRequest post(String path, Object body) {
return post(path).jsonBody(body);
}

public HTTPRequest put(String path){
public HTTPRequest put(String path) {
return request("PUT", path);
}

public HTTPRequest put(String path, Object body){
public HTTPRequest put(String path, Object body) {
return put(path).jsonBody(body);
}

public HTTPRequest delete(String path){
public HTTPRequest delete(String path) {
return request("DELETE", path);
}

Expand Down
Loading