|
| 1 | +package com.avast.sst.jdk.httpclient |
| 2 | + |
| 3 | +import java.net.http.HttpClient |
| 4 | +import java.net.{Authenticator, CookieHandler, ProxySelector} |
| 5 | +import java.time.{Duration => JDuration} |
| 6 | +import java.util.concurrent.Executor |
| 7 | +import javax.net.ssl.SSLContext |
| 8 | + |
| 9 | +object JdkHttpClientModule { |
| 10 | + |
| 11 | + /** Makes [[java.net.http.HttpClient]] initialized with the given config. */ |
| 12 | + def make( |
| 13 | + config: JdkHttpClientConfig, |
| 14 | + executor: Option[Executor] = None, |
| 15 | + sslContext: Option[SSLContext] = None, |
| 16 | + cookieHandler: Option[CookieHandler] = None, |
| 17 | + proxySelector: Option[ProxySelector] = None, |
| 18 | + authenticator: Option[Authenticator] = None |
| 19 | + ): HttpClient = { |
| 20 | + val builder = HttpClient.newBuilder() |
| 21 | + |
| 22 | + config.connectTimeout.map(d => JDuration.ofNanos(d.toNanos)).foreach(builder.connectTimeout) |
| 23 | + executor.foreach(builder.executor) |
| 24 | + sslContext.foreach(builder.sslContext) |
| 25 | + config.followRedirects.foreach(builder.followRedirects) |
| 26 | + config.priority.foreach(builder.priority) |
| 27 | + cookieHandler.foreach(builder.cookieHandler) |
| 28 | + proxySelector.foreach(builder.proxy) |
| 29 | + authenticator.foreach(builder.authenticator) |
| 30 | + |
| 31 | + builder.build() |
| 32 | + } |
| 33 | + |
| 34 | +} |
0 commit comments