Class Methanol
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 API, 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 cachinglayer. Transparentresponse decompression.- Intercept requests and responses going through this client.
- Specify an
AdapterCodecto 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
Nested ClassesModifier and TypeClassDescriptionstatic classMethanol.BaseBuilder<B extends Methanol.BaseBuilder<B>>A baseMethanolbuilder allowing to set the non-standard properties.static classA builder ofMethanolinstances.static interfaceAn object that intercepts requests before being sent and responses before being returned.static final classA builder forMethanolinstances with a pre-specified backendHttpClient.Nested classes/interfaces inherited from class HttpClient
HttpClient.Redirect, HttpClient.Version -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns this client'sauto Accept-Encodingsetting.Returns an immutable list of this client'sbackend interceptors.baseUri()Returns this client's base URI.cache()Returns this client'scache.caches()static Methanolcreate()Creates a defaultMethanolinstance.Returns this client's default headers.<T> Flow.Publisher<HttpResponse<T>> exchange(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Returns aPublisherfor 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 aPublisherfor the sequence ofHttpResponse<T>resulting from asynchronously sending the given request along with accepting incomingpush promisesusing the givenFunction.executor()Returns the headers timeout.Returns an immutable list of this client'sinterceptors.static Methanol.BuilderReturns a newMethanol.Builder.static Methanol.WithClientBuildernewBuilder(HttpClient backend) Returns a newMethanol.WithClientBuilderwith a prebuilt backend.Deprecated.proxy()Returns theread timeoutused for every request.Returns the defaultrequest timeoutused when no timeout is explicitly set on anHttpRequest.<T> HttpResponse<T> send(HttpRequest request, TypeRef<T> typeRef) Sendsthe given request and converts the response body into an object of the given type.<T> HttpResponse<T> send(HttpRequest request, Class<T> type) Sendsthe given request and converts the response body into an object of the given type.<T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, TypeRef<T> typeRef) Asynchronously sendsthe given request and converts the response body into an object of the given type.<T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Class<T> type) Asynchronously sendsthe given request and converts the response body into an object of the given type.<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 underlyingHttpClientused for sending requests.Returns this client'sUser-Agent.version()Methods inherited from class HttpClient
awaitTermination, close, isTerminated, newHttpClient, shutdown, shutdownNow
-
Method Details
-
exchange
public <T> Flow.Publisher<HttpResponse<T>> exchange(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) Returns aPublisherfor 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 aPublisherfor the sequence ofHttpResponse<T>resulting from asynchronously sending the given request along with accepting incomingpush promisesusing the givenFunction. The function accepts an incoming push promise by returning a non-nullBodyHandler<T>for handling the pushed response body. If anullhandler 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 underlyingHttpClientused for sending requests. -
userAgent
-
baseUri
-
requestTimeout
Returns the defaultrequest timeoutused when no timeout is explicitly set on anHttpRequest. -
headersTimeout
-
readTimeout
Returns theread timeoutused for every 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-Encodingsetting. -
cache
-
caches
-
adapterCodec
-
cookieHandler
- Specified by:
cookieHandlerin classHttpClient
-
connectTimeout
- Specified by:
connectTimeoutin classHttpClient
-
followRedirects
- Specified by:
followRedirectsin classHttpClient
-
proxy
- Specified by:
proxyin classHttpClient
-
sslContext
- Specified by:
sslContextin classHttpClient
-
sslParameters
- Specified by:
sslParametersin classHttpClient
-
authenticator
- Specified by:
authenticatorin classHttpClient
-
version
- Specified by:
versionin classHttpClient
-
executor
- Specified by:
executorin classHttpClient
-
newWebSocketBuilder
- Overrides:
newWebSocketBuilderin classHttpClient
-
send
public <T> HttpResponse<T> send(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) throws IOException, InterruptedException - Specified by:
sendin classHttpClient- Throws:
IOExceptionInterruptedException
-
sendAsync
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler) - Specified by:
sendAsyncin classHttpClient
-
sendAsync
public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, HttpResponse.BodyHandler<T> bodyHandler, @Nullable HttpResponse.PushPromiseHandler<T> pushPromiseHandler) - Specified by:
sendAsyncin classHttpClient
-
send
public <T> HttpResponse<T> send(HttpRequest request, Class<T> type) throws IOException, InterruptedException Sendsthe given request and converts the response body into an object of the given type.- Throws:
IOExceptionInterruptedException
-
sendAsync
Asynchronously sendsthe given request and converts the response body into an object of the given type. -
send
public <T> HttpResponse<T> send(HttpRequest request, TypeRef<T> typeRef) throws IOException, InterruptedException Sendsthe given request and converts the response body into an object of the given type.- Throws:
IOExceptionInterruptedException
-
sendAsync
Asynchronously sendsthe given request and converts the response body into an object of the given type. -
newBuilder
Returns a newMethanol.Builder. -
newBuilder
Returns a newMethanol.WithClientBuilderwith a prebuilt backend. -
create
Creates a defaultMethanolinstance.
-
backendInterceptors()