Skip to content

Commit

Permalink
Merge pull request #4 from JavaWebStack/dev
Browse files Browse the repository at this point in the history
Release 1.0.1
  • Loading branch information
JanHolger authored Sep 4, 2022
2 parents 89a793b + f9c8f48 commit f9914b8
Show file tree
Hide file tree
Showing 12 changed files with 1,133 additions and 151 deletions.
4 changes: 2 additions & 2 deletions 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 All @@ -22,6 +22,6 @@ You can find the current docs on our [website](https://docs.javawebstack.org/fra
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>http-client</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>
```
25 changes: 16 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<buildVersion>1.0.0-SNAPSHOT</buildVersion>
<buildVersion>1.0.1-SNAPSHOT</buildVersion>
</properties>

<groupId>org.javawebstack</groupId>
<artifactId>http-client</artifactId>
<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 @@ -32,6 +32,12 @@
<organization>JavaWebStack</organization>
<organizationUrl>https://javawebstack.org</organizationUrl>
</developer>
<developer>
<name>Julian Gojani</name>
<email>[email protected]</email>
<organization>JavaWebStack</organization>
<organizationUrl>https://javawebstack.org</organizationUrl>
</developer>
</developers>

<scm>
Expand All @@ -41,22 +47,23 @@
</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>
<version>1.0.0</version>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</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

0 comments on commit f9914b8

Please sign in to comment.