Skip to content

Commit cdaef58

Browse files
committed
refactor: inbox restclient -> httpclient로 수정한다
1 parent 54af45f commit cdaef58

File tree

4 files changed

+81
-36
lines changed

4 files changed

+81
-36
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.gitanimals.core
2+
3+
import org.springframework.http.HttpMethod
4+
import org.springframework.http.HttpStatus
5+
import org.springframework.http.client.ClientHttpResponse
6+
import org.springframework.web.client.ResponseErrorHandler
7+
import java.net.URI
8+
9+
10+
class HttpClientErrorHandler : ResponseErrorHandler {
11+
12+
override fun hasError(response: ClientHttpResponse): Boolean {
13+
return response.statusCode.isError
14+
}
15+
16+
override fun handleError(url: URI, method: HttpMethod, response: ClientHttpResponse) {
17+
val body = response.body.bufferedReader().use { it.readText() }
18+
when {
19+
response.statusCode.isSameCodeAs(HttpStatus.UNAUTHORIZED) ->
20+
throw AuthorizationException(body)
21+
22+
response.statusCode.is4xxClientError ->
23+
throw IllegalArgumentException(body)
24+
25+
else -> error(body)
26+
}
27+
}
28+
}

src/main/kotlin/org/gitanimals/inbox/app/IdentityApi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.gitanimals.inbox.app
22

3+
import org.springframework.web.service.annotation.GetExchange
4+
35
fun interface IdentityApi {
46

7+
@GetExchange("/users")
58
fun getUserByToken(token: String): UserResponse
69

710
data class UserResponse(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.gitanimals.inbox.infra
2+
3+
import org.gitanimals.core.HttpClientErrorHandler
4+
import org.gitanimals.core.filter.MDCFilter
5+
import org.gitanimals.inbox.app.IdentityApi
6+
import org.slf4j.MDC
7+
import org.springframework.beans.factory.annotation.Value
8+
import org.springframework.context.annotation.Bean
9+
import org.springframework.context.annotation.Configuration
10+
import org.springframework.web.client.RestClient
11+
import org.springframework.web.client.support.RestClientAdapter
12+
import org.springframework.web.service.invoker.HttpServiceProxyFactory
13+
14+
@Configuration
15+
class InboxHttpClientConfigurer(
16+
@Value("\${internal.secret}") private val internalSecret: String,
17+
) {
18+
19+
@Bean
20+
fun inboxIdentityApi(token: String): IdentityApi {
21+
val restClient = RestClient
22+
.builder()
23+
.requestInterceptor { request, body, execution ->
24+
request.headers.add(MDCFilter.TRACE_ID, MDC.get(MDCFilter.TRACE_ID))
25+
if (request.uri.path.startsWith("/internals")) {
26+
request.headers.add(
27+
INTERNAL_SECRET_KEY,
28+
internalSecret
29+
)
30+
}
31+
execution.execute(request, body)
32+
}
33+
.defaultStatusHandler(inboxHttpClientErrorHandler())
34+
.baseUrl("https://api.gitanimals.org")
35+
.build()
36+
37+
val httpServiceProxyFactory = HttpServiceProxyFactory
38+
.builderFor(RestClientAdapter.create(restClient))
39+
.build()
40+
41+
return httpServiceProxyFactory.createClient(IdentityApi::class.java)
42+
}
43+
44+
@Bean
45+
fun inboxHttpClientErrorHandler(): HttpClientErrorHandler = HttpClientErrorHandler()
46+
47+
private companion object {
48+
private const val INTERNAL_SECRET_KEY = "Internal-Secret"
49+
}
50+
}

src/main/kotlin/org/gitanimals/inbox/infra/RestIdentityApi.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)