|
| 1 | +package datadog.http.client; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.Proxy; |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.concurrent.CompletableFuture; |
| 8 | +import java.util.concurrent.Executor; |
| 9 | +import javax.annotation.Nullable; |
| 10 | + |
| 11 | +/** |
| 12 | + * This interface is an abstraction for HTTP clients, providing request execution capabilities. This |
| 13 | + * abstraction is implementation-agnostic and can be backed by third party libraries of the JDK |
| 14 | + * itself. |
| 15 | + * |
| 16 | + * <p>HttpClient instances should be reused across requests for connection pooling. |
| 17 | + */ |
| 18 | +public interface HttpClient { |
| 19 | + /** |
| 20 | + * Executes an HTTP request synchronously and returns the response. The caller is responsible for |
| 21 | + * closing the response. |
| 22 | + * |
| 23 | + * @param request the request to execute |
| 24 | + * @return the HTTP response |
| 25 | + * @throws IOException if an I/O error occurs |
| 26 | + */ |
| 27 | + HttpResponse execute(HttpRequest request) throws IOException; |
| 28 | + |
| 29 | + /** |
| 30 | + * Executes an HTTP request asynchronously and returns a {@link CompletableFuture}. The caller is |
| 31 | + * responsible for closing the response. |
| 32 | + * |
| 33 | + * @param request the request to execute |
| 34 | + * @return a CompletableFuture that completes with the HTTP response |
| 35 | + */ |
| 36 | + CompletableFuture<HttpResponse> executeAsync(HttpRequest request); |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates a new {@link Builder} for constructing HTTP clients. |
| 40 | + * |
| 41 | + * @return a new http client builder |
| 42 | + */ |
| 43 | + static Builder newBuilder() { |
| 44 | + return HttpProviders.newClientBuilder(); |
| 45 | + } |
| 46 | + |
| 47 | + /** Builder for constructing {@link HttpClient} instances. */ |
| 48 | + interface Builder { |
| 49 | + /** |
| 50 | + * Sets the client timeouts, including the connection. |
| 51 | + * |
| 52 | + * @param timeout the timeout duration |
| 53 | + * @return this builder |
| 54 | + */ |
| 55 | + Builder connectTimeout(Duration timeout); |
| 56 | + |
| 57 | + /** |
| 58 | + * Sets the proxy configuration. |
| 59 | + * |
| 60 | + * @param proxy the proxy to use |
| 61 | + * @return this builder |
| 62 | + */ |
| 63 | + Builder proxy(Proxy proxy); |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets proxy authentication credentials. |
| 67 | + * |
| 68 | + * @param username the proxy username |
| 69 | + * @param password the proxy password, or {@code null} to use an empty password |
| 70 | + * @return this builder |
| 71 | + */ |
| 72 | + Builder proxyAuthenticator(String username, @Nullable String password); |
| 73 | + |
| 74 | + /** |
| 75 | + * Configures the client to use a Unix domain socket. |
| 76 | + * |
| 77 | + * @param socketFile the Unix domain socket file |
| 78 | + * @return this builder |
| 79 | + */ |
| 80 | + Builder unixDomainSocket(File socketFile); |
| 81 | + |
| 82 | + /** |
| 83 | + * Configures the client to use a named pipe (Windows). |
| 84 | + * |
| 85 | + * @param pipeName the named pipe name |
| 86 | + * @return this builder |
| 87 | + */ |
| 88 | + Builder namedPipe(String pipeName); |
| 89 | + |
| 90 | + /** |
| 91 | + * Forces clear text (HTTP) connections, disabling TLS. |
| 92 | + * |
| 93 | + * @param clearText {@code true} to force HTTP, {@code false} to allow HTTPS |
| 94 | + * @return this builder |
| 95 | + */ |
| 96 | + Builder clearText(boolean clearText); |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets a custom executor for executing async requests. |
| 100 | + * |
| 101 | + * @param executor the executor to use for async requests |
| 102 | + * @return this builder |
| 103 | + */ |
| 104 | + Builder executor(Executor executor); |
| 105 | + |
| 106 | + /** |
| 107 | + * Builds the {@link HttpClient} with the configured settings. |
| 108 | + * |
| 109 | + * @return the constructed HttpClient |
| 110 | + */ |
| 111 | + HttpClient build(); |
| 112 | + } |
| 113 | +} |
0 commit comments