import io.netty.handler.ssl.SslContextBuilder
import zio.http.model.headers.Headers
import zio.http.service.ClientSSLHandler.ClientSSLOptions
import zio.http.service.{ChannelFactory, Client, EventLoopGroup}
import zio._
import java.io.InputStream
import java.security.KeyStore
import javax.net.ssl.TrustManagerFactory
object HttpsClient extends App {
val env = ChannelFactory.auto ++ EventLoopGroup.auto()
val url = "https://sports.api.decathlon.com/groups/water-aerobics"
val headers = Headers.host("sports.api.decathlon.com")
val trustStore: KeyStore = KeyStore.getInstance("JKS")
val trustStorePath: InputStream = getClass.getClassLoader.getResourceAsStream("truststore.jks")
val trustStorePassword: String = "changeit"
val trustManagerFactory: TrustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
trustStore.load(trustStorePath, trustStorePassword.toCharArray)
trustManagerFactory.init(trustStore)
val sslOption: ClientSSLOptions =
ClientSSLOptions.CustomSSL(SslContextBuilder.forClient().trustManager(trustManagerFactory).build())
val program = for {
res <- Client.request(url, headers, sslOption)
data <- res.bodyAsString
_ <- console.putStrLn { data }
} yield ()
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode]
= program.exitCode.provideCustomLayer(env)
}