@@ -47,6 +47,8 @@ dependencies { | |||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | ||||
implementation "org.jetbrains.kotlin:kotlin-reflect" | implementation "org.jetbrains.kotlin:kotlin-reflect" | ||||
implementation("org.springframework.boot:spring-boot-starter-webflux") | |||||
compileOnly group: 'jakarta.servlet', name: 'jakarta.servlet-api', version: '6.0.0' | compileOnly group: 'jakarta.servlet', name: 'jakarta.servlet-api', version: '6.0.0' | ||||
runtimeOnly 'com.mysql:mysql-connector-j' | runtimeOnly 'com.mysql:mysql-connector-j' | ||||
@@ -0,0 +1,54 @@ | |||||
package com.ffii.fpsms.api.service | |||||
import com.ffii.fpsms.m18.M18Config | |||||
import org.springframework.beans.factory.annotation.Value | |||||
import org.springframework.http.HttpHeaders | |||||
import org.springframework.http.MediaType | |||||
import org.springframework.stereotype.Service | |||||
import org.springframework.util.LinkedMultiValueMap | |||||
import org.springframework.web.reactive.function.client.WebClient | |||||
import org.springframework.web.reactive.function.client.bodyToMono | |||||
import reactor.core.publisher.Mono | |||||
@Service | |||||
open class ApiCallerService( | |||||
@Value("\${m18.config.base-url}") private val baseUrl: String, | |||||
private val m18Config: M18Config | |||||
) { | |||||
private val webClient: WebClient = WebClient.builder() | |||||
.baseUrl(baseUrl) | |||||
.defaultHeaders { headers -> | |||||
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | |||||
headers.set(HttpHeaders.AUTHORIZATION, "Bearer ${m18Config.ACCESS_TOKEN}") | |||||
} | |||||
.build() | |||||
fun get(urlPath: String, params: Map<String, String>?, customHeaders: Map<String, String>?): Mono<String> { | |||||
return webClient.get() | |||||
.uri { uriBuilder -> | |||||
uriBuilder | |||||
.apply { | |||||
path(urlPath) | |||||
// convert to multiValueMap | |||||
params?.let { params -> | |||||
queryParams( | |||||
LinkedMultiValueMap<String, String>().apply { | |||||
params.forEach { (key, values) -> | |||||
add(key, values) | |||||
} | |||||
} | |||||
) | |||||
} | |||||
} | |||||
.build() | |||||
} | |||||
.headers { headers -> | |||||
customHeaders?.forEach { (k, v) -> headers.set(k, v) } | |||||
} | |||||
.retrieve() | |||||
.bodyToMono(String::class.java) | |||||
} | |||||
} |
@@ -0,0 +1,26 @@ | |||||
package com.ffii.fpsms.m18 | |||||
import org.springframework.beans.factory.annotation.Value | |||||
import org.springframework.context.annotation.Bean | |||||
import org.springframework.context.annotation.Configuration | |||||
@Configuration | |||||
open class M18Config { | |||||
@Value("\${m18.config.grant-type}") | |||||
lateinit var GRANT_TYPE: String; | |||||
@Value("\${m18.config.client-id}") | |||||
lateinit var CLIENT_ID: String; | |||||
@Value("\${m18.config.client-secret}") | |||||
lateinit var CLIENT_SECRET: String; | |||||
@Value("\${m18.config.username}") | |||||
lateinit var USERNAME: String; | |||||
@Value("\${m18.config.password}") | |||||
lateinit var PASSWORD: String; | |||||
var ACCESS_TOKEN: String? = null; | |||||
} |
@@ -0,0 +1,9 @@ | |||||
package com.ffii.fpsms.m18.modals | |||||
data class TokenRequest( | |||||
val grant_type: String, | |||||
val client_id: String, | |||||
val client_secret: String, | |||||
val username: String, | |||||
val password: String, | |||||
) |
@@ -0,0 +1,8 @@ | |||||
package com.ffii.fpsms.m18.modals | |||||
data class TokenResponse( | |||||
val access_token: String, | |||||
val refresh_token: String, | |||||
val uid: Long, | |||||
val token_type: String | |||||
) |
@@ -0,0 +1,36 @@ | |||||
package com.ffii.fpsms.m18.service | |||||
import com.ffii.fpsms.api.service.ApiCallerService | |||||
import com.ffii.fpsms.m18.M18Config | |||||
import org.springframework.context.annotation.Bean | |||||
import org.springframework.stereotype.Component | |||||
import org.springframework.stereotype.Service | |||||
@Service | |||||
@Component | |||||
open class M18TokenService( | |||||
private val apiCallerService: ApiCallerService, | |||||
private val m18Config: M18Config | |||||
) { | |||||
@Bean | |||||
fun run() { | |||||
println("abcdasds") | |||||
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 | |||||
) | |||||
apiCallerService | |||||
.get("/oauth/token", params, null) | |||||
.subscribe( | |||||
{ response -> println("WebClient Response stored: $response") }, | |||||
{ error -> println("WebClient Error: ${error.message}") } | |||||
) | |||||
} | |||||
} |
@@ -25,4 +25,13 @@ spring: | |||||
storage_engine: innodb | storage_engine: innodb | ||||
logging: | logging: | ||||
config: 'classpath:log4j2.yml' | |||||
config: 'classpath:log4j2.yml' | |||||
m18: | |||||
config: | |||||
grant-type: password | |||||
client-id: M2Y1OGYxMmQtZDRiOS00OTA4LTgyNTktZDRkNzEzNWVkMzRm | |||||
client-secret: M2Y2YjQzYzQtZTc2Mi00OTFhLTkwYmItYmJhMzFjZjEyYmY5 | |||||
username: testingMTMS | |||||
password: db25f2fc14cd2d2b1e7af307241f548fb03c312a | |||||
base-url: http://16.162.251.126/jsf/rfws |