-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: 한 페이지에 PR을 100개씩 조회 * refactor: API 호출 병렬 처리 * feat: WebclientConfig 구현 * refactor: PR API RequestUrl을 만드는 메서드 분리 * refactor: 페이징 처리 * refactor: interface 분리 * chore: 디버깅용 로그 제거 * refactor: 매직넘버 상수화 * refactor: 와일드 카드 제거 * feat: SSEConnectionRefused 예외 생성 * feat: PullRequestService에서 SSEEmitter를 연결한다. * refactor: Connection 연결은 SseController가 담당한다. * refactor: PullRequest 동기화 기능에 SSE를 적용한다.
- Loading branch information
1 parent
4adf5c0
commit b749119
Showing
15 changed files
with
271 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
PUT localhost:8080/pull-requests/sync/mine?repoName=jwp-dashboard-jdbc | ||
Authorization: bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwibWVtYmVySWQiOjEsImV4cCI6MTY5Njg1MDcxMywiaWF0IjoxNjk2ODQ4OTEzfQ.tT9--wUY8qHWsttfKBXWPQUuzUVp8onoX6pJcRl_vgY | ||
|
||
### | ||
|
||
GET localhost:8080/pull-requests/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/com/integrated/techhub/auth/application/client/WebClientGithubClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.integrated.techhub.auth.application.client; | ||
|
||
import com.integrated.techhub.auth.application.client.dto.request.GithubTokenRefreshRequest; | ||
import com.integrated.techhub.auth.application.client.dto.request.GithubTokenRequest; | ||
import com.integrated.techhub.auth.application.client.dto.response.GithubPrInfoResponse; | ||
import com.integrated.techhub.auth.application.client.dto.response.OAuthGithubUsernameResponse; | ||
import com.integrated.techhub.auth.application.client.dto.response.OAuthTokensResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.BodyInserters; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.integrated.techhub.auth.util.GithubApiConstants.*; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class WebClientGithubClient implements GithubClient { | ||
|
||
private static final int LAST_PAGE = 40; | ||
private static final int PER_PAGE = 10; | ||
|
||
private final WebClient webClient; | ||
private final GithubClientProperties githubClientProperties; | ||
|
||
@Override | ||
public OAuthTokensResponse getGithubTokens(final String code) { | ||
final String clientId = githubClientProperties.clientId(); | ||
final String clientSecret = githubClientProperties.clientSecret(); | ||
|
||
return webClient.post() | ||
.uri(getGithubTokenUrl()) | ||
.body(BodyInserters.fromValue(new GithubTokenRequest(clientId, clientSecret, code))) | ||
.retrieve() | ||
.bodyToMono(OAuthTokensResponse.class).block(); | ||
} | ||
|
||
@Override | ||
public OAuthTokensResponse getNewAccessToken(final String refreshToken) { | ||
final String clientId = githubClientProperties.clientId(); | ||
final String clientSecret = githubClientProperties.clientSecret(); | ||
|
||
return webClient.post() | ||
.uri(getNewAccessTokenUrl(clientId, clientSecret, refreshToken)) | ||
.body(BodyInserters.fromValue(new GithubTokenRefreshRequest(clientId, clientSecret, refreshToken))) | ||
.retrieve() | ||
.bodyToMono(OAuthTokensResponse.class).block(); | ||
} | ||
|
||
@Override | ||
@Deprecated | ||
public OAuthGithubUsernameResponse getGithubUsername(final String accessToken) { | ||
final HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(accessToken); | ||
|
||
return webClient.get() | ||
.uri(getMemberInfoUrl()) | ||
.headers(httpHeaders -> httpHeaders.addAll(headers)) | ||
.retrieve() | ||
.bodyToMono(OAuthGithubUsernameResponse.class).block(); | ||
} | ||
|
||
/* | ||
* 조회 속도가 빠르긴 하지만 API 호출 횟수를 많이 사용 | ||
* 인증된 유저 기준 시간당 5,000회 | ||
* using: 사용자가 직접 요청하는 동기화 API | ||
* */ | ||
@Override | ||
public List<GithubPrInfoResponse> getPrsByRepoName(final String accessToken, final String repo) { | ||
final List<GithubPrInfoResponse> responses = new ArrayList<>(); | ||
final List<String> prRequestUrls = createPrApiRequestUrls(repo, LAST_PAGE); | ||
|
||
Flux.fromIterable(prRequestUrls) | ||
.flatMap(url -> fetchPrs(accessToken, url)) | ||
.collectList() | ||
.block() | ||
.forEach(githubPrInfoResponses -> responses.addAll(githubPrInfoResponses)); | ||
return responses; | ||
} | ||
|
||
private List<String> createPrApiRequestUrls(final String repo, final int lastPage) { | ||
List<String> prRequestUrls = new ArrayList<>(); | ||
for (int page = 1; page <= lastPage; page++) { | ||
prRequestUrls.add(getListPullRequestUrl(repo, page, PER_PAGE)); | ||
} | ||
return prRequestUrls; | ||
} | ||
|
||
private Flux<List<GithubPrInfoResponse>> fetchPrs(final String accessToken, final String requestUrl) { | ||
return webClient.get() | ||
.uri(requestUrl) | ||
.headers(headers -> headers.setBearerAuth(accessToken)) | ||
.retrieve() | ||
.bodyToFlux(GithubPrInfoResponse.class) | ||
.collectList() | ||
.flux(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/integrated/techhub/common/config/WebClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.integrated.techhub.common.config; | ||
|
||
import io.netty.channel.ChannelOption; | ||
import io.netty.handler.timeout.ReadTimeoutHandler; | ||
import io.netty.handler.timeout.WriteTimeoutHandler; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.reactive.ReactorClientHttpConnector; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.netty.http.client.HttpClient; | ||
|
||
import static org.springframework.http.HttpHeaders.CONTENT_TYPE; | ||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
@Configuration | ||
public class WebClientConfig { | ||
|
||
@Bean | ||
public WebClient webClient() { | ||
HttpClient httpClient = HttpClient.create() | ||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000) | ||
.doOnConnected(connection -> connection | ||
.addHandlerLast(new ReadTimeoutHandler(10)) | ||
.addHandlerLast(new WriteTimeoutHandler(10))); | ||
return WebClient.builder() | ||
.clientConnector(new ReactorClientHttpConnector(httpClient)) | ||
.defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.