java.lang.Object
java.net.http.HttpClient
com.github.mizosoft.methanol.Methanol
- All Implemented Interfaces:
AutoCloseable
An
HttpClient
with interceptors, request decoration, HTTP caching and reactive
extensions.
In addition to implementing the HttpClient
interface, this class allows to:
- Specify a
base URI
. - Specify a default
request timeout
. - Specify a read timeout.
- Add a set of default HTTP headers for inclusion in requests if absent.
- Add an
HTTP caching
layer. Transparent
response decompression.- Intercept requests and responses going through this client.
- Specify an
AdapterCodec
to automatically convert to/from request/response bodies. - Get
Publisher<HttpResponse<T>>
for asynchronous requests.
A Methanol
client relies on a standard HttpClient
instance for sending
requests, referred to as its backend. You can obtain builders for Methanol
using either
newBuilder()
or newBuilder(HttpClient)
. The latter takes a prebuilt backend,
while the former allows configuring a backend to be newly created each time Methanol.BaseBuilder.build()
is invoked. Note that HttpCaches
are not usable with a prebuilt
backend.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Methanol.BaseBuilder<B extends Methanol.BaseBuilder<B>>
A baseMethanol
builder allowing to set the non-standard properties.static final class
A builder ofMethanol
instances.static interface
An object that intercepts requests being sent over aMethanol
client.static final class
A builder forMethanol
instances with a pre-specified backendHttpClient
.Nested classes/interfaces inherited from class java.net.http.HttpClient
HttpClient.Redirect, HttpClient.Version
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Returns this client'sauto Accept-Encoding
setting.Returns an immutable list of this client'sbackend interceptors
.baseUri()
Returns this client's baseURI
.cache()
Returns this client'scache
.caches()
static Methanol
create()
Creates a defaultMethanol
instance.Returns this client's default headers.<T> Flow.Publisher
<HttpResponse<T>> exchange
(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Returns aPublisher
for theHttpResponse<T>
resulting from asynchronously sending the given request.<T> Flow.Publisher
<HttpResponse<T>> exchange
(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, Function<HttpRequest, @Nullable HttpResponse.BodyHandler<T>> pushPromiseMapper) Returns aPublisher
for the sequence ofHttpResponse<T>
resulting from asynchronously sending the given request along with accepting incomingpush promises
using the givenFunction
.executor()
Returns the headers timeout.Returns an immutable list of this client'sinterceptors
.static Methanol.Builder
Returns a newMethanol.Builder
.static Methanol.WithClientBuilder
newBuilder
(HttpClient backend) Returns a newMethanol.WithClientBuilder
with a prebuilt backend.Deprecated.proxy()
Returns theread timeout
used for each request.Returns the default request timeout used when not set in anHttpRequest
.send
(HttpRequest request) Likesend(HttpRequest, BodyHandler)
but defers handling the response body into the desired type using the providedResponsePayload
.<T> HttpResponse
<T> send
(HttpRequest request, TypeRef<T> bodyType) <T> HttpResponse
<T> send
(HttpRequest request, Class<T> bodyType) <T> HttpResponse
<T> send
(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) sendAsync
(HttpRequest request) Likesend(HttpRequest, BodyHandler)
but defers handling the response body into the desired type using the providedResponsePayload
.<T> CompletableFuture
<HttpResponse<T>> sendAsync
(HttpRequest request, TypeRef<T> bodyType) <T> CompletableFuture
<HttpResponse<T>> sendAsync
(HttpRequest request, Class<T> bodyType) <T> CompletableFuture
<HttpResponse<T>> sendAsync
(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) <T> CompletableFuture
<HttpResponse<T>> sendAsync
(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, @Nullable HttpResponse.PushPromiseHandler<T> pushPromiseHandler) Returns the underlyingHttpClient
used for sending requests.Returns this client'sUser-Agent
.version()
Methods inherited from class java.net.http.HttpClient
awaitTermination, close, isTerminated, newHttpClient, newWebSocketBuilder, shutdown, shutdownNow
-
Method Details
-
exchange
public <T> Flow.Publisher<HttpResponse<T>> exchange(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Returns aPublisher
for theHttpResponse<T>
resulting from asynchronously sending the given request. -
exchange
public <T> Flow.Publisher<HttpResponse<T>> exchange(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, Function<HttpRequest, @Nullable HttpResponse.BodyHandler<T>> pushPromiseMapper) Returns aPublisher
for the sequence ofHttpResponse<T>
resulting from asynchronously sending the given request along with accepting incomingpush promises
using the givenFunction
. The function accepts an incoming push promise by returning a non-null
BodyHandler<T>
for handling the pushed response body. If anull
handler is returned, the push promise will be rejected.Note that the published sequence has no specific order, and hence the main response is not guaranteed to be the first and may appear anywhere in the sequence.
-
underlyingClient
Returns the underlyingHttpClient
used for sending requests. -
userAgent
Returns this client'sUser-Agent
. -
baseUri
Returns this client's baseURI
. -
requestTimeout
Returns the default request timeout used when not set in anHttpRequest
. -
headersTimeout
Returns the headers timeout. -
readTimeout
Returns theread timeout
used for each request. -
interceptors
Returns an immutable list of this client'sinterceptors
. -
backendInterceptors
Returns an immutable list of this client'sbackend interceptors
. -
postDecorationInterceptors
Deprecated.Returns the list of interceptors invoked after request decoration. -
defaultHeaders
Returns this client's default headers. -
autoAcceptEncoding
public boolean autoAcceptEncoding()Returns this client'sauto Accept-Encoding
setting. -
cache
Returns this client'scache
. -
caches
-
adapterCodec
-
cookieHandler
- Specified by:
cookieHandler
in classHttpClient
-
connectTimeout
- Specified by:
connectTimeout
in classHttpClient
-
followRedirects
- Specified by:
followRedirects
in classHttpClient
-
proxy
- Specified by:
proxy
in classHttpClient
-
sslContext
- Specified by:
sslContext
in classHttpClient
-
sslParameters
- Specified by:
sslParameters
in classHttpClient
-
authenticator
- Specified by:
authenticator
in classHttpClient
-
version
- Specified by:
version
in classHttpClient
-
executor
- Specified by:
executor
in classHttpClient
-
send
public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) throws IOException, InterruptedException - Specified by:
send
in classHttpClient
- Throws:
IOException
InterruptedException
-
send
public HttpResponse<ResponsePayload> send(HttpRequest request) throws IOException, InterruptedException Likesend(HttpRequest, BodyHandler)
but defers handling the response body into the desired type using the providedResponsePayload
. The call completes as soon as the headers are received. Forgetting to close the returned payload (e.g. using try-with-resources) is a programming error.- Throws:
IOException
InterruptedException
-
sendAsync
Likesend(HttpRequest, BodyHandler)
but defers handling the response body into the desired type using the providedResponsePayload
. The call completes as soon as the headers are received. Forgetting to close the returned payload (e.g. using try-with-resources) is a programming error. -
sendAsync
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) - Specified by:
sendAsync
in classHttpClient
-
sendAsync
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, @Nullable HttpResponse.PushPromiseHandler<T> pushPromiseHandler) - Specified by:
sendAsync
in classHttpClient
-
send
public <T> HttpResponse<T> send(HttpRequest request, Class<T> bodyType) throws IOException, InterruptedException - Throws:
IOException
InterruptedException
-
sendAsync
-
send
public <T> HttpResponse<T> send(HttpRequest request, TypeRef<T> bodyType) throws IOException, InterruptedException - Throws:
IOException
InterruptedException
-
sendAsync
-
newBuilder
Returns a newMethanol.Builder
. -
newBuilder
Returns a newMethanol.WithClientBuilder
with a prebuilt backend. -
create
Creates a defaultMethanol
instance.
-
backendInterceptors()