|
|
@@ -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) |
|
|
|
} |
|
|
|