Skip to content

Commit 3b53d05

Browse files
committed
refactor: github rest api를 사용할때, token을 사용하도록 수정한다
1 parent 937d9d4 commit 3b53d05

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AnimationFacade(
2020
private val contributionApi: ContributionApi,
2121
private val sagaManager: SagaManager,
2222
private val eventPublisher: ApplicationEventPublisher,
23-
private val githubOpenApi: GithubOpenApi,
23+
private val githubRestApi: GithubRestApi,
2424
) {
2525

2626
private val logger = LoggerFactory.getLogger(this::class.simpleName)
@@ -61,23 +61,6 @@ class AnimationFacade(
6161
}
6262
}
6363

64-
private fun setUserAuthInfoIfNotSet(username: String) {
65-
runCatching {
66-
val user = userService.getUserByName(username)
67-
68-
if (user.isAuthInfoSet().not()) {
69-
val githubUserAuthInfo = githubOpenApi.getGithubUser(user.getName())
70-
userService.setAuthInfo(
71-
name = user.getName(),
72-
entryPoint = EntryPoint.GITHUB,
73-
githubUserAuthInfo.id,
74-
)
75-
}
76-
}.onFailure {
77-
logger.info("Fail to update userAuthInfo cause: ${it.message}", it)
78-
}
79-
}
80-
8164
fun createNewUser(username: String): User {
8265
return runCatching {
8366
val contributionYears = contributionApi.getAllContributionYears(username)
@@ -88,9 +71,28 @@ class AnimationFacade(
8871
name = username,
8972
contributions = contributionCountPerYear,
9073
)
74+
}.onSuccess {
75+
setUserAuthInfoIfNotSet(username)
9176
}.getOrElse {
9277
require(it !is RestClientException) { "Cannot create new user from username \"$username\"" }
9378
throw it
9479
}
9580
}
81+
82+
private fun setUserAuthInfoIfNotSet(username: String) {
83+
runCatching {
84+
val user = userService.getUserByName(username)
85+
86+
if (user.isAuthInfoSet().not()) {
87+
val githubUserAuthInfo = githubRestApi.getGithubUser(user.getName())
88+
userService.setAuthInfo(
89+
name = user.getName(),
90+
entryPoint = EntryPoint.GITHUB,
91+
githubUserAuthInfo.id,
92+
)
93+
}
94+
}.onFailure {
95+
logger.info("Fail to update userAuthInfo cause: ${it.message}", it)
96+
}
97+
}
9698
}

src/main/kotlin/org/gitanimals/render/app/GithubOpenApi.kt renamed to src/main/kotlin/org/gitanimals/render/app/GithubRestApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
44
import org.springframework.web.bind.annotation.PathVariable
55
import org.springframework.web.service.annotation.GetExchange
66

7-
fun interface GithubOpenApi {
7+
fun interface GithubRestApi {
88

99
@GetExchange("/users/{username}")
1010
fun getGithubUser(@PathVariable("username") username: String): GithubUserResponse

src/main/kotlin/org/gitanimals/render/infra/RenderHttpClientConfigurer.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package org.gitanimals.render.infra
33
import org.gitanimals.core.auth.InternalAuthRequestInterceptor
44
import org.gitanimals.core.filter.MDCFilter
55
import org.gitanimals.rank.infra.HttpClientErrorHandler
6-
import org.gitanimals.render.app.GithubOpenApi
6+
import org.gitanimals.render.app.GithubRestApi
77
import org.gitanimals.render.app.IdentityApi
88
import org.slf4j.MDC
99
import org.springframework.beans.factory.annotation.Value
1010
import org.springframework.context.annotation.Bean
1111
import org.springframework.context.annotation.Configuration
1212
import org.springframework.context.annotation.Profile
13+
import org.springframework.http.HttpHeaders
1314
import org.springframework.web.client.RestClient
1415
import org.springframework.web.client.support.RestClientAdapter
1516
import org.springframework.web.service.invoker.HttpServiceProxyFactory
@@ -18,6 +19,7 @@ import org.springframework.web.service.invoker.HttpServiceProxyFactory
1819
@Profile("!test")
1920
class RenderHttpClientConfigurer(
2021
@Value("\${internal.secret}") private val internalSecret: String,
22+
@Value("\${github.token}") private val token: String,
2123
private val internalAuthRequestInterceptor: InternalAuthRequestInterceptor,
2224
) {
2325

@@ -45,18 +47,22 @@ class RenderHttpClientConfigurer(
4547
}
4648

4749
@Bean
48-
fun renderGithubOpenApiHttpClient(): GithubOpenApi {
50+
fun renderGithubRestApiHttpClient(): GithubRestApi {
4951
val restClient = RestClient
5052
.builder()
5153
.defaultStatusHandler(renderHttpClientErrorHandler())
54+
.requestInterceptor { request, body, execution ->
55+
request.headers.add(HttpHeaders.AUTHORIZATION, token)
56+
execution.execute(request, body)
57+
}
5258
.baseUrl("https://api.github.com")
5359
.build()
5460

5561
val httpServiceProxyFactory = HttpServiceProxyFactory
5662
.builderFor(RestClientAdapter.create(restClient))
5763
.build()
5864

59-
return httpServiceProxyFactory.createClient(GithubOpenApi::class.java)
65+
return httpServiceProxyFactory.createClient(GithubRestApi::class.java)
6066
}
6167

6268
@Bean
@@ -88,7 +94,7 @@ class RenderTestHttpClientConfigurer {
8894
}
8995

9096
@Bean
91-
fun renderGithubOpenApiHttpClient(): GithubOpenApi {
97+
fun renderGithubRestApiHttpClient(): GithubRestApi {
9298
val restClient = RestClient
9399
.builder()
94100
.defaultStatusHandler(renderHttpClientErrorHandler())
@@ -99,7 +105,7 @@ class RenderTestHttpClientConfigurer {
99105
.builderFor(RestClientAdapter.create(restClient))
100106
.build()
101107

102-
return httpServiceProxyFactory.createClient(GithubOpenApi::class.java)
108+
return httpServiceProxyFactory.createClient(GithubRestApi::class.java)
103109
}
104110

105111
@Bean

0 commit comments

Comments
 (0)