Skip to content

Commit 9b0ed58

Browse files
authored
Replace RestTemplate with RestClient (#1005)
This commit replaces the RestTemplate usage in AgentChatClient with RestClient. It also removes the RestTemplate bean configured in AgentPlatformConfiguration, as it does not appear to be used anywhere. See: gh-1003 Signed-off-by: Arjen Poutsma <[email protected]>
1 parent c2bcb9a commit 9b0ed58

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

embabel-agent-api/src/main/kotlin/com/embabel/agent/spi/config/spring/AgentPlatformConfiguration.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import org.springframework.context.annotation.Bean
4444
import org.springframework.context.annotation.Configuration
4545
import org.springframework.context.annotation.Primary
4646
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
47-
import org.springframework.web.client.RestTemplate
4847

4948

5049
/**
@@ -102,9 +101,6 @@ class AgentPlatformConfiguration(
102101
@ConditionalOnMissingBean(ColorPalette::class)
103102
fun defaultColorPalette(): ColorPalette = DefaultColorPalette()
104103

105-
@Bean
106-
fun restTemplate() = RestTemplate()
107-
108104
@Bean
109105
@ConditionalOnMissingBean(name = ["embabelJacksonObjectMapper"])
110106
fun embabelJacksonObjectMapper(builder: Jackson2ObjectMapperBuilder): ObjectMapper {

embabel-agent-eval/src/main/kotlin/com/embabel/agent/eval/client/AgentChatClient.kt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package com.embabel.agent.eval.client
1717

18-
import org.springframework.http.HttpEntity
1918
import org.springframework.http.HttpHeaders
20-
import org.springframework.http.HttpMethod
2119
import org.springframework.stereotype.Service
22-
import org.springframework.web.client.RestTemplate
20+
import org.springframework.web.client.RestClient
21+
import org.springframework.web.client.body
2322

2423
data class KnowledgeContext(
2524
val name: String,
@@ -42,7 +41,7 @@ data class SessionCreationResponse(
4241
*/
4342
@Service
4443
class AgentChatClient(
45-
private val restTemplate: RestTemplate = RestTemplate(),
44+
private val restClient: RestClient = RestClient.create(),
4645
private val agentHost: String = "http://localhost:8081",
4746
private val agentChatPath: String = "/api/v1/chat",
4847
private val boogieHost: String = "http://localhost:8080",
@@ -57,25 +56,25 @@ class AgentChatClient(
5756
}
5857

5958
fun createKnowledgeContext(knowledgeContext: KnowledgeContext): String {
60-
val entity = HttpEntity(knowledgeContext, defaultHeaders)
61-
return restTemplate.exchange(
62-
"${boogieHost}/${boogieContextPath}",
63-
HttpMethod.PUT,
64-
entity,
65-
String::class.java,
66-
).body ?: throw IllegalStateException("No response body")
59+
return restClient
60+
.put()
61+
.uri("${boogieHost}/${boogieContextPath}")
62+
.headers { it.putAll(defaultHeaders) }
63+
.body(knowledgeContext)
64+
.retrieve()
65+
.body<String>()
66+
?: throw IllegalStateException("No response body")
6767
}
6868

6969
fun createSession(sessionCreationRequest: SessionCreationRequest): SessionCreationResponse {
70-
val url = "${agentHost}/${agentChatPath}/sessions"
71-
val entity = HttpEntity(sessionCreationRequest, defaultHeaders)
72-
val re = restTemplate.exchange(
73-
url,
74-
HttpMethod.PUT,
75-
entity,
76-
SessionCreationResponse::class.java,
77-
)
78-
return re.body ?: throw IllegalStateException("No response body")
70+
return restClient
71+
.put()
72+
.uri("${agentHost}/${agentChatPath}/sessions")
73+
.headers { it.putAll(defaultHeaders) }
74+
.body(sessionCreationRequest)
75+
.retrieve()
76+
.body<SessionCreationResponse>()
77+
?: throw IllegalStateException("No response body")
7978
}
8079

8180
// fun ingestDocument(knowledgeContext: KnowledgeContext): String {
@@ -89,21 +88,22 @@ class AgentChatClient(
8988
// }
9089

9190
fun getObjectContext(id: String): ObjectContext {
92-
return restTemplate.getForObject(
93-
"${agentHost}/${agentChatPath}/objectContexts/{id}",
94-
ObjectContext::class.java,
95-
id,
96-
) ?: throw IllegalStateException("No response body")
91+
return restClient
92+
.get()
93+
.uri("${agentHost}/${agentChatPath}/objectContexts/{id}", id)
94+
.retrieve()
95+
.body<ObjectContext>()
96+
?: throw IllegalStateException("No response body")
9797
}
9898

9999
fun respond(chatRequest: ChatRequest): MessageResponse {
100-
val entity = HttpEntity(chatRequest)
101-
return restTemplate.exchange(
102-
"${agentHost}/${agentChatPath}/messages",
103-
HttpMethod.PUT,
104-
entity,
105-
MessageResponse::class.java,
106-
).body ?: throw IllegalStateException("No response body")
100+
return restClient
101+
.put()
102+
.uri("${agentHost}/${agentChatPath}/messages")
103+
.body(chatRequest)
104+
.retrieve()
105+
.body<MessageResponse>()
106+
?: throw IllegalStateException("No response body")
107107
}
108108

109109
}

0 commit comments

Comments
 (0)