Class ApacheHttpClient

java.lang.Object
dev.langchain4j.http.client.apache.ApacheHttpClient
All Implemented Interfaces:
HttpClient

public class ApacheHttpClient extends Object implements HttpClient
  • Constructor Details

  • Method Details

    • builder

      public static ApacheHttpClientBuilder builder()
    • execute

      public SuccessfulHttpResponse execute(HttpRequest request) throws HttpException
      Description copied from interface: HttpClient
      Executes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.
      Specified by:
      execute in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a SuccessfulHttpResponse containing 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

      public CompletableFuture<SuccessfulHttpResponse> executeAsync(HttpRequest request)
      Description copied from interface: HttpClient
      Non-blocking counterpart of HttpClient.execute(HttpRequest). Returns immediately with a CompletableFuture that completes with the SuccessfulHttpResponse once the full response has been received, without blocking the calling thread. The future completes exceptionally with an HttpException for non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.
      Specified by:
      executeAsync in interface HttpClient
      Parameters:
      request - the HTTP request to be executed.
      Returns:
      a CompletableFuture of the SuccessfulHttpResponse.
    • stream

      Like HttpClient.stream(HttpRequest), but with a caller-supplied ServerSentEventParser.

      Incrementally streaming, like the JDK client: the response body is consumed off the socket by an AsyncResponseConsumer as it arrives, bytes are pushed through the parser's incremental mode, and each completed event is delivered immediately — nothing is buffered to end-of-response, and no thread is pinned for the lifetime of the stream. On any terminal signal (a downstream cancel, an error, or a buffer overflow) no further events are delivered and cancellation of the in-flight request is requested; note that Apache's async client does not always close the underlying connection promptly on cancellation (see cancelAbortsConnection in the client's cancellation contract test), whereas event delivery stops immediately.

      Specified by:
      stream in interface HttpClient
    • execute

      public void execute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener)
      Description copied from interface: HttpClient
      Executes 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 provided ServerSentEventListener.

      The execution flow is as follows:

      1. The request is initiated asynchronously
      2. Received SSE data is parsed using the provided parser
      3. Parsed events are delivered to the listener's appropriate methods
      4. 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.

      Specified by:
      execute in interface HttpClient
      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.