Skip to content

Commit c65ecc3

Browse files
authored
feat: JDK HTTP Client (#474)
feat: Add jdk-http-client and jdk-http-client-pureconfig subprojects build: Switch to JDK 11 for building but set target version to JDK 1.8
1 parent 5990892 commit c65ecc3

File tree

9 files changed

+104
-6
lines changed

9 files changed

+104
-6
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ jobs:
99
runs-on: ubuntu-20.04
1010
steps:
1111
- uses: actions/checkout@v2
12-
- name: Set up JDK 1.8
12+
- name: Set up JDK 11
1313
uses: actions/setup-java@v1
1414
with:
15-
java-version: 1.8
15+
java-version: 11
1616
- uses: coursier/cache-action@v5
1717
with:
1818
extraKey: ${{ secrets.BUILD_CACHE_VERSION }}

.github/workflows/pull.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ jobs:
66
runs-on: ubuntu-20.04
77
steps:
88
- uses: actions/checkout@v2
9-
- name: Set up JDK 1.8
9+
- name: Set up JDK 11
1010
uses: actions/setup-java@v1
1111
with:
12-
java-version: 1.8
12+
java-version: 11
1313
- uses: coursier/cache-action@v5
1414
with:
1515
extraKey: pr-${GITHUB_HEAD_REF}

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
runs-on: ubuntu-20.04
99
steps:
1010
- uses: actions/checkout@v2
11-
- name: Set up JDK 1.8
11+
- name: Set up JDK 11
1212
uses: actions/setup-java@v1
1313
with:
14-
java-version: 1.8
14+
java-version: 11
1515
- uses: coursier/cache-action@v5
1616
- name: Check/Compile/Test
1717
run: sbt check

build.sbt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ lazy val root = project
2323
http4sServerBlaze,
2424
http4sServerBlazePureConfig,
2525
http4sServerMicrometer,
26+
jdkHttpClient,
27+
jdkHttpClientPureConfig,
2628
jvm,
2729
jvmMicrometer,
2830
jvmPureConfig,
@@ -297,6 +299,22 @@ lazy val http4sServerMicrometer = project
297299
)
298300
)
299301

302+
lazy val jdkHttpClient = project
303+
.in(file("jdk-http-client"))
304+
.settings(BuildSettings.common)
305+
.settings(
306+
name := "sst-jdk-http-client"
307+
)
308+
309+
lazy val jdkHttpClientPureConfig = project
310+
.in(file("jdk-http-client-pureconfig"))
311+
.dependsOn(jdkHttpClient)
312+
.settings(BuildSettings.common)
313+
.settings(
314+
name := "sst-jdk-http-client-pureconfig",
315+
libraryDependencies += Dependencies.pureConfig
316+
)
317+
300318
lazy val jvm = project
301319
.in(file("jvm"))
302320
.settings(BuildSettings.common)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.avast.sst.jdk.httpclient.pureconfig
2+
3+
import com.avast.sst.jdk.httpclient.JdkHttpClientConfig
4+
import pureconfig.ConfigReader
5+
import pureconfig.generic.ProductHint
6+
import pureconfig.generic.semiauto.deriveReader
7+
8+
trait ConfigReaders {
9+
10+
implicit protected def hint[T]: ProductHint[T] = ProductHint.default
11+
12+
implicit val jdkHttpClientConfigReader: ConfigReader[JdkHttpClientConfig] = deriveReader
13+
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.avast.sst.jdk.httpclient.pureconfig
2+
3+
import pureconfig.ConfigFieldMapping
4+
import pureconfig.generic.ProductHint
5+
6+
/** Contains [[pureconfig.ConfigReader]] instances with default "kebab-case" naming convention. */
7+
object implicits extends ConfigReaders {
8+
9+
/** Contains [[pureconfig.ConfigReader]] instances with "kebab-case" naming convention.
10+
*
11+
* This is alias for the default `implicits._` import.
12+
*/
13+
object KebabCase extends ConfigReaders
14+
15+
/** Contains [[pureconfig.ConfigReader]] instances with "camelCase" naming convention. */
16+
object CamelCase extends ConfigReaders {
17+
implicit override protected def hint[T]: ProductHint[T] = ProductHint(ConfigFieldMapping(pureconfig.CamelCase, pureconfig.CamelCase))
18+
}
19+
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.avast.sst.jdk.httpclient
2+
3+
import java.net.http.HttpClient
4+
import scala.concurrent.duration.FiniteDuration
5+
6+
final case class JdkHttpClientConfig(
7+
connectTimeout: Option[FiniteDuration] = None,
8+
followRedirects: Option[HttpClient.Redirect] = None,
9+
version: Option[HttpClient.Version] = None,
10+
priority: Option[Int] = None
11+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

project/BuildSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ object BuildSettings {
4444
"-Ywarn-unused", // necessary for Scalafix RemoveUnused rule (not present in sbt-tpolecat for 2.13)
4545
"-P:silencer:checkUnused"
4646
) ++ (if (scalaVersion.value.startsWith("2.13")) List("-Wmacros:after") else List.empty),
47+
javacOptions ++= Seq("-source", "1.8", "-target", "1.8"),
4748
Test / publishArtifact := false
4849
)
4950

0 commit comments

Comments
 (0)