Skip to content

Commit

Permalink
#4 - ENH: Add withVersion() and withExecutor() methods to HttpClientC…
Browse files Browse the repository at this point in the history
…ontext.Builder
  • Loading branch information
rbygrave committed Apr 12, 2021
1 parent c114a66 commit ca363c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.CookieManager;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.concurrent.Executor;

import static java.util.Objects.requireNonNull;

Expand All @@ -23,6 +24,9 @@ class DHttpClientContextBuilder implements HttpClientContext.Builder {

private HttpClient.Redirect redirect = HttpClient.Redirect.NORMAL;

private HttpClient.Version version;
private Executor executor;

DHttpClientContextBuilder() {
}

Expand Down Expand Up @@ -67,6 +71,16 @@ public HttpClientContext.Builder withRedirect(HttpClient.Redirect redirect) {
this.redirect = redirect;
return this;
}
@Override
public HttpClientContext.Builder withVersion(HttpClient.Version version) {
this.version = version;
return this;
}
@Override
public HttpClientContext.Builder withExecutor(Executor executor) {
this.executor = executor;
return this;
}

@Override
public HttpClientContext build() {
Expand All @@ -86,6 +100,12 @@ private HttpClient defaultClient() {
if (cookieHandler != null) {
builder.cookieHandler(cookieHandler);
}
if (version != null) {
builder.version(version);
}
if (executor != null) {
builder.executor(executor);
}
return builder.build();
}

Expand Down
24 changes: 24 additions & 0 deletions client/src/main/java/io/avaje/http/client/HttpClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.Executor;

/**
* The HTTP client context that we use to build and process requests.
Expand Down Expand Up @@ -77,6 +78,8 @@ interface Builder {

/**
* Set the underlying HttpClient to use.
* <p>
* Used when we wish to control all options of the HttpClient.
*/
Builder with(HttpClient client);

Expand All @@ -87,6 +90,8 @@ interface Builder {

/**
* Set the default request timeout.
*
* @see java.net.http.HttpRequest.Builder#timeout(Duration)
*/
Builder withRequestTimeout(Duration requestTimeout);

Expand All @@ -105,14 +110,33 @@ interface Builder {

/**
* Specify a cookie handler to use on the HttpClient. This would override the default cookie handler.
*
* @see HttpClient.Builder#cookieHandler(CookieHandler)
*/
Builder withCookieHandler(CookieHandler cookieHandler);

/**
* Specify the redirect policy. Defaults to HttpClient.Redirect.NORMAL.
*
* @see HttpClient.Builder#followRedirects(HttpClient.Redirect)
*/
Builder withRedirect(HttpClient.Redirect redirect);

/**
* Specify the HTTP version. Defaults to not set.
*
* @see HttpClient.Builder#version(HttpClient.Version)
*/
Builder withVersion(HttpClient.Version version);

/**
* Specify the Executor to use for asynchronous tasks.
* If not specified a default executor will be used.
*
* @see HttpClient.Builder#executor(Executor)
*/
Builder withExecutor(Executor executor);

/**
* Build and return the context.
*/
Expand Down

0 comments on commit ca363c4

Please sign in to comment.