scala-pekko-client.apiSettings.mustache Maven / Gradle / Ivy
{{>licenseInfo}}
package {{invokerPackage}}
import java.util.concurrent.TimeUnit
import org.apache.pekko.actor.{ActorSystem, ExtendedActorSystem, Extension, ExtensionId, ExtensionIdProvider}
import org.apache.pekko.http.scaladsl.model.{StatusCode, StatusCodes}
import org.apache.pekko.http.scaladsl.model.headers.RawHeader
import com.typesafe.config.Config
import scala.jdk.CollectionConverters._
import scala.concurrent.duration.FiniteDuration
class ApiSettings(config: Config) extends Extension {
def this(system: ExtendedActorSystem) = this(system.settings.config)
private def cfg = config.getConfig("{{configKeyPath}}.{{configKey}}")
val alwaysTrustCertificates: Boolean = cfg.getBoolean("trust-certificates")
val defaultHeaders: List[RawHeader] = cfg.getConfig("default-headers").entrySet.asScala.toList.map(c => RawHeader(c.getKey, c.getValue.render))
val connectionTimeout = FiniteDuration(cfg.getDuration("connection-timeout", TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS)
val compressionEnabled: Boolean = cfg.getBoolean("compression.enabled")
val compressionSizeThreshold: Int = cfg.getBytes("compression.size-threshold").toInt
val customCodes: List[StatusCode]= cfg.getConfigList("custom-codes").asScala.toList.map { c =>
StatusCodes.custom(
c.getInt("code"),
c.getString("reason"),
if (c.hasPath("defaultMessage")) c.getString("defaultMessage") else c.getString("reason"),
c.getBoolean("success"),
if (c.hasPath("allowsEntity")) c.getBoolean("allowsEntity") else true
)
}
}
object ApiSettings extends ExtensionId[ApiSettings] with ExtensionIdProvider {
override def lookup = ApiSettings
override def createExtension(system: ExtendedActorSystem): ApiSettings =
new ApiSettings(system)
// needed to get the type right when used from Java
override def get(system: ActorSystem): ApiSettings = super.get(system)
}