From 618920816a9bafde238e086dad21bdd49d53bb67 Mon Sep 17 00:00:00 2001 From: "cyril.tsui" Date: Thu, 17 Jul 2025 11:40:55 +0800 Subject: [PATCH] Update M18 Token --- .../fpsms/api/service/ApiCallerService.kt | 37 ++++++++++--- .../ffii/fpsms/m18/service/M18TokenService.kt | 52 ------------------- 2 files changed, 29 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt diff --git a/src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt b/src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt index 943f063..90a9924 100644 --- a/src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt +++ b/src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt @@ -2,31 +2,28 @@ package com.ffii.fpsms.api.service import com.ffii.core.utils.JwtTokenUtil import com.ffii.fpsms.m18.M18Config -import com.ffii.fpsms.m18.service.M18TokenService +import com.ffii.fpsms.m18.model.M18TokenRequest +import com.ffii.fpsms.m18.model.M18TokenResponse +import jakarta.annotation.PostConstruct import org.slf4j.Logger import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Value +import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpHeaders import org.springframework.http.MediaType import org.springframework.stereotype.Service import org.springframework.util.LinkedMultiValueMap import org.springframework.util.MultiValueMap import org.springframework.web.reactive.function.client.WebClient -import org.springframework.web.reactive.function.client.bodyToMono import reactor.core.publisher.Mono import kotlin.reflect.full.memberProperties -import org.springframework.cloud.context.config.annotation.RefreshScope import org.springframework.context.annotation.Lazy -import org.springframework.http.HttpStatus import org.springframework.http.HttpStatusCode import org.springframework.web.reactive.function.client.ClientRequest import org.springframework.web.reactive.function.client.WebClientResponseException -import org.springframework.web.reactive.function.client.awaitBody @Service open class ApiCallerService( val m18Config: M18Config, - @Lazy val m18TokenService: M18TokenService, ) { val logger: Logger = LoggerFactory.getLogger(JwtTokenUtil::class.java) @@ -47,6 +44,30 @@ open class ApiCallerService( .codecs { configurer -> configurer.defaultCodecs().maxInMemorySize(10 * 1024 * 1024) } .build() + @PostConstruct + fun init() { + updateToken() + } + + fun updateToken() { + val params = M18TokenRequest( + grant_type = m18Config.GRANT_TYPE, + client_id = m18Config.CLIENT_ID, + client_secret = m18Config.CLIENT_SECRET, + username = m18Config.USERNAME, + password = m18Config.PASSWORD + ) + + get("/oauth/token", params, null) + .subscribe( + { response -> + m18Config.ACCESS_TOKEN = response.access_token + println("WebClient Response stored: $response") + }, + { error -> println("WebClient Error: ${error.message}") } + ) + } + /** * Performs a GET HTTP request to the specified URL path. * @@ -85,7 +106,7 @@ open class ApiCallerService( logger.error("Error Status: ${error.statusCode} - ${error.statusText}") logger.error("Error Message: ${error.message}") if (error.statusCode == HttpStatusCode.valueOf(400)) { - m18TokenService.run() + updateToken() } Mono.error(error) } diff --git a/src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt b/src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt deleted file mode 100644 index 9efb249..0000000 --- a/src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.ffii.fpsms.m18.service - -import com.ffii.fpsms.api.service.ApiCallerService -import com.ffii.fpsms.m18.M18Config -import com.ffii.fpsms.m18.model.M18TokenRequest -import com.ffii.fpsms.m18.model.M18TokenResponse -import jakarta.annotation.PostConstruct -import org.springframework.context.annotation.Bean -import org.springframework.stereotype.Service - - -@Service -open class M18TokenService( - private val apiCallerService: ApiCallerService, - private val m18Config: M18Config -) { - - @PostConstruct - fun run() { -// val params: MutableMap = mutableMapOf( -// "grant_type" to m18Config.GRANT_TYPE, -// "client_id" to m18Config.CLIENT_ID, -// "client_secret" to m18Config.CLIENT_SECRET, -// "username" to m18Config.USERNAME, -// "password" to m18Config.PASSWORD -// ) -// - val params = M18TokenRequest( -// grant_type = m18Config?.GRANT_TYPE ?: "password", -// client_id = m18Config?.CLIENT_ID ?: "M2Y1OGYxMmQtZDRiOS00OTA4LTgyNTktZDRkNzEzNWVkMzRm", -// client_secret = m18Config?.CLIENT_SECRET ?: "M2Y2YjQzYzQtZTc2Mi00OTFhLTkwYmItYmJhMzFjZjEyYmY5", -// username = m18Config?.USERNAME ?:"testingMTMS", -// password = m18Config?.PASSWORD ?: "db25f2fc14cd2d2b1e7af307241f548fb03c312a" - grant_type = "password", - client_id = "M2Y1OGYxMmQtZDRiOS00OTA4LTgyNTktZDRkNzEzNWVkMzRm", - client_secret = "M2Y2YjQzYzQtZTc2Mi00OTFhLTkwYmItYmJhMzFjZjEyYmY5", - username = "testingMTMS", - password = "db25f2fc14cd2d2b1e7af307241f548fb03c312a" - ) - - apiCallerService - .get("/oauth/token", params, null) - .subscribe( - { response -> - m18Config.ACCESS_TOKEN = response.access_token - println("WebClient Response stored: $response") - }, - { error -> println("WebClient Error: ${error.message}") } - ) - } - -} \ No newline at end of file