Skip to content

Commit

Permalink
[Core] Added Support to set Cookies to WebClient
Browse files Browse the repository at this point in the history
  • Loading branch information
rprudhvi committed Jan 9, 2024
1 parent 5f2ae1f commit 0ae55ea
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@

import io.github.selcukes.collections.Resources;
import io.github.selcukes.databind.utils.JsonUtils;
import lombok.Singular;
import lombok.SneakyThrows;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static java.net.http.HttpRequest.BodyPublisher;
import static java.net.http.HttpRequest.BodyPublishers;
Expand All @@ -40,11 +42,64 @@ public class WebClient {
private HttpClient.Builder clientBuilder;
private HttpRequest.Builder requestBuilder;
private BodyPublisher bodyPublisher;
@Singular
private final Map<String, String> cookies;
@Singular
private final Map<String, String> queryParams;
private final String baseUri;
private String endpoint;

public WebClient(final String uri) {
public WebClient(String baseUri) {
clientBuilder = HttpClient.newBuilder();
requestBuilder = HttpRequest.newBuilder()
.uri(Resources.toURI(uri));
requestBuilder = HttpRequest.newBuilder();
queryParams = new ConcurrentHashMap<>();
cookies = new ConcurrentHashMap<>();
this.baseUri = Objects.requireNonNull(baseUri, "baseUri must not be null");
this.endpoint = "";
}

/**
* Sets the endpoint for the request.
*
* @param endpoint The endpoint to set.
* @return The WebClient object.
*/
public WebClient endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}

/**
* Adds a cookie to the request.
*
* @param name The name of the cookie.
* @param value The value of the cookie.
* @return The WebClient object.
*/
public WebClient cookie(String name, String value) {
cookies.put(name, value);
return this;
}

/**
* Sets the body of the request.
*
* @param payload The payload to be set as the request body.
* @return The WebClient object.
*/
public WebClient body(final Object payload) {
this.bodyPublisher = bodyPublisher(payload);
return this;
}

/**
* This function creates a GET request and executes it.
*
* @return A Response object.
*/
public WebResponse get() {
requestBuilder.GET();
return execute();
}

/**
Expand All @@ -59,9 +114,10 @@ public WebClient(final String uri) {
*/
@SneakyThrows
public WebResponse post(final Object payload) {
contentType("application/json");
var request = requestBuilder.POST(bodyPublisher(payload)).build();
return execute(request);
contentType("application/json")
.body(payload);
requestBuilder.POST(bodyPublisher);
return execute();
}

/**
Expand All @@ -71,8 +127,8 @@ public WebResponse post(final Object payload) {
*/
@SneakyThrows
public WebResponse post() {
var request = requestBuilder.POST(bodyPublisher).build();
return execute(request);
requestBuilder.POST(bodyPublisher);
return execute();
}

/**
Expand All @@ -81,8 +137,8 @@ public WebResponse post() {
* @return A Response object.
*/
public WebResponse delete() {
var request = requestBuilder.DELETE().build();
return execute(request);
requestBuilder.DELETE();
return execute();
}

/**
Expand All @@ -97,8 +153,9 @@ public WebResponse delete() {
* @return A Response object
*/
public WebResponse put(final Object payload) {
var request = requestBuilder.PUT(bodyPublisher(payload)).build();
return execute(request);
body(payload);
requestBuilder.PUT(bodyPublisher);
return execute();
}

@SneakyThrows
Expand Down Expand Up @@ -141,20 +198,18 @@ private <K, V> BodyPublisher multiPartBody(final Map<K, V> data, final String bo
}

@SneakyThrows
private WebResponse execute(final HttpRequest request) {
private WebResponse execute() {
if (!cookies.isEmpty()) {
String cookieHeader = cookies.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("; "));
requestBuilder = requestBuilder.header("Cookie", cookieHeader);
clientBuilder.followRedirects(HttpClient.Redirect.NORMAL);
}
var request = requestBuilder.uri(buildUri()).build();
return new WebResponse(clientBuilder.build().send(request, ofString()));
}

/**
* This function creates a GET request and executes it.
*
* @return A Response object.
*/
public WebResponse get() {
var request = requestBuilder.GET().build();
return execute(request);
}

/**
* If the proxy parameter is a valid URL, then set the proxy host and port
* to the host and port of the URL
Expand Down Expand Up @@ -237,4 +292,31 @@ public WebClient contentType(final String type) {
return this;
}

/**
* Adds a query parameter to the request.
* <p>
* This method allows you to include query parameters in your HTTP request.
* The provided name and value are encoded and added to the query parameters
* map.
* </p>
*
* @param name The name of the query parameter.
* @param value The value of the query parameter.
* @return The {@code WebClient} object with the specified query
* parameter added.
*/
public WebClient queryParams(String name, String value) {
queryParams.put(encode(name), encode(value));
return this;
}

private String encode(String value) {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}

private URI buildUri() {
var joiner = new StringJoiner("&");
queryParams.forEach((key, value) -> joiner.add(encode(key) + "=" + encode(value)));
return Resources.toURI(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void uploadFileTest() {

@Test
public void requestTest() {
var client = new WebClient("https://reqres.in/api/users/2");
var responseBody = client.get().bodyJson();
var client = new WebClient("https://reqres.in/api");
var responseBody = client.endpoint("/users/2").get().bodyJson();
assertEquals(responseBody.at("/data/id").asText(), "2");
assertEquals(responseBody.at("/data/first_name").asText(), "Janet");
}
Expand All @@ -74,4 +74,29 @@ public void authTest() {
.get().bodyJson();
assertTrue(responseBody.at("/authenticated").asBoolean());
}

@Test
public void queryParamTest() {
var perPage = 20;
var client = new WebClient("https://api.github.com");
var responseBody = client
.queryParams("q", "john")
.queryParams("per_page", String.valueOf(perPage))
.endpoint("/search/users")
.get().bodyJson();
var items = responseBody.at("/items");
assertTrue(items.isArray());
assertEquals(items.size(), perPage);

}

@Test
public void cookieTest() {
var client = new WebClient("https://postman-echo.com/cookies");
var responseBody = client
.cookie("skill", "1234")
.endpoint("/set")
.get().bodyJson();
assertEquals(responseBody.at("/cookies/skill").asText(), "1234");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JsonUtils {
* @param object The object to be converted to JSON
* @return A JSON string
*/
public String toJson(final Object object) {
public <T> String toJson(final T object) {
try {
return new ObjectMapper().writeValueAsString(object);
} catch (Exception e) {
Expand All @@ -44,7 +44,7 @@ public String toJson(final Object object) {
* @param object The object to be converted to JSON
* @return A JSON string
*/
public String toPrettyJson(final Object object) {
public <T> String toPrettyJson(final T object) {
try {
return new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
} catch (Exception e) {
Expand Down

0 comments on commit 0ae55ea

Please sign in to comment.