From 1995268b620a5b58e5a31803e05abfb338de2bac Mon Sep 17 00:00:00 2001 From: Ewan Donovan Date: Mon, 3 Feb 2025 11:49:42 +0000 Subject: [PATCH] Replaces yaml with env variable. Defaults added. --- helm_deploy/hmpps-learner-records-api/values.yaml | 3 +++ .../hmpps/learnerrecordsapi/config/AppConfig.kt | 5 +++-- .../config/HttpClientConfiguration.kt | 12 ++++++------ src/main/resources/application-dev.yml | 3 --- src/main/resources/application-local.yml | 3 --- src/main/resources/application.yml | 3 --- .../config/HmppsBoldLrsExceptionHandlerTest.kt | 2 +- src/test/resources/application-test.yml | 3 --- 8 files changed, 13 insertions(+), 21 deletions(-) diff --git a/helm_deploy/hmpps-learner-records-api/values.yaml b/helm_deploy/hmpps-learner-records-api/values.yaml index b6ca1c7..86964b0 100644 --- a/helm_deploy/hmpps-learner-records-api/values.yaml +++ b/helm_deploy/hmpps-learner-records-api/values.yaml @@ -44,6 +44,9 @@ generic-service: PFX_FILE_PASSWORD: "PFX_FILE_PASSWORD" UK_PRN: "UK_PRN" VENDOR_ID: "VENDOR_ID" + LRS_CONNECT_TIMEOUT: 20 + LRS_WRITE_TIMEOUT: 20 + LRS_READ_TIMEOUT: 20 allowlist: groups: diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/AppConfig.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/AppConfig.kt index 0a35e3d..438776a 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/AppConfig.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/AppConfig.kt @@ -5,8 +5,9 @@ import org.springframework.context.annotation.Configuration @Configuration class AppConfig { fun ukprn(): String = System.getenv("UK_PRN") ?: throw IllegalArgumentException("UK_PRN environment variable not found.") - fun password(): String = System.getenv("ORG_PASSWORD") ?: throw IllegalArgumentException("ORG_PASSWORD environment variable not found.") - fun vendorId(): String = System.getenv("VENDOR_ID") ?: throw IllegalArgumentException("VENDOR_ID environment variable not found.") + fun lrsConnectTimeout(): Long = (System.getenv("LRS_CONNECT_TIMEOUT") ?: "20").toLong() + fun lrsWriteTimeout(): Long = (System.getenv("LRS_WRITE_TIMEOUT") ?: "20").toLong() + fun lrsReadTimeout(): Long = (System.getenv("LRS_READ_TIMEOUT") ?: "20").toLong() } diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HttpClientConfiguration.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HttpClientConfiguration.kt index a54bf3a..ae7de71 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HttpClientConfiguration.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HttpClientConfiguration.kt @@ -4,6 +4,7 @@ import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor.Level import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Configuration import retrofit2.Retrofit @@ -14,9 +15,8 @@ import java.util.concurrent.TimeUnit class HttpClientConfiguration( @Value("\${lrs.pfx-path}") val pfxFilePath: String, @Value("\${lrs.base-url}") val baseUrl: String, - @Value("\${lrs.connectTimeoutSeconds}") val connectTimeoutSeconds: Int, - @Value("\${lrs.writeTimeoutSeconds}") val writeTimeoutSeconds: Int, - @Value("\${lrs.readTimeoutSeconds}") val readTimeoutSeconds: Int, + @Autowired + private val appConfig: AppConfig, ) { fun buildSSLHttpClient(): OkHttpClient { log.info("Building HTTP client with SSL") @@ -29,9 +29,9 @@ class HttpClientConfiguration( val trustManager = sslContextConfiguration.getTrustManager() val httpClientBuilder = OkHttpClient.Builder() - .connectTimeout(connectTimeoutSeconds.toLong(), TimeUnit.SECONDS) - .writeTimeout(writeTimeoutSeconds.toLong(), TimeUnit.SECONDS) - .readTimeout(readTimeoutSeconds.toLong(), TimeUnit.SECONDS) + .connectTimeout(appConfig.lrsConnectTimeout(), TimeUnit.SECONDS) + .writeTimeout(appConfig.lrsWriteTimeout(), TimeUnit.SECONDS) + .readTimeout(appConfig.lrsReadTimeout(), TimeUnit.SECONDS) .sslSocketFactory(sslContext.socketFactory, trustManager) .addInterceptor(loggingInterceptor) diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 3a0f06c..32146ba 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -4,6 +4,3 @@ hmpps-auth: lrs: base-url: "https://cmp-ws.dev.lrs.education.gov.uk" pfx-path: "WebServiceClientCert.pfx" - connectTimeoutSeconds: 20 - writeTimeoutSeconds: 20 - readTimeoutSeconds: 20 \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 98e0e1b..a0d4ff5 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -4,6 +4,3 @@ hmpps-auth: lrs: base-url: "http://lrs-api:8080" pfx-path: "WebServiceClientCert.pfx" - connectTimeoutSeconds: 20 - writeTimeoutSeconds: 20 - readTimeoutSeconds: 20 \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 424855b..8f5aa37 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -54,6 +54,3 @@ management: lrs: base-url: ${lrs.base-url} pfx-path: ${lrs.pfx-path} - connectTimeoutSeconds: 20 - writeTimeoutSeconds: 20 - readTimeoutSeconds: 20 \ No newline at end of file diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HmppsBoldLrsExceptionHandlerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HmppsBoldLrsExceptionHandlerTest.kt index 84192f0..7cb4bf0 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HmppsBoldLrsExceptionHandlerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/learnerrecordsapi/config/HmppsBoldLrsExceptionHandlerTest.kt @@ -114,7 +114,7 @@ class HmppsBoldLrsExceptionHandlerTest : IntegrationTestBase() { } @Test - fun `should catch timeout exceptions (SocketTimeoutException) and return Gateway Timeout`() { + fun `should catch timeout exceptions (SocketTimeoutException) and return Request Timeout`() { val expectedResponse = HmppsBoldLrsExceptionHandler.ErrorResponse( status = HttpStatus.REQUEST_TIMEOUT, errorCode = "Request Timeout", diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index 9ecdee0..830ecb5 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -11,6 +11,3 @@ hmpps-auth: lrs: base-url: "http://localhost:8082" pfx-path: "WebServiceClientCert.pfx" - connectTimeoutSeconds: 3 - writeTimeoutSeconds: 3 - readTimeoutSeconds: 3 \ No newline at end of file