Parcourir la source

Update M18 Token

master
cyril.tsui il y a 1 mois
Parent
révision
618920816a
2 fichiers modifiés avec 29 ajouts et 60 suppressions
  1. +29
    -8
      src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt
  2. +0
    -52
      src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt

+ 29
- 8
src/main/java/com/ffii/fpsms/api/service/ApiCallerService.kt Voir le fichier

@@ -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<M18TokenResponse, M18TokenRequest>("/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)
}


+ 0
- 52
src/main/java/com/ffii/fpsms/m18/service/M18TokenService.kt Voir le fichier

@@ -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<String, String> = 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<M18TokenResponse, M18TokenRequest>("/oauth/token", params, null)
.subscribe(
{ response ->
m18Config.ACCESS_TOKEN = response.access_token
println("WebClient Response stored: $response")
},
{ error -> println("WebClient Error: ${error.message}") }
)
}

}

Chargement…
Annuler
Enregistrer