- java.lang.Object
-
- java.net.http.HttpClient
-
- com.github.mizosoft.methanol.Methanol
-
public class Methanol extends java.net.http.HttpClientAnHttpClientwith interceptors, request decoration, HTTP caching and reactive extensions.In addition to implementing the
HttpClientAPI, 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
Methanolclient relies on a standardHttpClientinstance for sending requests, referred to as its backend. You can obtain builders forMethanolusing eithernewBuilder()ornewBuilder(HttpClient). The latter takes a prebuilt backend, while the former allows configuring a backend to be newly created each timeMethanol.BaseBuilder.build()is invoked. Note thatHttpCachesare not usable with a prebuilt backend. - Specify a
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMethanol.BaseBuilder<B extends Methanol.BaseBuilder<B>>A baseMethanolbuilder allowing to set the non-standard properties.static classMethanol.BuilderA builder ofMethanolinstances.static interfaceMethanol.InterceptorAn object that intercepts requests before being sent and responses before being returned.static classMethanol.WithClientBuilderA builder forMethanolinstances with a pre-specified backendHttpClient.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Optional<AdapterCodec>adapterCodec()java.util.Optional<java.net.Authenticator>authenticator()booleanautoAcceptEncoding()Returns this client'sauto Accept-Encodingsetting.java.util.List<Methanol.Interceptor>backendInterceptors()Returns an immutable list of this client'sbackend interceptors.java.util.Optional<java.net.URI>baseUri()Returns this client's base URI.java.util.Optional<HttpCache>cache()Returns this client'scache.java.util.List<HttpCache>caches()java.util.Optional<java.time.Duration>connectTimeout()java.util.Optional<java.net.CookieHandler>cookieHandler()static Methanolcreate()Creates a defaultMethanolinstance.java.net.http.HttpHeadersdefaultHeaders()Returns this client's default headers.<T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>>exchange(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)Returns aPublisherfor theHttpResponse<T>resulting from asynchronously sending the given request.<T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>>exchange(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, java.util.function.Function<java.net.http.HttpRequest,@Nullable java.net.http.HttpResponse.BodyHandler<T>> pushPromiseMapper)Returns aPublisherfor the sequence ofHttpResponse<T>resulting from asynchronously sending the given request along with accepting incomingpush promisesusing the givenFunction.java.util.Optional<java.util.concurrent.Executor>executor()java.net.http.HttpClient.RedirectfollowRedirects()java.util.Optional<java.time.Duration>headersTimeout()Returns the headers timeout.java.util.List<Methanol.Interceptor>interceptors()Returns an immutable list of this client'sinterceptors.static Methanol.BuildernewBuilder()Returns a newMethanol.Builder.static Methanol.WithClientBuildernewBuilder(java.net.http.HttpClient backend)Returns a newMethanol.WithClientBuilderwith a prebuilt backend.java.net.http.WebSocket.BuildernewWebSocketBuilder()java.util.List<Methanol.Interceptor>postDecorationInterceptors()Deprecated.java.util.Optional<java.net.ProxySelector>proxy()java.util.Optional<java.time.Duration>readTimeout()Returns theread timeoutused for every request.java.util.Optional<java.time.Duration>requestTimeout()Returns the defaultrequest timeoutused when no timeout is explicitly set on anHttpRequest.<T> java.net.http.HttpResponse<T>send(java.net.http.HttpRequest request, TypeRef<T> typeRef)Sendsthe given request and converts the response body into an object of the given type.<T> java.net.http.HttpResponse<T>send(java.net.http.HttpRequest request, java.lang.Class<T> type)Sendsthe given request and converts the response body into an object of the given type.<T> java.net.http.HttpResponse<T>send(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)<T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>>sendAsync(java.net.http.HttpRequest request, TypeRef<T> typeRef)Asynchronously sendsthe given request and converts the response body into an object of the given type.<T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>>sendAsync(java.net.http.HttpRequest request, java.lang.Class<T> type)Asynchronously sendsthe given request and converts the response body into an object of the given type.<T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>>sendAsync(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)<T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>>sendAsync(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, @Nullable java.net.http.HttpResponse.PushPromiseHandler<T> pushPromiseHandler)javax.net.ssl.SSLContextsslContext()javax.net.ssl.SSLParameterssslParameters()java.net.http.HttpClientunderlyingClient()Returns the underlyingHttpClientused for sending requests.java.util.Optional<java.lang.String>userAgent()Returns this client'sUser-Agent.java.net.http.HttpClient.Versionversion()
-
-
-
Method Detail
-
exchange
public <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)Returns aPublisherfor theHttpResponse<T>resulting from asynchronously sending the given request.
-
exchange
public <T> java.util.concurrent.Flow.Publisher<java.net.http.HttpResponse<T>> exchange(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, java.util.function.Function<java.net.http.HttpRequest,@Nullable java.net.http.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
public java.net.http.HttpClient underlyingClient()
Returns the underlyingHttpClientused for sending requests.
-
userAgent
public java.util.Optional<java.lang.String> userAgent()
Returns this client'sUser-Agent.
-
baseUri
public java.util.Optional<java.net.URI> baseUri()
Returns this client's base URI.
-
requestTimeout
public java.util.Optional<java.time.Duration> requestTimeout()
Returns the defaultrequest timeoutused when no timeout is explicitly set on anHttpRequest.
-
headersTimeout
public java.util.Optional<java.time.Duration> headersTimeout()
Returns the headers timeout.
-
readTimeout
public java.util.Optional<java.time.Duration> readTimeout()
Returns theread timeoutused for every request.
-
interceptors
public java.util.List<Methanol.Interceptor> interceptors()
Returns an immutable list of this client'sinterceptors.
-
backendInterceptors
public java.util.List<Methanol.Interceptor> backendInterceptors()
Returns an immutable list of this client'sbackend interceptors.
-
postDecorationInterceptors
@Deprecated(since="1.5.0") public java.util.List<Methanol.Interceptor> postDecorationInterceptors()
Deprecated.Returns the list of interceptors invoked after request decoration.
-
defaultHeaders
public java.net.http.HttpHeaders defaultHeaders()
Returns this client's default headers.
-
autoAcceptEncoding
public boolean autoAcceptEncoding()
Returns this client'sauto Accept-Encodingsetting.
-
caches
public java.util.List<HttpCache> caches()
-
adapterCodec
public java.util.Optional<AdapterCodec> adapterCodec()
-
cookieHandler
public java.util.Optional<java.net.CookieHandler> cookieHandler()
- Specified by:
cookieHandlerin classjava.net.http.HttpClient
-
connectTimeout
public java.util.Optional<java.time.Duration> connectTimeout()
- Specified by:
connectTimeoutin classjava.net.http.HttpClient
-
followRedirects
public java.net.http.HttpClient.Redirect followRedirects()
- Specified by:
followRedirectsin classjava.net.http.HttpClient
-
proxy
public java.util.Optional<java.net.ProxySelector> proxy()
- Specified by:
proxyin classjava.net.http.HttpClient
-
sslContext
public javax.net.ssl.SSLContext sslContext()
- Specified by:
sslContextin classjava.net.http.HttpClient
-
sslParameters
public javax.net.ssl.SSLParameters sslParameters()
- Specified by:
sslParametersin classjava.net.http.HttpClient
-
authenticator
public java.util.Optional<java.net.Authenticator> authenticator()
- Specified by:
authenticatorin classjava.net.http.HttpClient
-
version
public java.net.http.HttpClient.Version version()
- Specified by:
versionin classjava.net.http.HttpClient
-
executor
public java.util.Optional<java.util.concurrent.Executor> executor()
- Specified by:
executorin classjava.net.http.HttpClient
-
newWebSocketBuilder
public java.net.http.WebSocket.Builder newWebSocketBuilder()
- Overrides:
newWebSocketBuilderin classjava.net.http.HttpClient
-
send
public <T> java.net.http.HttpResponse<T> send(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler) throws java.io.IOException, java.lang.InterruptedException- Specified by:
sendin classjava.net.http.HttpClient- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
sendAsync
public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler)- Specified by:
sendAsyncin classjava.net.http.HttpClient
-
sendAsync
public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync(java.net.http.HttpRequest request, java.net.http.HttpResponse.BodyHandler<T> bodyHandler, @Nullable java.net.http.HttpResponse.PushPromiseHandler<T> pushPromiseHandler)- Specified by:
sendAsyncin classjava.net.http.HttpClient
-
send
public <T> java.net.http.HttpResponse<T> send(java.net.http.HttpRequest request, java.lang.Class<T> type) throws java.io.IOException, java.lang.InterruptedExceptionSendsthe given request and converts the response body into an object of the given type.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
sendAsync
public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync(java.net.http.HttpRequest request, java.lang.Class<T> type)Asynchronously sendsthe given request and converts the response body into an object of the given type.
-
send
public <T> java.net.http.HttpResponse<T> send(java.net.http.HttpRequest request, TypeRef<T> typeRef) throws java.io.IOException, java.lang.InterruptedExceptionSendsthe given request and converts the response body into an object of the given type.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
sendAsync
public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> sendAsync(java.net.http.HttpRequest request, TypeRef<T> typeRef)Asynchronously sendsthe given request and converts the response body into an object of the given type.
-
newBuilder
public static Methanol.Builder newBuilder()
Returns a newMethanol.Builder.
-
newBuilder
public static Methanol.WithClientBuilder newBuilder(java.net.http.HttpClient backend)
Returns a newMethanol.WithClientBuilderwith a prebuilt backend.
-
create
public static Methanol create()
Creates a defaultMethanolinstance.
-
-