Skip to content

Commit

Permalink
Refactor: WebClient 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
RinRinPARK committed Aug 29, 2024
1 parent ac2a80a commit 9afe616
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {
implementation("org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.9.0-RC")
implementation("org.springframework.boot:spring-boot-starter-webflux")

// querydsl
implementation("com.querydsl:querydsl-jpa:5.0.0:jakarta")
Expand Down
39 changes: 20 additions & 19 deletions src/main/kotlin/com/swm_standard/phote/service/QuestionService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.swm_standard.phote.service

import com.fasterxml.jackson.databind.ObjectMapper
import com.swm_standard.phote.common.exception.ChatGptErrorException
import com.swm_standard.phote.common.exception.NotFoundException
import com.swm_standard.phote.dto.CreateQuestionRequest
Expand All @@ -19,16 +20,17 @@ import com.swm_standard.phote.repository.TagRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import org.springframework.web.client.RestTemplate
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBodyOrNull
import java.time.LocalDateTime
import java.util.UUID

Expand Down Expand Up @@ -146,24 +148,23 @@ class QuestionService(
}

suspend fun transformImage(imageUrl: String, imageCoordinates: List<List<Int>>?): String? {
val headers = HttpHeaders()
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8")
val webClient = WebClient.builder()
.baseUrl(lambdaUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=utf-8")
.build()

val body: MultiValueMap<String, Any> = LinkedMultiValueMap()
val body: MultiValueMap<String, String> = LinkedMultiValueMap()
body.add("url", imageUrl)
body.add("coor", imageCoordinates)

val transformImageRequest = HttpEntity(body, headers)
val lambdaResponse =
withContext(Dispatchers.IO) {
RestTemplate().exchange(
lambdaUrl,
HttpMethod.POST,
transformImageRequest,
String::class.java,
)
}
return lambdaResponse.body?.split("\"")?.get(1)
body.add("coor", ObjectMapper().writeValueAsString(imageCoordinates))

val lambdaResponse: String? =
webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromFormData(body))
.retrieve()
.awaitBodyOrNull<String>()

return lambdaResponse?.split("\"")?.get(1)
}

fun splitChatGPTResponse(chatGPTResponse: ChatGPTResponse?): List<String> {
Expand Down

0 comments on commit 9afe616

Please sign in to comment.