Class OkHttpClient
- All Implemented Interfaces:
HttpClient
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic OkHttpClientBuilderbuilder()execute(HttpRequest request) Executes a given HTTP request synchronously and returns the response.voidexecute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) Executes a given HTTP request asynchronously with server-sent events (SSE) handling.executeAsync(HttpRequest request) Non-blocking counterpart ofHttpClient.execute(HttpRequest).stream(HttpRequest request, ServerSentEventParser parser) LikeHttpClient.stream(HttpRequest), but with a caller-suppliedServerSentEventParser.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpClient
execute, stream
-
Constructor Details
-
OkHttpClient
-
-
Method Details
-
builder
-
execute
Description copied from interface:HttpClientExecutes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.- Specified by:
executein interfaceHttpClient- Parameters:
request- the HTTP request to be executed.- Returns:
- a
SuccessfulHttpResponsecontaining the response data for successful HTTP requests (2XX status codes) - Throws:
HttpException- if the server returns a client (4XX) or server (5XX) error response
-
executeAsync
Description copied from interface:HttpClientNon-blocking counterpart ofHttpClient.execute(HttpRequest). Returns immediately with aCompletableFuturethat completes with theSuccessfulHttpResponseonce the full response has been received, without blocking the calling thread. The future completes exceptionally with anHttpExceptionfor non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.An implementation must not throw and must not return
null: every failure, including an invalid request (for example an invalid URL or header), completes the returned future exceptionally.- Specified by:
executeAsyncin interfaceHttpClient- Parameters:
request- the HTTP request to be executed.- Returns:
- a
CompletableFutureof theSuccessfulHttpResponse.
-
stream
LikeHttpClient.stream(HttpRequest), but with a caller-suppliedServerSentEventParser.An implementation must not throw and must not return
null: every failure, including an invalid request, reaches the subscriber'sonError, afteronSubscribe.Events are delivered incrementally as they arrive. Note that OkHttp exposes the response body only as a blocking source, so — unlike the JDK client, which consumes the body reactively and pins no thread — this publisher reads and parses it on an OkHttp dispatcher thread for the lifetime of the stream. On any terminal signal (a downstream cancel, an error, or a buffer overflow) the underlying call is cancelled, which closes the stream, aborts the connection, and frees that thread.
- Specified by:
streamin interfaceHttpClient
-
execute
public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) Description copied from interface:HttpClientExecutes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the providedServerSentEventListener.The execution flow is as follows:
- The request is initiated asynchronously
- Received SSE data is parsed using the provided parser
- Parsed events are delivered to the listener's appropriate methods
- If an error occurs,
ServerSentEventListener.onError(Throwable)is called
If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.
An implementation does not throw: every failure, including an invalid request (for example an invalid URL or header), is passed to
ServerSentEventListener.onError(Throwable), possibly before this method returns. IfonErrorthrows while it is called on the calling thread, that exception is thrown from this method.- Specified by:
executein interfaceHttpClient- Parameters:
request- the HTTP request to be executed.parser- the parser to process incoming server-sent events.listener- the listener to receive parsed events and error notifications.
-